source-launch.compat.spec.ts 4.8 KB

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