Skip to content

Commit bccb825

Browse files
committed
[Utils] Adds support for diff based tests to lit's --update-tests
This adds an updater to lit's --update-tests flag with support for `diff`. If a RUN line containing the `diff` command fails, this function will use heuristics to try to deduce which file is the "reference" file, and copy the contents of the other file to the reference. If it cannot deduce which file is the reference file, it does nothing. The heuristics are currently: - does one of the files end in .expected while the other does not? Then the .expected file is the reference. - does one of the file paths contain the substring ".tmp" while the other does not? Then the file not containing ".tmp" is the reference. This matches cases where one file path is constructed using the `%t` substitution.
1 parent b2bdc5b commit bccb825

File tree

12 files changed

+86
-1
lines changed

12 files changed

+86
-1
lines changed

llvm/utils/lit/lit/DiffUpdater.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import shutil
2+
3+
4+
def get_source_and_target(a, b):
5+
"""
6+
Try to figure out which file is the test output and which is the reference.
7+
"""
8+
expected_suffix = ".expected"
9+
if a.endswith(expected_suffix) and not b.endswith(expected_suffix):
10+
return b, a
11+
if b.endswith(expected_suffix) and not a.endswith(expected_suffix):
12+
return a, b
13+
14+
tmp_substr = ".tmp"
15+
if tmp_substr in a and not tmp_substr in b:
16+
return a, b
17+
if tmp_substr in b and not tmp_substr in a:
18+
return b, a
19+
20+
return None
21+
22+
23+
def filter_flags(args):
24+
return [arg for arg in args if not arg.startswith("-")]
25+
26+
27+
def diff_test_updater(result, test):
28+
args = filter_flags(result.command.args)
29+
if len(args) != 3:
30+
return None
31+
[cmd, a, b] = args
32+
if cmd != "diff":
33+
return None
34+
res = get_source_and_target(a, b)
35+
if not res:
36+
return f"update-diff-test: could not deduce source and target from {a} and {b}"
37+
source, target = res
38+
shutil.copy(source, target)
39+
return f"update-diff-test: copied {source} to {target}"

llvm/utils/lit/lit/LitConfig.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lit.formats
99
import lit.TestingConfig
1010
import lit.util
11+
from lit.DiffUpdater import diff_test_updater
1112

1213
# LitConfig must be a new style class for properties to work
1314
class LitConfig(object):
@@ -93,7 +94,7 @@ def __init__(
9394
self.per_test_coverage = per_test_coverage
9495
self.gtest_sharding = bool(gtest_sharding)
9596
self.update_tests = update_tests
96-
self.test_updaters = []
97+
self.test_updaters = [diff_test_updater]
9798

9899
@property
99100
def maxIndividualTestTime(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BAR
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# There is no indication here of which file is the reference file to update
2+
# RUN: diff %S/1.in %S/2.in
3+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# RUN: mkdir %t
2+
# RUN: cp %S/1.in %t/1.txt
3+
# RUN: cp %S/2.in %t/2.txt
4+
5+
# There is no indication here of which file is the reference file to update
6+
# RUN: diff %t/1.txt %t/2.txt
7+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# RUN: mkdir %t
2+
# RUN: cp %S/1.in %t/my-file.expected
3+
# RUN: cp %S/2.in %t/my-file.txt
4+
# RUN: diff %t/my-file.expected %t/my-file.txt
5+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# RUN: mkdir %t
2+
# RUN: touch %S/empty.txt
3+
# RUN: cp %S/1.in %t/1.txt
4+
5+
# RUN: diff %t/1.txt %S/empty.txt
6+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# RUN: cp %S/1.in %t.txt
2+
# RUN: cp %S/2.in %S/diff-t-out.txt
3+
# RUN: diff %t.txt %S/diff-t-out.txt

0 commit comments

Comments
 (0)