source-launch.compat.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { chmod, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
  2. import { delimiter, join } from 'node:path'
  3. import { tmpdir } from 'node:os'
  4. import { fileURLToPath } from 'node:url'
  5. import { execa } from 'execa'
  6. import { describe, expect, it } from 'vitest'
  7. /**
  8. * Keyless smoke for SOURCE `dsh` execution: run `apps/cli/src/bin.ts`
  9. * with the exact production runtime vector (`node --import tsx/esm`, the
  10. * vector the root `dsh` script invokes after building) and assert the
  11. * required-config diagnostic. The Node compatibility matrix runs this
  12. * WHOLE file, so a Node release changing module hooks or TypeScript handling
  13. * breaks this gate instead of every developer's `pnpm dsh`; the built-bin
  14. * suite covers the published `lib/` entry, not this source chain.
  15. */
  16. const repoRoot = fileURLToPath(new URL('../../../', import.meta.url))
  17. const dshSourceBin = 'apps/cli/src/bin.ts'
  18. describe('dsh SOURCE launcher (node --import tsx/esm)', () => {
  19. it('runs the repository build before launching the source CLI', async () => {
  20. const stubDir = await mkdtemp(join(tmpdir(), 'dsh-source-launch-'))
  21. const invocationLog = join(stubDir, 'pnpm-args.json')
  22. const stubBody = "const { appendFileSync } = require('node:fs')\nappendFileSync(process.env.DSH_TEST_PNPM_LOG, JSON.stringify(process.argv.slice(2)))\n"
  23. await writeFile(join(stubDir, 'pnpm'), `#!/usr/bin/env node\n${stubBody}`)
  24. await chmod(join(stubDir, 'pnpm'), 0o755)
  25. await writeFile(join(stubDir, 'pnpm-stub.cjs'), stubBody)
  26. await writeFile(join(stubDir, 'pnpm.cmd'), '@echo off\r\nnode "%~dp0pnpm-stub.cjs" %*\r\n')
  27. const pathKey = Object.keys(process.env).find(key => key.toUpperCase() === 'PATH') ?? 'PATH'
  28. try {
  29. const result = await execa(process.execPath, ['--import', 'tsx/esm', 'scripts/run-source-dsh.ts', '--help'], {
  30. cwd: repoRoot,
  31. env: {
  32. ...process.env,
  33. [pathKey]: `${stubDir}${delimiter}${process.env[pathKey] ?? ''}`,
  34. DSH_TEST_PNPM_LOG: invocationLog,
  35. },
  36. reject: false,
  37. })
  38. expect(result.exitCode).toBe(0)
  39. expect(result.stdout).toMatch(/^Usage: dsh /)
  40. await expect(readFile(invocationLog, 'utf8')).resolves.toBe('["run","build"]')
  41. } finally {
  42. await rm(stubDir, { recursive: true, force: true })
  43. }
  44. })
  45. it('builds without mixing build logs into CLI stdout', async () => {
  46. const result = await execa('pnpm', ['dsh', '--help'], {
  47. cwd: repoRoot,
  48. timeout: 120_000,
  49. killSignal: 'SIGKILL',
  50. reject: false,
  51. })
  52. if (result.timedOut) {
  53. throw new Error(`pnpm dsh --help did not exit within 120s. stdout:\n${result.stdout}\nstderr:\n${result.stderr}`)
  54. }
  55. expect(result.exitCode).toBe(0)
  56. expect(result.stdout).toMatch(/^Usage: dsh /)
  57. expect(result.stdout).not.toContain('tsdown')
  58. expect(result.stdout).not.toContain('build:lib')
  59. }, 125_000)
  60. it('boots the source entry and requires a profile', async () => {
  61. const result = await execa(process.execPath, ['--import', 'tsx/esm', dshSourceBin], {
  62. cwd: repoRoot,
  63. input: '',
  64. timeout: 25_000,
  65. killSignal: 'SIGKILL',
  66. reject: false,
  67. })
  68. if (result.timedOut) {
  69. throw new Error(`dsh source launch did not exit within 25s. stdout:\n${result.stdout}\nstderr:\n${result.stderr}`)
  70. }
  71. expect(result.exitCode).not.toBe(0)
  72. expect(result.stderr).toContain('--profile <name> is required')
  73. expect(result.stdout).toBe('')
  74. }, 30_000)
  75. })