Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_pyramid(output, correct):
if output == correct:
return
err = Error((output, correct))
output = output.split("\n")
correct = correct.split("\n")
# check for trailing whitespace
if len(output) == len(correct) and all(ol.rstrip() == cl for ol, cl in zip(output, correct)):
err.helpers = "Did you add too much trailing whitespace to the end of your pyramid?"
raise err
def wait(self, timeout=5):
try:
self.child.expect(EOF, timeout=timeout)
except TIMEOUT:
e = Error("timed out while waiting for program to exit")
e.__context__ = TIMEOUT(timeout)
raise e
except UnicodeDecodeError:
raise Error("output not valid ASCII text")
self.kill()
if self.child.signalstatus == signal.SIGSEGV:
raise Error("failed to execute program due to segmentation fault")
self.exitstatus = self.child.exitstatus
return self
def wait(self, timeout=5):
try:
self.child.expect(EOF, timeout=timeout)
except TIMEOUT:
e = Error("timed out while waiting for program to exit")
e.__context__ = TIMEOUT(timeout)
raise e
except UnicodeDecodeError:
raise Error("output not valid ASCII text")
self.kill()
if self.child.signalstatus == signal.SIGSEGV:
raise Error("failed to execute program due to segmentation fault")
self.exitstatus = self.child.exitstatus
return self
def __init__(self, test, path):
dir, file = os.path.split(path)
name, _ = os.path.splitext(file)
# add directory of flask app to sys.path so we can import it properly
prevpath = sys.path[0]
try:
sys.path[0] = os.path.abspath(dir or ".")
mod = imp.load_source(name, file)
except (OSError, IOError) as e:
if e.errno == errno.ENOENT:
e = Error("could not find {}".format(file))
raise e
finally:
# restore sys.path
sys.path[0] = prevpath
try:
app = mod.app
except AttributeError:
raise Error("{} does not contain an app".format(file))
# initialize flask client
app.testing = True
self.client = app.test_client()
self.test = test
self.response = None
def exit(self, code=None, timeout=5):
self.wait(timeout)
if code is None:
return self.exitstatus
self.test.log.append("checking that program exited with status {}...".format(code))
if self.exitstatus != code:
raise Error("expected exit code {}, not {}".format(code, self.exitstatus))
return self
def _send(self, method, route, data, params, **kwargs):
"""Send request of type `method` to `route`"""
route = self._fmt_route(route, params)
self.test.log.append("sending {} request to {}".format(method.upper(), route))
try:
self.response = getattr(self.client, method.lower())(route, data=data, **kwargs)
except BaseException as e: # Catch all exceptions thrown by app
# TODO: Change Finance starter code for edX and remove this as well as app.testing = True in __init__
self.test.log.append("exception raised in application: {}: {}".format(type(e).__name__, e))
raise Error("application raised an exception (see log for details)")
return self