test(fetchall-guard): add line-shift tolerance and duplicate detection tests (#6872)#7787
Conversation
FakerHideInBush
left a comment
There was a problem hiding this comment.
Three concerns before merging:
1. test_fetchall_guard_ignores_unrelated_line_shifts hardcodes PATH=/usr/bin:/bin which will fail on macOS, Windows, and any CI environment where Python is installed outside those paths
result = run_guard(env={"PATH": "/usr/bin:/bin", "FETCHALL_BASELINE": str(TMP_BASELINE)})This wipes the entire process environment except PATH. If run_guard spawns python3 or the guard script, and Python is in /usr/local/bin (macOS Homebrew) or /home/runner/.local/bin (GitHub Actions), the guard process will fail with command not found or ModuleNotFoundError. Replace with:
env = {**os.environ, "FETCHALL_BASELINE": str(TMP_BASELINE)}
result = run_guard(env=env)2. The annotation in test_fetchall_guard_ignores_unrelated_line_shifts is on the line BEFORE .fetchall(), not on the same line — if the guard requires inline annotation, this test silently passes a case the guard would actually flag as a violation
The test writes:
def schema_bounded(conn):
# fetchall-ok: bounded-by-schema
return conn.execute('SELECT * FROM accounts').fetchall()The annotation # fetchall-ok is on a separate line, while .fetchall() is at the end of the return statement. If the guard scans for # fetchall-ok annotations on the same line as .fetchall() (which is the pattern used elsewhere in the codebase — e.g. return [dict(r) for r in c.execute(sql, params).fetchall() # fetchall-ok: already-paginated), then the guard would NOT recognize this as annotated, would report it as a new violation, and the test assertion returncode == 0 would fail.
The test should document whether prior-line comments are supported, and if not, the annotation in the test fixture should be placed on the same line as .fetchall().
3. test_fetchall_guard_catches_duplicate_content_new_call does not verify that BOTH violations are reported — a guard that deduplicates by SQL content would pass this test while missing one
result = run_guard()
assert result.returncode == 1
assert "new unannotated .fetchall()" in result.stdoutThe assertion only checks that the string appears at least once. If the guard deduplicates by SQL content (SELECT 1) and reports only dup_a, the test passes even though dup_b is silently skipped. Add assertions that both violation sites are reported:
assert "dup_a" in result.stdout
assert "dup_b" in result.stdoutc3af571 to
a2ef121
Compare
jaxint
left a comment
There was a problem hiding this comment.
Review Summary
This PR adds comprehensive tests for fetchall guard behavior.
New Tests:
- test_fetchall_guard_ignores_unrelated_line_shifts: Verifies adding/removing unrelated lines does NOT trigger the guard
- test_fetchall_guard_catches_new_distinct_call: Verifies genuinely new fetchall patterns ARE caught
- test_fetchall_guard_catches_duplicate_content_new_call: Verifies additional instances of existing patterns ARE caught
Test Quality:
- Validates line-number-independent normalization
- Clear test structure
- Proper cleanup in finally blocks
✅ APPROVED - Good test coverage for fetchall guard edge cases.
RTC RewardThis merged PR earned 5 RTC — sent to |
Closes #6872
RTC wallet: RTCfe13452d122263caf633ab1876bd9631133b68b1
Changes
Testing