Skip to content

Commit 710d3c8

Browse files
authored
Remove branch coverage and refactor coverage.py methods for accessing (#24180)
Removing branch coverage from the payload as the initial way it was being discovered / added was not documented / in the coverage.py API and therefore is not guaranteed to be stable. Removing for now with plans to add upstream and re-include in the extension here coverage PR: #24118
1 parent 30b7884 commit 710d3c8

File tree

9 files changed

+26
-92
lines changed

9 files changed

+26
-92
lines changed

python_files/tests/pytestadapter/test_coverage.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,3 @@ def test_simple_pytest_coverage():
4242
assert focal_function_coverage.get("lines_missed") is not None
4343
assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14, 17}
4444
assert set(focal_function_coverage.get("lines_missed")) == {18, 19, 6}
45-
assert (
46-
focal_function_coverage.get("executed_branches") > 0
47-
), "executed_branches are a number greater than 0."
48-
assert (
49-
focal_function_coverage.get("total_branches") > 0
50-
), "total_branches are a number greater than 0."

python_files/tests/unittestadapter/test_coverage.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,3 @@ def test_basic_coverage():
4949
assert focal_function_coverage.get("lines_missed") is not None
5050
assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14}
5151
assert set(focal_function_coverage.get("lines_missed")) == {6}
52-
assert (
53-
focal_function_coverage.get("executed_branches") > 0
54-
), "executed_branches are a number greater than 0."
55-
assert (
56-
focal_function_coverage.get("total_branches") > 0
57-
), "total_branches are a number greater than 0."

python_files/unittestadapter/execution.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import traceback
1111
import unittest
1212
from types import TracebackType
13-
from typing import Dict, Iterator, List, Optional, Tuple, Type, Union
13+
from typing import Dict, List, Optional, Set, Tuple, Type, Union
1414

1515
# Adds the scripts directory to the PATH as a workaround for enabling shell for test execution.
1616
path_var_name = "PATH" if "PATH" in os.environ else "Path"
@@ -375,31 +375,26 @@ def send_run_data(raw_data, test_run_pipe):
375375
)
376376

377377
if is_coverage_run:
378-
from coverage.plugin import FileReporter
379-
from coverage.report_core import get_analysis_to_report
380-
from coverage.results import Analysis
378+
import coverage
381379

382380
if not cov:
383381
raise VSCodeUnittestError("Coverage is enabled but cov is not set")
384382
cov.stop()
385383
cov.save()
386-
analysis_iterator: Iterator[Tuple[FileReporter, Analysis]] = get_analysis_to_report(
387-
cov, None
388-
)
389-
384+
cov.load()
385+
file_set: Set[str] = cov.get_data().measured_files()
390386
file_coverage_map: Dict[str, FileCoverageInfo] = {}
391-
for fr, analysis in analysis_iterator:
392-
file_str: str = fr.filename
393-
executed_branches = analysis.numbers.n_executed_branches
394-
total_branches = analysis.numbers.n_branches
395-
387+
for file in file_set:
388+
analysis = cov.analysis2(file)
389+
lines_executable = {int(line_no) for line_no in analysis[1]}
390+
lines_missed = {int(line_no) for line_no in analysis[3]}
391+
lines_covered = lines_executable - lines_missed
396392
file_info: FileCoverageInfo = {
397-
"lines_covered": list(analysis.executed), # set
398-
"lines_missed": list(analysis.missing), # set
399-
"executed_branches": executed_branches, # int
400-
"total_branches": total_branches, # int
393+
"lines_covered": list(lines_covered), # list of int
394+
"lines_missed": list(lines_missed), # list of int
401395
}
402-
file_coverage_map[file_str] = file_info
396+
file_coverage_map[file] = file_info
397+
403398
payload_cov: CoveragePayloadDict = CoveragePayloadDict(
404399
coverage=True,
405400
cwd=os.fspath(cwd),

python_files/unittestadapter/pvsc_utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ class EOTPayloadDict(TypedDict):
8484
class FileCoverageInfo(TypedDict):
8585
lines_covered: List[int]
8686
lines_missed: List[int]
87-
executed_branches: int
88-
total_branches: int
8987

9088

9189
class CoveragePayloadDict(Dict):

python_files/vscode_pytest/__init__.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
Any,
1515
Dict,
1616
Generator,
17-
Iterator,
1817
Literal,
1918
TypedDict,
2019
)
@@ -66,8 +65,6 @@ def __init__(self, message):
6665
TEST_RUN_PIPE = os.getenv("TEST_RUN_PIPE")
6766
SYMLINK_PATH = None
6867

