Skip to content

Commit d2f77ab

Browse files
committed
measured_contexts() and two simple tests of the global context
1 parent 2f1b8cf commit d2f77ab

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

coverage/data.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ def touch_file(self, filename, plugin_name=""):
445445

446446
self._validate()
447447

448+
def set_context(self, context):
449+
"""Set the context. Not implemented for JSON storage."""
450+
if context:
451+
raise CoverageException("JSON storage doesn't support contexts")
452+
448453
def write(self):
449454
"""Write the collected coverage data to a file.
450455

coverage/sqldata.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,16 @@ def _file_id(self, filename, add=False):
179179
return self._file_map.get(filename)
180180

181181
def set_context(self, context):
182-
"""Get the context id for `context`."""
182+
"""Set the current context for future `add_lines` etc."""
183183
self._start_using()
184-
if not context:
185-
self._context_id = 0
186-
else:
187-
with self._connect() as con:
188-
row = con.execute("select id from context where context = ?", (context,)).fetchone()
189-
if row is not None:
190-
self._context_id = row[0]
191-
else:
192-
cur = con.execute("insert into context (context) values (?)", (context,))
193-
self._context_id = cur.lastrowid
184+
context = context or ""
185+
with self._connect() as con:
186+
row = con.execute("select id from context where context = ?", (context,)).fetchone()
187+
if row is not None:
188+
self._context_id = row[0]
189+
else:
190+
cur = con.execute("insert into context (context) values (?)", (context,))
191+
self._context_id = cur.lastrowid
194192

195193
def add_lines(self, line_data):
196194
"""Add measured line data.
@@ -384,6 +382,13 @@ def measured_files(self):
384382
"""A set of all files that had been measured."""
385383
return set(self._file_map)
386384

385+
def measured_contexts(self):
386+
"""A set of all contexts that have been measured."""
387+
self._start_using()
388+
with self._connect() as con:
389+
contexts = set(row[0] for row in con.execute("select distinct(context) from context"))
390+
return contexts
391+
387392
def file_tracer(self, filename):
388393
"""Get the plugin name of the file tracer for a file.
389394

tests/test_context.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2+
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
3+
4+
"""Tests for context support."""
5+
6+
import coverage
7+
8+
from tests.coveragetest import CoverageTest
9+
10+
11+
class GlobalContextTest(CoverageTest):
12+
"""Tests of the global context."""
13+
14+
def setUp(self):
15+
super(GlobalContextTest, self).setUp()
16+
self.skip_unless_data_storage_is("sql")
17+
18+
def test_no_context(self):
19+
self.make_file("main.py", "a = 1")
20+
cov = coverage.Coverage()
21+
self.start_import_stop(cov, "main")
22+
data = cov.get_data()
23+
self.assertCountEqual(data.measured_contexts(), [""])
24+
25+
def test_global_context(self):
26+
self.make_file("main.py", "a = 1")
27+
cov = coverage.Coverage(context="gooey")
28+
self.start_import_stop(cov, "main")
29+
data = cov.get_data()
30+
self.assertCountEqual(data.measured_contexts(), ["gooey"])

tests/test_debug.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def test_debug_callers(self):
144144
self.assertRegex(real_messages[-1], r"^\s*\d+\.\w{4}: Writing data")
145145
self.assertRegex(last_line, r"\s+_write_file : .*coverage[/\\]data.py @\d+$")
146146
else:
147-
self.assertRegex(real_messages[-1], r"^\s*\d+\.\w{4}: Creating data file")
148-
self.assertRegex(last_line, r"\s+_create_db : .*coverage[/\\]sqldata.py @\d+$")
147+
self.assertRegex(real_messages[-1], r"^\s*\d+\.\w{4}: Adding lines")
148+
self.assertRegex(last_line, r"\s+add_lines : .*coverage[/\\]sqldata.py @\d+$")
149149

150150
def test_debug_config(self):
151151
out_lines = self.f1_debug_output(["config"])

0 commit comments

Comments
 (0)