def test_combining_with_aliases(self): covdata1 = CoverageData() covdata1.add_lines({ '/home/ned/proj/src/a.py': {1: None, 2: None}, '/home/ned/proj/src/sub/b.py': {3: None}, }) self.data_files.write(covdata1, suffix='1') covdata2 = CoverageData() covdata2.add_lines({ r'c:\ned\test\a.py': {4: None, 5: None}, r'c:\ned\test\sub\b.py': {6: None}, }) self.data_files.write(covdata2, suffix='2') covdata3 = CoverageData() aliases = PathAliases() aliases.add("/home/ned/proj/src/", "./") aliases.add(r"c:\ned\test", "./") self.data_files.combine_parallel_data(covdata3, aliases=aliases) apy = canonical_filename('./a.py') sub_bpy = canonical_filename('./sub/b.py') self.assert_summary(covdata3, {apy: 4, sub_bpy: 2}, fullpath=True) self.assert_measured_files(covdata3, [apy, sub_bpy])
def test_no_duplicate_lines(self): covdata = CoverageData() covdata.set_context("context1") covdata.add_lines(LINES_1) covdata.set_context("context2") covdata.add_lines(LINES_1) self.assertEqual(covdata.lines('a.py'), A_PY_LINES_1)
def test_explicit_suffix(self): self.assert_doesnt_exist(".coverage.SUFFIX") covdata = CoverageData() covdata.add_lines(LINES_1) self.data_files.write(covdata, suffix='SUFFIX') self.assert_exists(".coverage.SUFFIX") self.assert_doesnt_exist(".coverage")
def test_update_file_tracers(self): covdata1 = CoverageData() covdata1.add_lines({ "p1.html": dict.fromkeys([1, 2, 3, 4]), "p2.html": dict.fromkeys([5, 6, 7]), "main.py": dict.fromkeys([10, 11, 12]), }) covdata1.add_file_tracers({ "p1.html": "html.plugin", "p2.html": "html.plugin2", }) covdata2 = CoverageData() covdata2.add_lines({ "p1.html": dict.fromkeys([3, 4, 5, 6]), "p2.html": dict.fromkeys([7, 8, 9]), "p3.foo": dict.fromkeys([1000, 1001]), "main.py": dict.fromkeys([10, 11, 12]), }) covdata2.add_file_tracers({ "p1.html": "html.plugin", "p2.html": "html.plugin2", "p3.foo": "foo_plugin", }) covdata3 = CoverageData() covdata3.update(covdata1) covdata3.update(covdata2) self.assertEqual(covdata3.file_tracer("p1.html"), "html.plugin") self.assertEqual(covdata3.file_tracer("p2.html"), "html.plugin2") self.assertEqual(covdata3.file_tracer("p3.foo"), "foo_plugin") self.assertEqual(covdata3.file_tracer("main.py"), "")
def test_combining_with_aliases(self): covdata1 = CoverageData() covdata1.add_lines({ '/home/ned/proj/src/a.py': {1: None, 2: None}, '/home/ned/proj/src/sub/b.py': {3: None}, '/home/ned/proj/src/template.html': {10: None}, }) covdata1.add_file_tracers({ '/home/ned/proj/src/template.html': 'html.plugin', }) self.data_files.write(covdata1, suffix='1') covdata2 = CoverageData() covdata2.add_lines({ r'c:\ned\test\a.py': {4: None, 5: None}, r'c:\ned\test\sub\b.py': {3: None, 6: None}, }) self.data_files.write(covdata2, suffix='2') covdata3 = CoverageData() aliases = PathAliases() aliases.add("/home/ned/proj/src/", "./") aliases.add(r"c:\ned\test", "./") self.data_files.combine_parallel_data(covdata3, aliases=aliases) apy = canonical_filename('./a.py') sub_bpy = canonical_filename('./sub/b.py') template_html = canonical_filename('./template.html') self.assert_line_counts(covdata3, {apy: 4, sub_bpy: 2, template_html: 1}, fullpath=True) self.assert_measured_files(covdata3, [apy, sub_bpy, template_html]) self.assertEqual(covdata3.file_tracer(template_html), 'html.plugin')
def test_combining_from_files(self): covdata1 = CoverageData() covdata1.add_lines(LINES_1) os.makedirs('cov1') covdata1.write_file('cov1/.coverage.1') covdata2 = CoverageData() covdata2.add_lines(LINES_2) os.makedirs('cov2') covdata2.write_file('cov2/.coverage.2') # This data won't be included. covdata_xxx = CoverageData() covdata_xxx.add_arcs(ARCS_3) covdata_xxx.write_file('.coverage.xxx') covdata_xxx.write_file('cov2/.coverage.xxx') covdata3 = CoverageData() self.data_files.combine_parallel_data(covdata3, data_paths=['cov1', 'cov2/.coverage.2']) self.assert_line_counts(covdata3, SUMMARY_1_2) self.assert_measured_files(covdata3, MEASURED_FILES_1_2) self.assert_doesnt_exist("cov1/.coverage.1") self.assert_doesnt_exist("cov2/.coverage.2") self.assert_exists(".coverage.xxx") self.assert_exists("cov2/.coverage.xxx")
def test_cant_change_file_tracer_name(self): covdata = CoverageData() covdata.add_lines({"p1.foo": dict.fromkeys([1, 2, 3])}) covdata.add_file_tracers({"p1.foo": "p1.plugin"}) msg = "Conflicting file tracer name for 'p1.foo': 'p1.plugin' vs 'p1.plugin.foo'" with self.assertRaisesRegex(CoverageException, msg): covdata.add_file_tracers({"p1.foo": "p1.plugin.foo"})
def test_read_write_lines(self): covdata1 = CoverageData() covdata1.add_lines(LINES_1) covdata1.write_file("lines.dat") covdata2 = CoverageData() covdata2.read_file("lines.dat") self.assert_lines1_data(covdata2)
def test_writing_and_reading(self): covdata1 = CoverageData() covdata1.add_lines(LINES_1) self.data_files.write(covdata1) covdata2 = CoverageData() self.data_files.read(covdata2) self.assert_line_counts(covdata2, SUMMARY_1)
def test_writing_and_reading(self): covdata1 = CoverageData() covdata1.add_lines(DATA_1) self.data_files.write(covdata1) covdata2 = CoverageData() self.data_files.read(covdata2) self.assert_summary(covdata2, SUMMARY_1)
def test_cant_file_tracer_unmeasured_files(self): covdata = CoverageData() msg = "Can't add file tracer data for unmeasured file 'p1.foo'" with self.assertRaisesRegex(CoverageException, msg): covdata.add_file_tracers({"p1.foo": "p1.plugin"}) covdata.add_lines({"p2.html": dict.fromkeys([10, 11, 12])}) with self.assertRaisesRegex(CoverageException, msg): covdata.add_file_tracers({"p1.foo": "p1.plugin"})
def test_add_to_hash_with_lines(self): covdata = CoverageData() covdata.add_lines(LINES_1) hasher = mock.Mock() covdata.add_to_hash("a.py", hasher) self.assertEqual(hasher.method_calls, [ mock.call.update([1, 2]), # lines mock.call.update(""), # file_tracer name ])
def test_add_to_lines_hash_with_missing_file(self): # https://bitbucket.org/ned/coveragepy/issues/403 covdata = CoverageData() covdata.add_lines(LINES_1) hasher = mock.Mock() covdata.add_to_hash("missing.py", hasher) self.assertEqual(hasher.method_calls, [ mock.call.update([]), mock.call.update(None), ])
def test_writing_to_other_file(self): self.skipTest("This will be deleted!") # TODO covdata = CoverageData(".otherfile") covdata.add_lines(LINES_1) covdata.write() self.assert_doesnt_exist(".coverage") self.assert_exists(".otherfile") covdata.write(suffix="extra") self.assert_exists(".otherfile.extra") self.assert_doesnt_exist(".coverage")
def test_interleaved_erasing_bug716(self): # pytest-cov could produce this scenario. #716 covdata1 = CoverageData() covdata2 = CoverageData() # this used to create the .coverage database file.. covdata2.set_context("") # then this would erase it all.. covdata1.erase() # then this would try to use tables that no longer exist. # "no such table: meta" covdata2.add_lines(LINES_1)
def test_erasing(self): covdata1 = CoverageData() covdata1.add_lines(LINES_1) covdata1.write() covdata1.erase() self.assert_line_counts(covdata1, {}) covdata2 = CoverageData() covdata2.read() self.assert_line_counts(covdata2, {})
def test_file_tracer_name(self): covdata = CoverageData() covdata.add_lines({ "p1.foo": dict.fromkeys([1, 2, 3]), "p2.html": dict.fromkeys([10, 11, 12]), "main.py": dict.fromkeys([20]), }) covdata.add_file_tracers({"p1.foo": "p1.plugin", "p2.html": "p2.plugin"}) self.assertEqual(covdata.file_tracer("p1.foo"), "p1.plugin") self.assertEqual(covdata.file_tracer("main.py"), "") self.assertIsNone(covdata.file_tracer("p3.not_here"))
def test_writing_to_other_file(self): data_files = CoverageDataFiles(".otherfile") covdata = CoverageData() covdata.add_lines(LINES_1) data_files.write(covdata) self.assert_doesnt_exist(".coverage") self.assert_exists(".otherfile") data_files.write(covdata, suffix="extra") self.assert_exists(".otherfile.extra") self.assert_doesnt_exist(".coverage")
def test_update_cant_mix_lines_and_arcs(self): covdata1 = CoverageData() covdata1.add_lines(LINES_1) covdata2 = CoverageData() covdata2.add_arcs(ARCS_3) with self.assertRaisesRegex(CoverageException, "Can't combine arc data with line data"): covdata1.update(covdata2) with self.assertRaisesRegex(CoverageException, "Can't combine line data with arc data"): covdata2.update(covdata1)
def test_erasing(self): covdata1 = CoverageData() covdata1.add_lines(DATA_1) self.data_files.write(covdata1) covdata1.erase() self.assert_summary(covdata1, {}) self.data_files.erase() covdata2 = CoverageData() self.data_files.read(covdata2) self.assert_summary(covdata2, {})
def test_debug_output_without_debug_option(self): # With a debug object, but not the dataio option, we don't get debug # output. debug = DebugControlString(options=[]) covdata1 = CoverageData(debug=debug) covdata1.add_lines(LINES_1) self.data_files.write(covdata1) covdata2 = CoverageData(debug=debug) self.data_files.read(covdata2) self.assert_line_counts(covdata2, SUMMARY_1) self.assertEqual(debug.get_output(), "")
def test_combining(self): covdata1 = CoverageData() covdata1.add_lines(DATA_1) self.data_files.write(covdata1, suffix='1') covdata2 = CoverageData() covdata2.add_lines(DATA_2) self.data_files.write(covdata2, suffix='2') covdata3 = CoverageData() self.data_files.combine_parallel_data(covdata3) self.assert_summary(covdata3, SUMMARY_1_2) self.assert_measured_files(covdata3, MEASURED_FILES_1_2)
def test_update_lines(self): covdata1 = CoverageData() covdata1.add_lines(LINES_1) covdata2 = CoverageData() covdata2.add_lines(LINES_2) covdata3 = CoverageData() covdata3.update(covdata1) covdata3.update(covdata2) self.assert_line_counts(covdata3, SUMMARY_1_2) self.assert_measured_files(covdata3, MEASURED_FILES_1_2) self.assertEqual(covdata3.run_infos(), [])
def test_update_file_tracer_vs_no_file_tracer(self): covdata1 = CoverageData() covdata1.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) covdata1.add_file_tracers({"p1.html": "html.plugin"}) covdata2 = CoverageData() covdata2.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) msg = "Conflicting file tracer name for 'p1.html': 'html.plugin' vs ''" with self.assertRaisesRegex(CoverageException, msg): covdata1.update(covdata2) msg = "Conflicting file tracer name for 'p1.html': '' vs 'html.plugin'" with self.assertRaisesRegex(CoverageException, msg): covdata2.update(covdata1)
def test_file_format(self): # Write with CoverageData, then read the pickle explicitly. covdata = CoverageData() covdata.add_lines(DATA_1) self.data_files.write(covdata) with open(".coverage", 'rb') as fdata: data = pickle.load(fdata) lines = data['lines'] self.assertCountEqual(lines.keys(), MEASURED_FILES_1) self.assertCountEqual(lines['a.py'], A_PY_LINES_1) self.assertCountEqual(lines['b.py'], B_PY_LINES_1) # If not measuring branches, there's no arcs entry. self.assertEqual(data.get('arcs', 'not there'), 'not there')
def test_update_conflicting_file_tracers(self): covdata1 = CoverageData(suffix='1') covdata1.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) covdata1.add_file_tracers({"p1.html": "html.plugin"}) covdata2 = CoverageData(suffix='2') covdata2.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) covdata2.add_file_tracers({"p1.html": "html.other_plugin"}) msg = "Conflicting file tracer name for 'p1.html': u?'html.plugin' vs u?'html.other_plugin'" with self.assertRaisesRegex(CoverageException, msg): covdata1.update(covdata2) msg = "Conflicting file tracer name for 'p1.html': u?'html.other_plugin' vs u?'html.plugin'" with self.assertRaisesRegex(CoverageException, msg): covdata2.update(covdata1)
def test_file_format(self): # Write with CoverageData, then read the JSON explicitly. covdata = CoverageData() covdata.add_lines(LINES_1) self.data_files.write(covdata) data = self.read_json_data_file(".coverage") lines = data['lines'] self.assertCountEqual(lines.keys(), MEASURED_FILES_1) self.assertCountEqual(lines['a.py'], A_PY_LINES_1) self.assertCountEqual(lines['b.py'], B_PY_LINES_1) # If not measuring branches, there's no arcs entry. self.assertNotIn('arcs', data) # If no file tracers were involved, there's no file_tracers entry. self.assertNotIn('file_tracers', data)
def test_debug_output_with_debug_option(self): # With debug option dataio, we get debug output about reading and # writing files. debug = DebugControlString(options=["dataio"]) covdata1 = CoverageData(debug=debug) covdata1.add_lines(LINES_1) self.data_files.write(covdata1) covdata2 = CoverageData(debug=debug) self.data_files.read(covdata2) self.assert_line_counts(covdata2, SUMMARY_1) self.assertRegex( debug.get_output(), r"^Writing data to '.*\.coverage'\n" r"Reading data from '.*\.coverage'\n$" )
def test_debug_main(self): self.skip_unless_data_storage_is("json") covdata1 = CoverageData(".coverage") covdata1.add_lines(LINES_1) covdata1.write() debug_main([]) covdata2 = CoverageData("arcs.dat") covdata2.add_arcs(ARCS_3) covdata2.add_file_tracers({"y.py": "magic_plugin"}) covdata2.add_run_info(version="v3.14", chunks=["z", "a"]) covdata2.write() covdata3 = CoverageData("empty.dat") covdata3.write() debug_main(["arcs.dat", "empty.dat"]) expected = { ".coverage": { "lines": { "a.py": [1, 2], "b.py": [3], }, }, "arcs.dat": { "arcs": { "x.py": [[-1, 1], [1, 2], [2, 3], [3, -1]], "y.py": [[-1, 17], [17, 23], [23, -1]], }, "file_tracers": {"y.py": "magic_plugin"}, "runs": [ { "chunks": ["z", "a"], "version": "v3.14", }, ], }, "empty.dat": {}, } pieces = re.split(r"(?m)-+ ([\w.]+) -+$", self.stdout()) for name, json_out in zip(pieces[1::2], pieces[2::2]): json_got = json.loads(json_out) canonicalize_json_data(json_got) self.assertEqual(expected[name], json_got)
def test_combining_from_different_directories(self): covdata1 = CoverageData() covdata1.add_lines(DATA_1) os.makedirs('cov1') covdata1.write_file('cov1/.coverage.1') covdata2 = CoverageData() covdata2.add_lines(DATA_2) os.makedirs('cov2') covdata2.write_file('cov2/.coverage.2') covdata3 = CoverageData() self.data_files.combine_parallel_data(covdata3, data_dirs=[ 'cov1/', 'cov2/', ]) self.assert_summary(covdata3, SUMMARY_1_2) self.assert_measured_files(covdata3, MEASURED_FILES_1_2)
def test_no_lines_vs_unmeasured_file(self): covdata = CoverageData() covdata.add_lines(LINES_1) covdata.touch_file('zzz.py') self.assertEqual(covdata.lines('zzz.py'), []) self.assertIsNone(covdata.lines('no_such_file.py'))
def test_empty_lines_are_still_lines(self): covdata = CoverageData() covdata.add_lines({}) covdata.touch_file("abc.py") self.assertFalse(covdata.has_arcs())
def get_coverage_data(self, lines): """Get a CoverageData object that includes the requested lines.""" data = CoverageData() data.add_lines(lines) return data
def test_line_data_is_true(self): covdata = CoverageData() covdata.add_lines(LINES_1) assert covdata
def test_touch_file_with_lines(self): covdata = CoverageData() covdata.add_lines(LINES_1) covdata.touch_file('zzz.py') self.assert_measured_files(covdata, MEASURED_FILES_1 + ['zzz.py'])
def test_empty_line_data_is_false(self): covdata = CoverageData() covdata.add_lines({}) assert not covdata
def test_cant_add_lines_with_arcs(self): covdata = CoverageData() covdata.add_arcs(ARCS_3) with self.assertRaisesRegex(CoverageException, "Can't add lines to existing arc data"): covdata.add_lines(LINES_1)
def test_ok_to_add_lines_twice(self): covdata = CoverageData() covdata.add_lines(LINES_1) covdata.add_lines(LINES_2) self.assert_line_counts(covdata, SUMMARY_1_2) self.assert_measured_files(covdata, MEASURED_FILES_1_2)
def test_adding_lines(self): covdata = CoverageData() covdata.add_lines(LINES_1) self.assert_lines1_data(covdata)