template.spec.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. '--directory',
  41. process.cwd(),
  42. '--force',
  43. '--tauri-path',
  44. resolve(__dirname, '../../..'),
  45. '--before-build-command',
  46. '',
  47. '--before-dev-command',
  48. '',
  49. '--ci'
  50. ])
  51. if (outExists) {
  52. renameSync(cacheOutPath, outPath)
  53. }
  54. process.chdir(tauriFixturePath)
  55. const manifestPath = resolve(tauriFixturePath, 'Cargo.toml')
  56. const manifestFile = readFileSync(manifestPath).toString()
  57. writeFileSync(manifestPath, `workspace = { }\n${manifestFile}`)
  58. const configPath = resolve(tauriFixturePath, 'tauri.conf.json')
  59. const config = readFileSync(configPath).toString()
  60. writeFileSync(configPath, config.replace('com.tauri.dev', 'com.tauri.test'))
  61. await cli.run(['build', '--verbose'])
  62. process.chdir(cwd)
  63. })
  64. })
  65. function exec(
  66. bin: string,
  67. args?: string[],
  68. opts?: {
  69. cwd?: string
  70. }
  71. ) {
  72. process.platform === 'win32'
  73. ? spawnSync('cmd', ['/c', bin, ...(args ?? [])], { cwd: opts?.cwd })
  74. : spawnSync(bin, args, { cwd: opts?.cwd })
  75. }