import pytest from pipeline import secret_scan def test_ensure_ready_raises_when_gitleaks_missing_and_no_override(monkeypatch): monkeypatch.setattr(secret_scan, "gitleaks_available", lambda: False) monkeypatch.delenv("ALLOW_MISSING_GITLEAKS", raising=False) with pytest.raises(RuntimeError, match="Gitleaks"): secret_scan.ensure_secret_scanner_ready() def test_ensure_ready_passes_when_override_set(monkeypatch): monkeypatch.setattr(secret_scan, "gitleaks_available", lambda: False) monkeypatch.setenv("ALLOW_MISSING_GITLEAKS", "1") secret_scan.ensure_secret_scanner_ready() # must not raise def test_ensure_ready_passes_when_gitleaks_available(monkeypatch): monkeypatch.setattr(secret_scan, "gitleaks_available", lambda: True) monkeypatch.delenv("ALLOW_MISSING_GITLEAKS", raising=False) secret_scan.ensure_secret_scanner_ready() # must not raise def test_scan_code_raises_when_gitleaks_missing_and_no_override(monkeypatch): monkeypatch.setattr(secret_scan, "shutil", secret_scan.shutil) monkeypatch.setattr(secret_scan.shutil, "which", lambda name: None) monkeypatch.delenv("ALLOW_MISSING_GITLEAKS", raising=False) with pytest.raises(RuntimeError, match="fail-closed"): secret_scan.scan_code("x = 1") def test_scan_code_returns_warning_result_when_override_set(monkeypatch): monkeypatch.setattr(secret_scan.shutil, "which", lambda name: None) monkeypatch.setenv("ALLOW_MISSING_GITLEAKS", "1") result = secret_scan.scan_code("x = 1") assert result["passed"] is True assert result["warning"] == "gitleaks_not_installed_override_allowed" def test_truthy_variants(): for v in ("1", "true", "True", "yes", "on"): assert secret_scan._truthy(v) for v in ("0", "false", "", None, "no"): assert not secret_scan._truthy(v)