Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions llvm/utils/lit/lit/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2417,6 +2417,22 @@ def runOnce(
)


def _expandLateSubstitutionsExternal(commandLine):
filePaths = []

def _replaceReadFile(match):
filePath = match.group(1)
filePaths.append(filePath)
return "$(cat %s)" % shlex.quote(filePath)

commandLine = re.sub(r"%{readfile:([^}]*)}", _replaceReadFile, commandLine)
# Add test commands before the command to check if the file exists as
# cat inside a subshell will never return a non-zero exit code outside
# of the subshell.
for filePath in filePaths:
commandLine = "%s && test -e %s" % (commandLine, filePath)
return commandLine

def executeShTest(
test, litConfig, useExternalSh, extra_substitutions=[], preamble_commands=[]
):
Expand Down Expand Up @@ -2447,4 +2463,8 @@ def executeShTest(
recursion_limit=test.config.recursiveExpansionLimit,
)

if useExternalSh:
for index, command in enumerate(script):
script[index] = _expandLateSubstitutionsExternal(command)

return _runShTest(test, litConfig, useExternalSh, script, tmpBase)
14 changes: 13 additions & 1 deletion llvm/utils/lit/tests/Inputs/shtest-readfile/lit.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import os

import lit.formats
import lit.util

config.name = "shtest-readfile"
config.suffixes = [".txt"]
config.test_format = lit.formats.ShTest(execute_external=False)
lit_shell_env = os.environ.get("LIT_USE_INTERNAL_SHELL")
use_lit_shell = lit.util.pythonize_bool(lit_shell_env)
config.test_format = lit.formats.ShTest(execute_external=not use_lit_shell)
config.test_source_root = None
config.test_exec_root = None

# If we are testing with the external shell, remove the fake-externals from
# PATH so that we use mkdir in the tests.
if not use_lit_shell:
path_parts = config.environment["PATH"].split(os.path.pathsep)
path_parts = [path_part for path_part in path_parts if "fake-externals" not in path_part]
config.environment["PATH"] = os.path.pathsep.join(path_parts)
22 changes: 22 additions & 0 deletions llvm/utils/lit/tests/shtest-readfile-external.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Tests the readfile substitution.

# UNSUPPORTED: system-windows
# 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

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

# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
# CHECK: echo $(cat [[TEMP_PATH]]/absolute-paths.txt.tmp) && test -e [[TEMP_PATH]]/absolute-paths.txt.tmp {{.*}}
# CHECK: + echo hello

# CHECK-LABEL: FAIL: shtest-readfile :: file-does-not-exist.txt ({{[^)]*}})
# CHECK: echo $(cat /file/does/not/exist) && test -e /file/does/not/exist {{.*}}
# CHECK: cat: /file/does/not/exist: No such file or directory

# CHECK-LABEL: FAIL: shtest-readfile :: relative-paths.txt ({{[^)]*}})
# CHECK: echo $(cat rel_path_test_folder/test_file) && test -e rel_path_test_folder/test_file {{.*}}
# CHECK: + echo hello

# CHECK-LABEL: FAIL: shtest-readfile :: two-same-line.txt ({{[^)]*}})
# 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 {{.*}}
# CHECK: + echo hello bye
2 changes: 1 addition & 1 deletion llvm/utils/lit/tests/shtest-readfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Tests the readfile substitution.

# 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
# 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

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

Expand Down
Loading