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
149 lines
3.4 KiB
Python
149 lines
3.4 KiB
Python
"""
|
|
Tests for the check-silent-exceptions pre-commit hook.
|
|
|
|
Ensures the hook detects silent exception swallowing (except: pass) that
|
|
masks bugs and violates the project's no-fallbacks principle. Exceptions
|
|
should always be logged or re-raised.
|
|
"""
|
|
|
|
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-silent-exceptions")
|
|
check_file_fn = hook_module.check_file
|
|
|
|
|
|
def _write_and_check(tmp_path, code: str, filename: str = "module.py"):
|
|
p = tmp_path / filename
|
|
p.write_text(code, encoding="utf-8")
|
|
return check_file_fn(str(p))
|
|
|
|
|
|
class TestDetectsSilentExceptions:
|
|
"""Ensures bare except:pass and except Exception:pass are caught."""
|
|
|
|
def test_detects_except_exception_pass(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except Exception:
|
|
pass
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) >= 1
|
|
assert any("silent" in msg.lower() for _, msg in issues)
|
|
|
|
def test_detects_bare_except_pass(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except:
|
|
pass
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) >= 1
|
|
|
|
def test_detects_except_with_as_pass(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except Exception as e:
|
|
pass
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) >= 1
|
|
|
|
|
|
class TestAllowsProperHandling:
|
|
"""Ensures exceptions with logging or re-raise are not flagged."""
|
|
|
|
def test_allows_logger_debug(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except Exception:
|
|
logger.debug("Expected failure")
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|
|
|
|
def test_allows_logger_warning(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except Exception:
|
|
logger.warning("Something went wrong")
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|
|
|
|
def test_allows_logger_exception(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except Exception:
|
|
logger.exception("Unexpected error")
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|
|
|
|
def test_allows_reraise(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except Exception:
|
|
raise
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|
|
|
|
def test_allows_noqa_comment(self, tmp_path):
|
|
code = """
|
|
try:
|
|
do_work()
|
|
except Exception: # noqa: silent-exception
|
|
pass
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|
|
|
|
|
|
class TestAllowsSpecificExceptions:
|
|
"""Ensures catching specific exceptions (not Exception) is allowed."""
|
|
|
|
def test_allows_value_error_pass(self, tmp_path):
|
|
code = """
|
|
try:
|
|
int("abc")
|
|
except ValueError:
|
|
pass
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|
|
|
|
def test_allows_type_error_pass(self, tmp_path):
|
|
code = """
|
|
try:
|
|
len(None)
|
|
except TypeError:
|
|
pass
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|
|
|
|
def test_allows_key_error_pass(self, tmp_path):
|
|
code = """
|
|
try:
|
|
d = {}
|
|
val = d["missing"]
|
|
except KeyError:
|
|
pass
|
|
"""
|
|
issues = _write_and_check(tmp_path, code)
|
|
assert len(issues) == 0
|