Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions scripts/MANIFEST
Original file line number Diff line number Diff line change
@@ -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
154 changes: 140 additions & 14 deletions scripts/check_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion scripts/render_scorecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions scripts/score_seeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading
Loading