def compare_to_file(text, name): filename = os.path.join( os.path.dirname(__file__), 'golden_files', name + '.txt', ) if os.environ.get('FIX_STACK_DATA_TESTS'): string_to_file(text, filename) else: expected_output = file_to_string(filename) assert text == expected_output
def assert_sample_output(module): old = sys.stderr out = io.StringIO() sys.stderr = out try: assert sys.gettrace() is None module.main() assert sys.gettrace() is None finally: sys.stderr = old time = '12:34:56.78' time_pattern = re.sub(r'\d', r'\\d', time) normalised = re.sub(time_pattern, time, out.getvalue()).strip() normalised = re.sub(r'0x\w+', '0xABC', normalised) normalised = normalised.replace('<genexpr>.<genexpr>', '<genexpr>') normalised = normalised.replace('<list_iterator', '<tupleiterator') normalised = normalised.replace('<listiterator', '<tupleiterator') normalised = normalised.replace('<tuple_iterator', '<tupleiterator') normalised = normalised.replace('<sequenceiterator', '<tupleiterator') try: assert ( normalised == module.expected_output.strip() ) except AssertionError: if os.environ.get('FIX_SNOOP_TESTS'): path = module.__file__.rstrip('c') contents = file_to_string(path) match = re.search( r'expected_output = r?"""', contents, ) contents = contents[:match.end(0)] + '\n{}\n"""\n'.format(normalised) string_to_file(contents, path) else: print('\n\nNormalised actual output:\n\n' + normalised) raise # show pytest diff (may need -vv flag to see in full)
def test_nested_arguments(self): # Python 3 sees nested arguments as a syntax error, so I can't # define the function here normally # birdseye requires a source file so I can't just use exec # The file can't just live there because then the test runner imports it path = os.path.join(os.path.dirname(__file__), 'nested_arguments.py') string_to_file(""" def f((x, y), z): return x, y, z """, path) try: from tests.nested_arguments import f f = eye(f) call = get_call_stuff(get_call_ids(lambda: f((1, 2), 3))[0]).call self.assertEqual(call.arguments, '[["x", "1"], ["y", "2"], ["z", "3"]]') self.assertEqual(call.result, "(1, 2, 3)") finally: os.remove(path)
def compare_to_file(text, filename): if os.environ.get('FIX_SNOOP_TESTS'): string_to_file(text, filename) else: expected_output = file_to_string(filename) assert text == expected_output