Skip to content

Commit 57782ef

Browse files
[lit] Add support for env -i (#156939)
env -i is needed for some lit tests. The feature requires a minimal amount of work to support and there is no easy way to rewrite the tests that require it. At least two tests that need this: 1. clang/test/Driver/env.c 2. lldb/test/Shell/Host/TestCustomShell.test
1 parent 97cb30d commit 57782ef

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

clang/test/Driver/env.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// These tests try to ensure that the driver operates reasonably when run with
2-
// a strange environment. Unfortunately, it requires a normal shell and the
3-
// 'env' command that understands arguments, unlike the LIT built-in env.
4-
//
5-
// REQUIRES: shell
61
// The PATH variable is heavily used when trying to find a linker.
72
// RUN: env -i LC_ALL=C LD_LIBRARY_PATH="$LD_LIBRARY_PATH" CLANG_NO_DEFAULT_CONFIG=1 \
83
// RUN: %clang %s -### -o %t.o --target=i386-unknown-linux \

llvm/utils/lit/lit/TestRunner.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ def updateEnv(env, args):
326326
if arg == "-u":
327327
unset_next_env_var = True
328328
continue
329+
# Support for the -i flag which clears the environment
330+
if arg == "-i":
331+
env.env = {}
332+
continue
329333
if unset_next_env_var:
330334
unset_next_env_var = False
331335
if arg in env.env:
@@ -890,7 +894,12 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
890894
if os.path.isfile(exe_in_cwd):
891895
executable = exe_in_cwd
892896
if not executable:
893-
executable = lit.util.which(args[0], cmd_shenv.env["PATH"])
897+
# Use the path from cmd_shenv by default, but if the environment variable
898+
# is unset (like if the user is using env -i), use the standard path.
899+
path = (
900+
cmd_shenv.env["PATH"] if "PATH" in cmd_shenv.env else shenv.env["PATH"]
901+
)
902+
executable = lit.util.which(args[0], shenv.env["PATH"])
894903
if not executable:
895904
raise InternalShellError(j, "%r: command not found" % args[0])
896905

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Tests env command for clearing the environment
2+
3+
## Check and make sure preset environment variable were set in lit.cfg.
4+
#
5+
# RUN: env | FileCheck --check-prefix=CHECK-ENV-PRESET %s
6+
## Check clearing the entire environment.
7+
#
8+
# RUN: env -i | FileCheck --check-prefix=CHECK-ENV-CLEAR-1 %s
9+
#
10+
## Check setting a variable in a clear environment.
11+
#
12+
# RUN: env -i BAZ=3 | FileCheck --check-prefix=CHECK-ENV-ONE-1 %s
13+
#
14+
15+
# CHECK-ENV-PRESET: BAR = 2
16+
# CHECK-ENV-PRESET: FOO = 1
17+
18+
# CHECK-ENV-CLEAR-NOT: BAR
19+
# CHECK-ENV-CLEAR-NOT: FOO
20+
21+
# CHECK-ENV-ONE-NOT: BAR
22+
# CHECK-ENV-ONE: BAZ = 3
23+
# CHECK-ENV-ONE-NOT: FOO

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
## Test the env command (passing tests).
22

33
# RUN: %{lit} -a -v %{inputs}/shtest-env-positive \
4-
# RUN: | FileCheck -match-full-lines %s
4+
# RUN: | FileCheck -match-full-lines %s
55
#
66
# END.
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
@@ -39,6 +39,12 @@
3939
# CHECK-NOT: # error:
4040
# CHECK: --
4141

42+
# CHECK: PASS: shtest-env :: env-i.txt ({{[^)]*}})
43+
# CHECK: env -i | {{.*}}
44+
# CHECK: # executed command: env -i
45+
# CHECK-NOT: # error:
46+
# CHECK: --
47+
4248
# CHECK: PASS: shtest-env :: env-no-subcommand.txt ({{[^)]*}})
4349
# CHECK: env | {{.*}}
4450
# CHECK: # executed command: env
@@ -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)