69-
INCLUDE_BRANCHES = False
70-
7168

7269
def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
7370
global TEST_RUN_PIPE
@@ -84,10 +81,6 @@ def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
8481
global IS_DISCOVERY
8582
IS_DISCOVERY = True
8683

87-
if "--cov-branch" in args:
88-
global INCLUDE_BRANCHES
89-
INCLUDE_BRANCHES = True
90-
9184
# check if --rootdir is in the args
9285
for arg in args:
9386
if "--rootdir=" in arg:
@@ -366,8 +359,6 @@ def check_skipped_condition(item):
366359
class FileCoverageInfo(TypedDict):
367360
lines_covered: list[int]
368361
lines_missed: list[int]
369-
executed_branches: int
370-
total_branches: int
371362

372363

373364
def pytest_sessionfinish(session, exitstatus):
@@ -435,41 +426,26 @@ def pytest_sessionfinish(session, exitstatus):
435426
)
436427
# send end of transmission token
437428

438-
# send coverageee if enabled
429+
# send coverage if enabled
439430
is_coverage_run = os.environ.get("COVERAGE_ENABLED")
440431
if is_coverage_run == "True":
441432
# load the report and build the json result to return
442433
import coverage
443-
from coverage.report_core import get_analysis_to_report
444-
445-
if TYPE_CHECKING:
446-
from coverage.plugin import FileReporter
447-
from coverage.results import Analysis
448434

449435
cov = coverage.Coverage()
450436
cov.load()
451-
analysis_iterator: Iterator[tuple[FileReporter, Analysis]] = get_analysis_to_report(
452-
cov, None
453-
)
454-
437+
file_set: set[str] = cov.get_data().measured_files()
455438
file_coverage_map: dict[str, FileCoverageInfo] = {}
456-
for fr, analysis in analysis_iterator:
457-
file_str: str = fr.filename
458-
executed_branches = analysis.numbers.n_executed_branches
459-
total_branches = analysis.numbers.n_branches
460-
if not INCLUDE_BRANCHES:
461-
print("coverage not run with branches")
462-
# if covearge wasn't run with branches, set the total branches value to -1 to signal that it is not available
463-
executed_branches = 0
464-
total_branches = -1
465-
439+
for file in file_set:
440+
analysis = cov.analysis2(file)
441+
lines_executable = {int(line_no) for line_no in analysis[1]}
442+
lines_missed = {int(line_no) for line_no in analysis[3]}
443+
lines_covered = lines_executable - lines_missed
466444
file_info: FileCoverageInfo = {
467-
"lines_covered": list(analysis.executed), # set
468-
"lines_missed": list(analysis.missing), # set
469-
"executed_branches": executed_branches, # int
470-
"total_branches": total_branches, # int
445+
"lines_covered": list(lines_covered), # list of int
446+
"lines_missed": list(lines_missed), # list of int
471447
}
472-
file_coverage_map[file_str] = file_info
448+
file_coverage_map[file] = file_info
473449

474450
payload: CoveragePayloadDict = CoveragePayloadDict(
475451
coverage=True,

python_files/vscode_pytest/run_pytest_script.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ def run_pytest(args):
4545
coverage_enabled = True
4646
break
4747
if not coverage_enabled:
48-
print("Coverage is enabled, adding branch coverage as an argument.")
49-
args = [*args, "--cov=.", "--cov-branch"]
48+
args = [*args, "--cov=."]
5049

5150
run_test_ids_pipe = os.environ.get("RUN_TEST_IDS_PIPE")
5251
if run_test_ids_pipe:

src/client/testing/testController/common/resultResolver.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ export class PythonResultResolver implements ITestResultResolver {
133133
} else {
134134
this._resolveExecution(payload as ExecutionTestPayload, runInstance);
135135
}
136-
if ('coverage' in payload) {
137-
// coverage data is sent once per connection
138-
traceVerbose('Coverage data received.');
139-
this._resolveCoverage(payload as CoveragePayload, runInstance);
140-
}
141136
}
142137

