diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-07-21 20:32:39 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-07-21 20:32:39 -0400 |
commit | a591430903ed9108c8cb50369be0d9d9c1a0b200 (patch) | |
tree | 43f6da46cccada445b84c53e3be3629f2cfbe39a /tests/test_data.py | |
parent | 77d826ef74d4602730cea5c4ac4a440cfbeb3303 (diff) | |
download | python-coveragepy-a591430903ed9108c8cb50369be0d9d9c1a0b200.tar.gz |
Data files are now JSON instead of pickles. Fixes #236.
Diffstat (limited to 'tests/test_data.py')
-rw-r--r-- | tests/test_data.py | 140 |
1 files changed, 70 insertions, 70 deletions
diff --git a/tests/test_data.py b/tests/test_data.py index 4999f25..ea33a58 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -1,12 +1,12 @@ """Tests for coverage.data""" import glob +import json import os import os.path import mock -from coverage.backward import pickle from coverage.data import CoverageData, CoverageDataFiles from coverage.files import PathAliases, canonical_filename from coverage.misc import CoverageException @@ -88,17 +88,17 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_line_data_is_true(self): covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) self.assertTrue(covdata) def test_arc_data_is_true(self): covdata = CoverageData() - covdata.add_arcs(ARCS_3) + covdata.set_arcs(ARCS_3) self.assertTrue(covdata) def test_adding_lines(self): covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) self.assert_line_counts(covdata, SUMMARY_1) self.assert_measured_files(covdata, MEASURED_FILES_1) self.assertCountEqual(covdata.lines("a.py"), A_PY_LINES_1) @@ -106,7 +106,7 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_adding_arcs(self): covdata = CoverageData() - covdata.add_arcs(ARCS_3) + covdata.set_arcs(ARCS_3) self.assert_line_counts(covdata, SUMMARY_3) self.assert_measured_files(covdata, MEASURED_FILES_3) self.assertCountEqual(covdata.lines("x.py"), X_PY_LINES_3) @@ -115,40 +115,40 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): self.assertCountEqual(covdata.arcs("y.py"), Y_PY_ARCS_3) self.assertTrue(covdata.has_arcs()) - def test_cant_add_arcs_to_lines(self): + def test_cant_set_arcs_with_lines(self): covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) with self.assertRaisesRegex(CoverageException, "Can't add arcs to existing line data"): - covdata.add_arcs(ARCS_3) + covdata.set_arcs(ARCS_3) - def test_cant_add_lines_to_arcs(self): + def test_cant_set_lines_with_arcs(self): covdata = CoverageData() - covdata.add_arcs(ARCS_3) + covdata.set_arcs(ARCS_3) with self.assertRaisesRegex(CoverageException, "Can't add lines to existing arc data"): - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) def test_touch_file_with_lines(self): covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) covdata.touch_file('zzz.py') self.assert_measured_files(covdata, MEASURED_FILES_1 + ['zzz.py']) def test_touch_file_with_arcs(self): covdata = CoverageData() - covdata.add_arcs(ARCS_3) + covdata.set_arcs(ARCS_3) covdata.touch_file('zzz.py') self.assert_measured_files(covdata, MEASURED_FILES_3 + ['zzz.py']) def test_no_lines_vs_unmeasured_file(self): covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) covdata.touch_file('zzz.py') self.assertEqual(covdata.lines('zzz.py'), []) self.assertIsNone(covdata.lines('no_such_file.py')) def test_no_arcs_vs_unmeasured_file(self): covdata = CoverageData() - covdata.add_arcs(ARCS_3) + covdata.set_arcs(ARCS_3) covdata.touch_file('zzz.py') self.assertEqual(covdata.lines('zzz.py'), []) self.assertIsNone(covdata.lines('no_such_file.py')) @@ -157,12 +157,12 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_plugin_name(self): covdata = CoverageData() - covdata.add_lines({ + covdata.set_lines({ "p1.foo": dict.fromkeys([1, 2, 3]), "p2.html": dict.fromkeys([10, 11, 12]), "main.py": dict.fromkeys([20]), }) - covdata.add_plugins({"p1.foo": "p1.plugin", "p2.html": "p2.plugin"}) + covdata.set_plugins({"p1.foo": "p1.plugin", "p2.html": "p2.plugin"}) self.assertEqual(covdata.plugin_name("p1.foo"), "p1.plugin") self.assertEqual(covdata.plugin_name("main.py"), "") self.assertIsNone(covdata.plugin_name("p3.not_here")) @@ -171,27 +171,27 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): covdata = CoverageData() msg = "Can't add plugin data for unmeasured file 'p1.foo'" with self.assertRaisesRegex(CoverageException, msg): - covdata.add_plugins({"p1.foo": "p1.plugin"}) + covdata.set_plugins({"p1.foo": "p1.plugin"}) - covdata.add_lines({"p2.html": dict.fromkeys([10, 11, 12])}) + covdata.set_lines({"p2.html": dict.fromkeys([10, 11, 12])}) with self.assertRaisesRegex(CoverageException, msg): - covdata.add_plugins({"p1.foo": "p1.plugin"}) + covdata.set_plugins({"p1.foo": "p1.plugin"}) def test_cant_change_plugin_name(self): covdata = CoverageData() - covdata.add_lines({"p1.foo": dict.fromkeys([1, 2, 3])}) - covdata.add_plugins({"p1.foo": "p1.plugin"}) + covdata.set_lines({"p1.foo": dict.fromkeys([1, 2, 3])}) + covdata.set_plugins({"p1.foo": "p1.plugin"}) msg = "Conflicting plugin name for 'p1.foo': 'p1.plugin' vs 'p1.plugin.foo'" with self.assertRaisesRegex(CoverageException, msg): - covdata.add_plugins({"p1.foo": "p1.plugin.foo"}) + covdata.set_plugins({"p1.foo": "p1.plugin.foo"}) def test_update_lines(self): covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) covdata2 = CoverageData() - covdata2.add_lines(LINES_2) + covdata2.set_lines(LINES_2) covdata3 = CoverageData() covdata3.update(covdata1) @@ -202,10 +202,10 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_update_arcs(self): covdata1 = CoverageData() - covdata1.add_arcs(ARCS_3) + covdata1.set_arcs(ARCS_3) covdata2 = CoverageData() - covdata2.add_arcs(ARCS_4) + covdata2.set_arcs(ARCS_4) covdata3 = CoverageData() covdata3.update(covdata1) @@ -216,10 +216,10 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_update_cant_mix_lines_and_arcs(self): covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) covdata2 = CoverageData() - covdata2.add_arcs(ARCS_3) + covdata2.set_arcs(ARCS_3) with self.assertRaisesRegex(CoverageException, "Can't combine arc data with line data"): covdata1.update(covdata2) @@ -229,24 +229,24 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_update_plugins(self): covdata1 = CoverageData() - covdata1.add_lines({ + covdata1.set_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_plugins({ + covdata1.set_plugins({ "p1.html": "html.plugin", "p2.html": "html.plugin2", }) covdata2 = CoverageData() - covdata2.add_lines({ + covdata2.set_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_plugins({ + covdata2.set_plugins({ "p1.html": "html.plugin", "p2.html": "html.plugin2", "p3.foo": "foo_plugin", @@ -262,12 +262,12 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_update_conflicting_plugins(self): covdata1 = CoverageData() - covdata1.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) - covdata1.add_plugins({"p1.html": "html.plugin"}) + covdata1.set_lines({"p1.html": dict.fromkeys([1, 2, 3])}) + covdata1.set_plugins({"p1.html": "html.plugin"}) covdata2 = CoverageData() - covdata2.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) - covdata2.add_plugins({"p1.html": "html.other_plugin"}) + covdata2.set_lines({"p1.html": dict.fromkeys([1, 2, 3])}) + covdata2.set_plugins({"p1.html": "html.other_plugin"}) msg = "Conflicting plugin name for 'p1.html': 'html.plugin' vs 'html.other_plugin'" with self.assertRaisesRegex(CoverageException, msg): @@ -279,11 +279,11 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_update_plugin_vs_no_plugin(self): covdata1 = CoverageData() - covdata1.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) - covdata1.add_plugins({"p1.html": "html.plugin"}) + covdata1.set_lines({"p1.html": dict.fromkeys([1, 2, 3])}) + covdata1.set_plugins({"p1.html": "html.plugin"}) covdata2 = CoverageData() - covdata2.add_lines({"p1.html": dict.fromkeys([1, 2, 3])}) + covdata2.set_lines({"p1.html": dict.fromkeys([1, 2, 3])}) msg = "Conflicting plugin name for 'p1.html': 'html.plugin' vs ''" with self.assertRaisesRegex(CoverageException, msg): @@ -295,7 +295,7 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_add_to_hash_with_lines(self): covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) hasher = mock.Mock() covdata.add_to_hash("a.py", hasher) self.assertEqual(hasher.method_calls, [ @@ -305,8 +305,8 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): def test_add_to_hash_with_arcs(self): covdata = CoverageData() - covdata.add_arcs(ARCS_3) - covdata.add_plugins({"y.py": "hologram_plugin"}) + covdata.set_arcs(ARCS_3) + covdata.set_plugins({"y.py": "hologram_plugin"}) hasher = mock.Mock() covdata.add_to_hash("y.py", hasher) self.assertEqual(hasher.method_calls, [ @@ -320,7 +320,7 @@ class CoverageDataTestInTempDir(DataTestHelpers, CoverageTest): def test_read_write_lines(self): covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) covdata1.write_file("lines.dat") covdata2 = CoverageData() @@ -331,7 +331,7 @@ class CoverageDataTestInTempDir(DataTestHelpers, CoverageTest): def test_read_write_arcs(self): covdata1 = CoverageData() - covdata1.add_arcs(ARCS_3) + covdata1.set_arcs(ARCS_3) covdata1.write_file("arcs.dat") covdata2 = CoverageData() @@ -379,7 +379,7 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): def test_writing_and_reading(self): covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) self.data_files.write(covdata1) covdata2 = CoverageData() @@ -391,7 +391,7 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): # writing files. debug = DebugControlString(options=["dataio"]) covdata1 = CoverageData(debug=debug) - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) self.data_files.write(covdata1) covdata2 = CoverageData(debug=debug) @@ -409,7 +409,7 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): # output. debug = DebugControlString(options=[]) covdata1 = CoverageData(debug=debug) - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) self.data_files.write(covdata1) covdata2 = CoverageData(debug=debug) @@ -421,7 +421,7 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): def test_explicit_suffix(self): self.assert_doesnt_exist(".coverage.SUFFIX") covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) self.data_files.write(covdata, suffix='SUFFIX') self.assert_exists(".coverage.SUFFIX") self.assert_doesnt_exist(".coverage") @@ -431,7 +431,7 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): # suffix=True will make a randomly named data file. covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) self.data_files.write(covdata1, suffix=True) self.assert_doesnt_exist(".coverage") data_files1 = glob.glob(".coverage.*") @@ -439,7 +439,7 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): # Another suffix=True will choose a different name. covdata2 = CoverageData() - covdata2.add_lines(LINES_1) + covdata2.set_lines(LINES_1) self.data_files.write(covdata2, suffix=True) self.assert_doesnt_exist(".coverage") data_files2 = glob.glob(".coverage.*") @@ -453,13 +453,13 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): self.assert_doesnt_exist(".coverage.2") covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) self.data_files.write(covdata1, suffix='1') self.assert_exists(".coverage.1") self.assert_doesnt_exist(".coverage.2") covdata2 = CoverageData() - covdata2.add_lines(LINES_2) + covdata2.set_lines(LINES_2) self.data_files.write(covdata2, suffix='2') self.assert_exists(".coverage.2") @@ -472,7 +472,7 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): def test_erasing(self): covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) self.data_files.write(covdata1) covdata1.erase() @@ -484,13 +484,13 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): self.assert_line_counts(covdata2, {}) def test_file_format(self): - # Write with CoverageData, then read the pickle explicitly. + # Write with CoverageData, then read the JSON explicitly. covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) self.data_files.write(covdata) - with open(".coverage", 'rb') as fdata: - data = pickle.load(fdata) + with open(".coverage", 'r') as fdata: + data = json.load(fdata) lines = data['lines'] self.assertCountEqual(lines.keys(), MEASURED_FILES_1) @@ -500,24 +500,24 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): self.assertNotIn('arcs', data) def test_file_format_with_arcs(self): - # Write with CoverageData, then read the pickle explicitly. + # Write with CoverageData, then read the JSON explicitly. covdata = CoverageData() - covdata.add_arcs(ARCS_3) + covdata.set_arcs(ARCS_3) self.data_files.write(covdata) - with open(".coverage", 'rb') as fdata: - data = pickle.load(fdata) + with open(".coverage", 'r') as fdata: + data = json.load(fdata) self.assertNotIn('lines', data) arcs = data['arcs'] self.assertCountEqual(arcs.keys(), MEASURED_FILES_3) - self.assertCountEqual(arcs['x.py'], X_PY_ARCS_3) - self.assertCountEqual(arcs['y.py'], Y_PY_ARCS_3) + self.assertCountEqual(arcs['x.py'], map(list, X_PY_ARCS_3)) + self.assertCountEqual(arcs['y.py'], map(list, Y_PY_ARCS_3)) def test_writing_to_other_file(self): data_files = CoverageDataFiles(".otherfile") covdata = CoverageData() - covdata.add_lines(LINES_1) + covdata.set_lines(LINES_1) data_files.write(covdata) self.assert_doesnt_exist(".coverage") self.assert_exists(".otherfile") @@ -528,18 +528,18 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): def test_combining_with_aliases(self): covdata1 = CoverageData() - covdata1.add_lines({ + covdata1.set_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_plugins({ + covdata1.set_plugins({ '/home/ned/proj/src/template.html': 'html.plugin', }) self.data_files.write(covdata1, suffix='1') covdata2 = CoverageData() - covdata2.add_lines({ + covdata2.set_lines({ r'c:\ned\test\a.py': {4: None, 5: None}, r'c:\ned\test\sub\b.py': {3: None, 6: None}, }) @@ -561,12 +561,12 @@ class CoverageDataFilesTest(DataTestHelpers, CoverageTest): def test_combining_from_different_directories(self): covdata1 = CoverageData() - covdata1.add_lines(LINES_1) + covdata1.set_lines(LINES_1) os.makedirs('cov1') covdata1.write_file('cov1/.coverage.1') covdata2 = CoverageData() - covdata2.add_lines(LINES_2) + covdata2.set_lines(LINES_2) os.makedirs('cov2') covdata2.write_file('cov2/.coverage.2') |