Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

3.6 KB
Raw Blame
"""Run the workflow's scan against independent synthetic Git histories."""

# allow: no-sut-import -- exercises the repository's scanner workflow.

import json
import os
from pathlib import Path
import shutil
import subprocess

import pytest
import yaml


ROOT = Path(__file__).resolve().parents[3]


@pytest.mark.parametrize(
    ("event", "expected"),
    [
        ("schedule", {"historical.txt", "current.txt"}),
        ("workflow_dispatch", {"historical.txt", "current.txt"}),
        ("pull_request", {"current.txt"}),
    ],
)
def test_scan_preserves_reachable_history_and_excludes_other_branches(
    tmp_path, event, expected
):
    scanner = os.environ.get("GITLEAKS_PATH") or shutil.which("gitleaks")
    if scanner is None:
        if os.environ.get("REQUIRE_GITLEAKS") == "1":
            pytest.fail("Gitleaks is required for this selection")
        pytest.skip("Gitleaks is not installed")

    def git(*args):
        return subprocess.run(
            ["git", *args],
            cwd=tmp_path,
            check=True,
            capture_output=True,
            text=True,
            timeout=10,
        ).stdout.strip()

    git("init", "--initial-branch=main")
    git("config", "user.name", "Synthetic Fixture")
    git("config", "user.email", "[email protected]")
    # These markers exercise revision selection; they are not credentials.
    (tmp_path / ".gitleaks.toml").write_text(
        'title = "Synthetic scope fixture"\n'
        '[[rules]]\nid = "scope-fixture"\n'
        'regex = "SCAN_SCOPE_FIXTURE_[A-Z_]+"\n',
        encoding="utf-8",
    )
    (tmp_path / ".gitleaksignore").write_text("", encoding="utf-8")
    historical = tmp_path / "historical.txt"
    historical.write_text("SCAN_SCOPE_FIXTURE_HISTORICAL\n", encoding="utf-8")
    git("add", ".")
    git("commit", "-m", "Add historical fixture")
    historical.unlink()
    git("add", ".")
    git("commit", "-m", "Remove historical fixture")
    base = git("rev-parse", "HEAD")
    git("switch", "-c", "unrelated")
    (tmp_path / "unrelated.txt").write_text(
        "SCAN_SCOPE_FIXTURE_OTHER_BRANCH\n", encoding="utf-8"
    )
    git("add", ".")
    git("commit", "-m", "Add unrelated fixture")
    git("switch", "main")
    (tmp_path / "current.txt").write_text(
        "SCAN_SCOPE_FIXTURE_CURRENT\n", encoding="utf-8"
    )
    git("add", ".")
    git("commit", "-m", "Add current fixture")
    head = git("rev-parse", "HEAD")

    workflow = yaml.safe_load(
        (ROOT / ".github/workflows/gitleaks.yml").read_text(encoding="utf-8")
    )
    step = next(
        step
        for step in workflow["jobs"]["gitleaks"]["steps"]
        if step.get("id") == "gitleaks"
    )
    for name in ("SCAN_EVENT", "SCAN_BASE_SHA", "SCAN_HEAD_SHA"):
        assert name in step["env"]
    bin_dir = tmp_path / "bin"
    bin_dir.mkdir()
    (bin_dir / "gitleaks").symlink_to(Path(scanner).resolve())
    result = subprocess.run(
        ["bash", "-e", "-o", "pipefail", "-c", step["run"]],
        cwd=tmp_path,
        env={
            **os.environ,
            "PATH": str(bin_dir) + os.pathsep + os.defpath,
            "SCAN_EVENT": event,
            "SCAN_BASE_SHA": base,
            "SCAN_HEAD_SHA": head,
        },
        capture_output=True,
        text=True,
        timeout=30,
        check=False,
    )
    assert result.returncode == 1, result.stderr
    report = json.loads(
        (tmp_path / "gitleaks-results.sarif").read_text(encoding="utf-8")
    )
    findings = report["runs"][0]["results"]
    assert {
        result["locations"][0]["physicalLocation"]["artifactLocation"]["uri"]
        for result in findings
    } == expected