fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerRun build_tools/update-dependencies.sh64da7ca12

master
29.3 KiB849 linesraw
1#!/usr/bin/env python
2
3"""Command line test driver."""
4
5from __future__ import unicode_literals
6from __future__ import print_function
7import argparse
8import asyncio
9import datetime
10from difflib import SequenceMatcher
11import io
12import re
13import shlex
14import sys
15import unicodedata
16
17try:
18    from itertools import zip_longest
19except ImportError:
20    from itertools import izip_longest as zip_longest  # type: ignore
21
22
23# Directives can occur at the beginning of a line, or anywhere in a line that does not start with #.
24COMMENT_RE = r"^(?:[^#].*)?#\s*"
25
26# A regex showing how to run the file.
27RUN_RE = re.compile(COMMENT_RE + r"RUN:\s+(.*)\n")
28REQUIRES_RE = re.compile(COMMENT_RE + r"REQUIRES:\s+(.*)\n")
29
30# A regex capturing lines that should be checked against stdout.
31CHECK_STDOUT_RE = re.compile(COMMENT_RE + r"CHECK:\s+(.*)\n")
32
33# A regex capturing lines that should be checked against stderr.
34CHECK_STDERR_RE = re.compile(COMMENT_RE + r"CHECKERR:\s+(.*)\n")
35
36VARIABLE_OVERRIDE_RE = re.compile(r"\w+=.*")
37
38SKIP = object()
39
40
41def find_command(program):
42    import os
43
44    path, _ = os.path.split(program)
45    if path:
46        return os.path.isfile(program) and os.access(program, os.X_OK)
47    for path in os.environ["PATH"].split(os.pathsep):
48        exe = os.path.join(path, program)
49        if os.path.isfile(exe) and os.access(exe, os.X_OK):
50            return exe
51
52    return None
53
54
55class Config(object):
56    def __init__(self):
57        # Whether to have verbose output.
58        self.verbose = False
59        # Whether output gets ANSI colorization.
60        self.colorize = False
61        # Whether to show which file was tested.
62        self.progress = False
63
64    def colors(self):
65        """Return a dictionary mapping color names to ANSI escapes"""
66
67        def ansic(n):
68            return "\033[%dm" % n if self.colorize else ""
69
70        return {
71            "RESET": ansic(0),
72            "BOLD": ansic(1),
73            "NORMAL": ansic(39),
74            "BLACK": ansic(30),
75            "RED": ansic(31),
76            "GREEN": ansic(32),
77            "YELLOW": ansic(33),
78            "BLUE": ansic(34),
79            "MAGENTA": ansic(35),
80            "CYAN": ansic(36),
81            "LIGHTGRAY": ansic(37),
82            "DARKGRAY": ansic(90),
83            "LIGHTRED": ansic(91),
84            "LIGHTGREEN": ansic(92),
85            "LIGHTYELLOW": ansic(93),
86            "LIGHTBLUE": ansic(94),
87            "LIGHTMAGENTA": ansic(95),
88            "LIGHTCYAN": ansic(96),
89            "WHITE": ansic(97),
90        }
91
92
93def output(*args):
94    print("".join(args) + "\n")
95
96
97def esc(m):
98    map = {
99        "\n": "\\n",
100        "\\": "\\\\",
101        "\a": "\\a",
102        "\b": "\\b",
103        "\f": "\\f",
104        "\r": "\\r",
105        "\t": "\\t",
106        "\v": "\\v",
107    }
108    if m in map:
109        return map[m]
110    if unicodedata.category(m)[0] == "C":
111        return "\\x{:02x}".format(ord(m))
112    else:
113        return m
114
115
116def escape_string(s):
117    return "".join(esc(ch) for ch in s)
118
119
120class CheckerError(Exception):
121    """Exception subclass for check line parsing.
122
123    Attributes:
124      line: the Line object on which the exception occurred.
125    """
126
127    def __init__(self, message, line=None):
128        super(CheckerError, self).__init__(message)
129        self.line = line
130
131
132class Line(object):
133    """A line that remembers where it came from."""
134
135    def __init__(self, text, number, file):
136        self.text = text
137        self.number = number
138        self.file = file
139
140    def __hash__(self):
141        # Chosen by fair diceroll
142        # No, just kidding.
143        # HACK: We pass this to the Sequencematcher, which puts the Checks into a dict.
144        # To force it to match the regexes, we return a hash collision intentionally,
145        # so it falls back on __eq__().
146        #
147        # CheckCmd has the same thing.
148        return 0
149
150    def __eq__(self, other):
151        if other is None:
152            return False
153        if isinstance(other, CheckCmd):
154            return other.regex.match(self.text)
155        if isinstance(other, Line):
156            # We only compare the text here so SequenceMatcher can reshuffle these
157            return self.text == other.text
158        raise NotImplementedError
159
160    def subline(self, text):
161        """Return a substring of our line with the given text, preserving number and file."""
162        return Line(text, self.number, self.file)
163
164    @staticmethod
165    def readfile(file, name):
166        return [Line(text, idx + 1, name) for idx, text in enumerate(file)]
167
168    def is_empty_space(self):
169        return not self.text or self.text.isspace()
170
171    def escaped_text(self, for_formatting=False):
172        ret = escape_string(self.text.rstrip("\n"))
173        if for_formatting:
174            ret = ret.replace("{", "{{").replace("}", "}}")
175        return ret
176
177
178class RunCmd(object):
179    """A command to run on a given Checker.
180
181    Attributes:
182        args: Unexpanded shell command as a string.
183    """
184
185    def __init__(self, args, line):
186        self.args = args
187        self.line = line
188
189    @staticmethod
190    def parse(line):
191        if not shlex.split(line.text):
192            raise CheckerError("Invalid RUN command", line)
193        return RunCmd(line.text, line)
194
195
196class TestFailure(object):
197    def __init__(self, line, check, testrun, diff=None, lines=[], checks=[]):
198        self.line = line
199        self.check = check
200        self.testrun = testrun
201        self.error_annotation_lines = None
202        self.diff = diff
203        self.lines = lines
204        self.checks = checks
205        self.signal = None
206
207    def message(self):
208        fields = self.testrun.config.colors()
209        fields["name"] = self.testrun.name
210        fields["subbed_command"] = self.testrun.subbed_command
211        if self.line:
212            fields.update(
213                {
214                    "output_file": self.line.file,
215                    "output_lineno": self.line.number,
216                    "output_line": self.line.escaped_text(),
217                }
218            )
219        if self.check:
220            fields.update(
221                {
222                    "input_file": self.check.line.file,
223                    "input_lineno": self.check.line.number,
224                    "input_line": self.check.line.escaped_text(),
225                    "check_type": self.check.type,
226                }
227            )
228        filemsg = "" if self.testrun.config.progress else " in {name}"
229        fmtstrs = ["{RED}Failure{RESET}" + filemsg + ":", ""]
230        if self.signal:
231            fmtstrs += [
232                "  Process was killed by signal {BOLD}" + self.signal + "{RESET}",
233                "",
234            ]
235        if self.line and self.check:
236            fmtstrs += [
237                "  The {check_type} on line {input_lineno} wants:",
238                "    {BOLD}{input_line}{RESET}",
239                "",
240                "  which failed to match line {output_file}:{output_lineno}:",
241                "    {BOLD}{output_line}{RESET}",
242                "",
243            ]
244
245        elif self.check:
246            fmtstrs += [
247                "  The {check_type} on line {input_lineno} wants:",
248                "    {BOLD}{input_line}{RESET}",
249                "",
250                "  but there was no remaining output to match.",
251                "",
252            ]
253        else:
254            fmtstrs += [
255                "  There were no remaining checks left to match {output_file}:{output_lineno}:",
256                "    {BOLD}{output_line}{RESET}",
257                "",
258            ]
259        if self.error_annotation_lines:
260            fields["error_annotation"] = "    ".join(
261                [x.text for x in self.error_annotation_lines]
262            )
263            fields["error_annotation_lineno"] = str(
264                self.error_annotation_lines[0].number
265            )
266            if len(self.error_annotation_lines) > 1:
267                fields["error_annotation_lineno"] += ":" + str(
268                    self.error_annotation_lines[-1].number
269                )
270            fmtstrs += [
271                "  additional output on stderr:{error_annotation_lineno}:",
272                "    {BOLD}{error_annotation}{RESET}",
273            ]
274        if self.diff:
275            fmtstrs += ["  Context:"]
276            lasthi = 0
277            lastcheckline = None
278            for d in self.diff.get_grouped_opcodes():
279                for op, alo, ahi, blo, bhi in d:
280                    color = "{BOLD}"
281                    if op == "replace" or op == "delete":
282                        color = "{RED}"
283                    # We got a new chunk, so we print a marker.
284                    if alo > lasthi:
285                        fmtstrs += [
286                            "    [...] from line "
287                            + str(self.checks[blo].line.number)
288                            + " ("
289                            + self.lines[alo].file
290                            + ":"
291                            + str(self.lines[alo].number)
292                            + "):"
293                        ]
294                    lasthi = ahi
295
296                    # We print one "no more checks" after the last check and then skip any markers
297                    lastcheck = False
298                    for a, b in zip_longest(self.lines[alo:ahi], self.checks[blo:bhi]):
299                        # Clean up strings for use in a format string - double up the curlies.
300                        astr = (
301                            color + a.escaped_text(for_formatting=True) + "{RESET}"
302                            if a
303                            else ""
304                        )
305                        # Convince type checker that bstr will in fact be a string when read.
306                        bstr = ""
307                        if b:
308                            bstr = (
309                                "on line "
310                                + str(b.line.number)
311                                + ": {BLUE}"
312                                + b.line.escaped_text(for_formatting=True)
313                                + "{RESET}"
314                            )
315                            lastcheckline = b.line.number
316
317                        if op == "equal":
318                            fmtstrs += ["    " + astr]
319                        elif b and a:
320                            fmtstrs += [
321                                "    "
322                                + astr
323                                + " <= does not match "
324                                + b.type
325                                + " "
326                                + bstr
327                            ]
328                        elif b:
329                            fmtstrs += [
330                                "    "
331                                + astr
332                                + " <= nothing to match "
333                                + b.type
334                                + " "
335                                + bstr
336                            ]
337                        elif not b:
338                            string = "    " + astr
339                            if bhi == len(self.checks):
340                                if not lastcheck:
341                                    string += " <= no more checks"
342                                    lastcheck = True
343                            elif lastcheckline is not None:
344                                string += (
345                                    " <= no check matches this, previous check on line "
346                                    + str(lastcheckline)
347                                )
348                            else:
349                                string += " <= no check matches"
350                            fmtstrs.append(string)
351            fmtstrs.append("")
352        fmtstrs += ["  when running command:", "    {subbed_command}"]
353        return "\n".join(fmtstrs).format(**fields)
354
355    def print_message(self):
356        """Print our message to stdout."""
357        print(self.message())
358
359
360def perform_substitution(input_str, subs):
361    """Perform the substitutions described by subs to str
362    Return the substituted string.
363    """
364    # Sort our substitutions into a list of tuples (key, value), descending by length.
365    # It needs to be descending because we need to try longer substitutions first.
366    subs_ordered = sorted(subs.items(), key=lambda s: len(s[0]), reverse=True)
367
368    def subber(m):
369        # We get the entire sequence of characters.
370        # Replace just the prefix and return it.
371        text = m.group(1)
372        for key, replacement in subs_ordered:
373            if text.startswith(key):
374                # shell-quote the replacement, so it's usable in #RUN lines.
375                # We could loosen this and only do it for #RUN/#REQUIRES,
376                # but so far we don't need it anywhere.
377                return shlex.quote(replacement + text[len(key) :])
378        # No substitution found, so we default to running it as-is,
379        # which will end up running it via $PATH.
380        return text
381
382    return re.sub(r"%(%|[a-zA-Z0-9_-]+)", subber, input_str)
383
384
385def runproc(cmd, env=None):
386    """Wrapper around subprocess.Popen to save typing"""
387    return asyncio.run(runproc_async(cmd, env=env))
388
389
390async def runproc_async(cmd, env=None, cwd=None):
391    """Wrapper around subprocess.Popen to save typing"""
392    PIPE = asyncio.subprocess.PIPE
393    DEVNULL = asyncio.subprocess.DEVNULL
394    return await asyncio.create_subprocess_shell(
395        cmd, stdin=DEVNULL, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd
396    )
397
398
399class TestRun(object):
400    def __init__(self, name, runcmd, checker, subs, config, env=None, cwd=None):
401        self.name = name
402        self.runcmd = runcmd
403        self.subbed_command = perform_substitution(runcmd.args, subs)
404        self.checker = checker
405        self.subs = subs
406        self.config = config
407        self.env = env
408        self.cwd = cwd
409
410    def check(self, lines, checks):
411        # Reverse our lines and checks so we can pop off the end.
412        lineq = lines[::-1]
413        checkq = checks[::-1]
414        usedlines = []
415        usedchecks = []
416        mismatches = []
417        while lineq and checkq:
418            line = lineq[-1]
419            check = checkq[-1]
420            if check == line:
421                # This line matched this checker, continue on.
422                usedlines.append(line)
423                usedchecks.append(check)
424                lineq.pop()
425                checkq.pop()
426            elif line.is_empty_space():
427                # Skip all whitespace input lines.
428                lineq.pop()
429            else:
430                usedlines.append(line)
431                usedchecks.append(check)
432                mismatches.append((line, check))
433                # Failed to match.
434                lineq.pop()
435                checkq.pop()
436
437        # Drain empties
438        while lineq and lineq[-1].is_empty_space():
439            lineq.pop()
440
441        # Store the remaining lines for the diff
442        for i in lineq[::-1]:
443            if not i.is_empty_space():
444                usedlines.append(i)
445        # Store remaining checks for the diff
446        for i in checkq[::-1]:
447            usedchecks.append(i)
448
449        # If we have no more output, there's no reason to give
450        # SCREENFULS of text.
451        # So we truncate the check list.
452        if len(usedchecks) > len(usedlines):
453            usedchecks = usedchecks[: len(usedlines) + 5]
454
455        # Do a SequenceMatch! This gives us a diff-like thing.
456        diff = SequenceMatcher(a=usedlines, b=usedchecks, autojunk=False)
457        # If there's a mismatch or still lines or checkers, we have a failure.
458        # Otherwise it's success.
459        if mismatches:
460            return TestFailure(
461                mismatches[0][0],
462                mismatches[0][1],
463                self,
464                diff=diff,
465                lines=usedlines,
466                checks=usedchecks,
467            )
468        elif lineq:
469            return TestFailure(
470                lineq[-1], None, self, diff=diff, lines=usedlines, checks=usedchecks
471            )
472        elif checkq:
473            return TestFailure(
474                None, checkq[-1], self, diff=diff, lines=usedlines, checks=usedchecks
475            )
476        else:
477            # Success!
478            return None
479
480    def run(self):
481        """Run the command. Return a TestFailure, or None."""
482        return asyncio.run(self.run_async())
483
484    async def run_async(self):
485        """Run the command. Return a TestFailure, or None."""
486
487        def split_by_newlines(s):
488            """Decode a string and split it by newlines only,
489            retaining the newlines.
490            """
491            return [
492                s + "\n"
493                for s in s.decode("utf-8", errors="backslashreplace").split("\n")
494            ]
495
496        if self.config.verbose:
497            print(self.subbed_command)
498        proc = await runproc_async(self.subbed_command, env=self.env, cwd=self.cwd)
499        stdout, stderr = await proc.communicate()
500
501        # Work around type system limitations / bad API design which makes the typechecker unhappy.
502        if proc.returncode is None:
503            raise RuntimeError(
504                "After `proc.communicate()` the return code must be an int."
505            )
506        status = proc.returncode
507        # HACK: This is quite cheesy: POSIX specifies that sh should return 127 for a missing command.
508        # It's also possible that it'll be returned in other situations,
509        # most likely when the last command in a shell script doesn't exist.
510        # So we check if the command *we execute* exists, and complain then.
511        cmd = next(
512            (
513                word
514                for word in shlex.split(self.subbed_command)
515                if not VARIABLE_OVERRIDE_RE.match(word)
516            )
517        )
518        if status == 127 and not find_command(cmd):
519            raise CheckerError("Command could not be found: " + cmd)
520        if status == 126 and not find_command(cmd):
521            raise CheckerError("Command is not executable: " + cmd)
522
523        outlines = [
524            Line(text, idx + 1, "stdout")
525            for idx, text in enumerate(split_by_newlines(stdout))
526        ]
527        errlines = [
528            Line(text, idx + 1, "stderr")
529            for idx, text in enumerate(split_by_newlines(stderr))
530        ]
531        outfail = self.check(outlines, self.checker.outchecks)
532        errfail = self.check(errlines, self.checker.errchecks)
533        # It's possible that something going wrong on stdout resulted in new
534        # text being printed on stderr. If we have an outfailure, and either
535        # non-matching or unmatched stderr text, then annotate the outfail
536        # with it.
537        if outfail and errfail and errfail.line:
538            outfail.error_annotation_lines = errlines[errfail.line.number - 1 :]
539            # Trim a trailing newline
540            if outfail.error_annotation_lines[-1].text == "\n":
541                del outfail.error_annotation_lines[-1]
542        failure = outfail if outfail else errfail
543
544        if failure and status < 0:
545            # Process was killed by a signal and failed,
546            # add a message.
547            import signal
548
549            # Unfortunately strsignal only exists in python 3.8+,
550            # and signal.signals is 3.5+.
551            if hasattr(signal, "Signals"):
552                try:
553                    sig = signal.Signals(-status)
554                    failure.signal = sig.name + " (" + signal.strsignal(sig.value) + ")"
555                except ValueError:
556                    failure.signal = str(-status)
557            else:
558                # No easy way to get the full list,
559                # make up a dict.
560                signals = {
561                    signal.SIGABRT: "SIGABRT",
562                    signal.SIGBUS: "SIGBUS",
563                    signal.SIGFPE: "SIGFPE",
564                    signal.SIGILL: "SIGILL",
565                    signal.SIGSEGV: "SIGSEGV",
566                    signal.SIGTERM: "SIGTERM",
567                }
568                failure.signal = signals.get(-status, str(-status))
569
570        return failure
571
572
573class CheckCmd(object):
574    def __init__(self, line, checktype, regex):
575        self.line = line
576        self.type = checktype
577        self.regex = regex
578
579    def __hash__(self):
580        # HACK: We pass this to the Sequencematcher, which puts the Checks into a dict.
581        # To force it to match the regexes, we return a hash collision intentionally,
582        # so it falls back on __eq__().
583        #
584        # Line has the same thing.
585        return 0
586
587    def __eq__(self, other):
588        # "Magical" comparison with lines and strings.
589        # Typically I wouldn't use this, but it allows us to check if a line matches any check in a dict or list via
590        # the `in` operator.
591        if other is None:
592            return False
593        if isinstance(other, CheckCmd):
594            return self.regex == other.regex
595        if isinstance(other, Line):
596            return self.regex.match(other.text)
597        if isinstance(other, str):
598            return self.regex.match(other)
599        raise NotImplementedError
600
601    @staticmethod
602    def parse(line: Line, checktype: str) -> "CheckCmd":
603        # Everything inside {{}} is a regular expression.
604        # Everything outside of it is a literal string.
605        # Split around {{...}}. Then every odd index will be a regex, and
606        # evens will be literals.
607        # Note that if {{...}} appears first we will get an empty string in
608        # the split array, so the {{...}} matches are always at odd indexes.
609        bracket_re = re.compile(
610            r"""
611                \{\{   # Two open brackets
612                (.*?)  # Nongreedy capture
613                \}\}   # Two close brackets
614            """,
615            re.VERBOSE,
616        )
617        pieces = bracket_re.split(line.text)
618        even = True
619        re_strings = []
620        for i, piece in enumerate(pieces):
621            if even:
622                if i != 0 and piece.startswith("}"):
623                    raise CheckerError(
624                        "Expression ('{{%s}}}') is ambiguous, consider using '{{%s}}{{[}]}}' if you really mean that"
625                        % (pieces[i - 1], pieces[i - 1]),
626                        line,
627                    )
628                # piece is a literal string.
629                re_strings.append(re.escape(piece))
630            else:
631                # piece is a regex (found inside {{...}}).
632                # Verify the regex can be compiled.
633                try:
634                    re.compile(piece)
635                except re.error:
636                    raise CheckerError("Invalid regular expression: '%s'" % piece, line)
637                re_strings.append(piece)
638            even = not even
639        # Enclose each piece in a non-capturing group.
640        # This ensures that lower-precedence operators don't trip up catenation.
641        # For example: {{b|c}}d would result in /b|cd/ which is different.
642        # Backreferences are assumed to match across the entire string.
643        re_strings = ["(?:%s)" % s for s in re_strings]
644        # Anchor at beginning and end (allowing arbitrary whitespace), and maybe
645        # a terminating newline.
646        # We need the anchors because Python's match() matches an arbitrary prefix,
647        # not the entire string.
648        re_strings = [r"^\s*"] + re_strings + [r"\s*\n?$"]
649        full_re = re.compile("".join(re_strings))
650        return CheckCmd(line, checktype, full_re)
651
652
653class Checker(object):
654    def __init__(self, name, lines):
655        self.name = name
656
657        # Helper to yield subline containing group1 from all matching lines.
658        def group1s(regex):
659            for line in lines:
660                m = regex.match(line.text)
661                if m:
662                    yield line.subline(m.group(1))
663
664        # Find run commands.
665        self.runcmds = [RunCmd.parse(sl) for sl in group1s(RUN_RE)]
666        self.shebang_cmd = None
667        if not self.runcmds:
668            # If no RUN command has been given, fall back to the shebang.
669            if lines[0].text.startswith("#!"):
670                # Remove the "#!" at the beginning, and the newline at the end.
671                cmd = lines[0].text[2:-1]
672                self.shebang_cmd = cmd
673                self.runcmds = [RunCmd(cmd + " %s", lines[0])]
674            else:
675                raise CheckerError("No runlines ('# RUN') found")
676
677        self.requirecmds = [RunCmd.parse(sl) for sl in group1s(REQUIRES_RE)]
678
679        # Find check cmds.
680        self.outchecks = [
681            CheckCmd.parse(sl, "CHECK") for sl in group1s(CHECK_STDOUT_RE)
682        ]
683        self.errchecks = [
684            CheckCmd.parse(sl, "CHECKERR") for sl in group1s(CHECK_STDERR_RE)
685        ]
686
687
688def check_file(input_file, name, subs, config, failure_handler, env=None):
689    """Check a single file. Return a True on success, False on error."""
690    return asyncio.run(
691        check_file_async(input_file, name, subs, config, failure_handler, env=env)
692    )
693
694
695async def check_file_async(
696    input_file, name, subs, config, failure_handler, env=None, cwd=None
697):
698    """Check a single file. Return a True on success, False on error."""
699    success = True
700    lines = Line.readfile(input_file, name)
701    checker = Checker(name, lines)
702
703    # Run all the REQUIRES lines first,
704    # if any of them fail it's a SKIP
705    for reqcmd in checker.requirecmds:
706        proc = await runproc_async(
707            perform_substitution(reqcmd.args, subs), env=env, cwd=cwd
708        )
709        await proc.communicate()
710        # Work around type system limitations / bad API design which makes the typechecker unhappy.
711        if proc.returncode is None:
712            raise RuntimeError(
713                "After `proc.communicate()` the return code must be an int."
714            )
715        if proc.returncode > 0:
716            return SKIP
717
718    if checker.shebang_cmd is not None and not find_command(checker.shebang_cmd):
719        raise CheckerError("Command could not be found: " + checker.shebang_cmd)
720
721    # Only then run the RUN lines.
722    for runcmd in checker.runcmds:
723        failure = await TestRun(
724            name, runcmd, checker, subs, config, env=env, cwd=cwd
725        ).run_async()
726        if failure:
727            failure_handler(failure)
728            success = False
729    return success
730
731
732def check_path(path, subs, config, failure_handler, env=None):
733    return asyncio.run(check_path_async(path, subs, config, failure_handler, env=env))
734
735
736async def check_path_async(path, subs, config, failure_handler, env=None, cwd=None):
737    with io.open(path, encoding="utf-8") as fd:
738        return await check_file_async(
739            fd, path, subs, config, failure_handler, env=env, cwd=cwd
740        )
741
742
743def parse_subs(subs):
744    """Given a list of input substitutions like 'foo=bar',
745    return a dictionary like {foo:bar}, or exit if invalid.
746    """
747    result = {}
748    for sub in subs:
749        try:
750            key, val = sub.split("=", 1)
751            if not key:
752                print("Invalid substitution %s: empty key" % sub)
753                sys.exit(1)
754            if not val:
755                print("Invalid substitution %s: empty value" % sub)
756                sys.exit(1)
757            result[key] = val
758        except ValueError:
759            print("Invalid substitution %s: equal sign not found" % sub)
760            sys.exit(1)
761    return result
762
763
764def get_argparse():
765    """Return a littlecheck argument parser."""
766    parser = argparse.ArgumentParser(
767        description="littlecheck: command line tool tester."
768    )
769    parser.add_argument(
770        "-s",
771        "--substitute",
772        type=str,
773        help="Add a new substitution for RUN lines. Example: bash=/bin/bash",
774        action="append",
775        default=[],
776    )
777    parser.add_argument(
778        "-p",
779        "--progress",
780        action="store_true",
781        dest="progress",
782        help="Show the files to be checked",
783        default=False,
784    )
785    parser.add_argument(
786        "--force-color",
787        action="store_true",
788        dest="force_color",
789        help="Force usage of color even if not connected to a terminal",
790        default=False,
791    )
792    parser.add_argument("file", nargs="+", help="File to check")
793    return parser
794
795
796def main():
797    args = get_argparse().parse_args()
798    # Default substitution is %% -> %
799    def_subs = {"%": "%"}
800    def_subs.update(parse_subs(args.substitute))
801
802    tests_count = 0
803    failed = False
804    skip_count = 0
805    config = Config()
806    config.colorize = args.force_color or sys.stdout.isatty()
807    config.progress = args.progress
808    fields = config.colors()
809
810    for path in args.file:
811        tests_count += 1
812        fields["path"] = path
813        if config.progress:
814            print("Testing file {path} ... ".format(**fields), end="")
815            sys.stdout.flush()
816        subs = def_subs.copy()
817        subs["s"] = path
818        starttime = datetime.datetime.now()
819        ret = check_path(path, subs, config, TestFailure.print_message)
820        if ret is SKIP:
821            skip_count += 1
822        if not ret:
823            failed = True
824        elif config.progress:
825            endtime = datetime.datetime.now()
826            duration_ms = round((endtime - starttime).total_seconds() * 1000)
827            reason = "ok"
828            color = "{GREEN}"
829            if ret is SKIP:
830                reason = "SKIPPED"
831                color = "{BLUE}"
832            print(
833                (color + "{reason}{RESET} ({duration} ms)").format(
834                    duration=duration_ms, reason=reason, **fields
835                )
836            )
837
838    # To facilitate integration with testing frameworks, use exit code 125 to indicate that all
839    # tests have been skipped (primarily for use when tests are run one at a time). Exit code 125 is
840    # used to indicate to automated `git bisect` runs that a revision has been skipped; we use it
841    # for the same reasons git does.
842    if skip_count > 0 and skip_count == tests_count:
843        sys.exit(125)
844
845    sys.exit(1 if failed else 0)
846
847
848if __name__ == "__main__":
849    main()