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.

5.1 KB
Raw Blame
"""
Tests for the _hook_common pre-commit-hook shared-constants module.

Covers the invariants introduced when SECURE_LOGGING_DIRS was extracted
from check-sensitive-logging.py and custom-checks.py into _hook_common.py:

  * the constant is well-formed and its paths are substring-match-safe
  * both hook modules bind the SAME object (identity, not just equality),
    i.e. neither hook re-introduces a local literal that shadows the
    shared one
  * importing custom-checks, check-deprecated-db, check-ldr-db or
    check-research-id-type does not mutate the importer's environment
    (LDR_ALLOW_UNENCRYPTED is set inside main(), not at module scope)

The co-located-test pattern mirrors tests/hooks/test_commit_analysis.py
for the other shared module in .pre-commit-hooks/.
"""

import os
import subprocess
import sys
from importlib import import_module
from pathlib import Path

import pytest

HOOKS_DIR = Path(__file__).parent.parent.parent / ".pre-commit-hooks"
sys.path.insert(0, str(HOOKS_DIR))

from _hook_common import SECURE_LOGGING_DIRS  # noqa: E402

# Hook filenames are hyphenated, so they must be imported by string via
# import_module (the same idiom tests/security/test_sensitive_logging_hook.py
# uses). _hook_common itself is a valid identifier and is imported directly
# above, which is the contract this module exists to verify.
_check_sensitive_logging = import_module("check-sensitive-logging")
_custom_checks = import_module("custom-checks")


class TestSecureLoggingDirsShape:
    """SECURE_LOGGING_DIRS is well-formed for the substring match the hooks do."""

    def test_is_non_empty_tuple_of_str(self):
        assert isinstance(SECURE_LOGGING_DIRS, tuple)
        assert len(SECURE_LOGGING_DIRS) >= 1
        assert all(isinstance(d, str) for d in SECURE_LOGGING_DIRS)

    def test_entries_end_with_slash(self):
        # The hooks match via ``d in filename``; a missing trailing slash
        # would let "src/.../web_search_engines" match "web_search_engines_v2/".
        bad = [d for d in SECURE_LOGGING_DIRS if not d.endswith("/")]
        assert not bad, (
            "SECURE_LOGGING_DIRS entries must end with '/': " + ", ".join(bad)
        )


class TestHooksBindToHookCommon:
    """Both hook modules bind the shared constant by identity.

    Equality alone is not enough: if either hook re-introduced a local
    ``SECURE_LOGGING_DIRS = (...)`` literal that happened to match, the
    constant could drift again silently. Identity (``is``) proves the hook
    references the single source of truth in _hook_common rather than a
    private copy.
    """

    def test_check_sensitive_logging_binds_to_hook_common(self):
        assert (
            _check_sensitive_logging.SECURE_LOGGING_DIRS is SECURE_LOGGING_DIRS
        )

    def test_custom_checks_binds_to_hook_common(self):
        assert _custom_checks.SECURE_LOGGING_DIRS is SECURE_LOGGING_DIRS

    def test_both_hooks_bind_the_same_object(self):
        assert (
            _check_sensitive_logging.SECURE_LOGGING_DIRS
            is _custom_checks.SECURE_LOGGING_DIRS
        )


class TestHookImportPurity:
    """Importing these hooks must not mutate the process environment.

    ``LDR_ALLOW_UNENCRYPTED=true`` lets a hook tolerate legacy
    unencrypted dev DBs while it *runs*; setting it at module scope
    instead made every importer — including this test suite — silently
    mutate its own process env. Sibling hook tests still import modules
    that set it at module scope, so the shared pytest process env cannot
    be trusted for this assertion; it is checked in a pristine
    subprocess instead.

    Covers every hook that sets ``LDR_ALLOW_UNENCRYPTED``: custom-checks
    was the first to move the assignment into ``main()``;
    check-deprecated-db, check-ldr-db and check-research-id-type followed
    the same move.
    """

    @pytest.mark.parametrize(
        "hook_module_name",
        [
            "custom-checks",
            "check-deprecated-db",
            "check-ldr-db",
            "check-research-id-type",
        ],
    )
    def test_import_does_not_set_ldr_allow_unencrypted(self, hook_module_name):
        """A clean child that imports the hook must not gain the variable.

        Fails if the assignment is reverted to (or left at) module scope:
        the child would then print "true" instead of "None".
        """
        child_code = (
            "import os, sys\n"
            "from importlib import import_module\n"
            "sys.path.insert(0, sys.argv[1])\n"
            f"import_module({hook_module_name!r})\n"
            "print(os.environ.get('LDR_ALLOW_UNENCRYPTED'))\n"
        )
        env = {
            k: v for k, v in os.environ.items() if k != "LDR_ALLOW_UNENCRYPTED"
        }
        result = subprocess.run(
            [sys.executable, "-c", child_code, str(HOOKS_DIR)],
            capture_output=True,
            text=True,
            env=env,
        )
        assert result.returncode == 0, result.stderr
        assert result.stdout.strip() == "None", (
            f"importing {hook_module_name} must not set "
            "LDR_ALLOW_UNENCRYPTED; keep the assignment inside main()"
        )