def test_with_tracing(self): with tracing(trace_all=True) as store: f('Traced code') self.assertEqual( list(store.keys()), [__name__ + ':f'], # Qualified name of the function )
def configure(self, options, conf): super(PuppyParachute, self).configure(options, conf) if options.puppy_packages: self.enabled = True if self.enabled: try: import puppyparachute self.puppyparachute = puppyparachute from puppyparachute.tools import tracing except ImportError as e: log.error('Could not import puppyparachute: {}'.format(e)) self.enabled = False return if ( not options.puppy_packages and getattr(options, 'cover_packages', False) ): options.puppy_packages = options.cover_packages if options.puppy_packages: log.info('Tracing prefixes: {}'.format( ' '.join(options.puppy_packages))) self.tracer = tracing( packages=options.puppy_packages, )
def test_check_when_correct(self): # Write expected behavior like it's always been there # Clean up residues last_path = 'test_check-last.yml' rm(last_path) # Run a trace with tracing(trace_all=True) as store: f1('Traced code') # It's correct, so keep quiet check('test_check', store) # Do NOT dump the store self.assertFalse(os.path.exists(last_path))
def test_short_diff(self): with tracing(trace_all=True) as fndb1: f1('Traced code') with tracing(trace_all=True) as fndb2: f2('Traced code') with tracing(trace_all=True) as fndb3: f3('Traced code') store1 = freeze_db(fndb1) store2 = freeze_db(fndb2) store3 = freeze_db(fndb3) self.assertEqual( short_diff_db(store1, store1), '') self.assertEqual( short_diff_db(store1, store2), '! test_tools:f: x=Traced code -> traced code', ) self.assertEqual( short_diff_db(store1, store3), '- test_tools:f: x=Traced code -> TRACED CODE\n' '+ test_tools:g: x=Traced code -> Traced' )
def test_check_after_code_change(self): # Write expected behavior like it's always been there with open('test_check.yml', 'w') as fd: fd.write(f1_behavior) # Clean up residues last_path = 'test_check-last.yml' rm(last_path) # Run a trace with tracing(trace_all=True) as store: f2('Traced code') # Something has changed, tell us about it with self.assertRaises(AssertionError) as cm: check('test_check', store) # Write the new behavior and tell us where self.assertIn(last_path, str(cm.exception)) self.assertTrue(os.path.exists(last_path))
def test_annotate(self): # Run a trace with tracing(trace_all=True) as fndb: f('Traced code') # Dump it and load it back dump = format_db(fndb) store = load_db(dump) self.assertTrue(len(store), 5) afile = '{}-traced'.format(__file__) restored_file = '{}-restored'.format(__file__) rm(afile) rm(restored_file) # Annotated the function at the top of this very file annotate(store, __file__, afile) self.assertTrue(os.path.exists(afile)) # Doing it again yields the same output ''' XXX Doesn't work because of different filenames with open(afile) as fd: saved_afile = fd.read() annotate(store, afile, afile) with open(afile) as fd: self.assertEqual(fd.read(), saved_afile) ''' # Check the annotations n_annotations = 0 found_f = False found_inner = False found_method = False found_method_inner = False found_local_change = False # Do not erase regular comments found_random_comment = False polluted = False with open(afile) as fd: for line in fd: if line.strip().startswith('#?'): n_annotations += 1 if "x=" + "Traced code" in line: found_f = True if "y=" + "Yyy" in line: found_inner = True if ( "self" + "=" in line and "a=" + "X" in line ): found_method = True if "what=" + "Aaa" in line: found_method_inner = True if r"self=" in line and r".C {attr: X}" in line: found_local_change = True if '# Do not' + ' erase regular comments' == line.strip(): found_random_comment = True if "!!" + "python" in line: polluted = True self.assertTrue(found_f) self.assertTrue(found_inner) self.assertTrue(found_method) self.assertTrue(found_method_inner) self.assertTrue(found_local_change) self.assertTrue(found_random_comment) self.assertEqual(n_annotations, 4) self.assertTrue(not polluted) # Remove annotations from the file deannotate(afile, restored_file) # It must be identical to the original file with open(__file__) as f_orig, open(restored_file) as f_restored: restored = f_restored.read() self.assertTrue(not any( line.strip().startswith('#?') for line in restored.splitlines() )) self.assertEqual(f_orig.read(), restored) rm(afile) rm(restored_file)
def just_tracing(self): ' Record a trace in a file ' with tracing(trace_all=True) as fndb: f('Traced code') with open('some_entry_point.yml', 'w') as fd: fd.write(format_db(fndb))
def test_annotate(self): # Run a trace with tracing(trace_all=True) as fndb: f('Traced code') # Dump it and load it back dump = format_db(fndb) store = load_db(dump) self.assertTrue(len(store), 5) afile = '{}-traced'.format(__file__) restored_file = '{}-restored'.format(__file__) rm(afile) rm(restored_file) # Annotated the function at the top of this very file annotate(store, __file__, afile) self.assertTrue(os.path.exists(afile)) # Doing it again yields the same output ''' XXX Doesn't work because of different filenames with open(afile) as fd: saved_afile = fd.read() annotate(store, afile, afile) with open(afile) as fd: self.assertEqual(fd.read(), saved_afile) ''' # Check the annotations n_annotations = 0 found_f = False found_inner = False found_method = False found_method_inner = False found_local_change = False # Do not erase regular comments found_random_comment = False polluted = False with open(afile) as fd: for line in fd: if line.strip().startswith('#?'): n_annotations += 1 if "x=" + "Traced code" in line: found_f = True if "y=" + "Yyy" in line: found_inner = True if ("self" + "=" in line and "a=" + "X" in line): found_method = True if "what=" + "Aaa" in line: found_method_inner = True if r"self=" in line and r".C {attr: X}" in line: found_local_change = True if '# Do not' + ' erase regular comments' == line.strip(): found_random_comment = True if "!!" + "python" in line: polluted = True self.assertTrue(found_f) self.assertTrue(found_inner) self.assertTrue(found_method) self.assertTrue(found_method_inner) self.assertTrue(found_local_change) self.assertTrue(found_random_comment) self.assertEqual(n_annotations, 4) self.assertTrue(not polluted) # Remove annotations from the file deannotate(afile, restored_file) # It must be identical to the original file with open(__file__) as f_orig, open(restored_file) as f_restored: restored = f_restored.read() self.assertTrue(not any(line.strip().startswith('#?') for line in restored.splitlines())) self.assertEqual(f_orig.read(), restored) rm(afile) rm(restored_file)
def just_tracing(self): ' Record a trace in a file ' with tracing(trace_all=True) as fndb: f('Traced code') with open('some_entry_point.yml', 'w') as fd: fd.write(format_db(fndb))