Skip to content

Commit

Permalink
Update littlecheck.py
Browse files Browse the repository at this point in the history
  • Loading branch information
IlanCosman committed Feb 26, 2021
1 parent ceeb621 commit 2363bf8
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions littlecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@

SKIP = object()

def find_command(program):
import os

path, name = os.path.split(program)
if path:
return os.path.isfile(program) and os.access(program, os.X_OK)
for path in os.environ["PATH"].split(os.pathsep):
exe = os.path.join(path, program)
if os.path.isfile(exe) and os.access(exe, os.X_OK):
return exe

return None

class Config(object):
def __init__(self):
# Whether to have verbose output.
Expand Down Expand Up @@ -461,18 +474,15 @@ def split_by_newlines(s):
proc = runproc(self.subbed_command)
stdout, stderr = proc.communicate()
# HACK: This is quite cheesy: POSIX specifies that sh should return 127 for a missing command.
# Technically it's also possible to return it in other conditions.
# Practically, that's *probably* not going to happen.
# It's also possible that it'll be returned in other situations,
# most likely when the last command in a shell script doesn't exist.
# So we check if the command *we execute* exists, and complain then.
status = proc.returncode
if status == 127:
raise CheckerError("Command could not be found: " + self.subbed_command)
if status == 126:
raise CheckerError("Command is not executable: " + self.subbed_command)

# If a test returns 125, we skip it and don't even attempt to compare output.
# This is similar to what `git bisect run` does.
if status == 125:
return SKIP
cmd = shlex.split(self.subbed_command)[0]
if status == 127 and not find_command(cmd):
raise CheckerError("Command could not be found: " + cmd)
if status == 126 and not find_command(cmd):
raise CheckerError("Command is not executable: " + cmd)

outlines = [
Line(text, idx + 1, "stdout")
Expand Down Expand Up @@ -587,7 +597,10 @@ def group1s(regex):
# If no RUN command has been given, fall back to the shebang.
if lines[0].text.startswith("#!"):
# Remove the "#!" at the beginning, and the newline at the end.
self.runcmds = [RunCmd(lines[0].text[2:-1] + " %s", lines[0])]
cmd = lines[0].text[2:-1]
if not find_command(cmd):
raise CheckerError("Command could not be found: " + cmd)
self.runcmds = [RunCmd(cmd + " %s", lines[0])]
else:
raise CheckerError("No runlines ('# RUN') found")

Expand Down

0 comments on commit 2363bf8

Please sign in to comment.