Skip to content

Commit 229fda0

Browse files
committed
update: improve test selection performance
improve code for test selection to prevent multiple database calls
1 parent 5836afd commit 229fda0

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

pytest_rts/tests/test_git.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[
1414
(
1515
FAKE_DIFF_1,
16-
[
16+
{
1717
23,
1818
29,
1919
30,
@@ -42,10 +42,10 @@
4242
219,
4343
228,
4444
238,
45-
],
45+
},
4646
),
47-
(FAKE_DIFF_2, [83, 240]),
48-
(FAKE_DIFF_3, [16, 24]),
47+
(FAKE_DIFF_2, {83, 240}),
48+
(FAKE_DIFF_3, {16, 24}),
4949
],
5050
)
5151
def test_get_changed_lines(diff: str, real_changed_lines: List[int]) -> None:

pytest_rts/utils/common.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,21 @@ def get_tests_from_changes(
5050

5151
tests: Set[str] = set()
5252
for changed_file in file_diffs:
53-
changed_lines = get_changed_lines(file_diffs[changed_file])
54-
for line_number in coverage_data.contexts_by_lineno(changed_file):
55-
if line_number in changed_lines:
56-
tests.update(
57-
strip_pytest_cov_testname(testname)
58-
for testname in coverage_data.contexts_by_lineno(changed_file)[
59-
line_number
60-
]
61-
)
53+
54+
contexts = coverage_data.contexts_by_lineno(changed_file)
55+
if not contexts:
56+
continue
57+
58+
changed_lines_with_tests = get_changed_lines(
59+
file_diffs[changed_file]
60+
).intersection(contexts.keys())
61+
62+
tests.update(
63+
strip_pytest_cov_testname(testname)
64+
for line in changed_lines_with_tests
65+
for testname in contexts[line]
66+
)
67+
6268
return tests
6369

6470

pytest_rts/utils/git.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""This module contains code for git operations"""
22
import re
33
import subprocess
4-
from typing import Dict, List
4+
from typing import Dict, List, Set
55

66
from pydriller import GitRepository
77

@@ -23,7 +23,7 @@ def get_file_diff_dict_current(files: List[str]) -> Dict[str, str]:
2323
return {file_path: get_file_diff_data_current(file_path) for file_path in files}
2424

2525

26-
def get_changed_lines(diff: str) -> List[int]:
26+
def get_changed_lines(diff: str) -> Set[int]:
2727
"""Parse changed lines from git diff -U0 output.
2828
- Change data according to git diff output:
2929
- @@ -old0,old1 +new0,new1 @@
@@ -32,7 +32,7 @@ def get_changed_lines(diff: str) -> List[int]:
3232
"""
3333
regex = r"[@][@]\s+[-][0-9]+(?:,[0-9]+)?\s+[+][0-9]+(?:,[0-9]+)?\s+[@][@]"
3434
line_changes = re.findall(regex, diff)
35-
changed_lines = []
35+
changed_lines: Set[int] = set()
3636
for change in line_changes:
3737
changed_line = change.strip("@").split()
3838

@@ -45,9 +45,9 @@ def get_changed_lines(diff: str) -> List[int]:
4545
old[0] = old[0].strip("-")
4646

4747
if int(old[1]) == 0:
48-
changed_lines.append(int(old[0]))
48+
changed_lines.add(int(old[0]))
4949
else:
50-
changed_lines.extend(range(int(old[0]), int(old[0]) + int(old[1])))
50+
changed_lines.update(range(int(old[0]), int(old[0]) + int(old[1])))
5151

5252
return changed_lines
5353

0 commit comments

Comments
 (0)