Skip to content

Commit 5928619

Browse files
authored
[lit] Add support to print paths relative to CWD in the test summary report (#154317)
This patch adds a -r|--relative-paths option to llvm-lit, which when enabled will print test case names using paths relative to the current working directory. The legacy default without that option is that test cases are identified using a path relative to the test suite. Only the summary report is impacted. That normally include failed tests, unless unless options such as --show-pass. Background to this patch was the discussion here https://discourse.llvm.org/t/test-case-paths-in-llvm-lit-output-are-lacking-the-location-of-the-test-dir-itself/87973 with a goal to making it easier to copy-n-paste the path to the failing test cases. Examples showing difference in "Passed Tests" and "Failed Tests": > llvm-lit --show-pass test/Transforms/Foo PASS: LLVM :: Transforms/Foo/test1.txt (1 of 2) FAIL: LLVM :: Transforms/Foo/test2.txt (2 of 2) Passed Tests (1): LLVM :: Transforms/Foo/test1.txt Failed Tests (1): LLVM :: Transforms/Foo/test2.txt > llvm-lit --show-pass --relative-paths test/Transforms/Foo PASS: LLVM :: Transforms/Foo/test1.txt (1 of 2) FAIL: LLVM :: Transforms/Foo/test2.txt (2 of 2) Passed Tests (1): test/Transforms/Foo/test1.txt Failed Tests (1): test/Transforms/Foo/test2.txt
1 parent 60ee056 commit 5928619

File tree

8 files changed

+45
-6
lines changed

8 files changed

+45
-6
lines changed

llvm/utils/lit/lit/Test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ def isFailure(self):
307307
def getFullName(self):
308308
return self.suite.config.name + " :: " + "/".join(self.path_in_suite)
309309

310+
def getSummaryName(self, printPathRelativeCWD):
311+
if printPathRelativeCWD:
312+
return os.path.relpath(self.getFilePath())
313+
return self.getFullName()
314+
310315
def getFilePath(self):
311316
if self.file_path:
312317
return self.file_path

llvm/utils/lit/lit/cl_arguments.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ def parse_args():
9191
help="Enable -v, but for all tests not just failed tests.",
9292
action="store_true",
9393
)
94+
format_group.add_argument(
95+
"-r",
96+
"--relative-paths",
97+
dest="printPathRelativeCWD",
98+
help="Print paths relative to CWD",
99+
action="store_true",
100+
)
94101
format_group.add_argument(
95102
"-o",
96103
"--output",

llvm/utils/lit/lit/display.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ def print_result(self, test):
136136
# Show the test failure output, if requested.
137137
if (test.isFailure() and self.opts.showOutput) or self.opts.showAllOutput:
138138
if test.isFailure():
139-
print(
140-
"%s TEST '%s' FAILED %s" % ("*" * 20, test.getFullName(), "*" * 20)
141-
)
139+
print("%s TEST '%s' FAILED %s" % ("*" * 20, test_name, "*" * 20))
142140
out = test.result.output
143141
# Encode/decode so that, when using Python 3.6.5 in Windows 10,
144142
# print(out) doesn't raise UnicodeEncodeError if out contains
@@ -159,7 +157,7 @@ def print_result(self, test):
159157

160158
# Report test metrics, if present.
161159
if test.result.metrics:
162-
print("%s TEST '%s' RESULTS %s" % ("*" * 10, test.getFullName(), "*" * 10))
160+
print("%s TEST '%s' RESULTS %s" % ("*" * 10, test_name, "*" * 10))
163161
items = sorted(test.result.metrics.items())
164162
for metric_name, value in items:
165163
print("%s: %s " % (metric_name, value.format()))

llvm/utils/lit/lit/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,20 +329,21 @@ def print_results(tests, elapsed, opts):
329329
sorted(tests_by_code[code], key=lambda t: t.getFullName()),
330330
code,
331331
opts.shown_codes,
332+
opts.printPathRelativeCWD,
332333
)
333334

334335
print_summary(total_tests, tests_by_code, opts.quiet, elapsed)
335336

336337

337-
def print_group(tests, code, shown_codes):
338+
def print_group(tests, code, shown_codes, printPathRelativeCWD):
338339
if not tests:
339340
return
340341
if not code.isFailure and code not in shown_codes:
341342
return
342343
print("*" * 20)
343344
print("{} Tests ({}):".format(code.label, len(tests)))
344345
for test in tests:
345-
print(" %s" % test.getFullName())
346+
print(" %s" % test.getSummaryName(printPathRelativeCWD))
346347
sys.stdout.write("\n")
347348

348349

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import lit.formats
2+
3+
config.name = "print-relative-path"
4+
config.suffixes = [".txt"]
5+
config.test_format = lit.formats.ShTest()
6+
config.test_source_root = None
7+
config.test_exec_root = None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: echo %var
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# RUN: false
2+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RUN: %{lit} --ignore-fail --show-pass %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-DEFAULT %s
2+
# RUN: %{lit} --ignore-fail --show-pass -r %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
3+
# RUN: %{lit} --ignore-fail --show-pass --relative-paths %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
4+
5+
6+
# CHECK-DEFAULT: PASS: print-relative-path :: test.txt (1 of 2)
7+
# CHECK-DEFAULT: FAIL: print-relative-path :: test2.txt (2 of 2)
8+
# CHECK-DEFAULT: Passed Tests (1):
9+
# CHECK-DEFAULT: print-relative-path :: test.txt
10+
# CHECK-DEFAULT: Failed Tests (1):
11+
# CHECK-DEFAULT: print-relative-path :: test2.txt
12+
13+
# CHECK-RELATIVE: PASS: print-relative-path :: test.txt (1 of 2)
14+
# CHECK-RELATIVE: FAIL: print-relative-path :: test2.txt (2 of 2)
15+
# CHECK-RELATIVE: Passed Tests (1):
16+
# CHECK-RELATIVE: Inputs/print-relative-path/test.txt
17+
# CHECK-RELATIVE: Failed Tests (1):
18+
# CHECK-RELATIVE: Inputs/print-relative-path/test2.txt

0 commit comments

Comments
 (0)