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

194 lines
6.5 KiB
Python

"""
Tests for the check-utcnow-parens pre-commit hook.
Verifies that the hook flags default=utcnow and onupdate=utcnow
(without parentheses) while allowing the correct default=utcnow()
and server_default=utcnow() patterns.
"""
import subprocess
import sys
import tempfile
from pathlib import Path
HOOK_SCRIPT = (
Path(__file__).parent.parent.parent
/ ".pre-commit-hooks"
/ "check-utcnow-parens.py"
)
def _run_hook(content: str) -> subprocess.CompletedProcess:
"""Write content to a temp .py file and run the hook against it."""
with tempfile.NamedTemporaryFile(suffix=".py", mode="w", delete=False) as f:
f.write(content)
f.flush()
return subprocess.run(
[sys.executable, str(HOOK_SCRIPT), f.name],
capture_output=True,
text=True,
)
# =========================================================================
# Should flag (exit code 1)
# =========================================================================
class TestFlagsIncorrectPatterns:
"""Patterns that must be caught by the hook."""
def test_default_without_parens(self):
result = _run_hook(
"created_at = Column(UtcDateTime, default=utcnow, nullable=False)\n"
)
assert result.returncode == 1
assert "utcnow without parentheses" in result.stdout
def test_onupdate_without_parens(self):
result = _run_hook(
"updated_at = Column(UtcDateTime, onupdate=utcnow, nullable=False)\n"
)
assert result.returncode == 1
assert "utcnow without parentheses" in result.stdout
def test_both_default_and_onupdate_without_parens(self):
result = _run_hook(
"updated_at = Column(UtcDateTime, default=utcnow, onupdate=utcnow, nullable=False)\n"
)
assert result.returncode == 1
assert "utcnow without parentheses" in result.stdout
def test_default_without_parens_trailing_paren(self):
"""default=utcnow) — missing opening paren."""
result = _run_hook("created_at = Column(UtcDateTime, default=utcnow)\n")
assert result.returncode == 1
def test_default_without_parens_multiline(self):
result = _run_hook(
"updated_at = Column(\n"
" UtcDateTime, default=utcnow, onupdate=utcnow, nullable=False\n"
")\n"
)
assert result.returncode == 1
def test_default_with_spaces_around_equals(self):
result = _run_hook(
"created_at = Column(UtcDateTime, default = utcnow, nullable=False)\n"
)
assert result.returncode == 1
# =========================================================================
# Should pass (exit code 0)
# =========================================================================
class TestAllowsCorrectPatterns:
"""Patterns that must NOT be flagged."""
def test_default_with_parens(self):
result = _run_hook(
"created_at = Column(UtcDateTime, default=utcnow(), nullable=False)\n"
)
assert result.returncode == 0
def test_onupdate_with_parens(self):
result = _run_hook(
"updated_at = Column(UtcDateTime, onupdate=utcnow(), nullable=False)\n"
)
assert result.returncode == 0
def test_server_default_with_parens(self):
result = _run_hook(
"created_at = Column(UtcDateTime, server_default=utcnow(), nullable=False)\n"
)
assert result.returncode == 0
def test_both_with_parens(self):
result = _run_hook(
"updated_at = Column(UtcDateTime, default=utcnow(), onupdate=utcnow(), nullable=False)\n"
)
assert result.returncode == 0
def test_no_utcnow_at_all(self):
result = _run_hook("name = Column(String(100), nullable=False)\n")
assert result.returncode == 0
def test_utcnow_in_comment(self):
"""Comments mentioning utcnow() should not be flagged."""
result = _run_hook(
"# Note: created_at uses default=utcnow() in the model\n"
)
assert result.returncode == 0
def test_utcnow_import(self):
result = _run_hook("from sqlalchemy_utc import UtcDateTime, utcnow\n")
assert result.returncode == 0
def test_utcnow_assignment(self):
"""Direct assignment like existing_rating.created_at = utcnow()."""
result = _run_hook("existing_rating.created_at = utcnow()\n")
assert result.returncode == 0
def test_empty_file(self):
result = _run_hook("")
assert result.returncode == 0
# =========================================================================
# Comment / docstring skip (regression)
# =========================================================================
class TestCommentAndDocstringSkip:
"""Lines that start with a comment or docstring delimiter must not fire,
even if they contain the literal bad pattern."""
def test_comment_with_bare_utcnow_not_flagged(self):
"""Key regression: `# default=utcnow is wrong` must not fire."""
result = _run_hook("# default=utcnow is wrong — use default=utcnow()\n")
assert result.returncode == 0
def test_comment_with_onupdate_bare_utcnow_not_flagged(self):
result = _run_hook("# onupdate=utcnow was the old style\n")
assert result.returncode == 0
def test_triple_double_quote_line_not_flagged(self):
result = _run_hook('"""default=utcnow was the wrong style."""\n')
assert result.returncode == 0
def test_triple_single_quote_line_not_flagged(self):
result = _run_hook("'''default=utcnow was the wrong style.'''\n")
assert result.returncode == 0
def test_indented_comment_not_flagged(self):
"""Indented comments also respect the skip."""
result = _run_hook(" # default=utcnow\n")
assert result.returncode == 0
# =========================================================================
# Output format
# =========================================================================
class TestOutputFormat:
"""Verify the error messages include useful context."""
def test_shows_filename_and_line(self):
result = _run_hook(
"x = 1\n"
"created_at = Column(UtcDateTime, default=utcnow, nullable=False)\n"
)
assert result.returncode == 1
assert ":2:" in result.stdout # Line 2
def test_shows_hint(self):
result = _run_hook(
"created_at = Column(UtcDateTime, default=utcnow, nullable=False)\n"
)
assert "Hint:" in result.stdout
assert "SQL expression" in result.stdout