Fix test name comparison for names containing newlines#735
Open
pdelagrave wants to merge 2 commits intoEnricoMi:masterfrom
Open
Fix test name comparison for names containing newlines#735pdelagrave wants to merge 2 commits intoEnricoMi:masterfrom
pdelagrave wants to merge 2 commits intoEnricoMi:masterfrom
Conversation
Parameterized tests (e.g. JUnit 5 @valuesource with multi-line strings) can produce test names containing literal newlines. These names were serialized into annotations by joining with '\n' and deserialized by splitting on '\n', which broke multi-line names into fragments and caused spurious added/removed test counts in PR comments. Escape newlines (and backslashes) in test names before serialization, and unescape after deserialization. This preserves backwards compatibility: old annotations without escaping deserialize correctly since unescape is a no-op on strings without escape sequences.
EnricoMi
reviewed
Mar 24, 2026
Owner
EnricoMi
left a comment
There was a problem hiding this comment.
Thanks for fixing this. What will happen if the fixed action reads in existing annotations that already contain an escaped newline?
Comment on lines
+951
to
+969
| """Reverse escape_test_name, handling escape sequences in a single pass.""" | ||
| result = [] | ||
| i = 0 | ||
| while i < len(name): | ||
| if name[i] == '\\' and i + 1 < len(name): | ||
| c = name[i + 1] | ||
| if c == 'n': | ||
| result.append('\n') | ||
| i += 2 | ||
| elif c == '\\': | ||
| result.append('\\') | ||
| i += 2 | ||
| else: | ||
| result.append('\\') | ||
| i += 1 | ||
| else: | ||
| result.append(name[i]) | ||
| i += 1 | ||
| return ''.join(result) |
Owner
There was a problem hiding this comment.
Can we simply reverse using replace again?
Suggested change
| """Reverse escape_test_name, handling escape sequences in a single pass.""" | |
| result = [] | |
| i = 0 | |
| while i < len(name): | |
| if name[i] == '\\' and i + 1 < len(name): | |
| c = name[i + 1] | |
| if c == 'n': | |
| result.append('\n') | |
| i += 2 | |
| elif c == '\\': | |
| result.append('\\') | |
| i += 2 | |
| else: | |
| result.append('\\') | |
| i += 1 | |
| else: | |
| result.append(name[i]) | |
| i += 1 | |
| return ''.join(result) | |
| """Reverse escape_test_name.""" | |
| return name.replace('\\n', '\n').replace('\\\\', '\\') |
|
|
||
| def escape_test_name(name: str) -> str: | ||
| """Escape newlines in test names so they survive newline-delimited serialization.""" | ||
| return name.replace('\\', '\\\\').replace('\n', '\\n') |
Owner
There was a problem hiding this comment.
We may also escape "\r" while we are here.
Old annotations stored test names without escaping, so a test name containing literal backslash-n (e.g. from a Java string "path\\nname") would be incorrectly unescaped into a real newline by the new reader. Prepend a "v2" header line to raw_details when writing. On read, only unescape if the header is present; otherwise treat as legacy format and split without unescaping. This preserves exact backwards compatibility with old annotations. Also account for the header size in chunk budget calculations.
Author
Addressed in commit 8d06be0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Parameterized tests (e.g. JUnit 5
@ValueSourcewith multi-line strings, or@CsvSourcewith embedded\n) produce test names containing literal newline characters in the JUnit XML<testcase name="...">attribute.These names are serialized into check run annotations by joining with
'\n'(get_test_list_annotation) and deserialized by splitting on'\n'(get_test_list_from_annotation). When a test name itself contains a newline, the split produces phantom fragments that don't match real test names, causing spurious "removes X and adds Y tests" counts in PR comments -- even when no tests actually changed.For example, a JUnit parameterized test with name
[1] pattern=/core/*\n!/core/*/gets split into two fragments:[1] pattern=/core/*and!/core/*/, both counted as separate "tests" in the comparison.Solution
Escape newlines (as
\n) and backslashes (as\\) in test names before serialization, and unescape after deserialization. This is backwards compatible: old annotations without escaping deserialize correctly sinceunescape_test_nameis a no-op on strings that don't contain escape sequences.Changes
python/publish/__init__.py: Addedescape_test_name()/unescape_test_name()and applied escaping inget_test_list_annotation()python/publish/publisher.py: Applied unescaping inget_test_list_from_annotation()