Skip to content

Commit de64e91

Browse files
fix(ci): reject invalid changed-scope arguments (#120913)
* fix(ci): reject invalid changed-scope arguments Punchcard-Session: frost-willow-valley-xh * test(ci): distinguish full and fast scope fallbacks --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: qingminlong <[email protected]>
1 parent b6eee72 commit de64e91

2 files changed

Lines changed: 88 additions & 74 deletions

File tree

‎scripts/ci-changed-scope.mjs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -744,19 +744,20 @@ function isDirectRun() {
744744
export function parseArgs(argv) {
745745
const args = { base: "", head: "HEAD", mergeHeadFirstParent: false };
746746
for (let i = 0; i < argv.length; i += 1) {
747-
if (argv[i] === "--base") {
748-
args.base = requireOptionArgument(argv, i, "--base");
747+
const arg = argv[i];
748+
if (arg === "--base" || arg === "--head") {
749+
args[arg === "--base" ? "base" : "head"] = requireOptionArgument(argv, i, arg);
749750
i += 1;
750751
continue;
751752
}
752-
if (argv[i] === "--head") {
753-
args.head = requireOptionArgument(argv, i, "--head");
754-
i += 1;
755-
continue;
756-
}
757-
if (argv[i] === "--merge-head-first-parent") {
753+
if (arg === "--merge-head-first-parent") {
758754
args.mergeHeadFirstParent = true;
755+
continue;
759756
}
757+
throw new Error(`Unknown argument: ${arg}`);
758+
}
759+
if (!args.base) {
760+
throw new Error("--base is required");
760761
}
761762
return args;
762763
}

‎src/scripts/ci-changed-scope.test.ts‎

Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ describe("parseArgs", () => {
9595
expect(() => parseArgs(["--head"])).toThrow("--head requires a value");
9696
expect(() => parseArgs(["--head", "-h"])).toThrow("--head requires a value");
9797
expect(() => parseArgs(["--base", ""])).toThrow("--base requires a value");
98+
expect(() => parseArgs([])).toThrow("--base is required");
99+
expect(() => parseArgs(["--head", "HEAD"])).toThrow("--base is required");
100+
expect(() => parseArgs(["--base", "HEAD", "--mystery"])).toThrow("Unknown argument: --mystery");
98101
});
99102
});
100103

@@ -940,78 +943,88 @@ describe("detectChangedScope", () => {
940943
expect(parseGitHubOutput(fs.readFileSync(outputPath, "utf8")).changed_paths_json).toBe("null");
941944
});
942945

943-
it.each([
946+
it.each<[string, string, string, boolean, string[]?]>([
947+
["missing base", "", "missing", true, ["--head", "HEAD"]],
948+
["unknown option", "", "missing", true, ["--base", "HEAD", "--head", "HEAD", "--mystery"]],
944949
["empty diff without a manifest", "", "missing", false],
945950
["declared native test", "src/process/exec.windows.integration.test.ts", "valid", false],
946951
["Mac fixture helper", "test/scripts/mac-script-fixture.test-support.ts", "valid", false],
947952
["unrelated process test", "src/process/exec.test.ts", "valid", false],
948953
["missing manifest", "src/process/exec.test.ts", "missing", true],
949954
["invalid manifest", "src/process/exec.test.ts", "invalid", true],
950955
["empty native inventory", "src/process/exec.test.ts", "empty", true],
951-
])("runs zero-install scope detection for %s", (_label, changedPath, manifest, failSafe) => {
952-
const repoDir = fs.realpathSync(
953-
fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ci-scope-empty-")),
954-
);
955-
tempDirs.push(repoDir);
956-
const outputPath = path.join(repoDir, "github-output.txt");
957-
const scriptPath = path.join(repoDir, "scripts/ci-changed-scope.mjs");
958-
959-
execFileSync("git", ["init", "-b", "main"], { cwd: repoDir });
960-
execFileSync("git", ["config", "user.email", "[email protected]"], { cwd: repoDir });
961-
execFileSync("git", ["config", "user.name", "CI"], { cwd: repoDir });
962-
for (const sourcePath of [
963-
"scripts/ci-changed-scope.mjs",
964-
"scripts/lib/arg-utils.runtime.mjs",
965-
"scripts/lib/changed-path-facts.mjs",
966-
"scripts/lib/direct-run.mjs",
967-
"scripts/lib/merge-head-diff-base.mjs",
968-
]) {
969-
writeRepoFile(repoDir, sourcePath, fs.readFileSync(path.resolve(sourcePath), "utf8"));
970-
}
971-
fs.writeFileSync(path.join(repoDir, "README.md"), "test\n", "utf8");
972-
execFileSync("git", ["add", "README.md"], { cwd: repoDir });
973-
execFileSync("git", ["commit", "-m", "test"], { cwd: repoDir });
974-
975-
if (manifest !== "missing") {
976-
const contents =
977-
manifest === "valid"
978-
? fs.readFileSync("package.json", "utf8")
979-
: manifest === "invalid"
980-
? "{"
981-
: JSON.stringify({ scripts: { "test:windows:ci:1": "" } });
982-
writeRepoFile(repoDir, "package.json", contents);
983-
}
984-
if (changedPath) {
985-
writeRepoFile(repoDir, changedPath, "export {};\n");
986-
git(repoDir, ["add", changedPath]);
987-
git(repoDir, ["commit", "-m", "changed test"]);
988-
}
989-
const base = changedPath ? "HEAD^" : "HEAD";
990-
expect(fs.existsSync(path.join(repoDir, "node_modules"))).toBe(false);
991-
execFileSync(process.execPath, [scriptPath, "--base", base, "--head", "HEAD"], {
992-
cwd: repoDir,
993-
env: { ...process.env, GITHUB_OUTPUT: outputPath },
994-
});
995-
996-
const output = parseGitHubOutput(fs.readFileSync(outputPath, "utf8"));
997-
expect(Object.keys(output).toSorted()).toEqual(
998-
"changed_paths_json run_android run_changed_smoke run_control_ui_i18n run_fast_install_smoke run_full_install_smoke run_ios_build run_ios_screenshots run_macos run_macos_node run_native_i18n run_node run_node_fast_ci_routing run_node_fast_only run_node_fast_plugin_contracts run_skills_python run_ui_tests run_windows strict_control_ui_i18n strict_native_i18n".split(
999-
" ",
1000-
),
1001-
);
1002-
expect(output.changed_paths_json).toBe(
1003-
failSafe ? "null" : JSON.stringify(changedPath ? [changedPath] : []),
1004-
);
1005-
for (const [key, value] of Object.entries(output)) {
1006-
if (key !== "changed_paths_json") {
1007-
const selected =
1008-
(failSafe && !key.startsWith("run_node_fast")) ||
1009-
(key === "run_node" && Boolean(changedPath)) ||
1010-
(key === "run_macos_node" &&
1011-
changedPath === "test/scripts/mac-script-fixture.test-support.ts") ||
1012-
(key === "run_windows" && changedPath === "src/process/exec.windows.integration.test.ts");
1013-
expect(value, key).toBe(String(selected));
956+
])(
957+
"runs zero-install scope detection for %s",
958+
(_label, changedPath, manifest, failSafe, cliArgs) => {
959+
const repoDir = fs.realpathSync(
960+
fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ci-scope-empty-")),
961+
);
962+
tempDirs.push(repoDir);
963+
const outputPath = path.join(repoDir, "github-output.txt");
964+
const scriptPath = path.join(repoDir, "scripts/ci-changed-scope.mjs");
965+
966+
execFileSync("git", ["init", "-b", "main"], { cwd: repoDir });
967+
execFileSync("git", ["config", "user.email", "[email protected]"], { cwd: repoDir });
968+
execFileSync("git", ["config", "user.name", "CI"], { cwd: repoDir });
969+
for (const sourcePath of [
970+
"scripts/ci-changed-scope.mjs",
971+
"scripts/lib/arg-utils.runtime.mjs",
972+
"scripts/lib/changed-path-facts.mjs",
973+
"scripts/lib/direct-run.mjs",
974+
"scripts/lib/merge-head-diff-base.mjs",
975+
]) {
976+
writeRepoFile(repoDir, sourcePath, fs.readFileSync(path.resolve(sourcePath), "utf8"));
1014977
}
1015-
}
1016-
});
978+
fs.writeFileSync(path.join(repoDir, "README.md"), "test\n", "utf8");
979+
execFileSync("git", ["add", "README.md"], { cwd: repoDir });
980+
execFileSync("git", ["commit", "-m", "test"], { cwd: repoDir });
981+
982+
if (manifest !== "missing") {
983+
const contents =
984+
manifest === "valid"
985+
? fs.readFileSync("package.json", "utf8")
986+
: manifest === "invalid"
987+
? "{"
988+
: JSON.stringify({ scripts: { "test:windows:ci:1": "" } });
989+
writeRepoFile(repoDir, "package.json", contents);
990+
}
991+
if (changedPath) {
992+
writeRepoFile(repoDir, changedPath, "export {};\n");
993+
git(repoDir, ["add", changedPath]);
994+
git(repoDir, ["commit", "-m", "changed test"]);
995+
}
996+
const base = changedPath ? "HEAD^" : "HEAD";
997+
expect(fs.existsSync(path.join(repoDir, "node_modules"))).toBe(false);
998+
execFileSync(
999+
process.execPath,
1000+
[scriptPath, ...(cliArgs ?? ["--base", base, "--head", "HEAD"])],
1001+
{
1002+
cwd: repoDir,
1003+
env: { ...process.env, GITHUB_OUTPUT: outputPath },
1004+
},
1005+
);
1006+
1007+
const output = parseGitHubOutput(fs.readFileSync(outputPath, "utf8"));
1008+
expect(Object.keys(output).toSorted()).toEqual(
1009+
"changed_paths_json run_android run_changed_smoke run_control_ui_i18n run_fast_install_smoke run_full_install_smoke run_ios_build run_ios_screenshots run_macos run_macos_node run_native_i18n run_node run_node_fast_ci_routing run_node_fast_only run_node_fast_plugin_contracts run_skills_python run_ui_tests run_windows strict_control_ui_i18n strict_native_i18n".split(
1010+
" ",
1011+
),
1012+
);
1013+
expect(output.changed_paths_json).toBe(
1014+
failSafe ? "null" : JSON.stringify(changedPath ? [changedPath] : []),
1015+
);
1016+
for (const [key, value] of Object.entries(output)) {
1017+
if (key !== "changed_paths_json") {
1018+
const selected =
1019+
(failSafe && !key.startsWith("run_node_fast")) ||
1020+
(key === "run_node" && Boolean(changedPath)) ||
1021+
(key === "run_macos_node" &&
1022+
changedPath === "test/scripts/mac-script-fixture.test-support.ts") ||
1023+
(key === "run_windows" &&
1024+
changedPath === "src/process/exec.windows.integration.test.ts");
1025+
expect(value, key).toBe(String(selected));
1026+
}
1027+
}
1028+
},
1029+
);
10171030
});

0 commit comments

Comments
 (0)