build-exe-for-python-sdk.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { spawnSync } from 'node:child_process'
  2. import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { dirname, join, resolve } from 'node:path'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. const root = resolve(import.meta.dirname, '..')
  7. const script = resolve(root, 'scripts/build-exe-for-python-sdk.ts')
  8. const temporaryDirectories: string[] = []
  9. afterEach(() => {
  10. for (const directory of temporaryDirectories.splice(0)) {
  11. rmSync(directory, { recursive: true, force: true })
  12. }
  13. })
  14. function run(env: NodeJS.ProcessEnv, ...args: string[]) {
  15. return spawnSync(process.execPath, ['--import', 'tsx/esm', script, ...args], {
  16. cwd: root,
  17. encoding: 'utf8',
  18. env: isolatedPnpmEnvironment(env),
  19. })
  20. }
  21. describe('Python runtime executable builder CLI', () => {
  22. it('keeps the single-file dispatcher on the Python packaging surface', () => {
  23. const bootstrapPath = resolve(root, 'python/sdk-runtime/runtime-bootstrap.mjs')
  24. const bootstrap = readFileSync(bootstrapPath, 'utf8')
  25. const cliConfig = readFileSync(resolve(root, 'apps/cli/tsdown.config.ts'), 'utf8')
  26. const cliTsconfig = readFileSync(resolve(root, 'apps/cli/tsconfig.json'), 'utf8')
  27. const cliManifest = JSON.parse(readFileSync(resolve(root, 'apps/cli/package.json'), 'utf8')) as {
  28. dependencies?: Record<string, string>
  29. devDependencies?: Record<string, string>
  30. }
  31. const runtimeManifest = JSON.parse(readFileSync(resolve(root, 'python/sdk-runtime/package.json'), 'utf8')) as {
  32. dependencies?: Record<string, string>
  33. }
  34. expect(existsSync(resolve(root, 'apps/cli/src/runtime-bootstrap.ts'))).toBe(false)
  35. expect(cliConfig).not.toContain('runtime-bootstrap')
  36. expect(cliTsconfig).not.toContain('packages/subprocess/subprocess-local')
  37. expect(cliManifest.dependencies).not.toHaveProperty('@deepseek-ai/dsh-subprocess-local')
  38. expect(cliManifest.devDependencies).toHaveProperty('@deepseek-ai/dsh-subprocess-local')
  39. expect(runtimeManifest.dependencies).toHaveProperty('@deepseek-ai/dsh-subprocess-local')
  40. expect(bootstrap).toContain("import('@deepseek-ai/dsh/lib/bin.js')")
  41. expect(bootstrap).toContain('await runCli()')
  42. expect(bootstrap).toContain("import('@deepseek-ai/dsh-subprocess-local/runner')")
  43. expect(bootstrap).toContain('await runSelectedSubprocessRunner(selection)')
  44. })
  45. it('runs pnpm through its JavaScript entrypoint without a command shell', () => {
  46. const result = run(
  47. { npm_execpath: 'C:\\tools\\pnpm.cjs' },
  48. '--skip-build',
  49. '--dry-run',
  50. '--targets=node24-macos-arm64',
  51. )
  52. expect(result.status).toBe(0)
  53. expect(result.stdout).toContain(`${process.execPath} C:\\tools\\pnpm.cjs run verify-runtime-closure`)
  54. expect(result.stdout).toContain(`${process.execPath} C:\\tools\\pnpm.cjs --filter dsh-python-runtime-closure deploy`)
  55. expect(result.stdout).toContain('python/sdk-runtime/runtime-bootstrap.mjs')
  56. expect(result.stdout).toContain('runtime/node/runtime-bootstrap.mjs')
  57. expect(result.stdout).toContain(`${process.execPath} C:\\tools\\pnpm.cjs exec pkg`)
  58. expect(result.stdout).not.toMatch(/pnpm\.cmd/i)
  59. })
  60. it('resolves the pnpm package behind a Windows command shim', () => {
  61. const setup = mkdtempSync(join(tmpdir(), 'dsh-pnpm-home-'))
  62. temporaryDirectories.push(setup)
  63. const home = join(setup, 'node_modules', '.bin')
  64. const entrypoint = join(setup, 'node_modules', 'pnpm', 'bin', 'pnpm.mjs')
  65. mkdirSync(home, { recursive: true })
  66. mkdirSync(dirname(entrypoint), { recursive: true })
  67. writeFileSync(entrypoint, '')
  68. const result = run(
  69. { npm_execpath: 'C:\\tools\\pnpm.cmd', PNPM_HOME: home },
  70. '--skip-build',
  71. '--dry-run',
  72. '--targets=node24-macos-arm64',
  73. )
  74. expect(result.status).toBe(0)
  75. expect(result.stdout).toContain(`${process.execPath} ${entrypoint} run verify-runtime-closure`)
  76. expect(result.stdout).not.toMatch(/pnpm\.cmd/i)
  77. })
  78. it('rejects a Windows arm64 product before any build step', () => {
  79. const result = run(
  80. { npm_execpath: 'C:\\tools\\pnpm.cjs' },
  81. '--skip-build',
  82. '--dry-run',
  83. '--targets=node24-win-arm64',
  84. )
  85. expect(result.status).not.toBe(0)
  86. expect(result.stderr).toContain('Windows supports x64 only')
  87. expect(result.stdout).toBe('')
  88. })
  89. })
  90. function isolatedPnpmEnvironment(overrides: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
  91. const environment = Object.fromEntries(
  92. Object.entries(process.env).filter(([key]) => !['npm_execpath', 'pnpm_home'].includes(key.toLowerCase())),
  93. )
  94. return { ...environment, ...overrides }
  95. }