diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d97936..e261df2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,10 +24,23 @@ file to it (#39). Two holes the checker had are closed: a `Lenses run` claim higher than the lens sections present now fails instead of being corrected away (#50), and an Evidence citation must be a trailing part of a supplied path, not just share its basename (#49). Table rows split only -on unescaped pipes (#43). The checks pass their failure list explicitly -(#47), and `scripts/MANIFEST` pins each script's hash, gated by -`check.py` (#46). Each of these comes with a regression test in -`scripts/test_scripts.py`, which CI runs. +on unescaped pipes (#43), and a row of the wrong width now fails by +name in every reader: `check_review.py`'s Scorecard, which used to drop +it and report a missing row instead, `render_scorecard.py`, which passed +it through uncorrected, and `score_seeds.py`, which skipped it. That +includes header and rule rows, which were dropped at any width, and a +row of empty cells, which was taken for a rule row. Only the line after +a table's header is its rule row; a later row of dashes, which GitHub +shows as data, is named too. So is a row below the index header that +repeats its `ID`, and a finding in a header's place, which GitHub shows +as a header. A row too wide says a literal `|` is written `\|`. Three +siblings are older and filed: a row GitHub renders that no reader +reads, with no outer `|` or indented (#64); a mandated table with no +rule row or split by a blank line (#65); and a Scorecard metric written +twice or not in the template (#66). The checks pass their failure list +explicitly (#47), and `scripts/MANIFEST` pins each script's hash, gated +by `check.py` (#46). Each of these comes with a +regression test in `scripts/test_scripts.py`, which CI runs. **Every published review now passes the checker in CI.** `examples/web-service.md` had only its Scorecard arithmetic checked, and diff --git a/scripts/MANIFEST b/scripts/MANIFEST index e3b07da..7a4f0c4 100644 --- a/scripts/MANIFEST +++ b/scripts/MANIFEST @@ -1,5 +1,5 @@ f3d35ca8359d423aed41693095ca8eef9108e9c510824b3b84e81f0ee3008ba7 check.py -4dcec07435a68c47c230eb27d70595bd76b44a401c6960e66beb514cd8c03a05 check_review.py -cad6924afe9884328306dd13a20d5857555fe4868d831262eac1cc3d0783a382 render_scorecard.py -0db5a9cb54c36cd6953208e87387383f056017fa2e0a7ebd99c46cb9783e9122 score_seeds.py -86c632bc45e16c539754c8c9de4fab234e5f13e59a74c0405fa2bfb8a93ff3f6 test_scripts.py +b35395c9ae9a71c163a5b19a9f0f716dd24d6221aa0db159ba31b20e8aa1ce09 check_review.py +0e4cbbeecd19d1c5491302873ee531de7f2a67cfe6c8ee5704dc58f3f1e8c2b0 render_scorecard.py +1826ba11b67423970a48b619ac1cb874db7fedda78e6557d097bdc72ac63b445 score_seeds.py +71b4fd11f8460536bbeaaffa0705a75c1d3d22379b0dc86720e7a25534d26ab0 test_scripts.py diff --git a/scripts/check_review.py b/scripts/check_review.py index 4a580c8..23d247c 100755 --- a/scripts/check_review.py +++ b/scripts/check_review.py @@ -87,15 +87,110 @@ def unescape(cell): return plain(cell.replace('\\|', '|')) -def table_rows(body): - """Raw cells of every table row in a body, header and rule rows dropped.""" - rows = [] +def wrong_width(where, cells, want): + """The failure for a table row that is not `want` cells wide. + + Every reader of a mandated table names a row of the wrong width rather + than dropping it: an unescaped `|` inside a cell ends it (PROMPT.md, the + literal-pipe rule), and a dropped row surfaced later as "Scorecard has no + 'Governors' row", or as nothing at all (issue #43, D1). + """ + first = unescape(cells[0]) if cells else '' + # The hint only where it is the likely cause: a row too narrow has lost a + # cell, and no pipe escape restores one. + hint = ': a literal | inside a cell is written \\|' if len(cells) > want else '' + columns = 'column' if len(cells) == 1 else 'columns' + return (f'{where} row {first[:40]!r} is {len(cells)} {columns}, expected ' + f'{want}{hint}') + + +# A rule row (`|---|:--:|`) is one whose every cell is dashes with optional +# colons. At least one dash: `^:?-*:?$` also matched an empty cell, so a row +# of empty cells was dropped as a rule row and never read (issue #43, D1). +RULE_CELL = re.compile(r'^:?-+:?$') + + +def is_rule(cells): + return all(RULE_CELL.match(c) for c in cells) + + +def table_parts(body): + """(rows, rules, strays, heads) of the tables in a body. + + The rule row is the line straight after a table's header: GFM reads only + that one as the rule. `rules` holds those, `strays` every other row whose + cells are all dashes, and `rows` everything else, header included. A stray + used to be dropped as a rule row wherever it sat; GFM renders it as a data + row, so a reader that dropped it read a table GitHub does not show (issue + #43, D1). `heads` holds each table's header, the row above its rule row, + by position: a reader that skips its header by content skipped a data row + that merely repeated it. + """ + rows, rules, strays, heads = [], [], [], [] + at, first = 0, None for line in body.split('\n'): cells = split_row(line) - if cells is None or all(re.match(r'^:?-*:?$', c) for c in cells): + if cells is None: + at = 0 continue - rows.append(cells) - return rows + if at == 0: + first = cells + if is_rule(cells): + if at == 1: + rules.append(cells) + heads.append(first) + else: + strays.append(cells) + else: + rows.append(cells) + at += 1 + return rows, rules, strays, heads + + +def is_header(cells, heads): + """Whether these cells are, by identity, one of table_parts()'s heads.""" + return any(cells is head for head in heads) + + +def repeated_header(where, cells): + """The failure for a data row that repeats the header's first cell.""" + return (f'{where} row {unescape(cells[0])[:40]!r} repeats the header but is ' + f'not the table\'s header: GitHub shows it as a data row') + + +def misplaced_header(where, cells): + """The failure for a header-position row that is not the header. + + The row above a rule row is what GitHub shows as the header, so a finding + there is a finding no reader may skip: skipped by position, it was dropped + and the seed score undercounted without a word (issue #43, D1). + """ + return (f'{where} row {unescape(cells[0])[:40]!r} is above the rule row: ' + f'GitHub shows it as the header, not as a row') + + +def rule_rows(body): + """Cells of each table's rule row: the rows table_rows drops. + + GFM does not render a table whose rule row is not the header's width, so + a reader that drops rule rows must still name one of the wrong width. + """ + return table_parts(body)[1] + + +def stray_rules(where, body): + """The failure for each all-dash row that is not a table's rule row.""" + return [f'{where} row {unescape(cells[0])[:40]!r} is all dashes below the ' + f'rule row: GitHub shows it as a data row' + for cells in table_parts(body)[2]] + + +def table_rows(body): + """Raw cells of every table row in a body; rule rows and strays dropped. + + A caller that reads a mandated table names the strays with stray_rules(). + """ + return table_parts(body)[0] def outside_fences(lines): @@ -451,10 +546,24 @@ def lenses_run_row(review, vocab): return (int(m.group(1)), value) if m else None -def scorecard_cells(table): - """{metric: value} for every two-cell row of a Scorecard table body.""" - return {unescape(cells[0]): unescape(cells[1]) - for cells in table_rows(table) if len(cells) == 2} +def scorecard_cells(table, failures=None): + """{metric: value} for every two-cell row of a Scorecard table body. + + A row of any other width is named in `failures`, when given. Only + check_scorecard passes it; lenses_run_row reads the same table and does + not, so each such row is reported once per review. + """ + card = {} + for cells in table_rows(table): + if len(cells) == 2: + card[unescape(cells[0])] = unescape(cells[1]) + elif failures is not None: + failures.append(wrong_width('Scorecard', cells, 2)) + if failures is not None: + failures.extend(wrong_width('Scorecard', cells, 2) + for cells in rule_rows(table) if len(cells) != 2) + failures.extend(stray_rules('Scorecard', table)) + return card def check_lenses(review, lenses, scope, vocab, failures): @@ -531,11 +640,28 @@ def index_rows(review, vocab, failures): if body is None: failures.append('no "## FINDINGS INDEX" section') return [] - rows = [[unescape(c) for c in cells] for cells in table_rows(body) - if unescape(cells[0]) != 'ID'] + # The header and rule rows are dropped, but only once they are the right + # width: either one of the wrong width was dropped unnamed (issue #43, D1). + failures.extend(wrong_width('index', cells, 8) + for cells in rule_rows(body) if len(cells) != 8) + failures.extend(stray_rules('index', body)) + rows = [] + parts, _, _, heads = table_parts(body) + for cells in parts: + if is_header(cells, heads): + if unescape(cells[0]) != 'ID': + failures.append(misplaced_header('index', cells)) + elif len(cells) != 8: + failures.append(wrong_width('index', cells, 8)) + continue + if unescape(cells[0]) == 'ID': + # Skipped by content, this row was a finding no check read. + failures.append(repeated_header('index', cells)) + continue + rows.append([unescape(c) for c in cells]) for row in rows: if len(row) != 8: - failures.append(f'index row is {len(row)} columns, expected 8: {row[:1]}') + failures.append(wrong_width('index', row, 8)) continue prefix = vocab['id_prefixes'].get(lens_name(row[1])) if not re.fullmatch(r'[A-Z0-9]+-\d+', row[0]) or ( @@ -571,7 +697,7 @@ def check_scorecard(review, rows, prompt_path, vocab, failures): if table is None: failures.append('no "### Scorecard" section') return - card = scorecard_cells(table) + card = scorecard_cells(table, failures) version = instrument_version(review) if version is None: diff --git a/scripts/render_scorecard.py b/scripts/render_scorecard.py index 5911e09..5847352 100755 --- a/scripts/render_scorecard.py +++ b/scripts/render_scorecard.py @@ -127,7 +127,13 @@ def fix_row(line): # Escape-aware, as check_review.py parses it: a value carrying a # literal `\|` is one cell, not two (cycle-6 VAR-3, issue #43). cells = check_review.split_row(line) - if cells is None or len(cells) != 2: + if cells is None: + return line + if len(cells) != 2: + # Named, not passed through: a row this script cannot read is a + # row it silently left uncorrected, and a rule row of the wrong + # width means GFM renders no table at all (issue #43, D1). + refused.append(check_review.wrong_width('Scorecard', cells, 2)) return line raw_key, value = cells key = check_review.unescape(raw_key) @@ -197,6 +203,9 @@ def fix_row(line): return line table = '\n'.join(fix_row(line) for line in table.split('\n')) + # A stray is two cells wide, so fix_row reads it as a row named '---'; + # it is named here instead (issue #43, D1). + refused.extend(check_review.stray_rules('Scorecard', table)) refused.extend(f'instrument rejected: {f}' for f in failures) return '\n'.join(prefix + table.split('\n') + suffix), changes, notes, refused diff --git a/scripts/score_seeds.py b/scripts/score_seeds.py index 4fe41fa..03ede66 100755 --- a/scripts/score_seeds.py +++ b/scripts/score_seeds.py @@ -36,14 +36,32 @@ def findings(path): if body is None: sys.exit('no FINDINGS INDEX in review') out = [] - for cells in check_review.table_rows(body): + wrong = [check_review.wrong_width('index', cells, 8) + for cells in check_review.rule_rows(body) if len(cells) != 8] + wrong += check_review.stray_rules('index', body) + parts, _, _, heads = check_review.table_parts(body) + for cells in parts: c = [check_review.unescape(x) for x in cells] - if len(c) != 8 or c[0] == 'ID': + if check_review.is_header(cells, heads): + if c[0] != 'ID': + wrong.append(check_review.misplaced_header('index', cells)) + elif len(c) != 8: + wrong.append(check_review.wrong_width('index', cells, 8)) + continue + if c and c[0] == 'ID': + wrong.append(check_review.repeated_header('index', cells)) + continue + if len(c) != 8: + # Named, not skipped: a skipped row is a finding the score never + # saw, so the seed count could undercount silently (issue #43, D1). + wrong.append(check_review.wrong_width('index', cells, 8)) continue m = re.search(r':(\d+)(?:[-–](\d+))?', c[3]) if m: out.append((c[0], c[1], int(m.group(1)), int(m.group(2) or m.group(1)), c[4], c[5], c[6])) + if wrong: + sys.exit('\n'.join(wrong)) return out diff --git a/scripts/test_scripts.py b/scripts/test_scripts.py index 1d317a0..e4c46ed 100644 --- a/scripts/test_scripts.py +++ b/scripts/test_scripts.py @@ -212,7 +212,198 @@ def test_43_an_unescaped_pipe_still_fails(self): review = self.write('bare-pipes.md', text) code, out = self.check(review, EXAMPLE_ARTIFACT) self.assertEqual(code, 1, out) - self.assertIn('index row is 10 columns, expected 8', out) + self.assertIn("index row 'SUB-1' is 10 columns, expected 8: " + "a literal | inside a cell is written \\|", out) + + # -- issue #43 D1: every reader names a row of the wrong width --------- + + def with_bare_scorecard_pipe(self): + text = read(EXAMPLE) + governors = '· 🐍 Library/Framework ·' + self.assertIn(governors, text) + return text.replace(governors, '· 🐍 Library | Framework ·', 1) + + def test_43_a_bare_pipe_in_the_scorecard_is_named_once(self): + review = self.write('scorecard-pipe.md', self.with_bare_scorecard_pipe()) + code, out = self.check(review, EXAMPLE_ARTIFACT) + self.assertEqual(code, 1, out) + named = "Scorecard row 'Governors' is 3 columns, expected 2" + self.assertEqual(out.count(named), 1, out) + + def test_43_render_scorecard_refuses_a_wrong_width_row(self): + review = self.write('scorecard-pipe.md', self.with_bare_scorecard_pipe()) + code, out = run('render_scorecard.py', review) + self.assertEqual(code, 2, out) + self.assertIn("REFUSED: Scorecard row 'Governors' is 3 columns, expected 2", out) + + def scorecard_mutant(self, old, new): + text = read(EXAMPLE) + self.assertIn(old, text) + return self.write('scorecard-mutant.md', text.replace(old, new, 1)) + + def test_43_a_rule_row_of_the_wrong_width_is_named(self): + review = self.scorecard_mutant('| Metric | Value |\n|--------|-------|', + '| Metric | Value |\n|--------|-------|-------|') + named = "Scorecard row '--------' is 3 columns, expected 2" + code, out = self.check(review, EXAMPLE_ARTIFACT) + self.assertEqual(code, 1, out) + self.assertEqual(out.count(named), 1, out) + code, out = run('render_scorecard.py', review) + self.assertEqual(code, 2, out) + self.assertIn(f'REFUSED: {named}', out) + + def test_43_a_row_of_empty_cells_is_not_a_rule_row(self): + # Eight empty cells is the index's width, so only the rule-row + # pattern decides whether it is read: `^:?-*:?$` dropped it unread. + rule = '|----|------|-------|---------|----------|---------|----------------------|------------|\n' + text = read(EXAMPLE) + self.assertIn(rule, text) + review = self.write('empty-row.md', text.replace(rule, rule + '|' + ' |' * 8 + '\n', 1)) + code, out = self.check(review, EXAMPLE_ARTIFACT) + self.assertEqual(code, 1, out) + self.assertIn('index rows 18 |', out) + self.assertIn("FAIL: : Lens '' is not a lens or W5H1", out) + + def test_43_an_index_rule_row_of_the_wrong_width_is_named(self): + rule = '|----|------|-------|---------|----------|---------|----------------------|------------|' + named = "index row '----' is 9 columns, expected 8" + text = read(SEEDED) + self.assertIn(rule, text) + review = self.write('index-rule.md', text.replace(rule, rule + '---|', 1)) + code, out = self.check(review, SEEDED_ARTIFACT) + self.assertEqual(code, 1, out) + self.assertEqual(out.count(named), 1, out) + code, out = run('score_seeds.py', review, '--seeds', + os.path.join(ROOT, 'calibration', 'seeds.md')) + self.assertNotEqual(code, 0, out) + self.assertIn(named, out) + + def test_43_a_dash_row_below_the_index_rule_is_named(self): + # Only the line after the header is a rule row; GitHub shows a later + # all-dash row as data, so dropping it read a table it does not show. + rule = '|----|------|-------|---------|----------|---------|----------------------|------------|' + text = read(SEEDED) + self.assertIn(rule, text) + for extra, first in (('|' + ' - |' * 8, '-'), (rule, '----')): + named = (f"index row {first!r} is all dashes below the rule row: " + f"GitHub shows it as a data row") + review = self.write('index-dashes.md', text.replace(rule, rule + '\n' + extra, 1)) + code, out = self.check(review, SEEDED_ARTIFACT) + self.assertEqual(code, 1, out) + self.assertEqual(out.count(named), 1, out) + code, out = run('score_seeds.py', review, '--seeds', + os.path.join(ROOT, 'calibration', 'seeds.md')) + self.assertNotEqual(code, 0, out) + self.assertIn(named, out) + + def test_43_each_table_has_its_own_rule_row(self): + # The rule row is counted from each table's header, not the body's + # first line: a second table's rule is not a stray. + sys.path.insert(0, SCRIPTS) + try: + import check_review + finally: + sys.path.pop(0) + body = '| A | B |\n|---|---|\n| 1 | 2 |\n\ntext\n\n| C | D |\n|---|---|\n| 3 | 4 |\n' + rows, rules, strays, heads = check_review.table_parts(body) + self.assertEqual([h[0] for h in heads], ['A', 'C']) + self.assertEqual(len(rules), 2, (rules, strays)) + self.assertEqual(strays, []) + self.assertEqual(len(rows), 4, rows) + + def test_43_a_row_repeating_the_index_header_is_named(self): + # The header is the row above the rule row, by position. Skipped by + # content, a data row whose ID cell read 'ID' was never checked. + rule = '|----|------|-------|---------|----------|---------|----------------------|------------|' + extra = '| ID | Subtract | 1 | x | Major | Fix | hidden finding | High |' + named = ("index row 'ID' repeats the header but is not the table's " + "header: GitHub shows it as a data row") + text = read(SEEDED) + self.assertIn(rule, text) + review = self.write('index-id.md', text.replace(rule, rule + '\n' + extra, 1)) + code, out = self.check(review, SEEDED_ARTIFACT) + self.assertEqual(code, 1, out) + self.assertEqual(out.count(named), 1, out) + code, out = run('score_seeds.py', review, '--seeds', + os.path.join(ROOT, 'calibration', 'seeds.md')) + self.assertNotEqual(code, 0, out) + self.assertIn(named, out) + + def test_43_a_finding_in_the_header_place_is_named(self): + # Skipped by position, a finding above a rule row was dropped: a + # second table opened by one, or an index whose ID line was lost. + rule = '|----|------|-------|---------|----------|---------|----------------------|------------|' + header = '| ID | Lens | Cycle | Line(s) | Severity | Verdict | Claim (one sentence) | Confidence |' + sub9 = '| SUB-9 | Subtract | 1 | semver-2.0.0-seeded.md:10-11 | Major | Fix | hidden finding | High |' + text = read(SEEDED) + self.assertIn(header + '\n' + rule, text) + index = text.split('\n## FINDINGS INDEX\n', 1)[1] + rows = [l for l in index.split('\n') if l.startswith('| SUB-')] + first, last = rows[0], rows[-1] + # B: a second table under the index, opened by a finding. + second = text.replace(last, last + '\n\n' + sub9 + '\n' + rule, 1) + self.assertNotEqual(second, text) + # D: the ID line lost, so the first finding sits in the header's place. + lost = text.replace(header + '\n' + rule + '\n' + first, + first + '\n' + rule, 1) + self.assertNotEqual(lost, text) + for review_text, fid in ((second, 'SUB-9'), (lost, 'SUB-1')): + named = (f"index row {fid!r} is above the rule row: GitHub shows it " + f"as the header, not as a row") + review = self.write('header-place.md', review_text) + code, out = self.check(review, SEEDED_ARTIFACT) + self.assertEqual(code, 1, out) + self.assertEqual(out.count(named), 1, out) + code, out = run('score_seeds.py', review, '--seeds', + os.path.join(ROOT, 'calibration', 'seeds.md')) + self.assertNotEqual(code, 0, out) + self.assertIn(named, out) + + def test_43_a_dash_row_in_the_scorecard_body_is_named(self): + review = self.scorecard_mutant('|--------|-------|\n', + '|--------|-------|\n| Spare | x |\n|---|---|\n') + named = ("Scorecard row '---' is all dashes below the rule row: " + "GitHub shows it as a data row") + code, out = self.check(review, EXAMPLE_ARTIFACT) + self.assertEqual(code, 1, out) + self.assertEqual(out.count(named), 1, out) + code, out = run('render_scorecard.py', review) + self.assertEqual(code, 2, out) + self.assertIn(f'REFUSED: {named}', out) + + def test_43_a_narrow_row_gets_no_pipe_hint(self): + review = self.scorecard_mutant('|--------|-------|\n', + '|--------|-------|\n| Spare |\n') + code, out = self.check(review, EXAMPLE_ARTIFACT) + self.assertEqual(code, 1, out) + line = [l for l in out.splitlines() if "Scorecard row 'Spare'" in l] + self.assertEqual(len(line), 1, out) + self.assertTrue(line[0].rstrip().endswith('is 1 column, expected 2'), out) + + def test_43_an_index_header_of_the_wrong_width_is_named(self): + header = '| ID | Lens | Cycle | Line(s) | Severity | Verdict | Claim (one sentence) | Confidence |' + for review_path, artifact in ((EXAMPLE, EXAMPLE_ARTIFACT), (SEEDED, SEEDED_ARTIFACT)): + text = read(review_path) + self.assertIn(header, text) + review = self.write('header.md', text.replace(header, header + ' Extra |', 1)) + code, out = self.check(review, artifact) + self.assertEqual(code, 1, out) + self.assertIn("index row 'ID' is 9 columns, expected 8", out) + code, out = run('score_seeds.py', review, '--seeds', + os.path.join(ROOT, 'calibration', 'seeds.md')) + self.assertNotEqual(code, 0, out) + self.assertIn("index row 'ID' is 9 columns, expected 8", out) + + def test_43_score_seeds_names_a_wrong_width_row(self): + claim = '| SUB-2 | Subtract | 1 | semver-2.0.0-seeded.md:84-85 | Minor | Skip:Cobra | Clause 7' + text = read(SEEDED) + self.assertIn(claim, text) + review = self.write('seeded-pipe.md', text.replace( + claim, claim.replace('Clause 7', 'Clause | 7'), 1)) + code, out = run('score_seeds.py', review, '--seeds', + os.path.join(ROOT, 'calibration', 'seeds.md')) + self.assertNotEqual(code, 0, out) + self.assertIn("index row 'SUB-2' is 9 columns, expected 8", out) # -- issue #47: no failure state survives between calls ---------------- @@ -421,7 +612,7 @@ def entry_tables(self): if current is None: current = [] tables.append(current) - if not all(re.match(r'^:?-*:?$', c) for c in cells): + if not check_review.is_rule(cells): current.append([check_review.unescape(c) for c in cells]) self.assertEqual(len(tables), 2, tables) return [rows[1:] for rows in tables]