Skip to content

Commit f292102

Browse files
Sergio SisternesCopilot
andcommitted
fix(audit): extract DRIFT_SKIP_PREFIX constant; rewrite aggregate test
- Add DRIFT_SKIP_PREFIX constant to ci_checks.py; use it in the skip message and import it in audit.py to replace the brittle hardcoded string literal in the startswith() check. - Rewrite test_cache_miss_does_not_block_other_checks to call _check_drift directly (monkeypatch was previously a no-op via run_baseline_checks which never invokes _check_drift); now builds a CIAuditResult directly and asserts the aggregate passes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ad9edc4 commit f292102

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

src/apm_cli/commands/audit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ def _audit_content_scan(
695695
and not package
696696
and (project_root / "apm.yml").exists()
697697
):
698-
from ..policy.ci_checks import _check_drift
698+
from ..policy.ci_checks import DRIFT_SKIP_PREFIX, _check_drift
699699

700700
lockfile_path = get_lockfile_path(project_root)
701701
if lockfile_path.exists():
@@ -723,7 +723,7 @@ def _audit_content_scan(
723723
elif (
724724
drift_check.passed
725725
and not drift_findings
726-
and drift_check.message.startswith("drift skipped")
726+
and drift_check.message.startswith(DRIFT_SKIP_PREFIX)
727727
):
728728
click.echo(
729729
f"{STATUS_SYMBOLS['warning']} {drift_check.message}",

src/apm_cli/policy/ci_checks.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ def _check_includes_consent(
407407
)
408408

409409

410+
#: Prefix used in the drift :class:`CheckResult` message when the check is
411+
#: skipped due to a cold cache. ``audit.py`` imports this to detect the
412+
#: skip case without comparing against a raw string literal.
413+
DRIFT_SKIP_PREFIX = "drift skipped"
414+
415+
410416
def _check_drift(
411417
project_root: Path,
412418
lockfile: LockFile,
@@ -453,7 +459,7 @@ def _check_drift(
453459
name="drift",
454460
passed=True,
455461
message=(
456-
"drift skipped: install cache not populated "
462+
f"{DRIFT_SKIP_PREFIX}: install cache not populated "
457463
"(run 'apm install' first or pass --no-drift)"
458464
),
459465
),

tests/unit/policy/test_ci_checks.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,32 +1195,39 @@ def _raise_cache_miss(*_args: object, **_kwargs: object) -> None:
11951195
def test_cache_miss_does_not_block_other_checks(
11961196
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
11971197
) -> None:
1198-
"""A cache miss on drift must leave the CIAuditResult passing when
1199-
the baseline checks all pass -- drift skip-with-info must not
1200-
propagate a failure to the aggregate result."""
1198+
"""Adding the cache-miss drift result to a CIAuditResult must not
1199+
cause the aggregate to fail -- passed=True from the skip must
1200+
propagate correctly when combined with other passing checks."""
1201+
from apm_cli.deps.lockfile import LockFile
12011202
from apm_cli.install.drift import CacheMissError
1203+
from apm_cli.policy.ci_checks import _check_drift
12021204

1203-
_write_apm_yml(tmp_path, deps=["owner/repo#v1.0.0"])
12041205
_write_lockfile(
12051206
tmp_path,
1206-
textwrap.dedent("""\
1207-
lockfile_version: '1'
1207+
textwrap.dedent(""" lockfile_version: '1'
12081208
generated_at: '2025-01-01T00:00:00Z'
1209-
dependencies:
1210-
- repo_url: owner/repo
1211-
resolved_ref: v1.0.0
1212-
deployed_files:
1213-
- .github/prompts/test.md
1209+
dependencies: []
12141210
"""),
12151211
)
1216-
(tmp_path / ".github" / "prompts").mkdir(parents=True, exist_ok=True)
1217-
(tmp_path / ".github" / "prompts" / "test.md").write_text("# ok\n", encoding="utf-8")
1212+
lockfile = LockFile.read(tmp_path / "apm.lock.yaml")
1213+
assert lockfile is not None
12181214

12191215
def _raise_cache_miss(*_args: object, **_kwargs: object) -> None:
12201216
raise CacheMissError("cold cache")
12211217

12221218
monkeypatch.setattr("apm_cli.install.drift.run_replay", _raise_cache_miss)
12231219

1224-
result = run_baseline_checks(tmp_path)
1225-
# Baseline checks (no drift) must still pass.
1226-
assert result.passed
1220+
drift_result, findings = _check_drift(tmp_path, lockfile)
1221+
1222+
# Simulate a CIAuditResult that already has passing baseline checks
1223+
# plus the drift skip result; the aggregate must remain passing.
1224+
from apm_cli.policy.models import CIAuditResult
1225+
1226+
aggregate = CIAuditResult()
1227+
aggregate.checks.append(CheckResult(name="lockfile-exists", passed=True, message="ok"))
1228+
aggregate.checks.append(CheckResult(name="ref-consistency", passed=True, message="ok"))
1229+
aggregate.checks.append(drift_result)
1230+
1231+
assert drift_result.passed, "cache miss must produce a passing drift result"
1232+
assert findings == [], "cache miss must produce no findings"
1233+
assert aggregate.passed, "cache-miss skip must not fail the aggregate CIAuditResult"

0 commit comments

Comments
 (0)