diff --git a/scripts/auto-update.js b/scripts/auto-update.js index e943341d5..c6cd0dcd5 100644 --- a/scripts/auto-update.js +++ b/scripts/auto-update.js @@ -49,7 +49,11 @@ function parseArgs(argv) { } } - return parsed; + const dryRunEnv = process.env.ECC_DRY_RUN; + if (dryRunEnv !== undefined && dryRunEnv !== '0' && dryRunEnv !== '1') { + throw new Error('ECC_DRY_RUN must be "1" or "0" when set'); + } + return { ...parsed, dryRun: parsed.dryRun || dryRunEnv === '1' }; } function deriveRepoRootFromState(state) { diff --git a/scripts/nasiko.js b/scripts/nasiko.js index 71a240878..7a54782c5 100644 --- a/scripts/nasiko.js +++ b/scripts/nasiko.js @@ -51,7 +51,11 @@ function parseMutationArguments(argumentsList, command = 'install') { } if (!options.version) throw new Error(`Nasiko ${command} requires --version v0.1.0.`); if (options.installDir) validateInstallDirectory(options.installDir); - return options; + const dryRunEnv = process.env.ECC_DRY_RUN; + if (dryRunEnv !== undefined && dryRunEnv !== '0' && dryRunEnv !== '1') { + throw new Error('ECC_DRY_RUN must be "1" or "0" when set'); + } + return { ...options, dryRun: options.dryRun || dryRunEnv === '1' }; } function defaultExecutablePath() { diff --git a/scripts/setup.js b/scripts/setup.js index dddc9149d..65b734a65 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -59,6 +59,14 @@ Migration installs and verifies the destination before removing the source scope `); } +function resolveDryRunOption(dryRun) { + const dryRunEnv = process.env.ECC_DRY_RUN; + if (dryRunEnv !== undefined && dryRunEnv !== '0' && dryRunEnv !== '1') { + throw new Error('ECC_DRY_RUN must be "1" or "0" when set'); + } + return dryRun || dryRunEnv === '1'; +} + function parseArgs(argv) { const options = { dryRun: false, @@ -112,7 +120,7 @@ function parseArgs(argv) { if (options.moveScope && options.scope === undefined) { throw new Error('--move-scope requires an explicit --scope destination.'); } - return options; + return { ...options, dryRun: resolveDryRunOption(options.dryRun) }; } function questionWithCancellation(terminal, prompt) { diff --git a/tests/ci/nasiko-control-plane.test.js b/tests/ci/nasiko-control-plane.test.js index cc4eba6b0..3443e66cd 100644 --- a/tests/ci/nasiko-control-plane.test.js +++ b/tests/ci/nasiko-control-plane.test.js @@ -58,6 +58,27 @@ function tarGzipFixture({ ])); } +function writeNasikoInstallFixture(canonicalRoot) { + const { getQualifiedRelease, normalizePlatform } = require('../../scripts/lib/nasiko-release'); + const normalized = normalizePlatform(); + const release = getQualifiedRelease('v0.1.0'); + const executable = path.join(canonicalRoot, normalized.binaryName); + const metadataPath = path.join(canonicalRoot, '.ecc-nasiko-install.json'); + fs.writeFileSync(executable, 'placeholder binary', { mode: 0o700 }); + fs.writeFileSync(metadataPath, `${JSON.stringify({ + version: 'v0.1.0', + platform: release.os, + architecture: release.arch, + manifestDigest: release.manifestDigest, + artifactDigest: release.manifestDigest, + binaryDigest: release.binaryDigest, + installedPath: executable, + license: release.license, + sourceUrl: release.sourceUrl, + }, null, 2)}\n`, { mode: 0o600 }); + return { executable, metadataPath }; +} + async function main() { console.log('\n=== Testing Nasiko CLI lifecycle bridge ===\n'); @@ -523,6 +544,45 @@ async function main() { assert.ok(!packageJson.dependencies?.nasiko); assert.ok(!packageJson.optionalDependencies?.nasiko); }], + ['global ecc --dry-run keeps nasiko install and uninstall read-only', () => { + const { normalizePlatform } = require('../../scripts/lib/nasiko-release'); + const installRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-nasiko-global-dry-run-')); + try { + const canonicalRoot = fs.realpathSync(installRoot); + const executable = path.join(canonicalRoot, normalizePlatform().binaryName); + const baseEnv = Object.fromEntries( + Object.entries(process.env).filter(([key]) => key !== 'ECC_DRY_RUN') + ); + const installPreview = spawnSync(process.execPath, [ + path.join(REPO_ROOT, 'scripts', 'ecc.js'), + '--dry-run', 'nasiko', 'install', '--version', 'v0.1.0', '--yes', + '--install-dir', canonicalRoot, '--json', + ], { encoding: 'utf8', env: baseEnv }); + assert.strictEqual(installPreview.status, 0, installPreview.stderr); + assert.strictEqual(JSON.parse(installPreview.stdout).dryRun, true); + assert.strictEqual(fs.existsSync(executable), false); + + const { metadataPath } = writeNasikoInstallFixture(canonicalRoot); + const uninstallPreview = spawnSync(process.execPath, [ + path.join(REPO_ROOT, 'scripts', 'ecc.js'), + '--dry-run', 'nasiko', 'uninstall', '--version', 'v0.1.0', '--yes', + '--install-dir', canonicalRoot, '--json', + ], { encoding: 'utf8', env: baseEnv }); + assert.strictEqual(uninstallPreview.status, 0, uninstallPreview.stderr); + assert.strictEqual(JSON.parse(uninstallPreview.stdout).dryRun, true); + assert.strictEqual(fs.existsSync(executable), true); + assert.strictEqual(fs.existsSync(metadataPath), true); + + const invalid = spawnSync(process.execPath, [ + path.join(REPO_ROOT, 'scripts', 'nasiko.js'), + 'install', '--version', 'v0.1.0', '--install-dir', canonicalRoot, + ], { encoding: 'utf8', env: { ...baseEnv, ECC_DRY_RUN: 'yes' } }); + assert.strictEqual(invalid.status, 1); + assert.match(invalid.stderr, /ECC_DRY_RUN must be "1" or "0"/); + } finally { + fs.rmSync(installRoot, { recursive: true, force: true }); + } + }], ]; let passed = 0; diff --git a/tests/scripts/auto-update.test.js b/tests/scripts/auto-update.test.js index 98d678902..f2247553f 100644 --- a/tests/scripts/auto-update.test.js +++ b/tests/scripts/auto-update.test.js @@ -19,6 +19,7 @@ const { } = require('../../scripts/auto-update'); const { createInstallState, + writeInstallState, } = require('../../scripts/lib/install-state'); function test(name, fn) { @@ -83,6 +84,56 @@ function ensureFakeRepo(repoRoot) { fs.writeFileSync(path.join(repoRoot, 'scripts', 'install-apply.js'), '#!/usr/bin/env node\n'); } +function createBehindRemoteRepoFixture(fixture) { + const remoteRepo = path.join(fixture, 'remote.git'); + const localRepo = path.join(fixture, 'local-ecc'); + const pusherRepo = path.join(fixture, 'pusher'); + execFileSync('git', ['init', '--bare', '-b', 'main', remoteRepo], { encoding: 'utf8' }); + execFileSync('git', ['clone', remoteRepo, localRepo], { encoding: 'utf8' }); + execFileSync('git', ['config', 'user.email', 'test@example.com'], { cwd: localRepo, encoding: 'utf8' }); + execFileSync('git', ['config', 'user.name', 'Test'], { cwd: localRepo, encoding: 'utf8' }); + ensureFakeRepo(localRepo); + fs.writeFileSync( + path.join(localRepo, 'scripts', 'install-apply.js'), + '#!/usr/bin/env node\nprocess.stdout.write(JSON.stringify({ dryRun: process.argv.includes("--dry-run"), plan: {} }));\n' + ); + execFileSync('git', ['add', '.'], { cwd: localRepo, encoding: 'utf8' }); + execFileSync('git', ['commit', '-m', 'initial'], { cwd: localRepo, encoding: 'utf8' }); + execFileSync('git', ['push', '-u', 'origin', 'main'], { cwd: localRepo, encoding: 'utf8' }); + const headBefore = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: localRepo, encoding: 'utf8' }).trim(); + execFileSync('git', ['clone', remoteRepo, pusherRepo], { encoding: 'utf8' }); + execFileSync('git', ['config', 'user.email', 'test@example.com'], { cwd: pusherRepo, encoding: 'utf8' }); + execFileSync('git', ['config', 'user.name', 'Test'], { cwd: pusherRepo, encoding: 'utf8' }); + fs.writeFileSync(path.join(pusherRepo, 'UPSTREAM.txt'), 'new commit\n'); + execFileSync('git', ['add', 'UPSTREAM.txt'], { cwd: pusherRepo, encoding: 'utf8' }); + execFileSync('git', ['commit', '-m', 'upstream update'], { cwd: pusherRepo, encoding: 'utf8' }); + execFileSync('git', ['push', 'origin', 'main'], { cwd: pusherRepo, encoding: 'utf8' }); + return { localRepo, headBefore }; +} + +function seedCursorInstallState(projectRoot, headBefore) { + const targetRoot = path.join(projectRoot, '.cursor'); + const statePath = path.join(targetRoot, 'ecc-install-state.json'); + fs.mkdirSync(targetRoot, { recursive: true }); + writeInstallState(statePath, createInstallState({ + adapter: { id: 'cursor-project', target: 'cursor', kind: 'project' }, + targetRoot, + installStatePath: statePath, + request: { + profile: null, + modules: [], + includeComponents: [], + excludeComponents: [], + legacyLanguages: ['typescript'], + legacyMode: true, + hookConsent: 'declined', + }, + resolution: { selectedModules: ['legacy-cursor-install'], skippedModules: [] }, + source: { repoVersion: '1.10.0', repoCommit: headBefore, manifestVersion: 1 }, + operations: [], + })); +} + function runTests() { console.log('\n=== Testing auto-update.js ===\n'); @@ -658,6 +709,45 @@ function runTests() { } })) passed += 1; else failed += 1; + if (test('global --dry-run skips git pull and previews auto-update without mutating the repo', () => { + const fixture = createTempDir('auto-update-global-dry-run-'); + const homeDir = path.join(fixture, 'home'); + const projectRoot = path.join(fixture, 'project'); + const realRepoRoot = path.resolve(__dirname, '..', '..'); + fs.mkdirSync(homeDir); + fs.mkdirSync(projectRoot); + + try { + const { localRepo, headBefore } = createBehindRemoteRepoFixture(fixture); + seedCursorInstallState(projectRoot, headBefore); + const inheritedEnv = Object.fromEntries( + Object.entries(process.env).filter(([key]) => key !== 'ECC_DRY_RUN') + ); + const baseEnv = { ...inheritedEnv, HOME: homeDir, USERPROFILE: homeDir }; + const previewRaw = execFileSync(process.execPath, [ + path.join(realRepoRoot, 'scripts', 'ecc.js'), + '--dry-run', 'auto-update', '--target', 'cursor', '--repo-root', localRepo, '--json', + ], { cwd: projectRoot, env: baseEnv, encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 }); + const preview = JSON.parse(previewRaw); + + assert.strictEqual(preview.dryRun, true); + assert.strictEqual(preview.results[0].status, 'planned'); + assert.strictEqual(preview.results[0].payload.dryRun, true); + const headAfter = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: localRepo, encoding: 'utf8' }).trim(); + assert.strictEqual(headAfter, headBefore); + assert.strictEqual(fs.existsSync(path.join(localRepo, 'UPSTREAM.txt')), false); + + assert.throws( + () => execFileSync(process.execPath, [ + path.join(realRepoRoot, 'scripts', 'auto-update.js'), '--json', + ], { cwd: projectRoot, env: { ...baseEnv, ECC_DRY_RUN: 'yes' }, encoding: 'utf8', stdio: 'pipe' }), + error => /ECC_DRY_RUN must be "1" or "0"/.test(error.stderr) + ); + } finally { + cleanup(fixture); + } + })) passed += 1; else failed += 1; + console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`); process.exit(failed > 0 ? 1 : 0); } diff --git a/tests/scripts/setup.test.js b/tests/scripts/setup.test.js index 806e31e24..6145f78a5 100644 --- a/tests/scripts/setup.test.js +++ b/tests/scripts/setup.test.js @@ -301,6 +301,45 @@ test('dry-run JSON emits JSON only and reads inventory without mutation', () => }); }); +test('global ecc --dry-run setup previews without mutating plugins or settings', () => { + withFixture({}, fixture => { + const inheritedEnv = Object.fromEntries( + Object.entries(process.env).filter(([key]) => key !== 'ECC_DRY_RUN') + ); + const env = { + ...inheritedEnv, + HOME: fixture.homeDir, + USERPROFILE: fixture.homeDir, + CLAUDE_CONFIG_DIR: fixture.configDir, + PATH: `${fixture.binDir}${path.delimiter}${withoutShadowingClaude(process.env.PATH)}`, + ECC_TEST_CLAUDE_STATE: fixture.statePath, + ECC_TEST_CLAUDE_CALLS: fixture.callsPath, + }; + const result = spawnSync(process.execPath, [ + eccScript, + '--dry-run', + 'setup', + '--mode', 'claude-plugin', + '--scope', 'user', + '--hooks', 'standard', + '--yes', + '--json', + ], { cwd: fixture.projectRoot, env, encoding: 'utf8', timeout: 15000 }); + assert.strictEqual(result.status, 0, result.stderr); + const payload = JSON.parse(result.stdout); + assert.strictEqual(payload.action, 'would-install'); + assert.strictEqual(payload.dryRun, true); + assert.strictEqual(hasMutation(fixture), false); + assert.strictEqual(fs.existsSync(path.join(fixture.configDir, 'settings.json')), false); + + const invalid = runSetup(fixture, [ + '--mode', 'claude-plugin', '--scope', 'user', '--hooks', 'standard', '--yes', + ], { env: { ECC_DRY_RUN: 'yes' } }); + assert.strictEqual(invalid.status, 1); + assert.match(invalid.stderr, /ECC_DRY_RUN must be "1" or "0"/); + }); +}); + test('dry-run leaves a pristine HOME unchanged when Claude inventory creates backups', () => { withFixture({}, fixture => { assert.deepStrictEqual(fs.readdirSync(fixture.homeDir), []);