143138
public _resolveCoverage(payload: CoveragePayload, runInstance: TestRun): void {
@@ -149,22 +144,13 @@ export class PythonResultResolver implements ITestResultResolver {
149144
const fileCoverageMetrics = value;
150145
const linesCovered = fileCoverageMetrics.lines_covered ? fileCoverageMetrics.lines_covered : []; // undefined if no lines covered
151146
const linesMissed = fileCoverageMetrics.lines_missed ? fileCoverageMetrics.lines_missed : []; // undefined if no lines missed
152-
const executedBranches = fileCoverageMetrics.executed_branches;
153-
const totalBranches = fileCoverageMetrics.total_branches;
154147

155148
const lineCoverageCount = new TestCoverageCount(
156149
linesCovered.length,
157150
linesCovered.length + linesMissed.length,
158151
);
159152
const uri = Uri.file(fileNameStr);
160-
let fileCoverage: FileCoverage;
161-
if (totalBranches === -1) {
162-
// branch coverage was not enabled and should not be displayed
163-
fileCoverage = new FileCoverage(uri, lineCoverageCount);
164-
} else {
165-
const branchCoverageCount = new TestCoverageCount(executedBranches, totalBranches);
166-
fileCoverage = new FileCoverage(uri, lineCoverageCount, branchCoverageCount);
167-
}
153+
const fileCoverage = new FileCoverage(uri, lineCoverageCount);
168154
runInstance.addCoverage(fileCoverage);
169155

170156
// create detailed coverage array for each file (only line coverage on detailed, not branch)
@@ -189,7 +175,7 @@ export class PythonResultResolver implements ITestResultResolver {
189175
detailedCoverageArray.push(statementCoverage);
190176
}
191177

192-
this.detailedCoverageMap.set(fileNameStr, detailedCoverageArray);
178+
this.detailedCoverageMap.set(uri.fsPath, detailedCoverageArray);
193179
}
194180
}
195181

src/client/testing/testController/common/types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,6 @@ export type FileCoverageMetrics = {
279279
lines_covered: number[];
280280
// eslint-disable-next-line camelcase
281281
lines_missed: number[];
282-
// eslint-disable-next-line camelcase
283-
executed_branches: number;
284-
// eslint-disable-next-line camelcase
285-
total_branches: number;
286282
};
287283

288284
export type ExecutionTestPayload = {

src/test/testing/common/testingAdapter.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,6 @@ suite('End to End Tests: test adapters', () => {
768768
// since only one test was run, the other test in the same file will have missed coverage lines
769769
assert.strictEqual(simpleFileCov.lines_covered.length, 3, 'Expected 1 line to be covered in even.py');
770770
assert.strictEqual(simpleFileCov.lines_missed.length, 1, 'Expected 3 lines to be missed in even.py');
771-
assert.strictEqual(simpleFileCov.executed_branches, 1, 'Expected 3 lines to be missed in even.py');
772-
assert.strictEqual(simpleFileCov.total_branches, 2, 'Expected 3 lines to be missed in even.py');
773771
return Promise.resolve();
774772
};
775773

@@ -823,8 +821,6 @@ suite('End to End Tests: test adapters', () => {
823821
// since only one test was run, the other test in the same file will have missed coverage lines
824822
assert.strictEqual(simpleFileCov.lines_covered.length, 3, 'Expected 1 line to be covered in even.py');
825823
assert.strictEqual(simpleFileCov.lines_missed.length, 1, 'Expected 3 lines to be missed in even.py');
826-
assert.strictEqual(simpleFileCov.executed_branches, 1, 'Expected 3 lines to be missed in even.py');
827-
assert.strictEqual(simpleFileCov.total_branches, 2, 'Expected 3 lines to be missed in even.py');
828824

829825
return Promise.resolve();
830826
};

0 commit comments

Comments
 (0)