Blank-lines guard test for the generator
The reported symptom (a blank line after every line of generated source) was investigated Nov 29, 2026 and does NOT reproduce in the current generator: all 16 gen_matrix cases measure a blank-after-content ratio of 0.01-0.06 (section separators only), while double-spaced output scores ~1.0. This guard pins the clean output so a regression fails loudly: tests/blank_lines_guard.py checks every generated .cpp/.h under tests/out/ for a blank-after-content ratio >= 0.20, and run_matrix.sh runs it right after generation. Scope is source files only (README/Makefile are short files whose paragraph/rule breaks legitimately score 0.25-0.34). Verified: PASS on clean output (16 files); FAILs a double-spaced file at ratio 0.99. Matrix: all 16 cases + guard pass.
This commit is contained in:
1 parent
780853bdf2
commit
127035101e
2 files changed
+83
No files matched your search
Executable
+77
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Guard against the generator emitting a blank line after every line.
|
||||
|
||||
A "double-spaced" file has a blank line after (nearly) every content
|
||||
line. Clean generated output only uses blank lines as section
|
||||
separators: measured 0.01-0.06 blank-after-content ratio across all 16
|
||||
gen_matrix cases. A double-spaced file scores ~1.0. Threshold 0.20
|
||||
gives wide margin on both sides.
|
||||
|
||||
Usage: blank_lines_guard.py <dir> [<dir> ...]
|
||||
Scans every generated SOURCE file (.cpp/.h/.c/.cc) under each dir
|
||||
(recursively). Exits 1 and prints the offending file if any file's
|
||||
blank-after-content ratio is >= 0.20. Prints PASS otherwise.
|
||||
|
||||
Scope note: only source files are checked. The generated README.md
|
||||
and Makefile are short files whose paragraph/rule breaks legitimately
|
||||
push the ratio to ~0.25-0.34, so they are out of scope for this guard;
|
||||
the complaint was about generated source code.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
THRESHOLD = 0.20
|
||||
|
||||
|
||||
def ratio_of(path):
|
||||
with open(path, "r", errors="replace") as f:
|
||||
text = f.read()
|
||||
lines = text.split("\n")
|
||||
# Drop the trailing empty element produced by a final newline.
|
||||
if lines and lines[-1] == "":
|
||||
lines = lines[:-1]
|
||||
if not lines:
|
||||
return 0.0
|
||||
content = 0
|
||||
blank_after = 0
|
||||
for i, line in enumerate(lines):
|
||||
if line.strip() != "":
|
||||
content += 1
|
||||
if i + 1 < len(lines) and lines[i + 1].strip() == "":
|
||||
blank_after += 1
|
||||
if content == 0:
|
||||
return 0.0
|
||||
return blank_after / content
|
||||
|
||||
|
||||
def main():
|
||||
dirs = sys.argv[1:]
|
||||
if not dirs:
|
||||
print("usage: blank_lines_guard.py <dir> [...]")
|
||||
return 2
|
||||
bad = []
|
||||
checked = 0
|
||||
for d in dirs:
|
||||
for root, _sub, files in os.walk(d):
|
||||
for name in files:
|
||||
path = os.path.join(root, name)
|
||||
# Generated source files only (see scope note above).
|
||||
if not name.endswith((".cpp", ".h", ".c", ".cc")):
|
||||
continue
|
||||
try:
|
||||
r = ratio_of(path)
|
||||
except OSError:
|
||||
continue
|
||||
checked += 1
|
||||
if r >= THRESHOLD:
|
||||
bad.append((path, r))
|
||||
if bad:
|
||||
for path, r in bad:
|
||||
print("FAIL blank-lines %s ratio=%.2f (>= %.2f)" % (path, r, THRESHOLD))
|
||||
return 1
|
||||
print("PASS blank-lines guard (%d files, all ratios < %.2f)" % (checked, THRESHOLD))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -13,6 +13,12 @@ rm -rf tests/out
|
||||
echo "== generating =="
|
||||
tests/gen_matrix tests/out || exit 1
|
||||
|
||||
# Blank-lines guard: the generator must not emit a blank line after
|
||||
# every line of generated source. (The symptom was investigated
|
||||
# Nov 29, 2026 and does not reproduce in the current generator;
|
||||
# this guard pins the clean output so a regression fails loudly.)
|
||||
python3 tests/blank_lines_guard.py tests/out || exit 1
|
||||
|
||||
pass=0; fail=0; failed=""
|
||||
for d in tests/out/*/; do
|
||||
name=$(basename "$d")
|
||||
|
||||
Reference in new issue
Block a user