script-docs.py: add support for printing errors in GitHub actions format

develop
lethosor 2020-06-30 14:03:05 -04:00
parent 6a712b8b28
commit 20993f9a42
2 changed files with 30 additions and 12 deletions

@ -105,7 +105,7 @@ class TabLinter(Linter):
linters = [cls() for cls in Linter.__subclasses__() if not cls.ignore] linters = [cls() for cls in Linter.__subclasses__() if not cls.ignore]
def main(): def main():
is_github_actions = os.environ.get('GITHUB_ACTIONS') is_github_actions = bool(os.environ.get('GITHUB_ACTIONS'))
root_path = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else '.') root_path = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else '.')
if not os.path.exists(root_path): if not os.path.exists(root_path):
print('Nonexistent path: %s' % root_path) print('Nonexistent path: %s' % root_path)

@ -1,11 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import print_function
from io import open
import os import os
from os.path import basename, dirname, join, splitext from os.path import basename, dirname, join, splitext
import sys import sys
SCRIPT_PATH = sys.argv[1] if len(sys.argv) > 1 else 'scripts' SCRIPT_PATH = sys.argv[1] if len(sys.argv) > 1 else 'scripts'
IS_GITHUB_ACTIONS = bool(os.environ.get('GITHUB_ACTIONS'))
def expected_cmd(path): def expected_cmd(path):
"""Get the command from the name of a script.""" """Get the command from the name of a script."""
@ -20,42 +19,61 @@ def check_ls(fname, line):
line = line.strip() line = line.strip()
comment = '--' if fname.endswith('.lua') else '#' comment = '--' if fname.endswith('.lua') else '#'
if '[====[' in line or not line.startswith(comment): if '[====[' in line or not line.startswith(comment):
print('Error: no leading comment in ' + fname) print_error('missing leading comment (requred for `ls`)', fname)
return 1 return 1
return 0 return 0
def print_error(message, filename, line=None):
if not isinstance(line, int):
line = 1
print('Error: %s:%i: %s' % (filename, line, message))
if IS_GITHUB_ACTIONS:
print('::error file=%s,line=%i::%s' % (filename, line, message))
def check_file(fname): def check_file(fname):
errors, doclines = 0, [] errors, doclines = 0, []
tok1, tok2 = ('=begin', '=end') if fname.endswith('.rb') else \ tok1, tok2 = ('=begin', '=end') if fname.endswith('.rb') else \
('[====[', ']====]') ('[====[', ']====]')
doc_start_line = None
with open(fname, errors='ignore') as f: with open(fname, errors='ignore') as f:
lines = f.readlines() lines = f.readlines()
if not lines:
print_error('empty file', fname)
return 1
errors += check_ls(fname, lines[0]) errors += check_ls(fname, lines[0])
for l in lines: for i, l in enumerate(lines):
if doclines or l.strip().endswith(tok1): if doclines or l.strip().endswith(tok1):
if not doclines:
doc_start_line = i + 1
doclines.append(l.rstrip()) doclines.append(l.rstrip())
if l.startswith(tok2): if l.startswith(tok2):
break break
else: else:
if doclines: if doclines:
print('Error: docs start but not end: ' + fname) print_error('docs start but do not end', fname, doc_start_line)
else: else:
print('Error: no documentation in: ' + fname) print_error('no documentation found', fname)
return 1 return 1
if not doclines: if not doclines:
print('Error: missing or malformed documentation in: ' + fname) print_error('missing or malformed documentation', fname)
return 1 return 1
title, underline = [d for d in doclines title, underline = [d for d in doclines
if d and '=begin' not in d and '[====[' not in d][:2] if d and '=begin' not in d and '[====[' not in d][:2]
if underline != '=' * len(title): title_line = doc_start_line + doclines.index(title)
print('Error: title/underline mismatch:', fname, title, underline) expected_underline = '=' * len(title)
if underline != expected_underline:
print_error('title/underline mismatch: expected {!r}, got {!r}'.format(
expected_underline, underline),
fname, title_line + 1)
errors += 1 errors += 1
if title != expected_cmd(fname): if title != expected_cmd(fname):
print('Warning: expected script title {}, got {}'.format( print_error('expected script title {!r}, got {!r}'.format(
expected_cmd(fname), title)) expected_cmd(fname), title),
fname, title_line)
errors += 1 errors += 1
return errors return errors