|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { access, cp, mkdtemp, readFile, rm } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | +import process from 'node:process' |
| 6 | +import { afterEach, describe, expect, test } from 'vitest' |
| 7 | + |
| 8 | +const cliPath = path.resolve(import.meta.dirname, '../src/run.ts') |
| 9 | +const fixturesPath = path.resolve(import.meta.dirname, 'fixtures/monorepo') |
| 10 | +const temporaryDirectories: string[] = [] |
| 11 | + |
| 12 | +async function createFixture(relativePath: string): Promise<string> { |
| 13 | + const directory = await mkdtemp(path.join(tmpdir(), 'tsdown-migrate-')) |
| 14 | + temporaryDirectories.push(directory) |
| 15 | + await cp(path.join(fixturesPath, relativePath), directory, { |
| 16 | + recursive: true, |
| 17 | + }) |
| 18 | + return directory |
| 19 | +} |
| 20 | + |
| 21 | +function runMigration(cwd: string, args: string[]) { |
| 22 | + return spawnSync(process.execPath, [cliPath, ...args], { |
| 23 | + cwd, |
| 24 | + encoding: 'utf8', |
| 25 | + env: { ...process.env, NO_COLOR: '1' }, |
| 26 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 27 | + timeout: 5_000, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +afterEach(async () => { |
| 32 | + await Promise.all( |
| 33 | + temporaryDirectories |
| 34 | + .splice(0) |
| 35 | + .map((directory) => rm(directory, { force: true, recursive: true })), |
| 36 | + ) |
| 37 | +}) |
| 38 | + |
| 39 | +describe('non-interactive CLI', () => { |
| 40 | + test('requires explicit confirmation before changing files', async () => { |
| 41 | + const directory = await createFixture('packages/pkg1') |
| 42 | + const packageJsonPath = path.join(directory, 'package.json') |
| 43 | + const packageJson = await readFile(packageJsonPath, 'utf8') |
| 44 | + |
| 45 | + const result = runMigration(directory, []) |
| 46 | + |
| 47 | + expect(result.status).toBe(1) |
| 48 | + expect(result.stderr).toContain( |
| 49 | + 'Non-interactive migration requires explicit confirmation. Re-run with --yes.', |
| 50 | + ) |
| 51 | + expect(result.stderr).not.toContain('TTY initialization failed') |
| 52 | + expect(await readFile(packageJsonPath, 'utf8')).toBe(packageJson) |
| 53 | + await expect( |
| 54 | + access(path.join(directory, 'tsdown.config.ts')), |
| 55 | + ).rejects.toThrow() |
| 56 | + }) |
| 57 | + |
| 58 | + test('migrates without prompts when installation is skipped', async () => { |
| 59 | + const directory = await createFixture('packages/pkg1') |
| 60 | + |
| 61 | + const result = runMigration(directory, ['--yes', '--no-install']) |
| 62 | + |
| 63 | + expect(result.status).toBe(0) |
| 64 | + |
| 65 | + const packageJson = JSON.parse( |
| 66 | + await readFile(path.join(directory, 'package.json'), 'utf8'), |
| 67 | + ) |
| 68 | + expect(packageJson.devDependencies).toHaveProperty('tsdown') |
| 69 | + expect(packageJson.devDependencies).not.toHaveProperty('tsup') |
| 70 | + await expect( |
| 71 | + access(path.join(directory, 'tsdown.config.ts')), |
| 72 | + ).resolves.toBeUndefined() |
| 73 | + await expect( |
| 74 | + access(path.join(directory, 'tsup.config.ts')), |
| 75 | + ).rejects.toThrow() |
| 76 | + }) |
| 77 | + |
| 78 | + test('fails before changing files when no package manager is detected', async () => { |
| 79 | + const directory = await createFixture('packages/pkg1') |
| 80 | + const packageJsonPath = path.join(directory, 'package.json') |
| 81 | + const packageJson = await readFile(packageJsonPath, 'utf8') |
| 82 | + |
| 83 | + const result = runMigration(directory, ['--yes']) |
| 84 | + |
| 85 | + expect(result.status).toBe(1) |
| 86 | + expect(result.stderr).toContain( |
| 87 | + 'Unable to detect a package manager in a non-interactive environment.', |
| 88 | + ) |
| 89 | + expect(await readFile(packageJsonPath, 'utf8')).toBe(packageJson) |
| 90 | + await expect( |
| 91 | + access(path.join(directory, 'tsdown.config.ts')), |
| 92 | + ).rejects.toThrow() |
| 93 | + }) |
| 94 | + |
| 95 | + test('uses an explicitly selected package manager', async () => { |
| 96 | + const directory = await createFixture('packages/pkg1') |
| 97 | + |
| 98 | + const result = runMigration(directory, [ |
| 99 | + '--dry-run', |
| 100 | + '--package-manager', |
| 101 | + 'pnpm', |
| 102 | + ]) |
| 103 | + const output = `${result.stdout}${result.stderr}` |
| 104 | + |
| 105 | + expect(result.status).toBe(0) |
| 106 | + expect(output).not.toContain('Choose the agent') |
| 107 | + expect(output).not.toContain('TTY initialization failed') |
| 108 | + }) |
| 109 | + |
| 110 | + test('auto-detects the package manager', async () => { |
| 111 | + const directory = await createFixture('apps/pkg3') |
| 112 | + |
| 113 | + const result = runMigration(directory, ['--dry-run']) |
| 114 | + const output = `${result.stdout}${result.stderr}` |
| 115 | + |
| 116 | + expect(result.status).toBe(0) |
| 117 | + expect(output).not.toContain('Choose the agent') |
| 118 | + expect(output).not.toContain('TTY initialization failed') |
| 119 | + }) |
| 120 | + |
| 121 | + test('rejects an unsupported package manager', async () => { |
| 122 | + const directory = await createFixture('packages/pkg1') |
| 123 | + |
| 124 | + const result = runMigration(directory, [ |
| 125 | + '--dry-run', |
| 126 | + '--package-manager', |
| 127 | + 'invalid', |
| 128 | + ]) |
| 129 | + |
| 130 | + expect(result.status).toBe(1) |
| 131 | + expect(result.stderr).toContain('Unsupported package manager "invalid".') |
| 132 | + }) |
| 133 | +}) |
0 commit comments