Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
faho committed Nov 22, 2020
1 parent b9c24a3 commit 9836e67
Showing 1 changed file with 78 additions and 27 deletions.
105 changes: 78 additions & 27 deletions littlecheck/littlecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import shlex
import subprocess
import sys

try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
from difflib import SequenceMatcher

# Directives can occur at the beginning of a line, or anywhere in a line that does not start with #.
COMMENT_RE = r'^(?:[^#].*)?#\s*'
COMMENT_RE = r"^(?:[^#].*)?#\s*"

# A regex showing how to run the file.
RUN_RE = re.compile(COMMENT_RE + r"RUN:\s+(.*)\n")
Expand Down Expand Up @@ -139,9 +140,10 @@ def escaped_text(self, for_formatting=False):
ret = ret.replace("{", "{{").replace("}", "}}")
return ret


class RunCmd(object):
""" A command to run on a given Checker.
"""A command to run on a given Checker.
Attributes:
args: Unexpanded shell command as a string.
"""
Expand Down Expand Up @@ -215,10 +217,16 @@ def message(self):
"",
]
if self.error_annotation_lines:
fields["error_annotation"] = " ".join([x.text for x in self.error_annotation_lines])
fields["error_annotation_lineno"] = str(self.error_annotation_lines[0].number)
fields["error_annotation"] = " ".join(
[x.text for x in self.error_annotation_lines]
)
fields["error_annotation_lineno"] = str(
self.error_annotation_lines[0].number
)
if len(self.error_annotation_lines) > 1:
fields["error_annotation_lineno"] += ":" + str(self.error_annotation_lines[-1].number)
fields["error_annotation_lineno"] += ":" + str(
self.error_annotation_lines[-1].number
)
fmtstrs += [
" additional output on stderr:{error_annotation_lineno}:",
" {BOLD}{error_annotation}{RESET}",
Expand All @@ -229,40 +237,72 @@ def message(self):
lastcheckline = None
for d in self.diff.get_grouped_opcodes():
for op, alo, ahi, blo, bhi in d:
color="{BOLD}"
if op == 'replace' or op == 'delete':
color="{RED}"
color = "{BOLD}"
if op == "replace" or op == "delete":
color = "{RED}"
# We got a new chunk, so we print a marker.
if alo > lasthi:
fmtstrs += [
" [...] from line " + str(self.checks[blo].line.number)
+ " (" + self.lines[alo].file + ":" + str(self.lines[alo].number) + "):"
" [...] from line "
+ str(self.checks[blo].line.number)
+ " ("
+ self.lines[alo].file
+ ":"
+ str(self.lines[alo].number)
+ "):"
]
lasthi = ahi

# We print one "no more checks" after the last check and then skip any markers
lastcheck = False
for a, b in zip_longest(self.lines[alo:ahi], self.checks[blo:bhi]):
# Clean up strings for use in a format string - double up the curlies.
astr = color + a.escaped_text(for_formatting=True) + "{RESET}" if a else ""
astr = (
color + a.escaped_text(for_formatting=True) + "{RESET}"
if a
else ""
)
if b:
bstr = "'{BLUE}" + b.line.escaped_text(for_formatting=True) + "{RESET}'" + " on line " + str(b.line.number)
bstr = (
"'{BLUE}"
+ b.line.escaped_text(for_formatting=True)
+ "{RESET}'"
+ " on line "
+ str(b.line.number)
)
lastcheckline = b.line.number

if op == 'equal':
if op == "equal":
fmtstrs += [" " + astr]
elif b and a:
fmtstrs += [" " + astr + " <= does not match " + b.type + " " + bstr]
fmtstrs += [
" "
+ astr
+ " <= does not match "
+ b.type
+ " "
+ bstr
]
elif b:
fmtstrs += [" " + astr + " <= nothing to match " + b.type + " " + bstr]
fmtstrs += [
" "
+ astr
+ " <= nothing to match "
+ b.type
+ " "
+ bstr
]
elif not b:
string = " " + astr
if bhi == len(self.checks):
if not lastcheck:
string += " <= no more checks"
lastcheck = True
elif lastcheckline is not None:
string += " <= no check matches this, previous check on line " + str(lastcheckline)
string += (
" <= no check matches this, previous check on line "
+ str(lastcheckline)
)
else:
string += " <= no check matches"
fmtstrs.append(string)
Expand All @@ -276,8 +316,8 @@ def print_message(self):


def perform_substitution(input_str, subs):
""" Perform the substitutions described by subs to str
Return the substituted string.
"""Perform the substitutions described by subs to str
Return the substituted string.
"""
# Sort our substitutions into a list of tuples (key, value), descending by length.
# It needs to be descending because we need to try longer substitutions first.
Expand Down Expand Up @@ -368,11 +408,22 @@ def check(self, lines, checks):
# If there's a mismatch or still lines or checkers, we have a failure.
# Otherwise it's success.
if mismatches:
return TestFailure(mismatches[0][0], mismatches[0][1], self, diff=diff, lines=usedlines, checks=usedchecks)
return TestFailure(
mismatches[0][0],
mismatches[0][1],
self,
diff=diff,
lines=usedlines,
checks=usedchecks,
)
elif lineq:
return TestFailure(lineq[-1], None, self, diff=diff, lines=usedlines, checks=usedchecks)
return TestFailure(
lineq[-1], None, self, diff=diff, lines=usedlines, checks=usedchecks
)
elif checkq:
return TestFailure(None, checkq[-1], self, diff=diff, lines=usedlines, checks=usedchecks)
return TestFailure(
None, checkq[-1], self, diff=diff, lines=usedlines, checks=usedchecks
)
else:
# Success!
return None
Expand All @@ -381,8 +432,8 @@ def run(self):
""" Run the command. Return a TestFailure, or None. """

def split_by_newlines(s):
""" Decode a string and split it by newlines only,
retaining the newlines.
"""Decode a string and split it by newlines only,
retaining the newlines.
"""
return [s + "\n" for s in s.decode("utf-8").split("\n")]

Expand Down Expand Up @@ -420,7 +471,7 @@ def split_by_newlines(s):
# non-matching or unmatched stderr text, then annotate the outfail
# with it.
if outfail and errfail and errfail.line:
outfail.error_annotation_lines = errlines[errfail.line.number - 1:]
outfail.error_annotation_lines = errlines[errfail.line.number - 1 :]
# Trim a trailing newline
if outfail.error_annotation_lines[-1].text == "\n":
del outfail.error_annotation_lines[-1]
Expand Down Expand Up @@ -528,8 +579,8 @@ def check_path(path, subs, config, failure_handler):


def parse_subs(subs):
""" Given a list of input substitutions like 'foo=bar',
return a dictionary like {foo:bar}, or exit if invalid.
"""Given a list of input substitutions like 'foo=bar',
return a dictionary like {foo:bar}, or exit if invalid.
"""
result = {}
for sub in subs:
Expand Down

0 comments on commit 9836e67

Please sign in to comment.