Skip to content

Commit

Permalink
Fix #708: .filename is private, and don't combine over ourselves
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Apr 15, 2019
1 parent 6c12370 commit fdaf035
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ Unreleased
- Combining data stored in SQLite now goes about twice as fast, fixing `issue
761`_. Thanks, Stephan Richter.

- The ``filename`` attribute on `CoverageData` object has been made private.
You can use the ``filename`` method to get the actual file name being used
to store data, and the ``base_filename`` method to get the original filename
before parallelizing suffixes were added. This is part of fixing `issue
708`_.

- Line numbers in the HTML report now align properly with source lines, even
when Chrome's minimum font size is set, fixing `issue 748`_. Thanks Wen Ye.

.. _issue 702: https://github.com/nedbat/coveragepy/issues/702
.. _issue 708: https://github.com/nedbat/coveragepy/issues/708
.. _issue 746: https://github.com/nedbat/coveragepy/issues/746
.. _issue 748: https://github.com/nedbat/coveragepy/issues/748
.. _issue 761: https://github.com/nedbat/coveragepy/issues/761
Expand Down
2 changes: 1 addition & 1 deletion coverage/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def do_debug(self, args):
self.coverage.load()
data = self.coverage.get_data()
print(info_header("data"))
print("path: %s" % self.coverage.get_data().filename)
print("path: %s" % self.coverage.get_data().filename())
if data:
print("has_arcs: %r" % data.has_arcs())
summary = line_counts(data, fullpath=True)
Expand Down
8 changes: 7 additions & 1 deletion coverage/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def combine_parallel_data(data, aliases=None, data_paths=None, strict=False):
"""
# Because of the os.path.abspath in the constructor, data_dir will
# never be an empty string.
data_dir, local = os.path.split(data.filename)
data_dir, local = os.path.split(data.base_filename())
localdot = local + '.*'

data_paths = data_paths or [data_dir]
Expand All @@ -729,6 +729,12 @@ def combine_parallel_data(data, aliases=None, data_paths=None, strict=False):

files_combined = 0
for f in files_to_combine:
if f == data.filename():
# Sometimes we are combining into a file which is one of the
# parallel files. Skip that file.
if data._debug.should('dataio'):
data._debug.write("Skipping combining ourself: %r" % (f,))
continue
if data._debug.should('dataio'):
data._debug.write("Combining data file %r" % (f,))
try:
Expand Down
34 changes: 21 additions & 13 deletions coverage/sqldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def __init__(self, basename=None, suffix=None, warn=None, debug=None):
self._current_context_id = None

def _choose_filename(self):
self.filename = self._basename
self._filename = self._basename
suffix = filename_suffix(self._suffix)
if suffix:
self.filename += "." + suffix
self._filename += "." + suffix

def _reset(self):
if self._dbs:
Expand All @@ -113,8 +113,8 @@ def _reset(self):

def _create_db(self):
if self._debug.should('dataio'):
self._debug.write("Creating data file {!r}".format(self.filename))
self._dbs[get_thread_id()] = Sqlite(self.filename, self._debug)
self._debug.write("Creating data file {!r}".format(self._filename))
self._dbs[get_thread_id()] = Sqlite(self._filename, self._debug)
with self._dbs[get_thread_id()] as db:
for stmt in SCHEMA.split(';'):
stmt = " ".join(stmt.strip().split())
Expand All @@ -128,22 +128,22 @@ def _create_db(self):

def _open_db(self):
if self._debug.should('dataio'):
self._debug.write("Opening data file {!r}".format(self.filename))
self._dbs[get_thread_id()] = Sqlite(self.filename, self._debug)
self._debug.write("Opening data file {!r}".format(self._filename))
self._dbs[get_thread_id()] = Sqlite(self._filename, self._debug)
with self._dbs[get_thread_id()] as db:
try:
schema_version, = db.execute("select version from coverage_schema").fetchone()
except Exception as exc:
raise CoverageException(
"Data file {!r} doesn't seem to be a coverage data file: {}".format(
self.filename, exc
self._filename, exc
)
)
else:
if schema_version != SCHEMA_VERSION:
raise CoverageException(
"Couldn't use data file {!r}: wrong schema: {} instead of {}".format(
self.filename, schema_version, SCHEMA_VERSION
self._filename, schema_version, SCHEMA_VERSION
)
)

Expand All @@ -155,14 +155,14 @@ def _open_db(self):

def _connect(self):
if get_thread_id() not in self._dbs:
if os.path.exists(self.filename):
if os.path.exists(self._filename):
self._open_db()
else:
self._create_db()
return self._dbs[get_thread_id()]

def __nonzero__(self):
if (get_thread_id() not in self._dbs and not os.path.exists(self.filename)):
if (get_thread_id() not in self._dbs and not os.path.exists(self._filename)):
return False
try:
with self._connect() as con:
Expand Down Expand Up @@ -221,6 +221,14 @@ def _set_context_id(self):
cur = con.execute("insert into context (context) values (?)", (context,))
self._current_context_id = cur.lastrowid

def base_filename(self):
"""The base filename for storing data."""
return self._basename

def filename(self):
"""Where is the data stored?"""
return self._filename

def add_lines(self, line_data):
"""Add measured line data.
Expand Down Expand Up @@ -477,10 +485,10 @@ def erase(self, parallel=False):
"""
self._reset()
if self._debug.should('dataio'):
self._debug.write("Erasing data file {!r}".format(self.filename))
file_be_gone(self.filename)
self._debug.write("Erasing data file {!r}".format(self._filename))
file_be_gone(self._filename)
if parallel:
data_dir, local = os.path.split(self.filename)
data_dir, local = os.path.split(self._filename)
localdot = local + '.*'
pattern = os.path.join(os.path.abspath(data_dir), localdot)
for filename in glob.glob(pattern):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def test_debug_data(self):
2 files:
file1.py: 17 lines [a_plugin]
file2.py: 23 lines
""").replace("FILENAME", data.filename))
""").replace("FILENAME", data.filename()))

def test_debug_data_with_no_data(self):
data = CoverageData()
Expand All @@ -700,7 +700,7 @@ def test_debug_data_with_no_data(self):
-- data ------------------------------------------------------
path: FILENAME
No data collected
""").replace("FILENAME", data.filename))
""").replace("FILENAME", data.filename()))


class CmdLineStdoutTest(BaseCmdLineTest):
Expand Down

0 comments on commit fdaf035

Please sign in to comment.