def test_kernel_installation(self): call(["coconut", "--jupyter"], assert_output="Coconut: Successfully installed Coconut Jupyter kernel.") stdout, stderr, retcode = call_output(["jupyter", "kernelspec", "list"]) stdout, stderr = "".join(stdout), "".join(stderr) assert not retcode and not stderr, stderr for kernel in icoconut_kernel_names: assert kernel in stdout
def call(cmd, assert_output=False, check_mypy=False, check_errors=False, stderr_first=False, **kwargs): """Executes a shell command.""" print("\n>", (cmd if isinstance(cmd, str) else " ".join(cmd))) if assert_output is True: assert_output = "<success>" stdout, stderr, retcode = call_output(cmd, **kwargs) if stderr_first: out = stderr + stdout else: out = stdout + stderr lines = "".join(out).splitlines() for line in lines: print(line) assert not retcode for line in lines: if check_errors: assert "Traceback (most recent call last):" not in line assert "Exception" not in line assert "Error" not in line if check_mypy and all(test not in line for test in ignore_mypy_errs_with): assert "error:" not in line if assert_output is None: assert not lines elif assert_output is not False: assert lines and assert_output in lines[-1]
def call(cmd, assert_output=False, check_mypy=False, check_errors=True, stderr_first=False, expect_retcode=0, **kwargs): """Executes a shell command.""" print("\n>", (cmd if isinstance(cmd, str) else " ".join(cmd))) if assert_output is False: assert_output = ("", ) elif assert_output is True: assert_output = ("<success>", ) elif isinstance(assert_output, str): if "\n" not in assert_output: assert_output = (assert_output, ) else: assert_output = tuple(x if x is not True else "<success>" for x in assert_output) stdout, stderr, retcode = call_output(cmd, **kwargs) if stderr_first: out = stderr + stdout else: out = stdout + stderr out = "".join(out) lines = out.splitlines() if expect_retcode is not None: assert retcode == expect_retcode, "Return code not as expected ({retcode} != {expect_retcode}) in: {cmd!r}".format( retcode=retcode, expect_retcode=expect_retcode, cmd=cmd, ) for line in lines: assert "CoconutInternalException" not in line, "CoconutInternalException in " + repr( line) assert "<unprintable" not in line, "Unprintable error in " + repr(line) assert "*** glibc detected ***" not in line, "C error in " + repr(line) if check_errors: assert "Traceback (most recent call last):" not in line, "Traceback in " + repr( line) assert "Exception" not in line, "Exception in " + repr(line) assert "Error" not in line, "Error in " + repr(line) if check_mypy and all(test not in line for test in ignore_mypy_errs_with): assert "INTERNAL ERROR" not in line, "MyPy INTERNAL ERROR in " + repr( line) assert "error:" not in line, "MyPy error in " + repr(line) if isinstance(assert_output, str): got_output = "\n".join(lines) + "\n" assert assert_output in got_output, "Expected " + repr( assert_output) + "; got " + repr(got_output) else: last_line = lines[-1] if lines else "" if assert_output is None: assert not last_line, "Expected nothing; got " + repr(last_line) else: assert any(x in last_line for x in assert_output), "Expected " + ", ".join( assert_output) + "; got " + repr(last_line)
def test_kernel_installation(self): call(["coconut", "--jupyter"], assert_output=kernel_installation_msg) stdout, stderr, retcode = call_output( ["jupyter", "kernelspec", "list"]) stdout, stderr = "".join(stdout), "".join(stderr) assert not retcode and not stderr, stderr for kernel in (icoconut_custom_kernel_name, ) + icoconut_default_kernel_names: assert kernel in stdout
def call_test(args, ignore_errs=(), prepend_py=True): """Call args on the command line for a test.""" if prepend_py: args = [sys.executable, "-m"] + args stdout, stderr, retcode = call_output(args) stdout, stderr = "".join(stdout), "".join(stderr) (print)((stdout + stderr).strip()) clean_stderr = [] for line in stderr.splitlines(): if not any((ignore in line for ignore in _coconut.itertools.chain.from_iterable((_coconut_func() for _coconut_func in (lambda: always_ignore_errs, lambda: ignore_errs))))): clean_stderr.append(line) clean_stderr = "\n".join(clean_stderr) assert not retcode and not clean_stderr, clean_stderr return stdout
def test_jupyter_kernel(self): call(["coconut", "--jupyter"], assert_output= "Coconut: Successfully installed Coconut Jupyter kernel.") stdout, stderr, retcode = call_output( ["jupyter", "kernelspec", "list"]) stdout, stderr = "".join(stdout), "".join(stderr) assert not retcode and not stderr, stderr for kernel in icoconut_kernel_names: assert kernel in stdout call( ["coconut", "--jupyter", "console"], assert_output=("shutting down", "Jupyter error"), check_errors=False, allow_fail=True, )
def call(cmd, assert_output=False, check_mypy=False, check_errors=True, stderr_first=False, expect_retcode=0, **kwargs): """Executes a shell command.""" print("\n>", (cmd if isinstance(cmd, str) else " ".join(cmd))) if assert_output is False: assert_output = ("",) elif assert_output is True: assert_output = ("<success>",) elif isinstance(assert_output, str): assert_output = (assert_output,) else: assert_output = tuple(x if x is not True else "<success>" for x in assert_output) stdout, stderr, retcode = call_output(cmd, **kwargs) if stderr_first: out = stderr + stdout else: out = stdout + stderr out = "".join(out) lines = out.splitlines() if expect_retcode is not None: assert retcode == expect_retcode, "Return code not as expected ({} != {}) in: {!r}".format( retcode, expect_retcode, cmd, ) for line in lines: assert "CoconutInternalException" not in line, "CoconutInternalException in " + repr(line) assert "<unprintable" not in line, "Unprintable error in " + repr(line) assert "*** glibc detected ***" not in line, "C error in " + repr(line) if check_errors: assert "Traceback (most recent call last):" not in line, "Traceback in " + repr(line) assert "Exception" not in line, "Exception in " + repr(line) assert "Error" not in line, "Error in " + repr(line) if check_mypy and all(test not in line for test in ignore_mypy_errs_with): assert "INTERNAL ERROR" not in line, "MyPy INTERNAL ERROR in " + repr(line) assert "error:" not in line, "MyPy error in " + repr(line) last_line = lines[-1] if lines else "" if assert_output is None: assert not last_line, "Expected nothing; got " + repr(last_line) else: assert any(x in last_line for x in assert_output), "Expected " + ", ".join(assert_output) + "; got " + repr(last_line)
def call(raw_cmd, assert_output=False, check_mypy=False, check_errors=True, stderr_first=False, expect_retcode=0, convert_to_import=False, **kwargs): """Execute a shell command and assert that no errors were encountered.""" if isinstance(raw_cmd, str): cmd = raw_cmd.split() else: cmd = raw_cmd print() logger.log_cmd(cmd) if assert_output is False: assert_output = ("", ) elif assert_output is True: assert_output = ("<success>", ) elif isinstance(assert_output, str): if "\n" not in assert_output: assert_output = (assert_output, ) else: assert_output = tuple(x if x is not True else "<success>" for x in assert_output) if convert_to_import is None: convert_to_import = (cmd[0] == sys.executable and cmd[1] != "-c" and cmd[1:3] != ["-m", "coconut"]) if convert_to_import: assert cmd[0] == sys.executable if cmd[1] == "-m": module_name = cmd[2] extra_argv = cmd[3:] stdout, stderr, retcode = call_with_import(module_name, extra_argv) else: module_path = cmd[1] extra_argv = cmd[2:] module_dir = os.path.dirname(module_path) module_name = os.path.splitext(os.path.basename(module_path))[0] if os.path.isdir(module_path): module_name += ".__main__" with using_sys_path(module_dir): stdout, stderr, retcode = call_with_import( module_name, extra_argv) else: stdout, stderr, retcode = call_output(raw_cmd, **kwargs) if expect_retcode is not None: assert retcode == expect_retcode, "Return code not as expected ({retcode} != {expect_retcode}) in: {cmd!r}".format( retcode=retcode, expect_retcode=expect_retcode, cmd=raw_cmd, ) if stderr_first: out = stderr + stdout else: out = stdout + stderr out = "".join(out) raw_lines = out.splitlines() lines = [] i = 0 while True: if i >= len(raw_lines): break line = raw_lines[i] # ignore https://bugs.python.org/issue39098 errors if sys.version_info < ( 3, 9) and line == "Error in atexit._run_exitfuncs:": while True: i += 1 if i >= len(raw_lines): break new_line = raw_lines[i] if not new_line.startswith(" ") and not any( test in new_line for test in ignore_atexit_errors_with): i -= 1 break continue # combine mypy error lines if line.rstrip().endswith("error:"): line += raw_lines[i + 1] i += 1 lines.append(line) i += 1 for line in lines: assert "CoconutInternalException" not in line, "CoconutInternalException in " + repr( line) assert "<unprintable" not in line, "Unprintable error in " + repr(line) assert "*** glibc detected ***" not in line, "C error in " + repr(line) assert "INTERNAL ERROR" not in line, "MyPy INTERNAL ERROR in " + repr( line) if check_errors: assert "Traceback (most recent call last):" not in line, "Traceback in " + repr( line) assert "Exception" not in line, "Exception in " + repr(line) assert "Error" not in line, "Error in " + repr(line) if check_mypy and all(test not in line for test in ignore_mypy_errs_with): assert "error:" not in line, "MyPy error in " + repr(line) if isinstance(assert_output, str): got_output = "\n".join(raw_lines) + "\n" assert assert_output in got_output, "Expected " + repr( assert_output) + "; got " + repr(got_output) else: if not lines: last_line = "" elif "--mypy" in cmd: last_line = " ".join(lines[-2:]) else: last_line = lines[-1] if assert_output is None: assert not last_line, "Expected nothing; got:\n" + "\n".join( repr(li) for li in raw_lines) else: assert any(x in last_line for x in assert_output), ( "Expected " + ", ".join(repr(s) for s in assert_output) + "; got:\n" + "\n".join(repr(li) for li in raw_lines))