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(workbench): share saved-workspace setup across scan tests (#1370)

mldangelo-oai3b5557efc1cc
diff --git a/plugins/codex-security/tests/test_workbench_completion_binding.py b/plugins/codex-security/tests/test_workbench_completion_binding.py
index ff27188ad..8413c2d8a 100644
--- a/plugins/codex-security/tests/test_workbench_completion_binding.py
+++ b/plugins/codex-security/tests/test_workbench_completion_binding.py
@@ -14,10 +14,12 @@ from typing import Any
import pytest
from workbench_test_support import (
create_saved_workspace,
+ create_workspace,
empty_target_scan,
initialize_git_repository,
mark_deep_coordinator_succeeded,
run_workbench,
+ save_workspace,
source_plugin_version,
stable_target_id,
start_delivered_scan,
@@ -514,28 +516,15 @@ def test_completion_populates_coverage_mode_from_selected_scan_mode(tmp_path: Pa
target = tmp_path / f"target-{index}"
(target / "src").mkdir(parents=True)
workspace_id = str(uuid.uuid4())
- run_workbench(
+ create_workspace(
state_dir,
- "create-workspace",
- "--workspace-id",
workspace_id,
"--thread-id",
"thread-completion-binding",
"--target-path",
str(target),
)
- run_workbench(
- state_dir,
- "save-workspace",
- "--workspace-id",
- workspace_id,
- "--target-path",
- str(target),
- "--scope",
- scope,
- "--mode",
- mode,
- )
+ save_workspace(state_dir, workspace_id, str(target), scope, mode)
scan_id, scan_dir = start_workspace_scan(state_dir, workspace_id, tmp_path / "scans")
if mode == "deep":
run_workbench(
diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.py
index 248090ec8..7ffb4fa89 100644
--- a/plugins/codex-security/tests/test_workbench_db.py
+++ b/plugins/codex-security/tests/test_workbench_db.py
@@ -2371,10 +2371,8 @@ def test_workbench_populates_manifest_with_working_tree_digest(tmp_path: Path) -
revision = initialize_git_repository(target)
(target / "new-file.txt").write_text("selected content\n")
workspace_id = str(uuid.uuid4())
- created = run_workbench(
+ created = create_workspace(
state_dir,
- "create-workspace",
- "--workspace-id",
workspace_id,
"--target-path",
str(target),
@@ -2386,16 +2384,11 @@ def test_workbench_populates_manifest_with_working_tree_digest(tmp_path: Path) -
revision,
)
diff_target = created["diffTarget"]
- run_workbench(
+ save_workspace(
state_dir,
- "save-workspace",
- "--workspace-id",
workspace_id,
- "--target-path",
str(target),
- "--scope",
".",
- "--mode",
"diff",
"--diff-target-kind",
"working_tree",
@@ -2463,10 +2456,8 @@ def test_workbench_populates_completed_manifest_with_exact_diff_target(tmp_path:
target = tmp_path / "target"
revision = initialize_git_repository(target)
workspace_id = str(uuid.uuid4())
- run_workbench(
+ create_workspace(
state_dir,
- "create-workspace",
- "--workspace-id",
workspace_id,
"--target-path",
str(target),
@@ -2477,16 +2468,11 @@ def test_workbench_populates_completed_manifest_with_exact_diff_target(tmp_path:
"--diff-head-revision",
revision,
)
- saved = run_workbench(
+ saved = save_workspace(
state_dir,
- "save-workspace",
- "--workspace-id",
workspace_id,
- "--target-path",
str(target),
- "--scope",
".",
- "--mode",
"diff",
"--diff-target-kind",
"commit",
diff --git a/plugins/codex-security/tests/test_workbench_db_exports.py b/plugins/codex-security/tests/test_workbench_db_exports.py
index b1880ff23..e00f52822 100644
--- a/plugins/codex-security/tests/test_workbench_db_exports.py
+++ b/plugins/codex-security/tests/test_workbench_db_exports.py
@@ -18,12 +18,14 @@ import pytest
from workbench_test_support import (
create_saved_git_workspace,
create_saved_workspace,
+ create_workspace,
empty_target_scan,
initialize_git_repository,
mark_deep_coordinator_succeeded,
request_remediation,
request_remediation_action,
run_workbench,
+ save_workspace,
scan_command,
set_remediation,
start_delivered_scan,
@@ -881,28 +883,10 @@ def test_deep_csv_export_adds_only_candidate_id_column(
target = tmp_path / "target"
target.mkdir()
workspace_id = str(uuid.uuid4())
- run_workbench(
- state_dir,
- "create-workspace",
- "--workspace-id",
- workspace_id,
- "--thread-id",
- "thread-deep-export",
- "--target-path",
- str(target),
- )
- run_workbench(
- state_dir,
- "save-workspace",
- "--workspace-id",
- workspace_id,
- "--target-path",
- str(target),
- "--scope",
- ".",
- "--mode",
- "deep",
+ create_workspace(
+ state_dir, workspace_id, "--thread-id", "thread-deep-export", "--target-path", str(target)
)
+ save_workspace(state_dir, workspace_id, str(target), ".", "deep")
scan_id, scan_dir = start_workspace_scan(state_dir, workspace_id, tmp_path / "scans")
run_workbench(
state_dir,
diff --git a/plugins/codex-security/tests/test_workbench_finding_workflows.py b/plugins/codex-security/tests/test_workbench_finding_workflows.py
index c4a69b8de..3f3cd068e 100644
--- a/plugins/codex-security/tests/test_workbench_finding_workflows.py
+++ b/plugins/codex-security/tests/test_workbench_finding_workflows.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import json
import sqlite3
+from contextlib import closing
import pytest
@@ -224,7 +225,7 @@ def test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbenc
connection, history, lambda: TIMESTAMP, workbench_api["backfill_security_targets"]
)
- try:
+ with closing(connection):
migrate(tuple(m for m in migrations if m[0] <= 36))
with connection:
for state in (completed, unfinished, pending):
@@ -271,14 +272,12 @@ def test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbenc
assert row["scope_all_repositories"] is None
assert row["created_at"] == TIMESTAMP
assert row["updated_at"] == "2026-08-02T00:00:00Z"
- finally:
- connection.close()
# Reopen the actual file so this remains a persistence and migration test.
connection = sqlite3.connect(database)
connection.row_factory = sqlite3.Row
connection.execute("PRAGMA foreign_keys = ON")
- try:
+ with closing(connection):
for state in (completed, unfinished, pending):
assert workflow(workbench_api, connection, "get", workflow_id=state["id"]) == {
"workflow": state
@@ -301,5 +300,3 @@ def test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbenc
"status": "completed",
"result": {"scanId": "resumed-scan"},
}
- finally:
- connection.close()
diff --git a/plugins/codex-security/tests/test_workbench_prompt_only_scan.py b/plugins/codex-security/tests/test_workbench_prompt_only_scan.py
index b7675d16a..a29d1ab24 100644
--- a/plugins/codex-security/tests/test_workbench_prompt_only_scan.py
+++ b/plugins/codex-security/tests/test_workbench_prompt_only_scan.py
@@ -6,6 +6,7 @@ import runpy
import sqlite3
import uuid
from concurrent.futures import ThreadPoolExecutor
+from contextlib import closing
from pathlib import Path
from threading import Event
from unittest import mock
@@ -249,12 +250,9 @@ def test_setup_scan_reuses_checked_target_metadata(tmp_path: Path) -> None:
start_globals,
{"scan_target_identity": record_target_identity},
),
+ closing(start_globals["connect"]()) as connection,
):
- connection = start_globals["connect"]()
- try:
- started = start(connection, args)
- finally:
- connection.close()
+ started = start(connection, args)
assert len(observed_metadata) == 1
metadata = observed_metadata[0]
@@ -338,8 +336,7 @@ def test_prompt_registration_keeps_existing_scans_readable(
return real_identity(*args, **kwargs)
def register():
- connection = namespace["connect"]()
- try:
+ with closing(namespace["connect"]()) as connection:
return start(
connection,
argparse.Namespace(
@@ -360,8 +357,6 @@ def test_prompt_registration_keeps_existing_scans_readable(
),
headless_standard=False,
)
- finally:
- connection.close()
def read_scans():
read = get_scan(state_dir, str(scan_id))
diff --git a/plugins/codex-security/tests/test_workbench_scan_archive.py b/plugins/codex-security/tests/test_workbench_scan_archive.py
index b8066e772..8637af58a 100644
--- a/plugins/codex-security/tests/test_workbench_scan_archive.py
+++ b/plugins/codex-security/tests/test_workbench_scan_archive.py
@@ -8,7 +8,7 @@ from workbench_test_support import fail_scan, run_workbench
def register_scan(
- state_dir: Path, repository: Path, scan_dir: Path, *arguments: str
+ state_dir: Path, repository: Path, scan_dir: Path, *arguments: str, check: bool = True
) -> dict[str, object]:
return run_workbench(
state_dir,
@@ -27,6 +27,7 @@ def register_scan(
}
),
*arguments,
+ check=check,
)
@@ -129,25 +130,7 @@ def test_archive_requires_previous_directory_when_artifacts_exist(tmp_path: Path
),
)
- rejected = run_workbench(
- state_dir,
- "register-cli-scan",
- "--repository",
- str(repository),
- "--scan-dir",
- str(scan_dir),
- "--recipe-json",
- json.dumps(
- {
- "config": {},
- "mode": "standard",
- "repository": str(repository),
- "target": {"kind": "repository", "paths": []},
- }
- ),
- "--archive-existing",
- check=False,
- )
+ rejected = register_scan(state_dir, repository, scan_dir, "--archive-existing", check=False)
assert rejected["returncode"] != 0
assert "archived scan directory is required" in str(rejected["stderr"])
@@ -161,25 +144,7 @@ def test_archive_does_not_replace_a_running_scan(tmp_path: Path) -> None:
scan_dir.mkdir(mode=0o700)
previous = register_scan(state_dir, repository, scan_dir)
- rejected = run_workbench(
- state_dir,
- "register-cli-scan",
- "--repository",
- str(repository),
- "--scan-dir",
- str(scan_dir),
- "--recipe-json",
- json.dumps(
- {
- "config": {},
- "mode": "standard",
- "repository": str(repository),
- "target": {"kind": "repository", "paths": []},
- }
- ),
- "--archive-existing",
- check=False,
- )
+ rejected = register_scan(state_dir, repository, scan_dir, "--archive-existing", check=False)
assert rejected["returncode"] != 0
assert "Cannot archive the output of a running scan." in str(rejected["stderr"])
diff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.py
index 6e685bd7b..6a79568e2 100644
--- a/plugins/codex-security/tests/workbench_test_support.py
+++ b/plugins/codex-security/tests/workbench_test_support.py
@@ -726,8 +726,6 @@ def write_completed_contract(
scan_id: str,
target: Path,
*,
- artifact_scan_id: str | None = None,
- exclude_paths: list[str] | None = None,
identity_anchor: str = "archive-entry-write-without-containment",
include_paths: list[str] | None = None,
relative_path: str = "src/extract.py",
@@ -736,16 +734,13 @@ def write_completed_contract(
diff_base_revision: str | None = None,
diff_head_revision: str | None = None,
snapshot_digest: str | None = None,
- target_id: str | None = None,
coverage_mode: str = "repository",
inventory_strategy: str = "repository",
) -> None:
- artifact_scan_id = artifact_scan_id or scan_id
- exclude_paths = exclude_paths or []
include_paths = include_paths or ["."]
target_contract = {
"kind": target_kind,
- "targetId": target_id or stable_target_id(target),
+ "targetId": stable_target_id(target),
"displayName": target.name,
"snapshotDigest": snapshot_digest
or (
@@ -763,7 +758,7 @@ def write_completed_contract(
findings = {
"documentType": "codex-security.findings",
"schemaVersion": "1.0",
- "scanId": artifact_scan_id,
+ "scanId": scan_id,
"findings": [
{
"ruleId": "path-traversal.archive-extraction",
@@ -827,12 +822,12 @@ def write_completed_contract(
coverage = {
"documentType": "codex-security.coverage",
"schemaVersion": "1.0",
- "scanId": artifact_scan_id,
+ "scanId": scan_id,
"mode": coverage_mode,
"completeness": "complete",
"inventoryStrategy": inventory_strategy,
"includePaths": include_paths,
- "excludePaths": exclude_paths,
+ "excludePaths": [],
"surfaces": [
{
"id": "surface_archive_extraction",
@@ -848,7 +843,7 @@ def write_completed_contract(
"documentType": "codex-security.scan-manifest",
"schemaVersion": "1.0",
"scan": {
- "id": artifact_scan_id,
+ "id": scan_id,
"producer": {
"name": "codex-security-plugin",
"version": source_plugin_version(),
@@ -857,7 +852,7 @@ def write_completed_contract(
"startedAt": "2026-06-02T18:00:00Z",
"completedAt": "2026-06-02T18:09:00Z",
"target": target_contract,
- "scope": {"includePaths": include_paths, "excludePaths": exclude_paths},
+ "scope": {"includePaths": include_paths, "excludePaths": []},
"coverageRef": "coverage.json",
"findingsRef": "findings.json",
},