diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2016-09-22 09:55:35 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-09-22 09:55:35 -0400 |
commit | 7a72b3788ad02d530420c26130cffcd81b9673f2 (patch) | |
tree | f696ea8895e742b885d6a6247f0d26dd55583b1d | |
parent | a3a3f227d2d5fe96a3667ca51bad9c9c9a2142a1 (diff) | |
download | python-coveragepy-git-7a72b3788ad02d530420c26130cffcd81b9673f2.tar.gz |
Ensure random suffixes are different after forking
-rw-r--r-- | CHANGES.rst | 5 | ||||
-rw-r--r-- | coverage/data.py | 6 | ||||
-rw-r--r-- | tests/test_process.py | 15 |
3 files changed, 17 insertions, 9 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index d60cc58e..82bb217a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -18,6 +18,11 @@ Unreleased data, leaving you with an empty .coverage data file. Fixes issues `issue 412`_, `issue 516`_, and probably `issue 511`_. +- When forking subprocesses, the coverage data files would have the same random + number appended to the file name. This didn't cause problems, because the + file names had the process id also, making collisions (nearly) impossible. + But it was disconcerting. This is now fixed. + - Data files are now about 15% smaller. - Corrected the name of the jquery.ba-throttle-debounce.js library. Thanks, diff --git a/coverage/data.py b/coverage/data.py index 78590fea..98b9c8a6 100644 --- a/coverage/data.py +++ b/coverage/data.py @@ -657,10 +657,8 @@ class CoverageDataFiles(object): with open(_TEST_NAME_FILE) as f: test_name = f.read() extra = "." + test_name - suffix = "%s%s.%s.%06d" % ( - socket.gethostname(), extra, os.getpid(), - random.randint(0, 999999) - ) + dice = random.Random().randint(0, 999999) + suffix = "%s%s.%s.%06d" % (socket.gethostname(), extra, os.getpid(), dice) if suffix: filename += "." + suffix diff --git a/tests/test_process.py b/tests/test_process.py index ec19aed9..aa2045fd 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -23,13 +23,13 @@ TRY_EXECFILE = os.path.join(os.path.dirname(__file__), "modules/process_test/try class ProcessTest(CoverageTest): """Tests of the per-process behavior of coverage.py.""" + def data_files(self): + """Return the names of coverage data files in this directory.""" + return [f for f in os.listdir('.') if (f.startswith('.coverage.') or f == '.coverage')] + def number_of_data_files(self): """Return the number of coverage data files in this directory.""" - num = 0 - for f in os.listdir('.'): - if f.startswith('.coverage.') or f == '.coverage': - num += 1 - return num + return len(self.data_files()) def test_save_on_exit(self): self.make_file("mycode.py", """\ @@ -577,6 +577,11 @@ class ProcessTest(CoverageTest): # .coverage.machine.123 files. self.assertEqual(self.number_of_data_files(), 2) + # The two data files should have different random numbers at the end of + # the file name. + nums = set(name.rpartition(".")[-1] for name in self.data_files()) + self.assertEqual(len(nums), 2, "Same random: %s" % (self.data_files(),)) + # Combine the parallel coverage data files into .coverage . self.run_command("coverage combine") self.assert_exists(".coverage") |