Files
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

134 lines
4.1 KiB
Python

"""
Tests for the check-env-vars pre-commit hook.
Ensures the hook enforces centralized configuration through SettingsManager
instead of direct os.environ access, which prevents hardcoded secrets and
ensures consistent config management.
"""
import ast
import sys
from importlib import import_module
from pathlib import Path
HOOKS_DIR = Path(__file__).parent.parent.parent / ".pre-commit-hooks"
sys.path.insert(0, str(HOOKS_DIR))
hook_module = import_module("check-env-vars")
EnvVarChecker = hook_module.EnvVarChecker
def _check_code(
code: str, filename: str = "src/local_deep_research/module.py"
) -> list:
"""Parse code and run the env var checker."""
tree = ast.parse(code)
checker = EnvVarChecker(filename)
checker.visit(tree)
return checker.errors
class TestDetectsDirectEnvAccess:
"""Ensures os.environ usage is flagged in application code."""
def test_detects_os_environ_get(self):
code = 'import os\nval = os.environ.get("LDR_SECRET_KEY")\n'
errors = _check_code(code)
assert len(errors) >= 1
assert any("SettingsManager" in e[1] for e in errors)
def test_detects_os_environ_bracket(self):
code = 'import os\nval = os.environ["LDR_DATA_DIR"]\n'
errors = _check_code(code)
assert len(errors) >= 1
def test_detects_os_getenv(self):
code = 'import os\nval = os.getenv("LDR_API_KEY")\n'
errors = _check_code(code)
assert len(errors) >= 1
def test_detects_from_os_import_environ(self):
code = "from os import environ\n"
errors = _check_code(code)
assert len(errors) >= 1
def test_detects_environ_alias(self):
code = "import os\nenv = os.environ\n"
errors = _check_code(code)
assert len(errors) >= 1
def test_detects_in_environ_check(self):
code = 'import os\nif "LDR_KEY" in os.environ:\n pass\n'
errors = _check_code(code)
assert len(errors) >= 1
class TestAllowsSystemVars:
"""Ensures standard system env vars are not flagged."""
def test_allows_path(self):
code = 'import os\npath = os.environ.get("PATH")\n'
errors = _check_code(code)
assert len(errors) == 0
def test_allows_home(self):
code = 'import os\nhome = os.environ.get("HOME")\n'
errors = _check_code(code)
assert len(errors) == 0
def test_allows_ci(self):
code = 'import os\nis_ci = os.environ.get("CI")\n'
errors = _check_code(code)
assert len(errors) == 0
def test_allows_pytest_current_test(self):
code = 'import os\ntest = os.environ.get("PYTEST_CURRENT_TEST")\n'
errors = _check_code(code)
assert len(errors) == 0
class TestAllowsExemptFiles:
"""Ensures settings/config/test files are exempt."""
def test_allows_settings_files(self):
code = 'import os\nval = os.environ.get("LDR_KEY")\n'
errors = _check_code(code, filename="src/settings/manager.py")
assert len(errors) == 0
def test_allows_config_files(self):
code = 'import os\nval = os.environ.get("LDR_KEY")\n'
errors = _check_code(code, filename="src/config/paths.py")
assert len(errors) == 0
def test_allows_test_files(self):
code = 'import os\nval = os.environ.get("LDR_KEY")\n'
errors = _check_code(code, filename="tests/test_something.py")
assert len(errors) == 0
def test_allows_migration_files(self):
code = 'import os\nval = os.environ.get("LDR_KEY")\n'
errors = _check_code(code, filename="migrations/env.py")
assert len(errors) == 0
class TestAllowsSafePatterns:
"""Ensures normal code without env access is not flagged."""
def test_allows_normal_code(self):
code = """
def process_data(data):
return data.upper()
"""
errors = _check_code(code)
assert len(errors) == 0
def test_allows_settings_manager_usage(self):
code = """
from settings.manager import SettingsManager
settings = SettingsManager()
val = settings.get_setting("my_key")
"""
errors = _check_code(code)
assert len(errors) == 0