/** * CLI Commands Tests * * Proper tests for CLI commands using Commander's programmatic API. * Tests actual command execution, option parsing, and error handling. * * Unlike cli.test.ts which tests underlying utilities, these tests verify * the CLI commands themselves behave correctly. */ import { execSync } from 'child_process' import { Command } from 'commander' import { existsSync, readFileSync, writeFileSync } from 'fs' import { join } from 'path' import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' import { assertFileContains, assertFileExists, assertValidYAML } from '../helpers/assertions.js' import { createMinimalConfig } from '../helpers/fixtures.js' import { createWorkspaceWithCleanup, inWorkspace } from '../helpers/workspace.js' // Helper to run CLI commands programmatically async function runCLI( args: string[], cwd: string = process.cwd(), timeout: number = 5000 ): Promise<{ stdout: string stderr: string exitCode: number }> { const originalCwd = process.cwd() try { process.chdir(cwd) // Build the CLI path - use the project root, not cwd const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { throw new Error(`CLI not built. Run 'npm run build' first. Looking for: ${cliPath}`) } // Run via node with timeout const cmd = `node "${cliPath}" ${args.join(' ')}` try { const output = execSync(cmd, { cwd, encoding: 'utf-8', stdio: 'pipe', timeout, // Kill after timeout env: { ...process.env, CI: 'true', NO_COLOR: '1' } // Disable interactive prompts }) return { stdout: output, stderr: '', exitCode: 0 } } catch (error: any) { // Extract stdout/stderr from error if available const stdout = error.stdout?.toString() || '' const stderr = error.stderr?.toString() || error.message || '' const exitCode = error.status || 1 return { stdout, stderr, exitCode } } } finally { process.chdir(originalCwd) } } describe('CLI Commands', () => { let workspace: string let cleanup: () => void beforeAll(() => { // Check if CLI is built const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.warn('⚠️ CLI not built. Some tests may be skipped. Run: npm run build') } }) beforeEach(() => { ;[workspace, cleanup] = createWorkspaceWithCleanup('cli-commands') }) afterEach(() => { cleanup() }) describe('init command', () => { it('should create .pipecraftrc with default config', async () => { await inWorkspace(workspace, async () => { // Check if CLI is built before running const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const result = await runCLI(['init', '--force'], workspace) // Command should execute (may fail due to Pinion requirements, but tests the command exists) expect(result).toBeDefined() // If it succeeds, verify the config file if (result.exitCode === 0 && existsSync('.pipecraftrc')) { const config = assertValidYAML('.pipecraftrc') expect(config.ciProvider).toBeDefined() expect(config.branchFlow).toBeDefined() expect(config.domains).toBeDefined() } }) }) it('should respect --force flag to overwrite existing config', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } // Create existing config const existingConfig = createMinimalConfig({ initialBranch: 'old' }) writeFileSync('.pipecraftrc', JSON.stringify(existingConfig, null, 2)) // Run init with --force const result = await runCLI(['init', '--force'], workspace) expect(result.exitCode).toBe(0) // Config should be replaced with defaults const config = assertValidYAML('.pipecraftrc') expect(config.initialBranch).not.toBe('old') }) }) it('should fail without --force when config exists', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } // Create existing config const config = createMinimalConfig() writeFileSync('.pipecraftrc', JSON.stringify(config, null, 2)) // Run init without --force should fail or warn const result = await runCLI(['init'], workspace) // Should either fail or warn about existing file expect( result.exitCode !== 0 || result.stderr.includes('exists') || result.stdout.includes('exists') ).toBe(true) }) }) }) describe('validate command', () => { it('should validate a valid config file', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } // Create valid config const config = createMinimalConfig() writeFileSync('.pipecraftrc', JSON.stringify(config, null, 2)) const result = await runCLI(['validate'], workspace) expect(result.exitCode).toBe(0) expect(result.stdout).toContain('valid') }) }) it('should fail for invalid config', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } // Create invalid config (missing required fields) writeFileSync('.pipecraftrc', JSON.stringify({ invalid: true }, null, 2)) const result = await runCLI(['validate'], workspace) expect(result.exitCode).not.toBe(0) }) }) it('should validate custom config file with --config flag', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const config = createMinimalConfig() writeFileSync('custom-config.json', JSON.stringify(config, null, 2)) const result = await runCLI(['validate', '--config', 'custom-config.json'], workspace) expect(result.exitCode).toBe(0) }) }) }) describe('generate command', () => { it('should generate workflows from valid config', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } // Create valid config const config = createMinimalConfig() writeFileSync('.pipecraftrc', JSON.stringify(config, null, 2)) // Initialize git repo (required by preflight checks) execSync('git init', { cwd: workspace, stdio: 'pipe' }) execSync('git config user.email "test@test.com"', { cwd: workspace, stdio: 'pipe' }) execSync('git config user.name "Test"', { cwd: workspace, stdio: 'pipe' }) execSync('git remote add origin https://github.com/test/test.git', { cwd: workspace, stdio: 'pipe' }) const result = await runCLI(['generate', '--skip-checks'], workspace) // Should succeed (may have warnings but exit code 0) expect(result.exitCode).toBe(0) // Should create workflow directory expect(existsSync('.github/workflows')).toBe(true) }) }) it('should respect --dry-run flag', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const config = createMinimalConfig() writeFileSync('.pipecraftrc', JSON.stringify(config, null, 2)) execSync('git init', { cwd: workspace, stdio: 'pipe' }) execSync('git remote add origin https://github.com/test/test.git', { cwd: workspace, stdio: 'pipe' }) const result = await runCLI(['generate', '--dry-run', '--skip-checks'], workspace) // Should succeed expect(result.exitCode).toBe(0) // Should NOT create files expect(existsSync('.github/workflows/pipeline.yml')).toBe(false) }) }) it('should respect --verbose flag', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const config = createMinimalConfig() writeFileSync('.pipecraftrc', JSON.stringify(config, null, 2)) execSync('git init', { cwd: workspace, stdio: 'pipe' }) execSync('git remote add origin https://github.com/test/test.git', { cwd: workspace, stdio: 'pipe' }) const result = await runCLI(['generate', '--verbose', '--skip-checks'], workspace) // Should have more detailed output expect(result.stdout.length).toBeGreaterThan(0) }) }) it('should fail without valid config', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } // No config file const result = await runCLI(['generate'], workspace) expect(result.exitCode).not.toBe(0) }) }) }) describe('verify command', () => { it('should verify pipeline setup', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const config = createMinimalConfig() writeFileSync('.pipecraftrc', JSON.stringify(config, null, 2)) const result = await runCLI(['verify'], workspace) // Should run (may fail if workflows don't exist, but command should execute) expect(result).toBeDefined() }) }) }) describe('version command', () => { it('should display version with --check flag', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } // Create package.json with version writeFileSync('package.json', JSON.stringify({ version: '1.0.0' }, null, 2)) execSync('git init', { cwd: workspace, stdio: 'pipe' }) execSync('git config user.email "test@test.com"', { cwd: workspace, stdio: 'pipe' }) execSync('git config user.name "Test"', { cwd: workspace, stdio: 'pipe' }) const result = await runCLI(['version', '--check'], workspace) // Should display version info expect(result.stdout).toContain('version') }) }) }) describe('setup command', () => { it('should setup branches from branch flow', async () => { await inWorkspace(workspace, async () => { const cliPath = join(process.cwd(), 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const config = createMinimalConfig() writeFileSync('.pipecraftrc', JSON.stringify(config, null, 2)) // Initialize git repo execSync('git init', { cwd: workspace, stdio: 'pipe' }) execSync('git config user.email "test@test.com"', { cwd: workspace, stdio: 'pipe' }) execSync('git config user.name "Test"', { cwd: workspace, stdio: 'pipe' }) // Create initial commit writeFileSync('README.md', '# Test') execSync('git add .', { cwd: workspace, stdio: 'pipe' }) execSync('git commit -m "initial"', { cwd: workspace, stdio: 'pipe' }) const result = await runCLI(['setup'], workspace) // Command should execute expect(result).toBeDefined() }) }) }) describe('Global options', () => { it('should handle --help flag', async () => { const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const result = await runCLI(['--help'], workspace) expect(result.stdout).toContain('Usage') expect(result.stdout).toContain('Commands') }) it('should handle command-specific --help', async () => { const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const result = await runCLI(['generate', '--help'], workspace) expect(result.stdout).toContain('generate') }) it('should display version with --version', async () => { const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const result = await runCLI(['--version'], workspace) // Should show version number expect(result.stdout).toMatch(/\d+\.\d+\.\d+/) }) }) describe('Error handling', () => { it('should show error for unknown command', async () => { const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const result = await runCLI(['unknown-command'], workspace) expect(result.exitCode).not.toBe(0) expect(result.stderr.toLowerCase()).toContain('unknown') }) it('should show error for invalid option', async () => { const projectRoot = join(__dirname, '..', '..') const cliPath = join(projectRoot, 'dist', 'cli', 'index.js') if (!existsSync(cliPath)) { console.log('Skipping - CLI not built') return } const result = await runCLI(['init', '--invalid-option'], workspace) expect(result.exitCode).not.toBe(0) }) }) })