Skip to content

Commit 8baa4f9

Browse files
[𝘀𝗽𝗿] changes introduced through rebase
Created using spr 1.3.6 [skip ci]
1 parent c5b2e69 commit 8baa4f9

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

llvm/utils/lit/lit/TestRunner.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
722722
return std_fds
723723

724724

725-
def _expandLateSubstitutions(arguments, cwd):
725+
def _expandLateSubstitutions(cmd, arguments, cwd):
726726
for i, arg in enumerate(arguments):
727727
if not isinstance(arg, str):
728728
continue
@@ -731,8 +731,11 @@ def _replaceReadFile(match):
731731
filePath = match.group(1)
732732
if not os.path.isabs(filePath):
733733
filePath = os.path.join(cwd, filePath)
734-
with open(filePath) as fileHandle:
735-
return fileHandle.read()
734+
try:
735+
with open(filePath) as fileHandle:
736+
return fileHandle.read()
737+
except FileNotFoundError:
738+
raise InternalShellError(cmd, "File does not exist: %s" % filePath)
736739

737740
arguments[i] = re.sub(r"%{readfile:([^}]*)}", _replaceReadFile, arg)
738741

@@ -854,7 +857,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
854857
args[0] = expand_glob(args[0], cmd_shenv.cwd)[0]
855858

856859
# Expand all late substitutions.
857-
args = _expandLateSubstitutions(args, cmd_shenv.cwd)
860+
args = _expandLateSubstitutions(j, args, cmd_shenv.cwd)
858861

859862
inproc_builtin = inproc_builtins.get(args[0], None)
860863
if inproc_builtin and (args[0] != "echo" or len(cmd.commands) == 1):
@@ -2411,6 +2414,20 @@ def runOnce(
24112414
status, output, attempts=i + 1, max_allowed_attempts=attempts
24122415
)
24132416

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

24152432
def executeShTest(
24162433
test, litConfig, useExternalSh, extra_substitutions=[], preamble_commands=[]
@@ -2442,4 +2459,8 @@ def executeShTest(
24422459
recursion_limit=test.config.recursiveExpansionLimit,
24432460
)
24442461

2462+
if useExternalSh:
2463+
for index, command in enumerate(script):
2464+
script[index] = _expandLateSubstitutionsExternal(command)
2465+
24452466
return _runShTest(test, litConfig, useExternalSh, script, tmpBase)
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: 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)
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

llvm/utils/lit/tests/shtest-readfile.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
## Tests the readfile substitution.
22

3-
# RUN: not %{lit} -a -v %{inputs}/shtest-readfile | FileCheck -match-full-lines -DTEMP_PATH=%S/Inputs/shtest-readfile/Output %s
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
44

5-
# CHECK: -- Testing: 3 tests{{.*}}
5+
# CHECK: -- Testing: 4 tests{{.*}}
66

77
# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
88
# CHECK: echo hello
99
# CHECK: # executed command: echo '%{readfile:[[TEMP_PATH]]/absolute-paths.txt.tmp}'
1010

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+
1115
# CHECK-LABEL: FAIL: shtest-readfile :: relative-paths.txt ({{[^)]*}})
1216
# CHECK: echo hello
1317
# CHECK: # executed command: echo '%{readfile:rel_path_test_folder/test_file}'

0 commit comments

Comments
 (0)