Skip to content

Commit 5f5af8c

Browse files
committed
[llvm-lit] Update TestRunner.py for Redirection Handling and a test in lit
Added Redirection Handling: Enhanced TestRunner.py to correctly handle cases where the env command is run without arguments. It now checks if there are no arguments and, when redirection is specified, writes the environment variables directly to the redirected file or a temporary file. Test for Redirection Behavior: Created a new test in lit's internal shell to verify that when the env command is executedwithout arguments and with redirection, it properly outputs the environment variables to the specified file.
1 parent 0a00d32 commit 5f5af8c

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

llvm/utils/lit/lit/TestRunner.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,25 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
746746
env_str = "\n".join(
747747
f"{key}={value}" for key, value in sorted(cmd_shenv.env.items())
748748
)
749-
results.append(
750-
ShellCommandResult(
751-
j, env_str, "", 0, timeoutHelper.timeoutReached(), []
752-
)
749+
# Process redirections.
750+
stdin, stdout, stderr = processRedirects(
751+
j, default_stdin, cmd_shenv, opened_files
753752
)
753+
if stdout != default_stdin:
754+
# Write directly to the redirected file (stdout).
755+
stdout.write(env_str)
756+
results.append(
757+
ShellCommandResult(
758+
j, "", "", 0, timeoutHelper.timeoutReached(), []
759+
)
760+
)
761+
else:
762+
# Capture the output for cases without redirection.
763+
results.append(
764+
ShellCommandResult(
765+
j, env_str, "", 0, timeoutHelper.timeoutReached(), []
766+
)
767+
)
754768
return 0
755769
elif args[0] == "not":
756770
not_args.append(args.pop(0))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Test the env command with output redirection to a file.
2+
# RUN: rm -f %t
3+
# RUN: env > %t
4+
# RUN: FileCheck %s < %t
5+
6+
# CHECK: BAR=2
7+
# CHECK: FOO=1

llvm/utils/lit/tests/shtest-env-positive.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## Test the env command's successful executions.
99

10-
# CHECK: -- Testing: 9 tests{{.*}}
10+
# CHECK: -- Testing: 10 tests{{.*}}
1111

1212
# CHECK: PASS: shtest-env :: env-args-last-is-assign.txt ({{[^)]*}})
1313
# CHECK: env FOO=1
@@ -47,6 +47,12 @@
4747
# CHECK-NOT: # error:
4848
# CHECK: --
4949

50+
# CHECK: PASS: shtest-env :: env-temp-redirect.txt ({{[^)]*}})
51+
# CHECK: env {{.*}}/env-temp-redirect.txt.tmp
52+
# CHECK: # executed command: env
53+
# CHECK-NOT: # error:
54+
# CHECK: --
55+
5056
# CHECK: PASS: shtest-env :: env-u.txt ({{[^)]*}})
5157
# CHECK: env -u FOO | {{.*}}
5258
# CHECK: # executed command: env -u FOO
@@ -65,6 +71,6 @@
6571
# CHECK-NOT: # error:
6672
# CHECK: --
6773

68-
# CHECK: Total Discovered Tests: 9
69-
# CHECK: Passed: 9 {{\([0-9]*\.[0-9]*%\)}}
74+
# CHECK: Total Discovered Tests: 10
75+
# CHECK: Passed: 10 {{\([0-9]*\.[0-9]*%\)}}
7076
# CHECK-NOT: {{.}}

0 commit comments

Comments
 (0)