Skip to content

Commit 7fd5c2a

Browse files
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6
2 parents fdb06d9 + 28de81a commit 7fd5c2a

File tree

9 files changed

+128
-0
lines changed

9 files changed

+128
-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/utils/lit/lit/TestRunner.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,26 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
720720
return std_fds
721721

722722

723+
def _expandLateSubstitutions(cmd, arguments, cwd):
724+
for i, arg in enumerate(arguments):
725+
if not isinstance(arg, str):
726+
continue
727+
728+
def _replaceReadFile(match):
729+
filePath = match.group(1)
730+
if not os.path.isabs(filePath):
731+
filePath = os.path.join(cwd, filePath)
732+
try:
733+
with open(filePath) as fileHandle:
734+
return fileHandle.read()
735+
except FileNotFoundError:
736+
raise InternalShellError(cmd, "File does not exist: %s" % filePath)
737+
738+
arguments[i] = re.sub(r"%{readfile:([^}]*)}", _replaceReadFile, arg)
739+
740+
return arguments
741+
742+
723743
def _executeShCmd(cmd, shenv, results, timeoutHelper):
724744
if timeoutHelper.timeoutReached():
725745
# Prevent further recursion if the timeout has been hit
@@ -834,6 +854,9 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
834854
# Ensure args[0] is hashable.
835855
args[0] = expand_glob(args[0], cmd_shenv.cwd)[0]
836856

857+
# Expand all late substitutions.
858+
args = _expandLateSubstitutions(j, args, cmd_shenv.cwd)
859+
837860
inproc_builtin = inproc_builtins.get(args[0], None)
838861
if inproc_builtin and (args[0] != "echo" or len(cmd.commands) == 1):
839862
# env calling an in-process builtin is useless, so we take the safe
@@ -2389,6 +2412,20 @@ def runOnce(
23892412
status, output, attempts=i + 1, max_allowed_attempts=attempts
23902413
)
23912414

2415+
def _expandLateSubstitutionsExternal(commandLine):
2416+
filePaths = []
2417+
def _replaceReadFile(match):
2418+
filePath = match.group(1)
2419+
filePaths.append(filePath)
2420+
return "$(cat %s)" % filePath
2421+
2422+
commandLine = re.sub(r"%{readfile:([^}]*)}", _replaceReadFile, commandLine)
2423+
# Add test commands before the command to check if the file exists as
2424+
# cat inside a subshell will never return a non-zero exit code outside
2425+
# of the subshell.
2426+
for filePath in filePaths:
2427+
commandLine = "%s && test -e %s" % (commandLine, filePath)
2428+
return commandLine
23922429

