def htmlize(text, language): from pygments import highlight from pygments.formatters import HtmlFormatter as Formatter if language == 'Python': from pygments.lexers import PythonLexer as Lexer elif language == 'Perl': from pygments.lexers import PerlLexer as Lexer elif language == 'Ruby': from pygments.lexers import RubyLexer as Lexer elif language == 'PythonConsole': from pygments.lexers import PythonConsoleLexer as Lexer elif language == 'PythonTraceback': from pygments.lexers import PythonTracebackLexer as Lexer elif language == 'RubyConsole': from pygments.lexers import RubyConsoleLexer as Lexer elif language == 'HtmlDjango': from pygments.lexers import HtmlDjangoLexer as Lexer elif language == 'Html': from pygments.lexers import HtmlLexer as Lexer else: from pygments.lexers import TextLexer as Lexer """ Todo: I cant get this to work. lang_lexer = str(language + 'Lexer') Lexer = __import__('pygments.lexers', globals(), locals(), [lang_lexer, ]) Or from pygments.lexers import get_lexer_by_name Lexer = get_lexer_by_name(language.lower()) """ htmld = highlight(text, Lexer(), Formatter(linenos='table')) return htmld
class ColourTextTestResult(result.TestResult): """ A test result class that prints colour formatted text results to a stream. Based on https://github.com/python/cpython/blob/3.3/Lib/unittest/runner.py """ formatter = formatters.Terminal256Formatter() lexer = Lexer() separator1 = '=' * 70 separator2 = '-' * 70 indent = ' ' * 4 _terminal = Terminal() colours = { None: text_type, 'error': _terminal.bold_red, 'expected': _terminal.blue, 'fail': _terminal.bold_yellow, 'skip': text_type, 'success': _terminal.green, 'title': _terminal.blue, 'unexpected': _terminal.bold_red, } _test_class = None def __init__(self, stream, descriptions, verbosity): super(ColourTextTestResult, self).__init__(stream, descriptions, verbosity) self.stream = stream self.showAll = verbosity > 1 self.dots = verbosity == 1 self.descriptions = descriptions def getShortDescription(self, test): doc_first_line = test.shortDescription() if self.descriptions and doc_first_line: return self.indent + doc_first_line return self.indent + test._testMethodName def getLongDescription(self, test): doc_first_line = test.shortDescription() if self.descriptions and doc_first_line: return '\n'.join((str(test), doc_first_line)) return str(test) def getClassDescription(self, test): test_class = test.__class__ doc = test_class.__doc__ if self.descriptions and doc: return doc.strip().split('\n')[0].strip() return strclass(test_class) def startTest(self, test): super(ColourTextTestResult, self).startTest(test) if self.showAll: if self._test_class != test.__class__: self._test_class = test.__class__ title = self.getClassDescription(test) self.stream.writeln(self.colours['title'](title)) self.stream.write(self.getShortDescription(test)) self.stream.write(' ... ') self.stream.flush() def printResult(self, short, extended, colour_key=None): colour = self.colours[colour_key] if self.showAll: self.stream.writeln(colour(extended)) elif self.dots: self.stream.write(colour(short)) self.stream.flush() def addSuccess(self, test): super(ColourTextTestResult, self).addSuccess(test) self.printResult('.', 'ok', 'success') def addError(self, test, err): super(ColourTextTestResult, self).addError(test, err) self.printResult('E', 'ERROR', 'error') def addFailure(self, test, err): super(ColourTextTestResult, self).addFailure(test, err) self.printResult('F', 'FAIL', 'fail') def addSkip(self, test, reason): super(ColourTextTestResult, self).addSkip(test, reason) self.printResult('s', 'skipped {0!r}'.format(reason), 'skip') def addExpectedFailure(self, test, err): super(ColourTextTestResult, self).addExpectedFailure(test, err) self.printResult('x', 'expected failure', 'expected') def addUnexpectedSuccess(self, test): super(ColourTextTestResult, self).addUnexpectedSuccess(test) self.printResult('u', 'unexpected success', 'unexpected') def printErrors(self): if self.dots or self.showAll: self.stream.writeln() self.printErrorList('ERROR', self.errors) self.printErrorList('FAIL', self.failures) def printErrorList(self, flavour, errors): colour = self.colours[flavour.lower()] for test, err in errors: self.stream.writeln(self.separator1) title = '%s: %s' % (flavour, self.getLongDescription(test)) self.stream.writeln(colour(title)) self.stream.writeln(self.separator2) self.stream.writeln(highlight(err, self.lexer, self.formatter))
def test_func(test, f, *args, params=tuple(), **kwargs): try: golden = f(torch, *args, **kwargs) except: print(f"\x1b[1;31mgolden eval failed\x1b[m: {test}") raise try: symbols = f(nf, *args, **kwargs) symbols.name = test except: print(f"\x1b[1;31mconstruct failed\x1b[m: {test}") raise try: ev = symbols.eval() except: symbols.print() print(f"\x1b[1;31meval failed\x1b[m: {test}") raise if not array_close(golden, ev): print("Golden", golden) print("Eval", ev) symbols.print() print(f"\x1b[1;31meval() mismatch\x1b[m: {test}") raise ValueError(f"eval() mismatch: {test}") try: func = symbols.jit(*params) except: symbols.print() try: symbols.build_cfg().print() except: print("Unable to build CFG") print(f"\x1b[1;31mjit failed\x1b[m: {test}") raise try: cv = func(*args) except: print(f"\x1b[1;31mjit function invocation failed\x1b[m: {test}") if not array_close(golden, cv): symbols.print() try: symbols.build_cfg().print() except: print("Unable to build CFG") code = open(func.source).read() print(highlight(code, Lexer(), Formatter())) print("Golden", golden) print("JitEval", cv) print(f"\x1b[1;31mjit() mismatch\x1b[m: {test}") raise ValueError(f"jit() mismatch: {test}") print("\x1b[1;32mOK\x1b[m", test) import timeit iter = 1000 torch_time = timeit.timeit(lambda: f(torch, *args, **kwargs), number=iter) ndaf_time = timeit.timeit(lambda: func(*args), number=iter) print(" Gain", torch_time / ndaf_time)
class SSTTextTestResult(unittest.TestResult): """ A superclass to support SST required testing, this is a modified version of unittestTextTestResult from python 2.7 modified for SST's needs. """ separator1 = '=' * 70 separator2 = '-' * 70 indent = ' ' * 4 _test_class = None if blessings_loaded: _terminal = Terminal() colours = { None: text_type, 'error': _terminal.bold_yellow, 'expected': _terminal.green, 'fail': _terminal.bold_red, 'skip': _terminal.bold_blue, 'success': _terminal.green, 'title': _terminal.magenta, 'unexpected': _terminal.bold_red, } else: colours = { None: text_type } if pygments_loaded: formatter = formatters.Terminal256Formatter() lexer = Lexer() def __init__(self, stream, descriptions, verbosity, no_colour_output=False): super(SSTTextTestResult, self).__init__(stream, descriptions, verbosity) self.testsuitesresultsdict = SSTTestSuitesResultsDict() self._test_name = "undefined_testname" self._testcase_name = "undefined_testcasename" self._testsuite_name = "undefined_testsuitename" self._junit_test_case = None self.stream = stream self.showAll = verbosity > 1 self.dots = verbosity == 1 self.descriptions = descriptions if blessings_loaded: self.no_colour_output = no_colour_output else: self.no_colour_output = True def getShortDescription(self, test): doc_first_line = test.shortDescription() if self.descriptions and doc_first_line: return '\n'.join((str(test), doc_first_line)) else: return str(test) def getLongDescription(self, test): doc_first_line = test.shortDescription() if self.descriptions and doc_first_line: return '\n'.join((str(test), doc_first_line)) return str(test) def getClassDescription(self, test): test_class = test.__class__ doc = test_class.__doc__ if self.descriptions and doc: return doc.strip().split('\n')[0].strip() return strclass(test_class) ### def startTest(self, test): super(SSTTextTestResult, self).startTest(test) #log_forced("DEBUG - startTest: Test = {0}\n".format(test)) if self.showAll: if not test_engine_globals.TESTENGINE_CONCURRENTMODE: if self._test_class != test.__class__: self._test_class = test.__class__ title = self.getClassDescription(test) if self.no_colour_output: self.stream.writeln(self.colours[None](title)) else: self.stream.writeln(self.colours['title'](title)) self.stream.flush() self._test_name = "undefined_testname" _testname = getattr(test, 'testname', None) if _testname is not None: self._test_name = test.testname if self._is_test_of_type_ssttestcase(test): self._testcase_name = test.get_testcase_name() self._testsuite_name = test.get_testsuite_name() else: self._testcase_name = "FailedTest" self._testsuite_name = "FailedTest" timestamp = datetime.utcnow().strftime("%Y_%m%d_%H:%M:%S.%f utc") self._junit_test_case = JUnitTestCase(self._test_name, self._testcase_name, timestamp=timestamp) def stopTest(self, test): super(SSTTextTestResult, self).stopTest(test) #log_forced("DEBUG - stopTest: Test = {0}\n".format(test)) testruntime = 0 if self._is_test_of_type_ssttestcase(test): testruntime = test.get_test_runtime_sec() self._junit_test_case.junit_add_elapsed_sec(testruntime) if not self._is_test_of_type_ssttestcase(test): return if not test_engine_globals.TESTENGINE_CONCURRENTMODE: test_engine_globals.TESTRUN_JUNIT_TESTCASE_DICTLISTS['singlethread'].\ append(self._junit_test_case) else: test_engine_globals.TESTRUN_JUNIT_TESTCASE_DICTLISTS[self._testsuite_name].\ append(self._junit_test_case) ### def get_testsuites_results_dict(self): """ Return the test suites results dict """ return self.testsuitesresultsdict ### def printResult(self, test, short, extended, colour_key=None, showruntime=True): if self.no_colour_output: colour = self.colours[None] else: colour = self.colours[colour_key] if self.showAll: self.stream.write(self.indent) self.stream.write(colour(extended)) self.stream.write(" -- ") self.stream.write(self.getShortDescription(test)) testruntime = 0 if self._is_test_of_type_ssttestcase(test): testruntime = test.get_test_runtime_sec() if showruntime: self.stream.writeln(" [{0:.3f}s]".format(testruntime)) else: self.stream.writeln(" ".format(testruntime)) self.stream.flush() elif self.dots: self.stream.write(colour(short)) self.stream.flush() ### def addSuccess(self, test): super(SSTTextTestResult, self).addSuccess(test) #log_forced("DEBUG - addSuccess: Test = {0}\n".format(test)) self.printResult(test, '.', 'PASS', 'success') if not self._is_test_of_type_ssttestcase(test): return self.testsuitesresultsdict.add_success(test) def addError(self, test, err): super(SSTTextTestResult, self).addError(test, err) #log_forced("DEBUG - addError: Test = {0}, err = {1}\n".format(test, err)) self.printResult(test, 'E', 'ERROR', 'error') if not self._is_test_of_type_ssttestcase(test): return self.testsuitesresultsdict.add_error(test) _junit_test_case = getattr(self, '_junit_test_case', None) if _junit_test_case is not None: err_msg = self._get_err_info(err) _junit_test_case.junit_add_error_info(err_msg) def addFailure(self, test, err): super(SSTTextTestResult, self).addFailure(test, err) #log_forced("DEBUG - addFailure: Test = {0}, err = {1}\n".format(test, err)) self.printResult(test, 'F', 'FAIL', 'fail') if not self._is_test_of_type_ssttestcase(test): return self.testsuitesresultsdict.add_failure(test) _junit_test_case = getattr(self, '_junit_test_case', None) if _junit_test_case is not None: err_msg = self._get_err_info(err) _junit_test_case.junit_add_failure_info(err_msg) def addSkip(self, test, reason): super(SSTTextTestResult, self).addSkip(test, reason) #log_forced("DEBUG - addSkip: Test = {0}, reason = {1}\n".format(test, reason)) self.printResult(test, 's', 'SKIPPED: {0!r}'.format(reason), 'skip', showruntime=False) if not self._is_test_of_type_ssttestcase(test): return self.testsuitesresultsdict.add_skip(test) _junit_test_case = getattr(self, '_junit_test_case', None) if _junit_test_case is not None: _junit_test_case.junit_add_skipped_info(reason) def addExpectedFailure(self, test, err): # NOTE: This is not a failure, but an identified pass # since we are expecting a failure super(SSTTextTestResult, self).addExpectedFailure(test, err) #log_forced("DEBUG - addExpectedFailure: Test = {0}, err = {1}\n".format(test, err)) self.printResult(test, 'x', 'EXPECTED FAILURE', 'expected') if not self._is_test_of_type_ssttestcase(test): return self.testsuitesresultsdict.add_expected_failure(test) def addUnexpectedSuccess(self, test): # NOTE: This is a failure, since we passed, but were expecting a failure super(SSTTextTestResult, self).addUnexpectedSuccess(test) #log_forced("DEBUG - addUnexpectedSuccess: Test = {0}\n".format(test)) self.printResult(test, 'u', 'UNEXPECTED SUCCESS', 'unexpected') if not self._is_test_of_type_ssttestcase(test): return self.testsuitesresultsdict.add_unexpected_success(test) _junit_test_case = getattr(self, '_junit_test_case', None) if _junit_test_case is not None: _junit_test_case.junit_add_failure_info("RECEIVED SUCCESS WHEN EXPECTING A FAILURE") ### def printErrors(self): if self.dots or self.showAll: self.stream.writeln() log("=" * 70) log("=== TESTS FINISHED " + ("=" * 51)) log("=" * 70 + "\n") self.printErrorList('ERROR', self.errors) self.printErrorList('FAIL', self.failures) def printErrorList(self, flavour, errors): if self.no_colour_output: colour = self.colours[None] else: colour = self.colours[flavour.lower()] for test, err in errors: self.stream.writeln(self.separator1) title = '%s: %s' % (flavour, self.getLongDescription(test)) self.stream.writeln(colour(title)) self.stream.writeln(self.separator2) if pygments_loaded: self.stream.writeln(highlight(err, self.lexer, self.formatter)) else: self.stream.writeln(err) #### def _get_err_info(self, err): """Converts a sys.exc_info() into a string.""" exctype, value, tback = err msg_lines = traceback.format_exception_only(exctype, value) msg_lines = [x.replace('\n', ' ') for x in msg_lines] return ''.join(msg_lines) #### def _is_test_of_type_ssttestcase(self, test): """ Detirmine if this is is within a valid SSTTestCase object by checking if a unique SSTTestCase function exists return: True if this is a test within a valid SSTTestCase object """ return getattr(test, 'get_testcase_name', None) is not None
class ColoredTestResult(unittest.result.TestResult): """ A tes result implementation based on https://github.com/meshy/colour-runner/blob/master/colour_runner/result.py """ formatter = pygments.formatters.Terminal256Formatter() lexer = Lexer() separator1 = "=" * 70 separator2 = "-" * 70 indent = " " * 4 _terminal = blessings.Terminal() colours = { None: str, "error": _terminal.bold_red, "expected": _terminal.blue, "fail": _terminal.bold_yellow, "skip": str, "success": _terminal.green, "title": _terminal.blue, "unexpected": _terminal.bold_red, } _test_class = None def __init__(self, stream, descriptions, verbosity, no_colour=False): super().__init__(stream, descriptions) self.stream = stream self.showAll = verbosity > 1 self.dots = verbosity == 1 self.descriptions = descriptions self.no_colour = no_colour def get_short_description(self, test): doc_first_line = test.shortDescription() if self.descriptions and doc_first_line: return self.indent + doc_first_line return self.indent + test._testMethodName def get_long_description(self, test): doc_first_line = test.shortDescription() if self.descriptions and doc_first_line: return "\n".join((str(test), doc_first_line)) return str(test) def get_class_description(self, test): test_class = test.__class_under_test__ doc = test_class.__doc__ if self.descriptions and doc: return doc.strip().split("\n")[0].strip() return unittest.util.strclass(test_class) def startTest(self, test): super().startTest(test) if self.showAll: if self._test_class != test.__class__: self._test_class = test.__class__ title = self.get_class_description(test) self.stream.writeln(self.colours["title"](title)) self.stream.write(self.get_short_description(test)) self.stream.write(" ... ") self.stream.flush() def print_result(self, short, extended, colour_key=None): if self.no_colour: colour = self.colours[None] else: colour = self.colours[colour_key] if self.showAll: self.stream.writeln(colour(extended)) elif self.dots: self.stream.write(colour(short)) self.stream.flush() def addSuccess(self, test): super().addSuccess(test) self.print_result(".", "ok", "success") def addError(self, test, err): super().addError(test, err) self.print_result("E", "ERROR", "error") def addFailure(self, test, err): super().addFailure(test, err) self.print_result("F", "FAIL", "fail") def addSkip(self, test, reason): super().addSkip(test, reason) self.print_result("s", "skipped {0!r}".format(reason), "skip") def addExpectedFailure(self, test, err): super().addExpectedFailure(test, err) self.print_result("x", "expected failure", "expected") def addUnexpectedSuccess(self, test): super().addUnexpectedSuccess(test) self.print_result("u", "unexpected success", "unexpected") def printErrors(self): if self.dots or self.showAll: self.stream.writeln() self.printErrorList("ERROR", self.errors) self.printErrorList("FAIL", self.failures) def printErrorList(self, flavour, errors): if self.no_colour: colour = self.colours[None] else: colour = self.colours[flavour.lower()] for test, err in errors: self.stream.writeln(self.separator1) title = "%s: %s" % (flavour, self.get_long_description(test)) self.stream.writeln(colour(title)) self.stream.writeln(self.separator2) if self.no_colour: self.stream.writeln(err) else: self.stream.writeln(pygments.highlight(err, self.lexer, self.formatter))