Skip to content

Commit 98b4330

Browse files
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6
1 parent c70b9c8 commit 98b4330

File tree

10 files changed

+67
-0
lines changed

10 files changed

+67
-0
lines changed

llvm/docs/CommandGuide/lit.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ TestRunner.py:
664664
Otherwise, %t but with a single leading ``/`` removed.
665665
%:T On Windows, %/T but a ``:`` is removed if its the second character.
666666
Otherwise, %T but with a single leading ``/`` removed.
667+
%{readfile:<filename>} Reads the file specified.
667668
======================= ==============
668669

669670
Other substitutions are provided that are variations on this base set and

llvm/test/tools/llvm-cgdata/empty.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ RUN: printf '\000\000\000\000' >> %t_header.cgdata
3535
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_header.cgdata
3636
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_header.cgdata
3737
RUN: diff %t_header.cgdata %t_emptyheader.cgdata
38+
RUN: echo %{readfile:/tmp/test} > /tmp/test

llvm/utils/lit/lit/TestRunner.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,20 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
719719

720720
return std_fds
721721

722+
def _expandLateSubstitutions(arguments, cwd):
723+
for i, arg in enumerate(arguments):
724+
if not isinstance(arg, str):
725+
continue
726+
def _replaceReadFile(match):
727+
filePath = match.group(1)
728+
if not os.path.isabs(filePath):
729+
filePath = os.path.join(cwd, filePath)
730+
with open(filePath) as fileHandle:
731+
return fileHandle.read()
732+
733+
arguments[i] = re.sub(r"%{readfile:([^}]*)}", _replaceReadFile, arg)
734+
735+
return arguments
722736

723737
def _executeShCmd(cmd, shenv, results, timeoutHelper):
724738
if timeoutHelper.timeoutReached():
@@ -834,6 +848,9 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
834848
# Ensure args[0] is hashable.
835849
args[0] = expand_glob(args[0], cmd_shenv.cwd)[0]
836850

851+
# Expand all late substitutions
852+
args = _expandLateSubstitutions(args, cmd_shenv.cwd)
853+
837854
inproc_builtin = inproc_builtins.get(args[0], None)
838855
if inproc_builtin and (args[0] != "echo" or len(cmd.commands) == 1):
839856
# env calling an in-process builtin is useless, so we take the safe
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-4.514933e-03 absolute-paths.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Tests that readfile works with absolute paths
2+
# RUN: echo -n "hello" > %t
3+
# RUN: echo %{readfile:%t}
4+
5+
## Fail the test so we can assert on the output.
6+
# RUN: not echo return
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import lit.formats
2+
3+
config.name = "shtest-readfile"
4+
config.suffixes = [".txt"]
5+
config.test_format = lit.formats.ShTest(execute_external=False)
6+
config.test_source_root = None
7+
config.test_exec_root = None
8+
config.substitutions.append(("%{python}", '"%s"' % (sys.executable)))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Tests that readfile works with relative paths
2+
# RUN: mkdir -p rel_path_test_folder
3+
# RUN: echo -n "hello" > rel_path_test_folder/test_file
4+
# RUN: echo %{readfile:rel_path_test_folder/test_file}
5+
6+
## Fail the test so we can assert on the output.
7+
# RUN: not echo return
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Tests that readfile works with two substitutions on the same line to ensure the
2+
## regular expressions are setup correctly.
3+
# RUN: echo -n "hello" > %t.1
4+
# RUN: echo -n "bye" > %t.2
5+
# RUN: echo %{readfile:%t.1} %{readfile:%t.2}
6+
7+
## Fail the test so we can assert on the output.
8+
# RUN: not echo return
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Tests the readfile substitution
2+
3+
# RUN: not %{lit} -a -v %{inputs}/shtest-readfile | FileCheck -match-full-lines %s
4+
5+
# CHECK: -- Testing: 3 tests{{.*}}
6+
7+
# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
8+
# CHECK: echo hello
9+
# CHECK: # executed command: echo '%{readfile:{{.*}}}'
10+
11+
# CHECK-LABEL: FAIL: shtest-readfile :: relative-paths.txt ({{[^)]*}})
12+
# CHECK: echo hello
13+
# CHECK: # executed command: echo '%{readfile:rel_path_test_folder/test_file}'
14+
15+
# CHECK-LABEL: FAIL: shtest-readfile :: two-same-line.txt ({{[^)]*}})
16+
# CHECK: echo hello bye
17+
# CHECK: # executed command: echo '%{readfile:{{.*}}.1}' '%{readfile:{{.*}}.2}'

0 commit comments

Comments
 (0)