From 2bbc99f14925d7605ddc41673f8459f97dd6a441 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Fri, 22 Aug 2025 22:54:14 -0500 Subject: [PATCH 01/16] feat: add --all flag to migrate command for migrating all packages and implement tests --- src/commands/migrate.meta.ts | 13 +++-- src/commands/migrate.ts | 8 ++- src/test/migrate.test.ts | 81 +++++++++++++++++++++++++++ test/fixtures/multi-deps/lib/main.js | 7 +++ test/fixtures/multi-deps/package.json | 14 +++++ 5 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 src/test/migrate.test.ts create mode 100644 test/fixtures/multi-deps/lib/main.js create mode 100644 test/fixtures/multi-deps/package.json diff --git a/src/commands/migrate.meta.ts b/src/commands/migrate.meta.ts index c21b769..ff43584 100644 --- a/src/commands/migrate.meta.ts +++ b/src/commands/migrate.meta.ts @@ -2,20 +2,25 @@ export const meta = { name: 'migrate', description: 'Migrate from a package to a more performant alternative.', args: { - 'dry-run': { + all: { type: 'boolean', default: false, - description: `Don't apply any fixes, only show what would change.` + description: 'Run all available migrations' }, - interactive: { + 'dry-run': { type: 'boolean', default: false, - description: 'Run in interactive mode.' + description: `Don't apply any fixes, only show what would change.` }, include: { type: 'string', default: '**/*.{ts,js}', description: 'Files to migrate' + }, + interactive: { + type: 'boolean', + default: false, + description: 'Run in interactive mode.' } } } as const; diff --git a/src/commands/migrate.ts b/src/commands/migrate.ts index fe3b4c6..bc89585 100644 --- a/src/commands/migrate.ts +++ b/src/commands/migrate.ts @@ -14,6 +14,7 @@ export async function run(ctx: CommandContext) { const dryRun = ctx.values['dry-run'] === true; const interactive = ctx.values.interactive === true; const include = ctx.values.include; + const all = ctx.values.all === true; const fileSystem = new LocalFileSystem(process.cwd()); const packageJson = await getPackageJson(fileSystem); @@ -37,6 +38,11 @@ export async function run(ctx: CommandContext) { .map((rep) => rep.from) ); + // If --all flag is used, add all available migrations + if (all) { + targetModules.push(...fixableReplacementsTargets); + } + if (interactive) { const additionalTargets = await prompts.autocompleteMultiselect({ message: 'Select packages to migrate', @@ -62,7 +68,7 @@ export async function run(ctx: CommandContext) { if (targetModules.length === 0) { prompts.cancel( - 'Error: Please specify a package to migrate. For example, `migrate chalk`' + 'Error: Please specify a package to migrate. For example, `migrate chalk` or use `--all` to migrate all available packages' ); return; } diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts new file mode 100644 index 0000000..0c4189a --- /dev/null +++ b/src/test/migrate.test.ts @@ -0,0 +1,81 @@ +import {describe, it, expect, vi, beforeEach} from 'vitest'; +import {run} from '../commands/migrate.js'; +import {meta} from '../commands/migrate.meta.js'; +import type {CommandContext} from 'gunshi'; + +// Mock dependencies +vi.mock('@clack/prompts', () => ({ + intro: vi.fn(), + cancel: vi.fn(), + log: { + message: vi.fn() + }, + taskLog: vi.fn(() => ({ + message: vi.fn(), + success: vi.fn() + })), + outro: vi.fn(), + isCancel: vi.fn(() => false) +})); + +vi.mock('node:fs/promises', () => ({ + readFile: vi.fn(() => Promise.resolve('import chalk from "chalk";')), + writeFile: vi.fn(() => Promise.resolve()) +})); + +vi.mock('tinyglobby', () => ({ + glob: vi.fn(() => Promise.resolve(['/test/file.js'])) +})); + +vi.mock('../local-file-system.js', () => ({ + LocalFileSystem: vi.fn() +})); + +vi.mock('../file-system-utils.js', () => ({ + getPackageJson: vi.fn(() => Promise.resolve({ + dependencies: {chalk: '^4.0.0'}, + devDependencies: {} + })) +})); + +describe('migrate command', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it('should handle --all flag correctly', async () => { + const mockContext = { + positionals: ['migrate'], + values: { + 'dry-run': false, + interactive: false, + include: '**/*.{ts,js}', + all: true + }, + env: {cwd: '/test'} + } as CommandContext; + + await run(mockContext); + + // Verify that the command runs without errors when --all is used + expect(true).toBe(true); // Basic assertion that the function completes + }); + + it('should handle specific package migration', async () => { + const mockContext = { + positionals: ['migrate', 'chalk'], + values: { + 'dry-run': false, + interactive: false, + include: '**/*.{ts,js}', + all: false + }, + env: {cwd: '/test'} + } as CommandContext; + + await run(mockContext); + + // Verify that the command runs without errors when specific package is provided + expect(true).toBe(true); // Basic assertion that the function completes + }); +}); diff --git a/test/fixtures/multi-deps/lib/main.js b/test/fixtures/multi-deps/lib/main.js new file mode 100644 index 0000000..5a3b2b7 --- /dev/null +++ b/test/fixtures/multi-deps/lib/main.js @@ -0,0 +1,7 @@ +import * as pc from 'picocolors'; +import objectAssign from 'object-assign'; +import isString from 'is-string'; + +console.log(pc.blue('Hello World')); +console.log(objectAssign({}, {a: 1})); +console.log(isString('test')); diff --git a/test/fixtures/multi-deps/package.json b/test/fixtures/multi-deps/package.json new file mode 100644 index 0000000..e654c87 --- /dev/null +++ b/test/fixtures/multi-deps/package.json @@ -0,0 +1,14 @@ +{ + "name": "multi-deps", + "type": "module", + "private": true, + "version": "0.0.1", + "main": "lib/main.js", + "dependencies": { + "chalk": "^4.0.0", + "object-assign": "^4.1.1" + }, + "devDependencies": { + "is-string": "^1.0.5" + } +} From 084575c493f651affefb86836b5fee36ba16a60c Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Fri, 22 Aug 2025 22:55:26 -0500 Subject: [PATCH 02/16] format --- src/test/migrate.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index 0c4189a..f682425 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -32,10 +32,12 @@ vi.mock('../local-file-system.js', () => ({ })); vi.mock('../file-system-utils.js', () => ({ - getPackageJson: vi.fn(() => Promise.resolve({ - dependencies: {chalk: '^4.0.0'}, - devDependencies: {} - })) + getPackageJson: vi.fn(() => + Promise.resolve({ + dependencies: {chalk: '^4.0.0'}, + devDependencies: {} + }) + ) })); describe('migrate command', () => { From 64104069b9c90b8b62f0b52dc6155d0bdf3bdb17 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Wed, 27 Aug 2025 09:00:20 -0500 Subject: [PATCH 03/16] chore: migrate tests to spawn actual CLI process and optimize array handling --- src/commands/migrate.ts | 4 +- src/test/__snapshots__/migrate.test.ts.snap | 83 +++++++++++++ src/test/cli.test.ts | 30 +---- src/test/migrate.test.ts | 127 +++++++++----------- src/test/utils.ts | 46 +++++++ 5 files changed, 191 insertions(+), 99 deletions(-) create mode 100644 src/test/__snapshots__/migrate.test.ts.snap diff --git a/src/commands/migrate.ts b/src/commands/migrate.ts index bc89585..36d218d 100644 --- a/src/commands/migrate.ts +++ b/src/commands/migrate.ts @@ -40,7 +40,9 @@ export async function run(ctx: CommandContext) { // If --all flag is used, add all available migrations if (all) { - targetModules.push(...fixableReplacementsTargets); + for (const target of fixableReplacementsTargets) { + targetModules.push(target); + } } if (interactive) { diff --git a/src/test/__snapshots__/migrate.test.ts.snap b/src/test/__snapshots__/migrate.test.ts.snap new file mode 100644 index 0000000..62d2bc3 --- /dev/null +++ b/src/test/__snapshots__/migrate.test.ts.snap @@ -0,0 +1,83 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`migrate command > should handle --all flag correctly 1`] = ` +"e18e (cli ) + +┌ Migrating packages... +│ +│ Targets: chalk +│ +◇ /tmp/reporter-test-XXXXXX/lib/main.js... +│ +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ +◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +│ +└ Migration complete. + +" +`; + +exports[`migrate command > should handle custom include pattern 1`] = ` +"e18e (cli ) + +┌ Migrating packages... +│ +│ Targets: chalk +│ +◇ /tmp/reporter-test-XXXXXX/lib/main.js... +│ +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ +◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +│ +└ Migration complete. + +" +`; + +exports[`migrate command > should handle interactive mode 1`] = ` +"e18e (cli ) + +┌ Migrating packages... +[?25l│ +◆ Select packages to migrate + +│ Search: _ +│ ◼ chalk +│ ↑/↓ to navigate • Space: select • Enter: confirm • Type: to search +└" +`; + +exports[`migrate command > should handle specific package migration 1`] = ` +"e18e (cli ) + +┌ Migrating packages... +│ +│ Targets: chalk +│ +◇ /tmp/reporter-test-XXXXXX/lib/main.js... +│ +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ +◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +│ +└ Migration complete. + +" +`; diff --git a/src/test/cli.test.ts b/src/test/cli.test.ts index a170d10..8d036f3 100644 --- a/src/test/cli.test.ts +++ b/src/test/cli.test.ts @@ -1,17 +1,11 @@ import {describe, it, expect, beforeAll, afterAll} from 'vitest'; -import {spawn} from 'node:child_process'; import path from 'node:path'; import fs from 'node:fs/promises'; -import {createTempDir, cleanupTempDir, createTestPackage} from './utils.js'; +import {createTempDir, cleanupTempDir, createTestPackage, runCliProcess, stripVersion} from './utils.js'; import {pack as packAsTarball} from '@publint/pack'; let mockTarballPath: string; let tempDir: string; -const stripVersion = (str: string): string => - str.replace( - new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), - '(cli )' - ); beforeAll(async () => { // Create a temporary directory for the test package @@ -59,28 +53,6 @@ afterAll(async () => { await cleanupTempDir(tempDir); }); -function runCliProcess( - args: string[], - cwd?: string -): Promise<{stdout: string; stderr: string; code: number | null}> { - return new Promise((resolve) => { - const cliPath = path.resolve(__dirname, '../../lib/cli.js'); - const proc = spawn('node', [cliPath, ...args], { - env: process.env, - cwd: cwd || process.cwd() - }); - let stdout = ''; - let stderr = ''; - proc.stdout.on('data', (data) => (stdout += data.toString())); - proc.stderr.on('data', (data) => (stderr += data.toString())); - proc.on('error', (err) => { - stderr += String(err); - resolve({stdout, stderr, code: 1}); - }); - proc.on('close', (code) => resolve({stdout, stderr, code})); - }); -} - describe('CLI', () => { it('should run successfully with default options', async () => { const {stdout, stderr, code} = await runCliProcess( diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index f682425..57e5073 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -1,83 +1,72 @@ -import {describe, it, expect, vi, beforeEach} from 'vitest'; -import {run} from '../commands/migrate.js'; -import {meta} from '../commands/migrate.meta.js'; -import type {CommandContext} from 'gunshi'; +import {describe, it, expect, beforeAll, afterAll} from 'vitest'; +import path from 'node:path'; +import fs from 'node:fs/promises'; +import {createTempDir, cleanupTempDir, runCliProcess, stripVersion} from './utils.js'; -// Mock dependencies -vi.mock('@clack/prompts', () => ({ - intro: vi.fn(), - cancel: vi.fn(), - log: { - message: vi.fn() - }, - taskLog: vi.fn(() => ({ - message: vi.fn(), - success: vi.fn() - })), - outro: vi.fn(), - isCancel: vi.fn(() => false) -})); +let tempDir: string; -vi.mock('node:fs/promises', () => ({ - readFile: vi.fn(() => Promise.resolve('import chalk from "chalk";')), - writeFile: vi.fn(() => Promise.resolve()) -})); +beforeAll(async () => { + // Create a temporary directory for the test + tempDir = await createTempDir(); -vi.mock('tinyglobby', () => ({ - glob: vi.fn(() => Promise.resolve(['/test/file.js'])) -})); - -vi.mock('../local-file-system.js', () => ({ - LocalFileSystem: vi.fn() -})); + // Copy the basic-chalk fixture to the temp dir + const fixturePath = path.join(process.cwd(), 'test/fixtures/basic-chalk'); + await fs.cp(fixturePath, tempDir, {recursive: true}); +}); -vi.mock('../file-system-utils.js', () => ({ - getPackageJson: vi.fn(() => - Promise.resolve({ - dependencies: {chalk: '^4.0.0'}, - devDependencies: {} - }) - ) -})); +afterAll(async () => { + await cleanupTempDir(tempDir); +}); describe('migrate command', () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - it('should handle --all flag correctly', async () => { - const mockContext = { - positionals: ['migrate'], - values: { - 'dry-run': false, - interactive: false, - include: '**/*.{ts,js}', - all: true - }, - env: {cwd: '/test'} - } as CommandContext; - - await run(mockContext); - - // Verify that the command runs without errors when --all is used - expect(true).toBe(true); // Basic assertion that the function completes + const {stdout, stderr, code} = await runCliProcess( + ['migrate', '--all', '--dry-run'], + tempDir + ); + + if (code !== 0) { + console.error('CLI Error:', stderr); + } + + expect(code).toBe(0); + expect(stripVersion(stdout)).toMatchSnapshot(); + expect(stderr).toBe(''); }); it('should handle specific package migration', async () => { - const mockContext = { - positionals: ['migrate', 'chalk'], - values: { - 'dry-run': false, - interactive: false, - include: '**/*.{ts,js}', - all: false - }, - env: {cwd: '/test'} - } as CommandContext; + const {stdout, stderr, code} = await runCliProcess( + ['migrate', 'chalk', '--dry-run'], + tempDir + ); + + expect(code).toBe(0); + expect(stripVersion(stdout)).toMatchSnapshot(); + expect(stderr).toBe(''); + }); - await run(mockContext); + it('should handle interactive mode', async () => { + // Test interactive mode by providing input to the prompt + // Press Enter to accept the default selection + const {stdout, stderr, code} = await runCliProcess( + ['migrate', '--interactive', 'chalk', '--dry-run'], + tempDir, + '\n' // Press Enter to accept default + ); + + expect(code).toBe(0); + expect(stripVersion(stdout)).toMatchSnapshot(); + expect(stderr).toBe(''); + }); - // Verify that the command runs without errors when specific package is provided - expect(true).toBe(true); // Basic assertion that the function completes + it('should handle custom include pattern', async () => { + const {stdout, stderr, code} = await runCliProcess( + ['migrate', 'chalk', '--include', '**/*.js', '--dry-run'], + tempDir + ); + + expect(code).toBe(0); + expect(stripVersion(stdout)).toMatchSnapshot(); + expect(stderr).toBe(''); }); }); diff --git a/src/test/utils.ts b/src/test/utils.ts index 072de60..3acb15e 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -1,6 +1,7 @@ import fs from 'node:fs/promises'; import path from 'node:path'; import os from 'node:os'; +import {spawn} from 'node:child_process'; export interface TestPackage { name: string; @@ -72,3 +73,48 @@ export function createMockTarball(files: Array<{name: string; content: any}>) { rootDir: 'package' }; } + +export function runCliProcess( + args: string[], + cwd?: string, + input?: string +): Promise<{stdout: string; stderr: string; code: number | null}> { + return new Promise((resolve) => { + const cliPath = path.resolve(__dirname, '../../lib/cli.js'); + const proc = spawn('node', [cliPath, ...args], { + env: process.env, + cwd: cwd || process.cwd(), + stdio: input ? ['pipe', 'pipe', 'pipe'] : ['inherit', 'pipe', 'pipe'] + }); + let stdout = ''; + let stderr = ''; + if (proc.stdout) { + proc.stdout.on('data', (data) => (stdout += data.toString())); + } + if (proc.stderr) { + proc.stderr.on('data', (data) => (stderr += data.toString())); + } + proc.on('error', (err) => { + stderr += String(err); + resolve({stdout, stderr, code: 1}); + }); + proc.on('close', (code) => resolve({stdout, stderr, code})); + + // If input is provided, write it to stdin + if (input && proc.stdin) { + proc.stdin.write(input); + proc.stdin.end(); + } + }); +} + +export const stripVersion = (str: string): string => + str + .replace( + new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), + '(cli )' + ) + .replace( + /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, + '/tmp/reporter-test-XXXXXX' + ); From 26d7f918efd654fa9c100b12c480e1050806aa4d Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Wed, 27 Aug 2025 09:01:39 -0500 Subject: [PATCH 04/16] format --- src/test/cli.test.ts | 8 +++++++- src/test/migrate.test.ts | 17 +++++++++++------ src/test/utils.ts | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/test/cli.test.ts b/src/test/cli.test.ts index 8d036f3..eaf83f5 100644 --- a/src/test/cli.test.ts +++ b/src/test/cli.test.ts @@ -1,7 +1,13 @@ import {describe, it, expect, beforeAll, afterAll} from 'vitest'; import path from 'node:path'; import fs from 'node:fs/promises'; -import {createTempDir, cleanupTempDir, createTestPackage, runCliProcess, stripVersion} from './utils.js'; +import { + createTempDir, + cleanupTempDir, + createTestPackage, + runCliProcess, + stripVersion +} from './utils.js'; import {pack as packAsTarball} from '@publint/pack'; let mockTarballPath: string; diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index 57e5073..5122d37 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -1,7 +1,12 @@ import {describe, it, expect, beforeAll, afterAll} from 'vitest'; import path from 'node:path'; import fs from 'node:fs/promises'; -import {createTempDir, cleanupTempDir, runCliProcess, stripVersion} from './utils.js'; +import { + createTempDir, + cleanupTempDir, + runCliProcess, + stripVersion +} from './utils.js'; let tempDir: string; @@ -24,11 +29,11 @@ describe('migrate command', () => { ['migrate', '--all', '--dry-run'], tempDir ); - + if (code !== 0) { console.error('CLI Error:', stderr); } - + expect(code).toBe(0); expect(stripVersion(stdout)).toMatchSnapshot(); expect(stderr).toBe(''); @@ -39,7 +44,7 @@ describe('migrate command', () => { ['migrate', 'chalk', '--dry-run'], tempDir ); - + expect(code).toBe(0); expect(stripVersion(stdout)).toMatchSnapshot(); expect(stderr).toBe(''); @@ -53,7 +58,7 @@ describe('migrate command', () => { tempDir, '\n' // Press Enter to accept default ); - + expect(code).toBe(0); expect(stripVersion(stdout)).toMatchSnapshot(); expect(stderr).toBe(''); @@ -64,7 +69,7 @@ describe('migrate command', () => { ['migrate', 'chalk', '--include', '**/*.js', '--dry-run'], tempDir ); - + expect(code).toBe(0); expect(stripVersion(stdout)).toMatchSnapshot(); expect(stderr).toBe(''); diff --git a/src/test/utils.ts b/src/test/utils.ts index 3acb15e..ec27a69 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -99,7 +99,7 @@ export function runCliProcess( resolve({stdout, stderr, code: 1}); }); proc.on('close', (code) => resolve({stdout, stderr, code})); - + // If input is provided, write it to stdin if (input && proc.stdin) { proc.stdin.write(input); From 7279f67f71262611bc55b071fc9dee3bf03d2332 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Wed, 27 Aug 2025 09:12:49 -0500 Subject: [PATCH 05/16] fix: update stripVersion function to handle additional temporary file paths --- src/test/utils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/utils.ts b/src/test/utils.ts index ec27a69..29a6731 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -117,4 +117,8 @@ export const stripVersion = (str: string): string => .replace( /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX' + ) + .replace( + /\/tmp\/reporter-test-[^/]+/g, + '/tmp/reporter-test-XXXXXX' ); From 8502fa72839506c3453ba3e8f625ee8ee4563b5a Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Wed, 27 Aug 2025 09:14:05 -0500 Subject: [PATCH 06/16] format --- src/test/utils.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/utils.ts b/src/test/utils.ts index 29a6731..2b94c95 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -118,7 +118,4 @@ export const stripVersion = (str: string): string => /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX' ) - .replace( - /\/tmp\/reporter-test-[^/]+/g, - '/tmp/reporter-test-XXXXXX' - ); + .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); From 643c46f0711201db4011c9c6a636cae0f7a3400f Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Wed, 27 Aug 2025 09:58:58 -0500 Subject: [PATCH 07/16] test: update migrate tests to check for essential output content instead of exact formatting --- src/test/migrate.test.ts | 23 +++++++++++++++++++---- src/test/utils.ts | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index 5122d37..e15418a 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -35,7 +35,11 @@ describe('migrate command', () => { } expect(code).toBe(0); - expect(stripVersion(stdout)).toMatchSnapshot(); + // Check for essential content rather than exact formatting + const normalizedOutput = stripVersion(stdout); + expect(normalizedOutput).toContain('Migrating packages'); + expect(normalizedOutput).toContain('chalk'); + expect(normalizedOutput).toContain('Migration complete'); expect(stderr).toBe(''); }); @@ -46,7 +50,11 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout)).toMatchSnapshot(); + // Check for essential content rather than exact formatting + const normalizedOutput = stripVersion(stdout); + expect(normalizedOutput).toContain('Migrating packages'); + expect(normalizedOutput).toContain('chalk'); + expect(normalizedOutput).toContain('Migration complete'); expect(stderr).toBe(''); }); @@ -60,7 +68,10 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout)).toMatchSnapshot(); + // Check for essential content rather than exact formatting + const normalizedOutput = stripVersion(stdout); + expect(normalizedOutput).toContain('Migrating packages'); + expect(normalizedOutput).toContain('Select packages to migrate'); expect(stderr).toBe(''); }); @@ -71,7 +82,11 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout)).toMatchSnapshot(); + // Check for essential content rather than exact formatting + const normalizedOutput = stripVersion(stdout); + expect(normalizedOutput).toContain('Migrating packages'); + expect(normalizedOutput).toContain('chalk'); + expect(normalizedOutput).toContain('Migration complete'); expect(stderr).toBe(''); }); }); diff --git a/src/test/utils.ts b/src/test/utils.ts index 2b94c95..a872434 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -118,4 +118,4 @@ export const stripVersion = (str: string): string => /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX' ) - .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); + .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); \ No newline at end of file From 0aa351b6c9a108a295987256306365c251d9464d Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Wed, 27 Aug 2025 10:01:05 -0500 Subject: [PATCH 08/16] format --- src/test/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/utils.ts b/src/test/utils.ts index a872434..2b94c95 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -118,4 +118,4 @@ export const stripVersion = (str: string): string => /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX' ) - .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); \ No newline at end of file + .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); From 888156ae65c56c6a584b9256efbf6c2a27bdd3b5 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Thu, 28 Aug 2025 00:34:45 -0500 Subject: [PATCH 09/16] Apply suggestions. --- src/test/__snapshots__/migrate.test.ts.snap | 58 +++++++++++++ src/test/cli.test.ts | 4 +- src/test/migrate.test.ts | 93 ++++++++++++--------- src/test/utils.ts | 8 +- 4 files changed, 118 insertions(+), 45 deletions(-) diff --git a/src/test/__snapshots__/migrate.test.ts.snap b/src/test/__snapshots__/migrate.test.ts.snap index 62d2bc3..1205fa2 100644 --- a/src/test/__snapshots__/migrate.test.ts.snap +++ b/src/test/__snapshots__/migrate.test.ts.snap @@ -1,5 +1,35 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`migrate command > should actually modify files when not using --dry-run 1`] = ` +"e18e (cli ) + +┌ Migrating packages... +│ +│ Targets: chalk +│ +◇ /tmp/reporter-test-XXXXXX/lib/main.js... +│ +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ migrating chalk to picocolors +│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ +◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +│ +└ Migration complete. + +" +`; + +exports[`migrate command > should actually modify files when not using --dry-run 2`] = ` +"import * as pc from 'picocolors'; + +console.log(pc.cyan('I am _so_ cyan')); +" +`; + exports[`migrate command > should handle --all flag correctly 1`] = ` "e18e (cli ) @@ -23,6 +53,13 @@ exports[`migrate command > should handle --all flag correctly 1`] = ` " `; +exports[`migrate command > should handle --all flag correctly 2`] = ` +"import chalk from 'chalk'; + +console.log(chalk.cyan('I am _so_ cyan')); +" +`; + exports[`migrate command > should handle custom include pattern 1`] = ` "e18e (cli ) @@ -46,6 +83,13 @@ exports[`migrate command > should handle custom include pattern 1`] = ` " `; +exports[`migrate command > should handle custom include pattern 2`] = ` +"import chalk from 'chalk'; + +console.log(chalk.cyan('I am _so_ cyan')); +" +`; + exports[`migrate command > should handle interactive mode 1`] = ` "e18e (cli ) @@ -59,6 +103,13 @@ exports[`migrate command > should handle interactive mode 1`] = ` └" `; +exports[`migrate command > should handle interactive mode 2`] = ` +"import chalk from 'chalk'; + +console.log(chalk.cyan('I am _so_ cyan')); +" +`; + exports[`migrate command > should handle specific package migration 1`] = ` "e18e (cli ) @@ -81,3 +132,10 @@ exports[`migrate command > should handle specific package migration 1`] = ` " `; + +exports[`migrate command > should handle specific package migration 2`] = ` +"import chalk from 'chalk'; + +console.log(chalk.cyan('I am _so_ cyan')); +" +`; diff --git a/src/test/cli.test.ts b/src/test/cli.test.ts index eaf83f5..dc89cf4 100644 --- a/src/test/cli.test.ts +++ b/src/test/cli.test.ts @@ -69,7 +69,7 @@ describe('CLI', () => { console.error('CLI Error:', stderr); } expect(code).toBe(0); - expect(stripVersion(stdout)).toMatchSnapshot(); + expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toBe(''); }); @@ -79,7 +79,7 @@ describe('CLI', () => { tempDir ); expect(code).toBe(0); - expect(stripVersion(stdout)).toMatchSnapshot(); + expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toMatchSnapshot(); }); }); diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index e15418a..b82f879 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -1,4 +1,4 @@ -import {describe, it, expect, beforeAll, afterAll} from 'vitest'; +import {describe, it, expect, beforeEach, afterEach} from 'vitest'; import path from 'node:path'; import fs from 'node:fs/promises'; import { @@ -8,39 +8,36 @@ import { stripVersion } from './utils.js'; -let tempDir: string; - -beforeAll(async () => { - // Create a temporary directory for the test - tempDir = await createTempDir(); +describe('migrate command', () => { + let tempDir: string; - // Copy the basic-chalk fixture to the temp dir - const fixturePath = path.join(process.cwd(), 'test/fixtures/basic-chalk'); - await fs.cp(fixturePath, tempDir, {recursive: true}); -}); + beforeEach(async () => { + // Create a temporary directory for each test + tempDir = await createTempDir(); -afterAll(async () => { - await cleanupTempDir(tempDir); -}); + // Copy the basic-chalk fixture to the temp dir + const fixturePath = path.join(process.cwd(), 'test/fixtures/basic-chalk'); + await fs.cp(fixturePath, tempDir, {recursive: true}); + }); -describe('migrate command', () => { + afterEach(async () => { + // Clean up the temporary directory after each test + await cleanupTempDir(tempDir); + }); it('should handle --all flag correctly', async () => { const {stdout, stderr, code} = await runCliProcess( ['migrate', '--all', '--dry-run'], tempDir ); - if (code !== 0) { - console.error('CLI Error:', stderr); - } - expect(code).toBe(0); - // Check for essential content rather than exact formatting - const normalizedOutput = stripVersion(stdout); - expect(normalizedOutput).toContain('Migrating packages'); - expect(normalizedOutput).toContain('chalk'); - expect(normalizedOutput).toContain('Migration complete'); + expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toBe(''); + + // Check that the file was NOT modified in dry-run mode + const mainJsPath = path.join(tempDir, 'lib/main.js'); + const fileContent = await fs.readFile(mainJsPath, 'utf-8'); + expect(fileContent).toMatchSnapshot(); }); it('should handle specific package migration', async () => { @@ -50,29 +47,32 @@ describe('migrate command', () => { ); expect(code).toBe(0); - // Check for essential content rather than exact formatting - const normalizedOutput = stripVersion(stdout); - expect(normalizedOutput).toContain('Migrating packages'); - expect(normalizedOutput).toContain('chalk'); - expect(normalizedOutput).toContain('Migration complete'); + expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toBe(''); + + // Check that the file was NOT modified in dry-run mode + const mainJsPath = path.join(tempDir, 'lib/main.js'); + const fileContent = await fs.readFile(mainJsPath, 'utf-8'); + expect(fileContent).toMatchSnapshot(); }); it('should handle interactive mode', async () => { // Test interactive mode by providing input to the prompt // Press Enter to accept the default selection const {stdout, stderr, code} = await runCliProcess( - ['migrate', '--interactive', 'chalk', '--dry-run'], + ['migrate', '--all', '--interactive', '--dry-run'], tempDir, '\n' // Press Enter to accept default ); expect(code).toBe(0); - // Check for essential content rather than exact formatting - const normalizedOutput = stripVersion(stdout); - expect(normalizedOutput).toContain('Migrating packages'); - expect(normalizedOutput).toContain('Select packages to migrate'); + expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toBe(''); + + // Check that the file was NOT modified in dry-run mode + const mainJsPath = path.join(tempDir, 'lib/main.js'); + const fileContent = await fs.readFile(mainJsPath, 'utf-8'); + expect(fileContent).toMatchSnapshot(); }); it('should handle custom include pattern', async () => { @@ -82,11 +82,28 @@ describe('migrate command', () => { ); expect(code).toBe(0); - // Check for essential content rather than exact formatting - const normalizedOutput = stripVersion(stdout); - expect(normalizedOutput).toContain('Migrating packages'); - expect(normalizedOutput).toContain('chalk'); - expect(normalizedOutput).toContain('Migration complete'); + expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toBe(''); + + // Check that the file was NOT modified in dry-run mode + const mainJsPath = path.join(tempDir, 'lib/main.js'); + const fileContent = await fs.readFile(mainJsPath, 'utf-8'); + expect(fileContent).toMatchSnapshot(); + }); + + it('should actually modify files when not using --dry-run', async () => { + const {stdout, stderr, code} = await runCliProcess( + ['migrate', 'chalk'], + tempDir + ); + + expect(code).toBe(0); + expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(stderr).toBe(''); + + // Check that the file was actually modified (chalk import should be replaced with picocolors) + const mainJsPath = path.join(tempDir, 'lib/main.js'); + const fileContent = await fs.readFile(mainJsPath, 'utf-8'); + expect(fileContent).toMatchSnapshot(); }); }); diff --git a/src/test/utils.ts b/src/test/utils.ts index 2b94c95..932f013 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -108,14 +108,12 @@ export function runCliProcess( }); } -export const stripVersion = (str: string): string => +export const stripVersion = (str: string, cwd: string = process.cwd()): string => str .replace( new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), '(cli )' ) - .replace( - /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, - '/tmp/reporter-test-XXXXXX' - ) + .replace(new RegExp(cwd.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), '{cwd}') + .replace(/\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX') .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); From e86e47dee119e0c4e7f4c35c81c8772b75889550 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Thu, 28 Aug 2025 00:35:25 -0500 Subject: [PATCH 10/16] format --- src/test/utils.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/test/utils.ts b/src/test/utils.ts index 932f013..a32e6ee 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -108,12 +108,21 @@ export function runCliProcess( }); } -export const stripVersion = (str: string, cwd: string = process.cwd()): string => +export const stripVersion = ( + str: string, + cwd: string = process.cwd() +): string => str .replace( new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), '(cli )' ) - .replace(new RegExp(cwd.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), '{cwd}') - .replace(/\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX') + .replace( + new RegExp(cwd.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), + '{cwd}' + ) + .replace( + /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, + '/tmp/reporter-test-XXXXXX' + ) .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); From fe3b06019ff79e1dc465353651417d6a55fdcca7 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Thu, 28 Aug 2025 10:02:43 -0500 Subject: [PATCH 11/16] apply more suggestions --- src/test/__snapshots__/migrate.test.ts.snap | 48 ++++++++++----------- src/test/migrate.test.ts | 10 ++--- src/test/utils.ts | 10 +---- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/test/__snapshots__/migrate.test.ts.snap b/src/test/__snapshots__/migrate.test.ts.snap index 1205fa2..7d8ba72 100644 --- a/src/test/__snapshots__/migrate.test.ts.snap +++ b/src/test/__snapshots__/migrate.test.ts.snap @@ -7,16 +7,16 @@ exports[`migrate command > should actually modify files when not using --dry-run │ │ Targets: chalk │ -◇ /tmp/reporter-test-XXXXXX/lib/main.js... +◇ /private{cwd}/lib/main.js... │ -│ loading /tmp/reporter-test-XXXXXX/lib/main.js -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ writing /private{cwd}/lib/main.js │ -◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +◆ /private{cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -37,16 +37,16 @@ exports[`migrate command > should handle --all flag correctly 1`] = ` │ │ Targets: chalk │ -◇ /tmp/reporter-test-XXXXXX/lib/main.js... +◇ /private{cwd}/lib/main.js... │ -│ loading /tmp/reporter-test-XXXXXX/lib/main.js -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ writing /private{cwd}/lib/main.js │ -◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +◆ /private{cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -67,16 +67,16 @@ exports[`migrate command > should handle custom include pattern 1`] = ` │ │ Targets: chalk │ -◇ /tmp/reporter-test-XXXXXX/lib/main.js... +◇ /private{cwd}/lib/main.js... │ -│ loading /tmp/reporter-test-XXXXXX/lib/main.js -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ writing /private{cwd}/lib/main.js │ -◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +◆ /private{cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -117,16 +117,16 @@ exports[`migrate command > should handle specific package migration 1`] = ` │ │ Targets: chalk │ -◇ /tmp/reporter-test-XXXXXX/lib/main.js... +◇ /private{cwd}/lib/main.js... │ -│ loading /tmp/reporter-test-XXXXXX/lib/main.js -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /tmp/reporter-test-XXXXXX/lib/main.js +│ loading /private{cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /tmp/reporter-test-XXXXXX/lib/main.js +│ writing /private{cwd}/lib/main.js │ -◆ /tmp/reporter-test-XXXXXX/lib/main.js (1 migrated) +◆ /private{cwd}/lib/main.js (1 migrated) │ └ Migration complete. diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index b82f879..76eeab7 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -31,7 +31,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -47,7 +47,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -66,7 +66,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -82,7 +82,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -98,7 +98,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was actually modified (chalk import should be replaced with picocolors) diff --git a/src/test/utils.ts b/src/test/utils.ts index a32e6ee..9b8b5e0 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -117,12 +117,4 @@ export const stripVersion = ( new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), '(cli )' ) - .replace( - new RegExp(cwd.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), - '{cwd}' - ) - .replace( - /\/private\/var\/folders\/[^/]+\/[^/]+\/T\/reporter-test-[^/]+/g, - '/tmp/reporter-test-XXXXXX' - ) - .replace(/\/tmp\/reporter-test-[^/]+/g, '/tmp/reporter-test-XXXXXX'); + .replaceAll(cwd, '{cwd}'); From 508f87ae32421c8cabd7b21bba12462e97f2001b Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Thu, 28 Aug 2025 10:55:48 -0500 Subject: [PATCH 12/16] test --- src/test/utils.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/utils.ts b/src/test/utils.ts index 9b8b5e0..38b7dca 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -117,4 +117,6 @@ export const stripVersion = ( new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), '(cli )' ) - .replaceAll(cwd, '{cwd}'); + .replaceAll(cwd, '{cwd}') + .replaceAll(cwd.replace('/var/folders', '/private/var/folders'), '{cwd}') + .replaceAll(cwd.replace('/private/var/folders', '/var/folders'), '{cwd}'); From 6ff434b644cbad93205dafd59922426b9618d3f1 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Thu, 28 Aug 2025 11:22:05 -0500 Subject: [PATCH 13/16] test-2 --- src/test/__snapshots__/migrate.test.ts.snap | 48 ++++++++++----------- src/test/cli.test.ts | 4 +- src/test/migrate.test.ts | 10 ++--- src/test/utils.ts | 17 +++++--- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/test/__snapshots__/migrate.test.ts.snap b/src/test/__snapshots__/migrate.test.ts.snap index 7d8ba72..f535260 100644 --- a/src/test/__snapshots__/migrate.test.ts.snap +++ b/src/test/__snapshots__/migrate.test.ts.snap @@ -7,16 +7,16 @@ exports[`migrate command > should actually modify files when not using --dry-run │ │ Targets: chalk │ -◇ /private{cwd}/lib/main.js... +◇ {cwd}/lib/main.js... │ -│ loading /private{cwd}/lib/main.js -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /private{cwd}/lib/main.js +│ writing {cwd}/lib/main.js │ -◆ /private{cwd}/lib/main.js (1 migrated) +◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -37,16 +37,16 @@ exports[`migrate command > should handle --all flag correctly 1`] = ` │ │ Targets: chalk │ -◇ /private{cwd}/lib/main.js... +◇ {cwd}/lib/main.js... │ -│ loading /private{cwd}/lib/main.js -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /private{cwd}/lib/main.js +│ writing {cwd}/lib/main.js │ -◆ /private{cwd}/lib/main.js (1 migrated) +◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -67,16 +67,16 @@ exports[`migrate command > should handle custom include pattern 1`] = ` │ │ Targets: chalk │ -◇ /private{cwd}/lib/main.js... +◇ {cwd}/lib/main.js... │ -│ loading /private{cwd}/lib/main.js -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /private{cwd}/lib/main.js +│ writing {cwd}/lib/main.js │ -◆ /private{cwd}/lib/main.js (1 migrated) +◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -117,16 +117,16 @@ exports[`migrate command > should handle specific package migration 1`] = ` │ │ Targets: chalk │ -◇ /private{cwd}/lib/main.js... +◇ {cwd}/lib/main.js... │ -│ loading /private{cwd}/lib/main.js -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading /private{cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ writing /private{cwd}/lib/main.js +│ writing {cwd}/lib/main.js │ -◆ /private{cwd}/lib/main.js (1 migrated) +◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. diff --git a/src/test/cli.test.ts b/src/test/cli.test.ts index dc89cf4..eebfbfc 100644 --- a/src/test/cli.test.ts +++ b/src/test/cli.test.ts @@ -69,7 +69,7 @@ describe('CLI', () => { console.error('CLI Error:', stderr); } expect(code).toBe(0); - expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(await stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toBe(''); }); @@ -79,7 +79,7 @@ describe('CLI', () => { tempDir ); expect(code).toBe(0); - expect(stripVersion(stdout, process.cwd())).toMatchSnapshot(); + expect(await stripVersion(stdout, process.cwd())).toMatchSnapshot(); expect(stderr).toMatchSnapshot(); }); }); diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index 76eeab7..0d49700 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -31,7 +31,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); + expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -47,7 +47,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); + expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -66,7 +66,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); + expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -82,7 +82,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); + expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was NOT modified in dry-run mode @@ -98,7 +98,7 @@ describe('migrate command', () => { ); expect(code).toBe(0); - expect(stripVersion(stdout, tempDir)).toMatchSnapshot(); + expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); // Check that the file was actually modified (chalk import should be replaced with picocolors) diff --git a/src/test/utils.ts b/src/test/utils.ts index 38b7dca..97a6b58 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -108,15 +108,20 @@ export function runCliProcess( }); } -export const stripVersion = ( +const cachedRealPaths = new Map(); + +export const stripVersion = async ( str: string, cwd: string = process.cwd() -): string => - str +): Promise => { + const cwdRealPath = cachedRealPaths.get(cwd) ?? await fs.realpath(cwd); + cachedRealPaths.set(cwd, cwdRealPath); + + return str .replace( new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), '(cli )' ) - .replaceAll(cwd, '{cwd}') - .replaceAll(cwd.replace('/var/folders', '/private/var/folders'), '{cwd}') - .replaceAll(cwd.replace('/private/var/folders', '/var/folders'), '{cwd}'); + .replaceAll(cwdRealPath, '{cwd}') + .replaceAll(cwd, '{cwd}'); +}; From e18213286f570ca2baf005538176967522830642 Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Thu, 28 Aug 2025 11:23:11 -0500 Subject: [PATCH 14/16] format --- src/test/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/utils.ts b/src/test/utils.ts index 97a6b58..98e9831 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -114,9 +114,9 @@ export const stripVersion = async ( str: string, cwd: string = process.cwd() ): Promise => { - const cwdRealPath = cachedRealPaths.get(cwd) ?? await fs.realpath(cwd); + const cwdRealPath = cachedRealPaths.get(cwd) ?? (await fs.realpath(cwd)); cachedRealPaths.set(cwd, cwdRealPath); - + return str .replace( new RegExp(/\(cli v\d+\.\d+\.\d+(?:-\S+)?\)/, 'g'), From 4fd2b920b1b3e3881b17db4f5bdd489de7535d0c Mon Sep 17 00:00:00 2001 From: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> Date: Thu, 28 Aug 2025 12:27:45 -0500 Subject: [PATCH 15/16] refactor tests --- src/test/__snapshots__/migrate.test.ts.snap | 70 +++++++++++++++------ src/test/migrate.test.ts | 40 ++++++++---- 2 files changed, 78 insertions(+), 32 deletions(-) diff --git a/src/test/__snapshots__/migrate.test.ts.snap b/src/test/__snapshots__/migrate.test.ts.snap index f535260..db49c38 100644 --- a/src/test/__snapshots__/migrate.test.ts.snap +++ b/src/test/__snapshots__/migrate.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`migrate command > should actually modify files when not using --dry-run 1`] = ` +exports[`migrate command > should handle custom include pattern 1`] = ` "e18e (cli ) ┌ Migrating packages... @@ -23,14 +23,34 @@ exports[`migrate command > should actually modify files when not using --dry-run " `; -exports[`migrate command > should actually modify files when not using --dry-run 2`] = ` +exports[`migrate command > should handle custom include pattern 2`] = ` "import * as pc from 'picocolors'; console.log(pc.cyan('I am _so_ cyan')); " `; -exports[`migrate command > should handle --all flag correctly 1`] = ` +exports[`migrate command > should handle interactive mode 1`] = ` +"e18e (cli ) + +┌ Migrating packages... +[?25l│ +◆ Select packages to migrate + +│ Search: _ +│ ◼ chalk +│ ↑/↓ to navigate • Space: select • Enter: confirm • Type: to search +└" +`; + +exports[`migrate command > should handle interactive mode 2`] = ` +"import chalk from 'chalk'; + +console.log(chalk.cyan('I am _so_ cyan')); +" +`; + +exports[`migrate command > should migrate specific package 1`] = ` "e18e (cli ) ┌ Migrating packages... @@ -53,14 +73,14 @@ exports[`migrate command > should handle --all flag correctly 1`] = ` " `; -exports[`migrate command > should handle --all flag correctly 2`] = ` -"import chalk from 'chalk'; +exports[`migrate command > should migrate specific package 2`] = ` +"import * as pc from 'picocolors'; -console.log(chalk.cyan('I am _so_ cyan')); +console.log(pc.cyan('I am _so_ cyan')); " `; -exports[`migrate command > should handle custom include pattern 1`] = ` +exports[`migrate command > should migrate with --all flag 1`] = ` "e18e (cli ) ┌ Migrating packages... @@ -83,34 +103,44 @@ exports[`migrate command > should handle custom include pattern 1`] = ` " `; -exports[`migrate command > should handle custom include pattern 2`] = ` -"import chalk from 'chalk'; +exports[`migrate command > should migrate with --all flag 2`] = ` +"import * as pc from 'picocolors'; -console.log(chalk.cyan('I am _so_ cyan')); +console.log(pc.cyan('I am _so_ cyan')); " `; -exports[`migrate command > should handle interactive mode 1`] = ` +exports[`migrate command > should not modify files with --all flag in dry-run mode 1`] = ` "e18e (cli ) ┌ Migrating packages... -[?25l│ -◆ Select packages to migrate +│ +│ Targets: chalk +│ +◇ {cwd}/lib/main.js... +│ +│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js +│ migrating chalk to picocolors +│ loading {cwd}/lib/main.js +│ migrating chalk to picocolors +│ writing {cwd}/lib/main.js +│ +◆ {cwd}/lib/main.js (1 migrated) +│ +└ Migration complete. -│ Search: _ -│ ◼ chalk -│ ↑/↓ to navigate • Space: select • Enter: confirm • Type: to search -└" +" `; -exports[`migrate command > should handle interactive mode 2`] = ` +exports[`migrate command > should not modify files with --all flag in dry-run mode 2`] = ` "import chalk from 'chalk'; console.log(chalk.cyan('I am _so_ cyan')); " `; -exports[`migrate command > should handle specific package migration 1`] = ` +exports[`migrate command > should not modify files with specific package in dry-run mode 1`] = ` "e18e (cli ) ┌ Migrating packages... @@ -133,7 +163,7 @@ exports[`migrate command > should handle specific package migration 1`] = ` " `; -exports[`migrate command > should handle specific package migration 2`] = ` +exports[`migrate command > should not modify files with specific package in dry-run mode 2`] = ` "import chalk from 'chalk'; console.log(chalk.cyan('I am _so_ cyan')); diff --git a/src/test/migrate.test.ts b/src/test/migrate.test.ts index 0d49700..e4b9f9f 100644 --- a/src/test/migrate.test.ts +++ b/src/test/migrate.test.ts @@ -24,9 +24,9 @@ describe('migrate command', () => { // Clean up the temporary directory after each test await cleanupTempDir(tempDir); }); - it('should handle --all flag correctly', async () => { + it('should migrate with --all flag', async () => { const {stdout, stderr, code} = await runCliProcess( - ['migrate', '--all', '--dry-run'], + ['migrate', '--all'], tempDir ); @@ -34,15 +34,15 @@ describe('migrate command', () => { expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); - // Check that the file was NOT modified in dry-run mode + // Check that the file was actually modified (chalk import should be replaced with picocolors) const mainJsPath = path.join(tempDir, 'lib/main.js'); const fileContent = await fs.readFile(mainJsPath, 'utf-8'); expect(fileContent).toMatchSnapshot(); }); - it('should handle specific package migration', async () => { + it('should migrate specific package', async () => { const {stdout, stderr, code} = await runCliProcess( - ['migrate', 'chalk', '--dry-run'], + ['migrate', 'chalk'], tempDir ); @@ -50,7 +50,7 @@ describe('migrate command', () => { expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); - // Check that the file was NOT modified in dry-run mode + // Check that the file was actually modified (chalk import should be replaced with picocolors) const mainJsPath = path.join(tempDir, 'lib/main.js'); const fileContent = await fs.readFile(mainJsPath, 'utf-8'); expect(fileContent).toMatchSnapshot(); @@ -60,7 +60,7 @@ describe('migrate command', () => { // Test interactive mode by providing input to the prompt // Press Enter to accept the default selection const {stdout, stderr, code} = await runCliProcess( - ['migrate', '--all', '--interactive', '--dry-run'], + ['migrate', '--all', '--interactive'], tempDir, '\n' // Press Enter to accept default ); @@ -69,7 +69,7 @@ describe('migrate command', () => { expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); - // Check that the file was NOT modified in dry-run mode + // Check that the file was actually modified const mainJsPath = path.join(tempDir, 'lib/main.js'); const fileContent = await fs.readFile(mainJsPath, 'utf-8'); expect(fileContent).toMatchSnapshot(); @@ -77,7 +77,23 @@ describe('migrate command', () => { it('should handle custom include pattern', async () => { const {stdout, stderr, code} = await runCliProcess( - ['migrate', 'chalk', '--include', '**/*.js', '--dry-run'], + ['migrate', 'chalk', '--include', '**/*.js'], + tempDir + ); + + expect(code).toBe(0); + expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); + expect(stderr).toBe(''); + + // Check that the file was actually modified + const mainJsPath = path.join(tempDir, 'lib/main.js'); + const fileContent = await fs.readFile(mainJsPath, 'utf-8'); + expect(fileContent).toMatchSnapshot(); + }); + + it('should not modify files with --all flag in dry-run mode', async () => { + const {stdout, stderr, code} = await runCliProcess( + ['migrate', '--all', '--dry-run'], tempDir ); @@ -91,9 +107,9 @@ describe('migrate command', () => { expect(fileContent).toMatchSnapshot(); }); - it('should actually modify files when not using --dry-run', async () => { + it('should not modify files with specific package in dry-run mode', async () => { const {stdout, stderr, code} = await runCliProcess( - ['migrate', 'chalk'], + ['migrate', 'chalk', '--dry-run'], tempDir ); @@ -101,7 +117,7 @@ describe('migrate command', () => { expect(await stripVersion(stdout, tempDir)).toMatchSnapshot(); expect(stderr).toBe(''); - // Check that the file was actually modified (chalk import should be replaced with picocolors) + // Check that the file was NOT modified in dry-run mode const mainJsPath = path.join(tempDir, 'lib/main.js'); const fileContent = await fs.readFile(mainJsPath, 'utf-8'); expect(fileContent).toMatchSnapshot(); From c61797c98a3d7172ffdf23fb4455b4c0d5144927 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Thu, 28 Aug 2025 22:34:04 +0100 Subject: [PATCH 16/16] test: update snapshots --- src/test/__snapshots__/migrate.test.ts.snap | 30 ++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/test/__snapshots__/migrate.test.ts.snap b/src/test/__snapshots__/migrate.test.ts.snap index db49c38..5df5eed 100644 --- a/src/test/__snapshots__/migrate.test.ts.snap +++ b/src/test/__snapshots__/migrate.test.ts.snap @@ -10,12 +10,12 @@ exports[`migrate command > should handle custom include pattern 1`] = ` ◇ {cwd}/lib/main.js... │ │ loading {cwd}/lib/main.js -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors │ writing {cwd}/lib/main.js -│ +│ ◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -60,12 +60,12 @@ exports[`migrate command > should migrate specific package 1`] = ` ◇ {cwd}/lib/main.js... │ │ loading {cwd}/lib/main.js -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors │ writing {cwd}/lib/main.js -│ +│ ◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -90,12 +90,12 @@ exports[`migrate command > should migrate with --all flag 1`] = ` ◇ {cwd}/lib/main.js... │ │ loading {cwd}/lib/main.js -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors │ writing {cwd}/lib/main.js -│ +│ ◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -120,12 +120,12 @@ exports[`migrate command > should not modify files with --all flag in dry-run mo ◇ {cwd}/lib/main.js... │ │ loading {cwd}/lib/main.js -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors │ writing {cwd}/lib/main.js -│ +│ ◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete. @@ -150,12 +150,12 @@ exports[`migrate command > should not modify files with specific package in dry- ◇ {cwd}/lib/main.js... │ │ loading {cwd}/lib/main.js -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors -│ loading {cwd}/lib/main.js +│ loading {cwd}/lib/main.js │ migrating chalk to picocolors │ writing {cwd}/lib/main.js -│ +│ ◆ {cwd}/lib/main.js (1 migrated) │ └ Migration complete.