-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[Utils] Adds support for diff based tests to lit's --update-tests #154147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2bdc5b
Reland "[Utils] Add new --update-tests flag to llvm-lit"
hnrklssn bccb825
[Utils] Adds support for diff based tests to lit's --update-tests
hnrklssn 64b9237
[Utils] Document DiffUpdater.py
hnrklssn aa45e26
Add whitespace
hnrklssn 6f656a4
Merge branch 'main' into update-diff-tests
hnrklssn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import shutil | ||
|
||
""" | ||
This file provides the `diff_test_updater` function, which is invoked on failed RUN lines when lit is executed with --update-tests. | ||
It checks whether the failed command is `diff` and, if so, uses heuristics to determine which file is the checked-in reference file and which file is output from the test case. | ||
The heuristics are currently as follows: | ||
- if exactly one file ends with ".expected" (common pattern in LLVM), that file is the reference file and the other is the output file | ||
- if exactly one file path contains ".tmp" (e.g. because it contains the expansion of "%t"), that file is the reference file and the other is the output file | ||
If the command matches one of these patterns the output file content is copied to the reference file to make the test pass. | ||
Otherwise the test is ignored. | ||
|
||
Possible improvements: | ||
- Support stdin patterns like "my_binary %s | diff expected.txt" | ||
- Scan RUN lines to see if a file is the source of output from a previous command. | ||
If it is then it is not a reference file that can be copied to, regardless of name, since the test will overwrite it anyways. | ||
- Only update the parts that need updating (based on the diff output). Could help avoid noisy updates when e.g. whitespace changes are ignored. | ||
""" | ||
|
||
|
||
def get_source_and_target(a, b): | ||
""" | ||
Try to figure out which file is the test output and which is the reference. | ||
""" | ||
expected_suffix = ".expected" | ||
if a.endswith(expected_suffix) and not b.endswith(expected_suffix): | ||
return b, a | ||
if b.endswith(expected_suffix) and not a.endswith(expected_suffix): | ||
return a, b | ||
|
||
tmp_substr = ".tmp" | ||
if tmp_substr in a and not tmp_substr in b: | ||
return a, b | ||
if tmp_substr in b and not tmp_substr in a: | ||
return b, a | ||
|
||
return None | ||
|
||
|
||
def filter_flags(args): | ||
return [arg for arg in args if not arg.startswith("-")] | ||
|
||
|
||
def diff_test_updater(result, test): | ||
args = filter_flags(result.command.args) | ||
if len(args) != 3: | ||
return None | ||
[cmd, a, b] = args | ||
if cmd != "diff": | ||
return None | ||
res = get_source_and_target(a, b) | ||
if not res: | ||
return f"update-diff-test: could not deduce source and target from {a} and {b}" | ||
source, target = res | ||
shutil.copy(source, target) | ||
return f"update-diff-test: copied {source} to {target}" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.txt | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FOO |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BAR |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# There is no indication here of which file is the reference file to update | ||
# RUN: diff %S/1.in %S/2.in | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# RUN: mkdir %t | ||
# RUN: cp %S/1.in %t/1.txt | ||
# RUN: cp %S/2.in %t/2.txt | ||
|
||
# There is no indication here of which file is the reference file to update | ||
# RUN: diff %t/1.txt %t/2.txt | ||
|
5 changes: 5 additions & 0 deletions
5
llvm/utils/lit/tests/Inputs/diff-test-update/diff-expected.test
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# RUN: mkdir %t | ||
# RUN: cp %S/1.in %t/my-file.expected | ||
# RUN: cp %S/2.in %t/my-file.txt | ||
# RUN: diff %t/my-file.expected %t/my-file.txt | ||
|
6 changes: 6 additions & 0 deletions
6
llvm/utils/lit/tests/Inputs/diff-test-update/diff-tmp-dir.test
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# RUN: mkdir %t | ||
# RUN: touch %S/empty.txt | ||
# RUN: cp %S/1.in %t/1.txt | ||
|
||
# RUN: diff %t/1.txt %S/empty.txt | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# RUN: cp %S/1.in %t.txt | ||
# RUN: cp %S/2.in %S/diff-t-out.txt | ||
# RUN: diff %t.txt %S/diff-t-out.txt |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import lit.formats | ||
|
||
config.name = "diff-test-update" | ||
config.suffixes = [".test"] | ||
config.test_format = lit.formats.ShTest() | ||
config.test_source_root = None | ||
config.test_exec_root = None | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# RUN: not %{lit} --update-tests -v %S/Inputs/diff-test-update | FileCheck %s | ||
|
||
# CHECK: # update-diff-test: could not deduce source and target from {{.*}}/Inputs/diff-test-update/1.in and {{.*}}/Inputs/diff-test-update/2.in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, should be addressed in #157576 |
||
# CHECK: # update-diff-test: could not deduce source and target from {{.*}}/diff-test-update/Output/diff-bail2.test.tmp/1.txt and {{.*}}/diff-test-update/Output/diff-bail2.test.tmp/2.txt | ||
# CHECK: # update-diff-test: copied {{.*}}/Output/diff-expected.test.tmp/my-file.txt to {{.*}}/Output/diff-expected.test.tmp/my-file.expected | ||
# CHECK: # update-diff-test: copied {{.*}}/Output/diff-tmp-dir.test.tmp/1.txt to {{.*}}/Inputs/diff-test-update/empty.txt | ||
# CHECK: # update-diff-test: copied {{.*}}/Inputs/diff-test-update/Output/diff-tmp.test.tmp.txt to {{.*}}/Inputs/diff-test-update/diff-t-out.txt | ||
|
||
|
||
# CHECK: Failed: 5 (100.00%) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%t
's are unique, so where does this take effect?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
llvm/utils/lit/tests/Inputs/diff-test-update/diff-tmp-dir.test
creates an empty file at%S/empty.txt
to have a file outside%t
(to test the heuristic that if one file is in%t
and the other isn't, the%t
file is the one to copy from) that can be overwritten by the test without triggering a git diff. But now that you mention it I realise that needs to be erased if it has been written to by a previous test run... BRB creating a follow-upThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I could also make it more explicit and mark only
empty.txt
as gitignoredThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
follow-up: #157576