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.5 KB
Raw Blame
# allow: no-sut-import — tests a shell script (.github/scripts/check-file-writes.sh), not Python code
"""Tests for the mkdir scanner bypass detection in check-file-writes.sh.

Verifies that raw mkdir() calls on the same line as create_directory()
are NOT silently exempted by the safe-keyword filter.
"""

import subprocess
import textwrap
from pathlib import Path

import pytest


SCANNER = (
    Path(__file__).resolve().parents[2]
    / ".github"
    / "scripts"
    / "check-file-writes.sh"
)


def _run_scanner(tmp_path: Path, source_files: dict[str, str]) -> str:
    """Run check-file-writes.sh from a temp repo root with src/ structure."""
    for rel_path, content in source_files.items():
        full = tmp_path / rel_path
        full.parent.mkdir(parents=True, exist_ok=True)
        full.write_text(textwrap.dedent(content))

    result = subprocess.run(
        ["bash", str(SCANNER)],
        capture_output=True,
        text=True,
        timeout=30,
        cwd=str(tmp_path),
    )
    return result.stdout + result.stderr


def test_scanner_flags_mkdir_same_line_as_create_directory(tmp_path):
    """A single line with both .mkdir() and create_directory() must be flagged."""
    output = _run_scanner(
        tmp_path,
        {
            "src/app/bypass.py": """
                from pathlib import Path
                from security.directory_creation import create_directory

                def bypass():
                    Path("bad").mkdir(); create_directory("good")
            """,
        },
    )
    assert "bypass" in output.lower() or "raw" in output.lower(), (
        f"Expected bypass to be flagged, got:\n{output}"
    )


def test_scanner_does_not_flag_legitimate_create_directory(tmp_path):
    """Lines that only call create_directory() without raw mkdir should pass."""
    output = _run_scanner(
        tmp_path,
        {
            "src/app/safe.py": """
                from security.directory_creation import create_directory

                def safe_setup():
                    create_directory("good_path")
            """,
        },
    )
    assert "passed" in output.lower(), f"Expected pass, got:\n{output}"


@pytest.mark.parametrize("direct_import", ["mkdir", "makedirs"])
def test_scanner_flags_direct_os_imports(
    tmp_path: Path, direct_import: str
) -> None:
    output = _run_scanner(
        tmp_path,
        {
            "src/app/direct_import.py": f"""
                from os import {direct_import}

                def bypass():
                    {direct_import}("bad")
            """,
        },
    )
    assert "found raw directory-creation calls" in output.lower(), (
        f"Expected direct {direct_import} import to be flagged, got:\n{output}"
    )


def test_scanner_does_not_allowlist_all_of_config_paths(tmp_path: Path) -> None:
    output = _run_scanner(
        tmp_path,
        {
            "src/local_deep_research/config/paths.py": """
                from pathlib import Path

                def create_backup_directory():
                    Path("backup").mkdir()
            """,
        },
    )
    assert "found raw directory-creation calls" in output.lower(), (
        f"Expected config/paths.py raw mkdir to be flagged, got:\n{output}"
    )


def test_scanner_ignores_mkdir_mentions_in_comments(tmp_path: Path) -> None:
    output = _run_scanner(
        tmp_path,
        {
            "src/app/comment.py": """
                # Explain why callers no longer use mkdir()
            """,
        },
    )
    assert "passed" in output.lower(), f"Expected pass, got:\n{output}"