Skip to content

Instantly share code, notes, and snippets.

@milesrichardson
Created September 12, 2014 20:08
Show Gist options
  • Save milesrichardson/01a7faabaad148e9c7e8 to your computer and use it in GitHub Desktop.
Save milesrichardson/01a7faabaad148e9c7e8 to your computer and use it in GitHub Desktop.
CPSC 323 Command Line Test Framework
#! /usr/bin/env python
#
# Requires envoy:
# https://github.com/kennethreitz/envoy
import envoy
class BashColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
base_cmd = "./parse"
SUCCESS = 0
ERR_MISSING_PATH = 1
ERR_INVALID_OPTION = 2
ERR_BAD_PATH = 3
ERR_MAXDEPTH = 4
ERR_MISSING_ARGS = 5
ERR_PATH_PRECEDE = 6
stderr_contains = {
# No path specified
"-PL": (ERR_MISSING_PATH, "missing"),
"-P": (ERR_MISSING_PATH, "missing"),
"-P .": (SUCCESS, ""),
"-p": (ERR_INVALID_OPTION, "invalid"),
"-l": (ERR_INVALID_OPTION, "invalid"),
"": (ERR_MISSING_PATH, "missing"),
# Both -P and -L no error
"-P -L .": (SUCCESS, ""),
# Unknown option
"-q .": (ERR_INVALID_OPTION, "invalid"),
# No such file or directory (stat failed)
". -newer kdjkj": (ERR_BAD_PATH, "no such"),
"......": (ERR_BAD_PATH, "no such"),
# Should succeed
".": (SUCCESS, ""),
". .": (SUCCESS, ""),
"..": (SUCCESS, ""),
". . .": (SUCCESS, ""),
# Max depth
". -maxdepth 0": (SUCCESS, ""),
". -maxdepth -1": (ERR_MAXDEPTH, "positive"),
". -maxdepth -0": (ERR_MAXDEPTH, "positive"),
". -maxdepth 99.99": (ERR_MAXDEPTH, "trailing"),
". -maxdepth 99.9": (ERR_MAXDEPTH, "trailing"),
". -maxdepth 0x2": (ERR_MAXDEPTH, "positive"),
# Missing arguments
". -print -newer": (ERR_MISSING_ARGS, "additional"),
# Out of order
". -depth .": (ERR_PATH_PRECEDE, "precede"), # Exit err
". -print -depth": (0, "positional") # But do not exit
}
i = 1
for args in stderr_contains.keys():
cmd = "%s %s" % (base_cmd, args)
r = envoy.run(cmd)
expected_code = stderr_contains[args][0]
expected_output = stderr_contains[args][1]
success = True
reasons = []
if r.std_err.count('\n') > 1:
success = False
reasons.append("too many lines")
if r.status_code != expected_code:
success = False
reasons.append("invalid status code")
if r.std_err == "" and len(expected_output) > 0:
success = False
reasons.append("blank stderr")
if len(r.std_err) > 0 and not expected_output in r.std_err.lower():
success = False
reasons.append("unexpected stderr msg")
if success:
result = "%s Success! :D%s" % (BashColors.OKBLUE,
BashColors.ENDC)
else:
reason = ', '.join(reasons)
result = "%s F A I L (%s) %s" % (BashColors.FAIL,
reason, BashColors.ENDC)
if success:
continue
print 'CASE #%d: %s' % (i, result)
print '\tcommmand: %s' % (cmd)
print '\texpected: "%s" in stderr, status code %d' \
% (stderr_contains[args][1],
stderr_contains[args][0])
print '\treceived: stderr(%s)(%s)' % (r.status_code,
r.std_err.replace('\n', '\\n'))
# print 'RESULT: %s' % result
i = i + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment