[llvm] [lit] Add support to print relative paths when outputting test names (PR #154317)
Björn Pettersson via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 19 13:01:15 PDT 2025
https://github.com/bjope updated https://github.com/llvm/llvm-project/pull/154317
>From 153193b0bd9dcdcd1bc1e6ab65a2e3e066afd5c2 Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Tue, 19 Aug 2025 13:38:06 +0200
Subject: [PATCH 1/3] [lit] Add support to print relative paths when outputting
test names
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.
Examples:
> llvm-lit test/Transforms/ADCE/willreturn.ll
PASS: LLVM :: Transforms/ADCE/willreturn.ll
> llvm-lit -r test/Transforms/ADCE/willreturn.ll
PASS: LLVM :: test/Transforms/ADCE/willreturn.ll
The option is currently only impacting how tests are identified in
console output. Not other kinds of reports.
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
---
llvm/utils/lit/lit/Test.py | 6 ++++++
llvm/utils/lit/lit/cl_arguments.py | 7 +++++++
llvm/utils/lit/lit/display.py | 6 +++---
llvm/utils/lit/lit/main.py | 5 +++--
.../lit/tests/Inputs/print-relative-path/lit.cfg | 7 +++++++
.../tests/Inputs/print-relative-path/test.txt | 1 +
.../tests/Inputs/print-relative-path/test2.txt | 2 ++
llvm/utils/lit/tests/print-relative-path.py | 16 ++++++++++++++++
8 files changed, 45 insertions(+), 5 deletions(-)
create mode 100644 llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg
create mode 100644 llvm/utils/lit/tests/Inputs/print-relative-path/test.txt
create mode 100644 llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt
create mode 100644 llvm/utils/lit/tests/print-relative-path.py
diff --git a/llvm/utils/lit/lit/Test.py b/llvm/utils/lit/lit/Test.py
index 7290977724170..da5de3bfe836c 100644
--- a/llvm/utils/lit/lit/Test.py
+++ b/llvm/utils/lit/lit/Test.py
@@ -307,6 +307,12 @@ def isFailure(self):
def getFullName(self):
return self.suite.config.name + " :: " + "/".join(self.path_in_suite)
+ def getFullNameRelPath(self, printRelativePaths):
+ if printRelativePaths:
+ return (self.suite.config.name + " :: " +
+ os.path.relpath(self.getFilePath()))
+ return self.getFullName()
+
def getFilePath(self):
if self.file_path:
return self.file_path
diff --git a/llvm/utils/lit/lit/cl_arguments.py b/llvm/utils/lit/lit/cl_arguments.py
index 8f9211ee3f538..29c6cf3e9e1c9 100644
--- a/llvm/utils/lit/lit/cl_arguments.py
+++ b/llvm/utils/lit/lit/cl_arguments.py
@@ -91,6 +91,13 @@ def parse_args():
help="Enable -v, but for all tests not just failed tests.",
action="store_true",
)
+ format_group.add_argument(
+ "-r",
+ "--relative-paths",
+ dest="printRelativePaths",
+ help="Print paths relative to CWD",
+ action="store_true",
+ )
format_group.add_argument(
"-o",
"--output",
diff --git a/llvm/utils/lit/lit/display.py b/llvm/utils/lit/lit/display.py
index 3a224f734bf71..7303ceabdfc60 100644
--- a/llvm/utils/lit/lit/display.py
+++ b/llvm/utils/lit/lit/display.py
@@ -116,7 +116,7 @@ def clear(self, interrupted):
def print_result(self, test):
# Show the test result line.
- test_name = test.getFullName()
+ test_name = test.getFullNameRelPath(self.opts.printRelativePaths)
extra_info = ""
if test.result.attempts > 1:
@@ -137,7 +137,7 @@ def print_result(self, test):
if (test.isFailure() and self.opts.showOutput) or self.opts.showAllOutput:
if test.isFailure():
print(
- "%s TEST '%s' FAILED %s" % ("*" * 20, test.getFullName(), "*" * 20)
+ "%s TEST '%s' FAILED %s" % ("*" * 20, test_name, "*" * 20)
)
out = test.result.output
# Encode/decode so that, when using Python 3.6.5 in Windows 10,
@@ -159,7 +159,7 @@ def print_result(self, test):
# Report test metrics, if present.
if test.result.metrics:
- print("%s TEST '%s' RESULTS %s" % ("*" * 10, test.getFullName(), "*" * 10))
+ print("%s TEST '%s' RESULTS %s" % ("*" * 10, test_name, "*" * 10))
items = sorted(test.result.metrics.items())
for metric_name, value in items:
print("%s: %s " % (metric_name, value.format()))
diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py
index 5255e2c5e1b51..7f52e194089b1 100755
--- a/llvm/utils/lit/lit/main.py
+++ b/llvm/utils/lit/lit/main.py
@@ -329,12 +329,13 @@ def print_results(tests, elapsed, opts):
sorted(tests_by_code[code], key=lambda t: t.getFullName()),
code,
opts.shown_codes,
+ opts.printRelativePaths
)
print_summary(total_tests, tests_by_code, opts.quiet, elapsed)
-def print_group(tests, code, shown_codes):
+def print_group(tests, code, shown_codes, printRelativePaths):
if not tests:
return
if not code.isFailure and code not in shown_codes:
@@ -342,7 +343,7 @@ def print_group(tests, code, shown_codes):
print("*" * 20)
print("{} Tests ({}):".format(code.label, len(tests)))
for test in tests:
- print(" %s" % test.getFullName())
+ print(" %s" % test.getFullNameRelPath(printRelativePaths))
sys.stdout.write("\n")
diff --git a/llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg b/llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg
new file mode 100644
index 0000000000000..2cf5104e124dc
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg
@@ -0,0 +1,7 @@
+import lit.formats
+
+config.name = "print-relative-path"
+config.suffixes = [".txt"]
+config.test_format = lit.formats.ShTest()
+config.test_source_root = None
+config.test_exec_root = None
diff --git a/llvm/utils/lit/tests/Inputs/print-relative-path/test.txt b/llvm/utils/lit/tests/Inputs/print-relative-path/test.txt
new file mode 100644
index 0000000000000..0ddee88ac0bf5
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/print-relative-path/test.txt
@@ -0,0 +1 @@
+# RUN: echo %var
diff --git a/llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt b/llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt
new file mode 100644
index 0000000000000..3c0fa612b5fb1
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt
@@ -0,0 +1,2 @@
+# RUN: false
+
diff --git a/llvm/utils/lit/tests/print-relative-path.py b/llvm/utils/lit/tests/print-relative-path.py
new file mode 100644
index 0000000000000..27f2b2b34a2d8
--- /dev/null
+++ b/llvm/utils/lit/tests/print-relative-path.py
@@ -0,0 +1,16 @@
+# RUN: not %{lit} %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-DEFAULT %s
+# RUN: not %{lit} -r %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
+# RUN: not %{lit} --relative-paths %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
+
+
+# CHECK-DEFAULT: PASS: print-relative-path :: test.txt (1 of 2)
+# CHECK-DEFAULT-NEXT: FAIL: print-relative-path :: test2.txt (2 of 2)
+# CHECK-DEFAULT-NEXT: ********************
+# CHECK-DEFAULT-NEXT: Failed Tests (1):
+# CHECK-DEFAULT-NEXT: print-relative-path :: test2.txt
+
+# CHECK-RELATIVE: PASS: print-relative-path :: Inputs/print-relative-path/test.txt (1 of 2)
+# CHECK-RELATIVE-NEXT: FAIL: print-relative-path :: Inputs/print-relative-path/test2.txt (2 of 2)
+# CHECK-RELATIVE-NEXT: ********************
+# CHECK-RELATIVE-NEXT: Failed Tests (1):
+# CHECK-RELATIVE-NEXT: print-relative-path :: Inputs/print-relative-path/test2.txt
>From fa9011e2f51b853942f67d6c835dd4c1934cc51a Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Tue, 19 Aug 2025 16:26:12 +0200
Subject: [PATCH 2/3] Fixup formatting
---
llvm/utils/lit/lit/Test.py | 3 +--
llvm/utils/lit/lit/display.py | 4 +---
llvm/utils/lit/lit/main.py | 2 +-
3 files changed, 3 insertions(+), 6 deletions(-)
diff --git a/llvm/utils/lit/lit/Test.py b/llvm/utils/lit/lit/Test.py
index da5de3bfe836c..8773913ab1ad8 100644
--- a/llvm/utils/lit/lit/Test.py
+++ b/llvm/utils/lit/lit/Test.py
@@ -309,8 +309,7 @@ def getFullName(self):
def getFullNameRelPath(self, printRelativePaths):
if printRelativePaths:
- return (self.suite.config.name + " :: " +
- os.path.relpath(self.getFilePath()))
+ return self.suite.config.name + " :: " + os.path.relpath(self.getFilePath())
return self.getFullName()
def getFilePath(self):
diff --git a/llvm/utils/lit/lit/display.py b/llvm/utils/lit/lit/display.py
index 7303ceabdfc60..629cbf7c455e1 100644
--- a/llvm/utils/lit/lit/display.py
+++ b/llvm/utils/lit/lit/display.py
@@ -136,9 +136,7 @@ def print_result(self, test):
# Show the test failure output, if requested.
if (test.isFailure() and self.opts.showOutput) or self.opts.showAllOutput:
if test.isFailure():
- print(
- "%s TEST '%s' FAILED %s" % ("*" * 20, test_name, "*" * 20)
- )
+ print("%s TEST '%s' FAILED %s" % ("*" * 20, test_name, "*" * 20))
out = test.result.output
# Encode/decode so that, when using Python 3.6.5 in Windows 10,
# print(out) doesn't raise UnicodeEncodeError if out contains
diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py
index 7f52e194089b1..654b34b74adc8 100755
--- a/llvm/utils/lit/lit/main.py
+++ b/llvm/utils/lit/lit/main.py
@@ -329,7 +329,7 @@ def print_results(tests, elapsed, opts):
sorted(tests_by_code[code], key=lambda t: t.getFullName()),
code,
opts.shown_codes,
- opts.printRelativePaths
+ opts.printRelativePaths,
)
print_summary(total_tests, tests_by_code, opts.quiet, elapsed)
>From a1d1cbe9a3bc435ff9887d64dd4ebdf0e531f3df Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Tue, 19 Aug 2025 21:14:34 +0200
Subject: [PATCH 3/3] Only impact summary report, skip "suite :: " prefix
Fixup to only impact paths in the summary report (undoing changes
in display.py).
Also skipping the "suite :: " prefix in such output.
---
llvm/utils/lit/lit/Test.py | 6 ++---
llvm/utils/lit/lit/cl_arguments.py | 2 +-
llvm/utils/lit/lit/display.py | 2 +-
llvm/utils/lit/lit/main.py | 6 ++---
llvm/utils/lit/tests/print-relative-path.py | 26 +++++++++++----------
5 files changed, 22 insertions(+), 20 deletions(-)
diff --git a/llvm/utils/lit/lit/Test.py b/llvm/utils/lit/lit/Test.py
index 8773913ab1ad8..83a09eb784fd3 100644
--- a/llvm/utils/lit/lit/Test.py
+++ b/llvm/utils/lit/lit/Test.py
@@ -307,9 +307,9 @@ def isFailure(self):
def getFullName(self):
return self.suite.config.name + " :: " + "/".join(self.path_in_suite)
- def getFullNameRelPath(self, printRelativePaths):
- if printRelativePaths:
- return self.suite.config.name + " :: " + os.path.relpath(self.getFilePath())
+ def getSummaryName(self, printPathRelativeCWD):
+ if printPathRelativeCWD:
+ return os.path.relpath(self.getFilePath())
return self.getFullName()
def getFilePath(self):
diff --git a/llvm/utils/lit/lit/cl_arguments.py b/llvm/utils/lit/lit/cl_arguments.py
index 29c6cf3e9e1c9..8238bc42395af 100644
--- a/llvm/utils/lit/lit/cl_arguments.py
+++ b/llvm/utils/lit/lit/cl_arguments.py
@@ -94,7 +94,7 @@ def parse_args():
format_group.add_argument(
"-r",
"--relative-paths",
- dest="printRelativePaths",
+ dest="printPathRelativeCWD",
help="Print paths relative to CWD",
action="store_true",
)
diff --git a/llvm/utils/lit/lit/display.py b/llvm/utils/lit/lit/display.py
index 629cbf7c455e1..b565bbc7a4f93 100644
--- a/llvm/utils/lit/lit/display.py
+++ b/llvm/utils/lit/lit/display.py
@@ -116,7 +116,7 @@ def clear(self, interrupted):
def print_result(self, test):
# Show the test result line.
- test_name = test.getFullNameRelPath(self.opts.printRelativePaths)
+ test_name = test.getFullName()
extra_info = ""
if test.result.attempts > 1:
diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py
index 654b34b74adc8..a585cc0abdd48 100755
--- a/llvm/utils/lit/lit/main.py
+++ b/llvm/utils/lit/lit/main.py
@@ -329,13 +329,13 @@ def print_results(tests, elapsed, opts):
sorted(tests_by_code[code], key=lambda t: t.getFullName()),
code,
opts.shown_codes,
- opts.printRelativePaths,
+ opts.printPathRelativeCWD,
)
print_summary(total_tests, tests_by_code, opts.quiet, elapsed)
-def print_group(tests, code, shown_codes, printRelativePaths):
+def print_group(tests, code, shown_codes, printPathRelativeCWD):
if not tests:
return
if not code.isFailure and code not in shown_codes:
@@ -343,7 +343,7 @@ def print_group(tests, code, shown_codes, printRelativePaths):
print("*" * 20)
print("{} Tests ({}):".format(code.label, len(tests)))
for test in tests:
- print(" %s" % test.getFullNameRelPath(printRelativePaths))
+ print(" %s" % test.getSummaryName(printPathRelativeCWD))
sys.stdout.write("\n")
diff --git a/llvm/utils/lit/tests/print-relative-path.py b/llvm/utils/lit/tests/print-relative-path.py
index 27f2b2b34a2d8..7f3809ccd0121 100644
--- a/llvm/utils/lit/tests/print-relative-path.py
+++ b/llvm/utils/lit/tests/print-relative-path.py
@@ -1,16 +1,18 @@
-# RUN: not %{lit} %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-DEFAULT %s
-# RUN: not %{lit} -r %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
-# RUN: not %{lit} --relative-paths %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
+# RUN: %{lit} --ignore-fail --show-pass %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-DEFAULT %s
+# RUN: %{lit} --ignore-fail --show-pass -r %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
+# RUN: %{lit} --ignore-fail --show-pass --relative-paths %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
# CHECK-DEFAULT: PASS: print-relative-path :: test.txt (1 of 2)
-# CHECK-DEFAULT-NEXT: FAIL: print-relative-path :: test2.txt (2 of 2)
-# CHECK-DEFAULT-NEXT: ********************
-# CHECK-DEFAULT-NEXT: Failed Tests (1):
-# CHECK-DEFAULT-NEXT: print-relative-path :: test2.txt
+# CHECK-DEFAULT: FAIL: print-relative-path :: test2.txt (2 of 2)
+# CHECK-DEFAULT: Passed Tests (1):
+# CHECK-DEFAULT: print-relative-path :: test.txt
+# CHECK-DEFAULT: Failed Tests (1):
+# CHECK-DEFAULT: print-relative-path :: test2.txt
-# CHECK-RELATIVE: PASS: print-relative-path :: Inputs/print-relative-path/test.txt (1 of 2)
-# CHECK-RELATIVE-NEXT: FAIL: print-relative-path :: Inputs/print-relative-path/test2.txt (2 of 2)
-# CHECK-RELATIVE-NEXT: ********************
-# CHECK-RELATIVE-NEXT: Failed Tests (1):
-# CHECK-RELATIVE-NEXT: print-relative-path :: Inputs/print-relative-path/test2.txt
+# CHECK-RELATIVE: PASS: print-relative-path :: test.txt (1 of 2)
+# CHECK-RELATIVE: FAIL: print-relative-path :: test2.txt (2 of 2)
+# CHECK-RELATIVE: Passed Tests (1):
+# CHECK-RELATIVE: Inputs/print-relative-path/test.txt
+# CHECK-RELATIVE: Failed Tests (1):
+# CHECK-RELATIVE: Inputs/print-relative-path/test2.txt
More information about the llvm-commits
mailing list