23932430
def executeShTest(
23942431
test, litConfig, useExternalSh, extra_substitutions=[], preamble_commands=[]
@@ -2420,4 +2457,8 @@ def executeShTest(
24202457
recursion_limit=test.config.recursiveExpansionLimit,
24212458
)
24222459

2460+
if useExternalSh:
2461+
for index, command in enumerate(script):
2462+
script[index] = _expandLateSubstitutionsExternal(command)
2463+
24232464
return _runShTest(test, litConfig, useExternalSh, script, tmpBase)
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Test that readfile reports information appropriately when the file specified
2+
## does not exist.
3+
4+
# RUN: echo %{readfile:/file/does/not/exist}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
import lit.formats
4+
import lit.util
5+
6+
config.name = "shtest-readfile"
7+
config.suffixes = [".txt"]
8+
lit_shell_env = os.environ.get("LIT_USE_INTERNAL_SHELL")
9+
use_lit_shell = lit.util.pythonize_bool(lit_shell_env)
10+
config.test_format = lit.formats.ShTest(execute_external=not use_lit_shell)
11+
config.test_source_root = None
12+
config.test_exec_root = None
13+
14+
# If we are testing with the external shell, remove the fake-externals from
15+
# PATH so that we use mkdir in the tests.
16+
if not use_lit_shell:
17+
path_parts = config.environment["PATH"].split(os.path.pathsep)
18+
path_parts = [path_part for path_part in path_parts if "fake-externals" not in path_part]
19+
config.environment["PATH"] = os.path.pathsep.join(path_parts)
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 set up 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Tests the readfile substitution.
2+
3+
# RUN: env LIT_USE_INTERNAL_SHELL=0 not %{lit} -a -v %{inputs}/shtest-readfile | FileCheck -match-full-lines -DTEMP_PATH=%S/Inputs/shtest-readfile/Output %s
4+
5+
# CHECK: -- Testing: 4 tests{{.*}}
6+
7+
# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
8+
# CHECK: echo $(cat [[TEMP_PATH]]/absolute-paths.txt.tmp) && test -e /home/gha/llvm-project/build/utils/lit/tests/Inputs/shtest-readfile/Output/absolute-paths.txt.tmp {{.*}}
9+
# CHECK: + echo hello
10+
11+
# CHECK-LABEL: FAIL: shtest-readfile :: file-does-not-exist.txt ({{[^)]*}})
12+
# CHECK: echo $(cat /file/does/not/exist) && test -e /file/does/not/exist {{.*}}
13+
# CHECK: cat: /file/does/not/exist: No such file or directory
14+
15+
# CHECK-LABEL: FAIL: shtest-readfile :: relative-paths.txt ({{[^)]*}})
16+
# CHECK: echo $(cat rel_path_test_folder/test_file) && test -e rel_path_test_folder/test_file {{.*}}
17+
# CHECK: + echo hello
18+
19+
# CHECK-LABEL: FAIL: shtest-readfile :: two-same-line.txt ({{[^)]*}})
20+
# CHECK: echo $(cat /home/gha/llvm-project/build/utils/lit/tests/Inputs/shtest-readfile/Output/two-same-line.txt.tmp.1) $(cat /home/gha/llvm-project/build/utils/lit/tests/Inputs/shtest-readfile/Output/two-same-line.txt.tmp.2) && test -e /home/gha/llvm-project/build/utils/lit/tests/Inputs/shtest-readfile/Output/two-same-line.txt.tmp.1 && test -e /home/gha/llvm-project/build/utils/lit/tests/Inputs/shtest-readfile/Output/two-same-line.txt.tmp.2 {{.*}}
21+
# CHECK: + echo hello bye
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Tests the readfile substitution.
2+
3+
# RUN: env LIT_USE_INTERNAL_SHELL=1 not %{lit} -a -v %{inputs}/shtest-readfile | FileCheck -match-full-lines -DTEMP_PATH=%S/Inputs/shtest-readfile/Output %s
4+
5+
# CHECK: -- Testing: 4 tests{{.*}}
6+
7+
# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
8+
# CHECK: echo hello
9+
# CHECK: # executed command: echo '%{readfile:[[TEMP_PATH]]/absolute-paths.txt.tmp}'
10+
11+
# CHECK-LABEL: FAIL: shtest-readfile :: file-does-not-exist.txt ({{[^)]*}})
12+
# CHECK: # executed command: @echo 'echo %{readfile:/file/does/not/exist}'
13+
# CHECK: # | File does not exist: /file/does/not/exist
14+
15+
# CHECK-LABEL: FAIL: shtest-readfile :: relative-paths.txt ({{[^)]*}})
16+
# CHECK: echo hello
17+
# CHECK: # executed command: echo '%{readfile:rel_path_test_folder/test_file}'
18+
19+
# CHECK-LABEL: FAIL: shtest-readfile :: two-same-line.txt ({{[^)]*}})
20+
# CHECK: echo hello bye
21+
# CHECK: # executed command: echo '%{readfile:[[TEMP_PATH]]/two-same-line.txt.tmp.1}' '%{readfile:[[TEMP_PATH]]/two-same-line.txt.tmp.2}'

0 commit comments

Comments
 (0)