Skip to content

Commit 04b1e87

Browse files
[lit] Add support for readfile to external shell
This patch adds support for the new lit %{readfile:<filename>} substitution to the external shell. The implementation currently just appends some test commands to ensure the file exists and uses a subshell with cat. This is intended to enable running tests using the substitution in the external shell before we fully switch over to the internal shell. This code is designed to be temporary with us deleting it once everything has migrated over to the internal shell and we are able to remove the external shell code paths. Reviewers: petrhosek, cmtice, pogo59, ilovepi, arichardson Reviewed By: cmtice Pull Request: llvm/llvm-project#159431
1 parent 38800a2 commit 04b1e87

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

lit/TestRunner.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,22 @@ def runOnce(
24172417
)
24182418

24192419

2420+
def _expandLateSubstitutionsExternal(commandLine):
2421+
filePaths = []
2422+
2423+
def _replaceReadFile(match):
2424+
filePath = match.group(1)
2425+
filePaths.append(filePath)
2426+
return "$(cat %s)" % shlex.quote(filePath)
2427+
2428+
commandLine = re.sub(r"%{readfile:([^}]*)}", _replaceReadFile, commandLine)
2429+
# Add test commands before the command to check if the file exists as
2430+
# cat inside a subshell will never return a non-zero exit code outside
2431+
# of the subshell.
2432+
for filePath in filePaths:
2433+
commandLine = "%s && test -e %s" % (commandLine, filePath)
2434+
return commandLine
2435+
24202436
def executeShTest(
24212437
test, litConfig, useExternalSh, extra_substitutions=[], preamble_commands=[]
24222438
):
@@ -2447,4 +2463,8 @@ def executeShTest(
24472463
recursion_limit=test.config.recursiveExpansionLimit,
24482464
)
24492465

2466+
if useExternalSh:
2467+
for index, command in enumerate(script):
2468+
script[index] = _expandLateSubstitutionsExternal(command)
2469+
24502470
return _runShTest(test, litConfig, useExternalSh, script, tmpBase)
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
import os
2+
13
import lit.formats
4+
import lit.util
25

36
config.name = "shtest-readfile"
47
config.suffixes = [".txt"]
5-
config.test_format = lit.formats.ShTest(execute_external=False)
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)
611
config.test_source_root = None
712
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)

tests/shtest-readfile-external.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Tests the readfile substitution.
2+
3+
# UNSUPPORTED: system-windows
4+
# 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
5+
6+
# CHECK: -- Testing: 4 tests{{.*}}
7+
8+
# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
9+
# CHECK: echo $(cat [[TEMP_PATH]]/absolute-paths.txt.tmp) && test -e [[TEMP_PATH]]/absolute-paths.txt.tmp {{.*}}
10+
# CHECK: + echo hello
11+
12+
# CHECK-LABEL: FAIL: shtest-readfile :: file-does-not-exist.txt ({{[^)]*}})
13+
# CHECK: echo $(cat /file/does/not/exist) && test -e /file/does/not/exist {{.*}}
14+
# CHECK: cat: /file/does/not/exist: No such file or directory
15+
16+
# CHECK-LABEL: FAIL: shtest-readfile :: relative-paths.txt ({{[^)]*}})
17+
# CHECK: echo $(cat rel_path_test_folder/test_file) && test -e rel_path_test_folder/test_file {{.*}}
18+
# CHECK: + echo hello
19+
20+
# CHECK-LABEL: FAIL: shtest-readfile :: two-same-line.txt ({{[^)]*}})
21+
# CHECK: echo $(cat [[TEMP_PATH]]/two-same-line.txt.tmp.1) $(cat [[TEMP_PATH]]/two-same-line.txt.tmp.2) && test -e [[TEMP_PATH]]/two-same-line.txt.tmp.1 && test -e [[TEMP_PATH]]/two-same-line.txt.tmp.2 {{.*}}
22+
# CHECK: + echo hello bye

tests/shtest-readfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Tests the readfile substitution.
22

3-
# RUN: not %{lit} -a -v %{inputs}/shtest-readfile | FileCheck -match-full-lines -DTEMP_PATH=%S%{fs-sep}Inputs%{fs-sep}shtest-readfile%{fs-sep}Output %s
3+
# RUN: env LIT_USE_INTERNAL_SHELL=1 not %{lit} -a -v %{inputs}/shtest-readfile | FileCheck -match-full-lines -DTEMP_PATH=%S%{fs-sep}Inputs%{fs-sep}shtest-readfile%{fs-sep}Output %s
44

55
# CHECK: -- Testing: 4 tests{{.*}}
66

0 commit comments

Comments
 (0)