template.spec.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import { resolve } from 'node:path'
  5. import { spawnSync } from 'node:child_process'
  6. import {
  7. existsSync,
  8. readFileSync,
  9. writeFileSync,
  10. rmSync,
  11. renameSync
  12. } from 'node:fs'
  13. import { beforeAll, describe, it } from 'vitest'
  14. // Build CLI before tests, for local usage only.
  15. // CI builds the CLI on different platforms and architectures
  16. if (!process.env.CI) {
  17. beforeAll(() => {
  18. const cliDir = resolve(__dirname, '..')
  19. exec('pnpm', ['build:debug'], { cwd: cliDir })
  20. })
  21. }
  22. describe('[CLI] @tauri-apps/cli template', () => {
  23. it('init a project and builds it', { timeout: 15 * 60 * 1000 }, async () => {
  24. const cwd = process.cwd()
  25. const fixturePath = resolve(__dirname, './fixtures/empty')
  26. const tauriFixturePath = resolve(fixturePath, 'src-tauri')
  27. const outPath = resolve(tauriFixturePath, 'target')
  28. const cacheOutPath = resolve(fixturePath, 'target')
  29. process.chdir(fixturePath)
  30. const outExists = existsSync(outPath)
  31. if (outExists) {
  32. if (existsSync(cacheOutPath)) {
  33. rmSync(cacheOutPath, { recursive: true, force: true })
  34. }
  35. renameSync(outPath, cacheOutPath)
  36. }
  37. const cli = await import('../main.js')
  38. await cli.run([
  39. 'init',
  40. '-vvv',
  41. '--directory',
  42. process.cwd(),
  43. '--force',
  44. '--tauri-path',
  45. resolve(__dirname, '../../..'),
  46. '--before-build-command',
  47. '',
  48. '--before-dev-command',
  49. '',
  50. '--ci'
  51. ])
  52. if (outExists) {
  53. renameSync(cacheOutPath, outPath)
  54. }
  55. process.chdir(tauriFixturePath)
  56. const manifestPath = resolve(tauriFixturePath, 'Cargo.toml')
  57. const manifestFile = readFileSync(manifestPath).toString()
  58. writeFileSync(manifestPath, `workspace = { }\n${manifestFile}`)
  59. const configPath = resolve(tauriFixturePath, 'tauri.conf.json')
  60. const config = readFileSync(configPath).toString()
  61. writeFileSync(configPath, config.replace('com.tauri.dev', 'com.tauri.test'))
  62. await cli.run(['build'])
  63. process.chdir(cwd)
  64. })
  65. })
  66. function exec(
  67. bin: string,
  68. args?: string[],
  69. opts?: {
  70. cwd?: string
  71. }
  72. ) {
  73. process.platform === 'win32'
  74. ? spawnSync('cmd', ['/c', bin, ...(args ?? [])], { cwd: opts?.cwd })
  75. : spawnSync(bin, args, { cwd: opts?.cwd })
  76. }