Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bin/sync-models.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';

const KNOWN_ARGS = new Set(['--dry-run', '--verbose', '-v']);
const unknownArgs = process.argv.slice(2).filter(arg => !KNOWN_ARGS.has(arg));
if (unknownArgs.length > 0) {
console.error(`[sync-models] Error: unknown argument '${unknownArgs[0]}'`);
console.error('Usage: sync-models.mjs [--dry-run] [--verbose|-v]');
process.exit(2);
}

const DRY_RUN = process.argv.includes('--dry-run');
const VERBOSE = process.argv.includes('--verbose') || process.argv.includes('-v');

Expand Down
59 changes: 59 additions & 0 deletions bin/sync-models.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// If parseJsonc's declaration shape changes, this extraction fails loudly
// rather than silently testing nothing.
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -68,3 +70,60 @@ test('still throws on a genuinely malformed config', () => {
assert.throws(() => parseJsonc('{ "a": }'));
assert.throws(() => parseJsonc('{ "a": 1'));
});

// Strict argv validation (#3297): unknown arguments are a usage error,
// reported before any fetch/discovery/write. These probes run the real
// script with an isolated HOME and an explicit OPENCODE_CONFIG_PATH so the
// temp target config — never the live config — is the only write candidate.
const scriptPath = path.join(binDir, 'sync-models.mjs');

function runWithTempConfig(args) {
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'sync-models-test-home-'));
const configPath = path.join(tmpHome, 'opencode.jsonc');
const before = '{\n // temp probe config\n "provider": {}\n}\n';
fs.writeFileSync(configPath, before);
const result = spawnSync(process.execPath, [scriptPath, ...args], {
encoding: 'utf-8',
timeout: 60000,
env: {
...process.env,
HOME: tmpHome,
OPENCODE_CONFIG_PATH: configPath,
},
});
const after = fs.readFileSync(configPath, 'utf-8');
const backupExists = fs.existsSync(`${configPath}.backup`);
fs.rmSync(tmpHome, { recursive: true, force: true });
return { result, before, after, backupExists };
}

test('rejects --bogus-flag non-zero and leaves the target config byte-identical', () => {
const { result, before, after, backupExists } = runWithTempConfig(['--bogus-flag']);
assert.notEqual(result.status, 0);
assert.match(`${result.stderr}${result.stdout}`, /--bogus-flag/);
assert.equal(after, before);
assert.equal(backupExists, false);
});

test('rejects typo --dry-rnu non-zero, names it, and leaves the target config byte-identical', () => {
const { result, before, after, backupExists } = runWithTempConfig(['--dry-rnu']);
assert.notEqual(result.status, 0);
assert.match(`${result.stderr}${result.stdout}`, /--dry-rnu/);
assert.equal(after, before);
assert.equal(backupExists, false);
});

test('rejects a positional argument non-zero and leaves the target config byte-identical', () => {
const { result, before, after, backupExists } = runWithTempConfig(['some-positional']);
assert.notEqual(result.status, 0);
assert.match(`${result.stderr}${result.stdout}`, /some-positional/);
assert.equal(after, before);
assert.equal(backupExists, false);
});

test('--dry-run still exits 0 writing nothing', () => {
const { result, before, after, backupExists } = runWithTempConfig(['--dry-run']);
assert.equal(result.status, 0);
assert.equal(after, before);
assert.equal(backupExists, false);
});