Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

test: reuse rank command fixtures (#1463)

mldangelo-oai46585a3d0a09
diff --git a/plugins/codex-security/tests/test_generate_rank_input.py b/plugins/codex-security/tests/test_generate_rank_input.py
index 61d2c0420..009ca1e5c 100644
--- a/plugins/codex-security/tests/test_generate_rank_input.py
+++ b/plugins/codex-security/tests/test_generate_rank_input.py
@@ -15,9 +15,11 @@ SCRIPT = PLUGIN_ROOT / "scripts" / "generate_rank_input.py"
GOLDEN_DIR = Path(__file__).resolve().parent / "goldens"
-def run_cli(*args: str, check: bool = True) -> subprocess.CompletedProcess[str]:
+def run_repo_cli(
+ command: str, repo: str | Path, output: Path, *args: str, check: bool = True
+) -> subprocess.CompletedProcess[str]:
return subprocess.run(
- [sys.executable, str(SCRIPT), *args],
+ [sys.executable, str(SCRIPT), command, "--repo", str(repo), *args, "--out", str(output)],
check=check,
capture_output=True,
text=True,
@@ -68,15 +70,7 @@ def test_make_repo_rank_input_matches_golden_and_filters_noise(tmp_path: Path) -
(repo / "README.md").write_text("Deployment instructions", encoding="utf-8")
output = tmp_path / "rank_input.jsonl"
- run_cli(
- "make-repo-rank-input",
- "--repo",
- str(repo),
- "--scope",
- "src",
- "--out",
- str(output),
- )
+ run_repo_cli("make-repo-rank-input", repo, output, "--scope", "src")
assert output.read_text(encoding="utf-8") == (GOLDEN_DIR / "rank_input.jsonl").read_text(
encoding="utf-8"
@@ -96,12 +90,14 @@ def test_rank_input_keeps_large_generated_python(tmp_path: Path, mode: str) -> N
output = tmp_path / "rank_input.jsonl"
if mode == "repo":
- arguments = ["make-repo-rank-input", "--repo", str(repo), "--scope", "src"]
+ command = "make-repo-rank-input"
+ arguments = ["--scope", "src"]
else:
git(repo, "add", ".")
git(repo, "commit", "-qm", "change")
- arguments = ["make-diff-rank-input", "--repo", str(repo), "--base", base, "--head", "HEAD"]
- run_cli(*arguments, "--preview-bytes", "128", "--out", str(output))
+ command = "make-diff-rank-input"
+ arguments = ["--base", base, "--head", "HEAD"]
+ run_repo_cli(command, repo, output, *arguments, "--preview-bytes", "128")
rows = read_jsonl(output)
assert [row["path"] for row in rows] == ["src/generated.py", "src/normal.py"]
@@ -126,17 +122,7 @@ def test_revision_previews_use_the_worktree_read_budget(tmp_path: Path) -> None:
git(repo, "commit", "-qm", "change")
output = tmp_path / "rank.jsonl"
- run_cli(
- "make-diff-rank-input",
- "--repo",
- str(repo),
- "--base",
- base,
- "--head",
- "HEAD",
- "--out",
- str(output),
- )
+ run_repo_cli("make-diff-rank-input", repo, output, "--base", base, "--head", "HEAD")
preview = read_jsonl(output)[0]["preview"]
assert preview.startswith("def visible():\n pass\n# comment")
@@ -155,17 +141,7 @@ def test_revision_rank_input_classifies_bytes_beyond_the_preview_sample(tmp_path
git(repo, "commit", "-qm", "change")
output = tmp_path / "rank.jsonl"
- run_cli(
- "make-diff-rank-input",
- "--repo",
- str(repo),
- "--base",
- base,
- "--head",
- "HEAD",
- "--out",
- str(output),
- )
+ run_repo_cli("make-diff-rank-input", repo, output, "--base", base, "--head", "HEAD")
assert read_jsonl(output) == []
@@ -250,24 +226,18 @@ def test_rank_input_includes_source_cases(tmp_path: Path, mode: str) -> None:
if mode in {"revisions", "staged"}:
git(repo, "add", ".")
if mode == "repo":
- arguments = ["make-repo-rank-input", "--repo", str(repo), "--scope", "."]
+ command = "make-repo-rank-input"
+ arguments = ["--scope", "."]
else:
diff_mode = "revisions" if mode == "revisions" else "local-patch"
- arguments = [
- "make-diff-rank-input",
- "--repo",
- str(repo),
- "--base",
- base,
- "--mode",
- diff_mode,
- ]
+ command = "make-diff-rank-input"
+ arguments = ["--base", base, "--mode", diff_mode]
if mode == "revisions":
git(repo, "commit", "-qm", "change")
arguments.extend(["--head", git(repo, "rev-parse", "HEAD")])
git(repo, "checkout", "-q", base)
- run_cli(*arguments, "--out", str(output))
+ run_repo_cli(command, repo, output, *arguments)
assert read_jsonl(output) == [
{
@@ -285,14 +255,12 @@ def test_make_repo_rank_input_rejects_scope_outside_repo(tmp_path: Path) -> None
outside = tmp_path / "outside"
outside.mkdir()
- result = run_cli(
+ result = run_repo_cli(
"make-repo-rank-input",
- "--repo",
- str(repo),
+ repo,
+ tmp_path / "rank.jsonl",
"--scope",
str(outside),
- "--out",
- str(tmp_path / "rank.jsonl"),
check=False,
)
@@ -311,15 +279,7 @@ def test_make_repo_rank_input_does_not_follow_file_symlinks(tmp_path: Path) -> N
(source / "inside-link.py").symlink_to(source / "runtime.py")
output = tmp_path / "rank_input.jsonl"
- run_cli(
- "make-repo-rank-input",
- "--repo",
- str(repo),
- "--scope",
- "src",
- "--out",
- str(output),
- )
+ run_repo_cli("make-repo-rank-input", repo, output, "--scope", "src")
assert read_jsonl(output) == [
{"path": "src/runtime.py", "area": "src", "preview": "runtime = True"}
@@ -338,15 +298,7 @@ def test_make_repo_rank_input_preserves_legacy_tilde_scope(
monkeypatch.setenv("USERPROFILE", str(home))
output = tmp_path / "rank_input.jsonl"
- run_cli(
- "make-repo-rank-input",
- "--repo",
- "~/repo",
- "--scope",
- "~/repo/src",
- "--out",
- str(output),
- )
+ run_repo_cli("make-repo-rank-input", "~/repo", output, "--scope", "~/repo/src")
assert [row["path"] for row in read_jsonl(output)] == ["src/runtime.py"]
@@ -383,15 +335,7 @@ def test_make_repo_rank_input_combines_explicit_files_and_directories(tmp_path:
)
output = tmp_path / "rank_input.jsonl"
- run_cli(
- "make-repo-rank-input",
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli("make-repo-rank-input", repo, output, "--scopes-file", str(scopes))
assert [row["path"] for row in read_jsonl(output)] == sorted(
[
@@ -425,15 +369,7 @@ def test_repo_input_preserves_every_requested_directory_file(tmp_path: Path, com
scopes.write_text(json.dumps(["src", "src/runtime.py", "src/.git/config"]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
- run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli(command, repo, output, "--scopes-file", str(scopes))
assert [row["path"] for row in read_jsonl(output)] == [
"src/Dockerfile",
@@ -452,14 +388,12 @@ def test_make_repo_scope_input_rejects_paths_outside_repository(tmp_path: Path)
scopes.write_text(json.dumps(["../outside.py"]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
- result = run_cli(
+ result = run_repo_cli(
"make-repo-scope-input",
- "--repo",
- str(repo),
+ repo,
+ output,
"--scopes-file",
str(scopes),
- "--out",
- str(output),
check=False,
)
@@ -481,14 +415,12 @@ def test_make_repo_scope_input_rejects_explicit_symlink_scopes(tmp_path: Path, s
scopes.write_text(json.dumps([scope]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
- result = run_cli(
+ result = run_repo_cli(
"make-repo-scope-input",
- "--repo",
- str(repo),
+ repo,
+ output,
"--scopes-file",
str(scopes),
- "--out",
- str(output),
check=False,
)
@@ -527,15 +459,7 @@ def test_repo_input_handles_replaced_tracked_directories(
scopes.write_text(json.dumps([scope]), encoding="utf-8")
output = tmp_path / "input.jsonl"
- run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli(command, repo, output, "--scopes-file", str(scopes))
expected = ["src/handler.py"]
if replacement == "file":
@@ -562,15 +486,7 @@ def test_repo_input_preserves_tracked_ignored_and_binary_files(
scopes.write_text(json.dumps(["src"]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
- run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli(command, repo, output, "--scopes-file", str(scopes))
expected = ["src/vendor/dependency.py"]
if command == "make-repo-scope-input":
@@ -594,15 +510,7 @@ def test_repo_input_respects_ignored_directory_descendants(tmp_path: Path, comma
scopes.write_text(json.dumps(["src"]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
- run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli(command, repo, output, "--scopes-file", str(scopes))
assert {row["path"] for row in read_jsonl(output)} == {
"src/handler.py",
@@ -621,15 +529,7 @@ def test_repo_input_keeps_explicitly_requested_ignored_file(tmp_path: Path, comm
scopes.write_text(json.dumps(["src/.env"]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
- run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli(command, repo, output, "--scopes-file", str(scopes))
assert [row["path"] for row in read_jsonl(output)] == ["src/.env"]
@@ -646,15 +546,7 @@ def test_repo_input_uses_git_ignore_rules(tmp_path: Path, command: str) -> None:
scopes.write_text(json.dumps(["src"]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
- run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli(command, repo, output, "--scopes-file", str(scopes))
assert [row["path"] for row in read_jsonl(output)] == ["src/handler.py"]
@@ -671,15 +563,7 @@ def test_repo_input_falls_back_without_git_or_ripgrep(
output = tmp_path / "scoped-source-input.jsonl"
monkeypatch.setenv("PATH", str(tmp_path / "missing-tools"))
- run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli(command, repo, output, "--scopes-file", str(scopes))
assert [row["path"] for row in read_jsonl(output)] == ["src/handler.py"]
@@ -697,16 +581,7 @@ def test_repo_input_fails_closed_when_ignore_rules_cannot_be_applied(
output = tmp_path / "scoped-source-input.jsonl"
monkeypatch.setenv("PATH", str(tmp_path / "missing-tools"))
- result = run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- check=False,
- )
+ result = run_repo_cli(command, repo, output, "--scopes-file", str(scopes), check=False)
assert result.returncode != 0
assert "without Git or ripgrep" in result.stderr
@@ -727,16 +602,7 @@ def test_repo_input_fails_closed_for_git_private_excludes_without_tools(
output = tmp_path / "scoped-source-input.jsonl"
monkeypatch.setenv("PATH", str(tmp_path / "missing-tools"))
- result = run_cli(
- command,
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- check=False,
- )
+ result = run_repo_cli(command, repo, output, "--scopes-file", str(scopes), check=False)
assert result.returncode != 0
assert "without Git or ripgrep" in result.stderr
@@ -753,15 +619,7 @@ def test_make_repo_rank_input_keeps_explicit_binary_file_without_preview(tmp_pat
scopes.write_text(json.dumps(["payload.bin"]), encoding="utf-8")
output = tmp_path / "rank_input.jsonl"
- run_cli(
- "make-repo-rank-input",
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli("make-repo-rank-input", repo, output, "--scopes-file", str(scopes))
assert read_jsonl(output) == [{"path": "payload.bin", "area": "payload.bin", "preview": ""}]
@@ -778,15 +636,7 @@ def test_make_repo_rank_input_bounds_explicit_source_like_binary(tmp_path: Path)
scopes.write_text(json.dumps(["src", "src/payload.py"]), encoding="utf-8")
output = tmp_path / "rank_input.jsonl"
- run_cli(
- "make-repo-rank-input",
- "--repo",
- str(repo),
- "--scopes-file",
- str(scopes),
- "--out",
- str(output),
- )
+ run_repo_cli("make-repo-rank-input", repo, output, "--scopes-file", str(scopes))
assert read_jsonl(output) == [{"path": "src/payload.py", "area": "src", "preview": ""}]
@@ -836,16 +686,7 @@ def test_make_diff_rank_input_keeps_changed_and_deleted_text(tmp_path: Path, mod
git(repo, "checkout", "-q", base)
output = tmp_path / "diff.jsonl"
- run_cli(
- "make-diff-rank-input",
- "--repo",
- str(repo),
- "--base",
- base,
- *arguments,
- "--out",
- str(output),
- )
+ run_repo_cli("make-diff-rank-input", repo, output, "--base", base, *arguments)
rows = read_jsonl(output)
assert [row["path"] for row in rows] == [
@@ -871,17 +712,7 @@ def test_make_diff_rank_input_uses_empty_tree_for_root_commit(tmp_path: Path) ->
empty_tree = git(repo, "hash-object", "-t", "tree", "--stdin", input="")
output = tmp_path / "root.jsonl"
- run_cli(
- "make-diff-rank-input",
- "--repo",
- str(repo),
- "--base",
- empty_tree,
- "--head",
- head,
- "--out",
- str(output),
- )
+ run_repo_cli("make-diff-rank-input", repo, output, "--base", empty_tree, "--head", head)
assert [row["path"] for row in read_jsonl(output)] == ["src/root.py"]
@@ -931,17 +762,7 @@ def test_make_diff_rank_input_supports_shallow_tips_without_merge_base(
assert merge_base.returncode == 1
output = tmp_path / "shallow.jsonl"
- run_cli(
- "make-diff-rank-input",
- "--repo",
- str(shallow),
- "--base",
- "origin/main",
- "--head",
- "HEAD",
- "--out",
- str(output),
- )
+ run_repo_cli("make-diff-rank-input", shallow, output, "--base", "origin/main", "--head", "HEAD")
assert [row["path"] for row in read_jsonl(output)] == [
"src/feature.py",
@@ -974,17 +795,7 @@ def test_make_diff_rank_input_combines_staged_and_unstaged_patch(tmp_path: Path)
git(nested, "commit", "-qm", "nested repository")
output = tmp_path / "patch.jsonl"
- run_cli(
- "make-diff-rank-input",
- "--repo",
- str(repo),
- "--base",
- "HEAD",
- "--mode",
- "local-patch",
- "--out",
- str(output),
- )
+ run_repo_cli("make-diff-rank-input", repo, output, "--base", "HEAD", "--mode", "local-patch")
assert [row["path"] for row in read_jsonl(output)] == [
".github/workflows/ci.yaml",
@@ -1015,7 +826,8 @@ def test_make_rank_input_decodes_bom_marked_utf16_source(tmp_path: Path, mode: s
}
if mode in {"repo", "explicit-file"}:
- arguments = ["make-repo-rank-input", "--repo", str(repo)]
+ command = "make-repo-rank-input"
+ arguments = []
if mode == "explicit-file":
expected.update({"src/binary.ps1": "", "src/decoded-nul.ps1": ""})
scopes = tmp_path / "target-paths.json"
@@ -1024,13 +836,14 @@ def test_make_rank_input_decodes_bom_marked_utf16_source(tmp_path: Path, mode: s
else:
arguments.extend(["--scope", "src"])
else:
- arguments = ["make-diff-rank-input", "--repo", str(repo), "--base", base, "--mode", mode]
+ command = "make-diff-rank-input"
+ arguments = ["--base", base, "--mode", mode]
if mode == "revisions":
git(repo, "add", ".")
git(repo, "commit", "-qm", "add encoded source")
arguments.extend(["--head", git(repo, "rev-parse", "HEAD")])
git(repo, "checkout", "-q", base)
- run_cli(*arguments, "--out", str(output))
+ run_repo_cli(command, repo, output, *arguments)
assert {row["path"]: row["preview"] for row in read_jsonl(output)} == expected