Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions pkgs/mcpx/scripts/affected-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,37 +263,48 @@ def e2e_tests_mentioning(mcpx, names):
return [name for name, _ in selected]


def changed_test_names(mcpx, e2e_rel_paths, root, base):
def changed_test_names(mcpx, e2e_rel_paths, root, base, untracked=()):
"""Test functions a diff actually changes, per edited e2e file:
parse each test's line span in the current file (brace counting
over noise-stripped source, as in e2e_tests_mentioning) and keep
the tests whose span intersects a changed line range from a
zero-context diff. Hunk-header function context is NOT enough:
git names the function PRECEDING the hunk, so an edit at the top
of a test is attributed to the previous test."""
of a test is attributed to the previous test. An untracked file
has no diff at all: the whole file is the change, so it is
rendered as one whole-file added range and every test in it
counts as changed -- the same result a staged new file with
identical content produces."""
names = []
for p in sorted(e2e_rel_paths):
full = os.path.join(mcpx, p)
if not os.path.exists(full):
continue
diff = run(
["git", "diff", "--unified=0", base, "--", f"pkgs/mcpx/{p}"],
root,
check=False,
)
ranges = []
for line in diff.splitlines():
m = re.match(r"@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@", line)
if m:
s = int(m.group(1))
c = int(m.group(2)) if m.group(2) is not None else 1
# A zero-count range is a pure deletion: the change
# sits between new-file lines s and s+1; attribute it
# to the test spanning line s.
ranges.append((s, s + c - 1) if c else (s, s))
text = open(full, encoding="utf-8", errors="replace").read()
if f"pkgs/mcpx/{p}" in untracked:
# `git diff <base>` is silent about untracked files: the
# whole file is newly added, so every test in it is a
# changed test -- exactly what the staged-new-file diff
# yields for identical content.
ranges = [(1, max(1, len(text.splitlines())))]
else:
diff = run(
["git", "diff", "--unified=0", base, "--", f"pkgs/mcpx/{p}"],
root,
check=False,
)
ranges = []
for line in diff.splitlines():
m = re.match(r"@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@", line)
if m:
s = int(m.group(1))
c = int(m.group(2)) if m.group(2) is not None else 1
# A zero-count range is a pure deletion: the change
# sits between new-file lines s and s+1; attribute it
# to the test spanning line s.
ranges.append((s, s + c - 1) if c else (s, s))
if not ranges:
continue
text = open(full, encoding="utf-8", errors="replace").read()
syntax = strip_go_noise(text)
for m in re.finditer(r"^func (Test\w+)\(t \*testing\.T\) \{", syntax, re.M):
start_line = syntax.count("\n", 0, m.start()) + 1
Expand Down Expand Up @@ -547,7 +558,7 @@ def pkg_arg(p):
e2e_diff += untracked_added_text(mcpx, sorted(edited_e2e_all), untracked)
if "newEnv" in e2e_diff:
harness = True
changed_e2e_tests = changed_test_names(mcpx, edited_e2e_all, root, args.base)
changed_e2e_tests = changed_test_names(mcpx, edited_e2e_all, root, args.base, untracked)
sample = []
if harness:
# The rule-4 sample is reserved its slots before the budget is
Expand Down
41 changes: 40 additions & 1 deletion pkgs/mcpx/scripts/tests/test_affected_untracked.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

* T1b importer selection (exported idents come from the diff text),
* T2 registry-name selection (names come from the diff text),
* the internal/e2e `newEnv` harness rule (matched against the e2e diff).
* the internal/e2e `newEnv` harness rule (matched against the e2e diff),
* T2 changed-test prioritization (`changed_test_names` reads a per-file
zero-context diff, which is empty for untracked files) -- issue #10874.

Each parity test builds a scratch git repo with a pkgs/mcpx layout,
applies identical content once staged (`git add`) and once untracked,
Expand Down Expand Up @@ -104,6 +106,11 @@ def t2_run_set(self, output):
self.assertIsNotNone(m, msg="no T2 e2e selection in output:\n" + output)
return set(m.group(1).split("|"))

def t2_run_list(self, output):
m = re.search(r"\./internal/e2e -run '\^\((.+?)\)\$'", output)
self.assertIsNotNone(m, msg="no T2 e2e selection in output:\n" + output)
return m.group(1).split("|")

@unittest.skipUnless(GO, "go is required for the T1b go-list lookup")
def test_t1b_untracked_parity_with_staged(self):
self.write("internal/aaa/aaa.go", "package aaa\n\nfunc Existing() {}\n")
Expand Down Expand Up @@ -173,6 +180,38 @@ def test_harness_newenv_untracked_parity_with_staged(self):
self.assertIn(sample, untracked_set)
self.assertEqual(staged_set, untracked_set)

def test_changed_tests_untracked_parity_over_t2_budget(self):
self.write(
"internal/e2e/base_test.go",
'package e2e\n\nimport "testing"\n\nfunc TestBaseSanity(t *testing.T) {}\n',
)
base = self.commit()
# 18 tests in one new e2e file, defined in reverse name order:
# changed-test prioritization follows file order while the
# alphabetical fill does not, so over the 15-test T2 budget the
# two orders truncate to different sets. Parity here proves the
# untracked file's tests really enter the prioritized segment
# (issue #10874: changed_test_names was diff-only, and a git
# diff has no hunks for an untracked file).
body = "".join(
"func TestZulu%02d(t *testing.T) {}\n\n" % n for n in range(18, 0, -1)
)
self.write(
"internal/e2e/zz_new_test.go",
'package e2e\n\nimport "testing"\n\n' + body,
)
self.stage("internal/e2e/zz_new_test.go")
staged_out = self.run_selector(base)
staged_list = self.t2_run_list(staged_out)
self.assertEqual(len(staged_list), 15)
self.assertEqual(staged_list[0], "TestZulu18")
self.assertEqual(staged_list[-1], "TestZulu04")
self.unstage("internal/e2e/zz_new_test.go")
untracked_out = self.run_selector(base)
untracked_list = self.t2_run_list(untracked_out)
self.assertEqual(staged_list, untracked_list)
self.assertEqual(staged_out, untracked_out)

def test_deleted_tracked_file_behavior_preserved(self):
self.write("internal/ddd/old.go", "package ddd\n\nfunc Old() {}\n")
base = self.commit()
Expand Down