Skip to content

Commit 8e92136

Browse files
authored
[llvm-lit] Print environment variables when using env without subcommand (#98414)
This patch addresses an issue with lit's internal shell when env is without any arguments, it fails with exit code 127 because `env` requires a subcommand. This patch addresses the issue by encoding the command to properly return environment variables even when no arguments are provided. The error occurred when running the command ` LIT_USE_INTERNAL_SHELL=1 ninja check-llvm`. fixes: #102383 This is part of the test cleanups proposed in the RFC: [[RFC] Enabling the Lit Internal Shell by Default](https://discourse.llvm.org/t/rfc-enabling-the-lit-internal-shell-by-default/80179)
1 parent f28aa0d commit 8e92136

28 files changed

+265
-225
lines changed

lit/TestRunner.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,16 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
742742
cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env)
743743
args = updateEnv(cmd_shenv, args)
744744
if not args:
745-
raise InternalShellError(j, "Error: 'env' requires a" " subcommand")
745+
# Return the environment variables if no argument is provided.
746+
env_str = "\n".join(
747+
f"{key}={value}" for key, value in sorted(cmd_shenv.env.items())
748+
)
749+
results.append(
750+
ShellCommandResult(
751+
j, env_str, "", 0, timeoutHelper.timeoutReached(), []
752+
)
753+
)
754+
return 0
746755
elif args[0] == "not":
747756
not_args.append(args.pop(0))
748757
not_count += 1

tests/Inputs/shtest-env/lit.cfg renamed to tests/Inputs/shtest-env-negative/lit.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ config.test_source_root = None
77
config.test_exec_root = None
88
config.environment["FOO"] = "1"
99
config.environment["BAR"] = "2"
10+
config.environment["QUX"] = "3"
1011
config.substitutions.append(("%{python}", '"%s"' % (sys.executable)))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Tests the behaviour of chaining env commands together.
2+
3+
## Check that internal env can call internal env.
4+
# RUN: env env | FileCheck -check-prefix=CHECK-2-EMPTY-ARGS %s
5+
#
6+
# CHECK-2-EMPTY-ARGS: BAR = 2
7+
# CHECK-2-EMPTY-ARGS: FOO = 1
8+
9+
## Check setting variables in a nested env call.
10+
# RUN: env FOO=2 env BAR=1 | FileCheck -check-prefix=CHECK-2-VAL %s
11+
#
12+
# CHECK-2-VAL: BAR = 1
13+
# CHECK-2-VAL: FOO = 2
14+
15+
## Check unsetting variables in a nested env call.
16+
# RUN: env -u FOO env -u BAR | FileCheck -check-prefix=CHECK-2-U %s
17+
#
18+
# CHECK-2-U-NOT: BAR
19+
# CHECK-2-U-NOT: FOO
20+
21+
## Check mixed setting and unsetting in nested env calls.
22+
# RUN: env -u FOO BAR=1 env -u BAR FOO=2 | FileCheck -check-prefix=CHECK-2-U-VAL %s
23+
#
24+
# CHECK-2-U-VAL-NOT: BAR
25+
# CHECK-2-U-VAL: FOO = 2
26+
27+
## Check setting, unsetting, and adding a new variable in nested env calls.
28+
# RUN: env -u FOO BAR=1 env -u BAR FOO=2 env BAZ=3 | FileCheck -check-prefix=CHECK-3 %s
29+
#
30+
# CHECK-3-NOT: BAR
31+
# CHECK-3: BAZ = 3
32+
# CHECK-3: FOO = 2
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Tests the env command in various scenarios: without arguments, setting, unsetting, and mixing envrionment variables.
2+
3+
## Check default environment.
4+
# RUN: env | FileCheck -check-prefix=NO-ARGS %s
5+
#
6+
# NO-ARGS: BAR=2
7+
# NO-ARGS: FOO=1
8+
# NO-ARGS: QUX=3
9+
10+
## Set environment variables.
11+
# RUN: env FOO=2 BAR=1 | FileCheck -check-prefix=SET-VAL %s
12+
#
13+
# SET-VAL: BAR=1
14+
# SET-VAL: FOO=2
15+
# SET-VAL: QUX=3
16+
17+
## Unset environment variables.
18+
# RUN: env -u FOO -u BAR | FileCheck -check-prefix=UNSET-U %s
19+
#
20+
# UNSET-U-NOT: BAR
21+
# UNSET-U-NOT: FOO
22+
# UNSET-U: QUX=3
23+
24+
## Mixed set and unset environment variables.
25+
# RUN: env -u FOO BAR=1 -u BAR FOO=2 | FileCheck -check-prefix=MIXED-SET-UNSET %s
26+
#
27+
# MIXED-SET-UNSET-NOT: BAR
28+
# MIXED-SET-UNSET: FOO=2
29+
# MIXED-SET-UNSET: QUX=3
30+
31+
## Mixed set and unset with additional variable.
32+
# RUN: env -u FOO BAR=1 -u BAR FOO=2 BAZ=4 | FileCheck -check-prefix=MIXED-SET-UNSET-ADD-3 %s
33+
#
34+
# MIXED-SET-UNSET-ADD-NOT: BAR
35+
# MIXED-SET-UNSET-ADD: BAZ=4
36+
# MIXED-SET-UNSET-ADD: FOO=2
37+
# MIXED-SET-UNSET-ADD: QUX=3
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Tests env command for preset variables and handling single/multiple unsets.
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+
#
7+
## Check single unset of environment variable.
8+
#
9+
# RUN: env -u FOO | FileCheck --check-prefix=CHECK-ENV-UNSET-1 %s
10+
#
11+
## Check multiple unsets of environment variables.
12+
#
13+
# RUN: env -u FOO -u BAR | FileCheck --check-prefix=CHECK-ENV-UNSET-MULTIPLE %s
14+
15+
# CHECK-ENV-PRESET: BAR = 2
16+
# CHECK-ENV-PRESET: FOO = 1
17+
18+
# CHECK-ENV-UNSET-1: BAR = 2
19+
# CHECK-ENV-UNSET-1-NOT: FOO
20+
21+
# CHECK-ENV-UNSET-MULTIPLE-NOT: BAR
22+
# CHECK-ENV-UNSET-MULTIPLE-NOT: FOO
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Tests env command for setting single and multiple environment variables.
2+
3+
## Check for simple one environment variable setting.
4+
#
5+
# RUN: env A_FOO=999 | FileCheck --check-prefix=CHECK-ENV-1 %s
6+
#
7+
## Check for multiple environment variable settings.
8+
#
9+
# RUN: env A_FOO=1 B_BAR=2 C_OOF=3 | FileCheck --check-prefix=CHECK-ENV-MULTIPLE %s
10+
11+
# CHECK-ENV-1: A_FOO = 999
12+
13+
# CHECK-ENV-MULTIPLE: A_FOO = 1
14+
# CHECK-ENV-MULTIPLE: B_BAR = 2
15+
# CHECK-ENV-MULTIPLE: C_OOF = 3
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import lit.formats
2+
3+
config.name = "shtest-env"
4+
config.suffixes = [".txt"]
5+
config.test_format = lit.formats.ShTest()
6+
config.test_source_root = None
7+
config.test_exec_root = None
8+
config.environment["FOO"] = "1"
9+
config.environment["BAR"] = "2"
10+
config.environment["QUX"] = "3"
11+
config.substitutions.append(("%{python}", '"%s"' % (sys.executable)))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Tests env command for setting and unsetting single and multiple environment variables.
2+
3+
## Check for setting and removing one environment variable
4+
#
5+
# RUN: env A_FOO=999 -u FOO | FileCheck --check-prefix=CHECK-ENV-1 %s
6+
#
7+
## Check for setting/unsetting multiple environment variables
8+
#
9+
# RUN: env A_FOO=1 -u FOO B_BAR=2 -u BAR C_OOF=3 | FileCheck --check-prefix=CHECK-ENV-MULTIPLE %s
10+
11+
# CHECK-ENV-1: A_FOO = 999
12+
# CHECK-ENV-1-NOT: FOO
13+
14+
# CHECK-ENV-MULTIPLE: A_FOO = 1
15+
# CHECK-ENV-MULTIPLE-NOT: BAR
16+
# CHECK-ENV-MULTIPLE: B_BAR = 2
17+
# CHECK-ENV-MULTIPLE: C_OOF = 3
18+
# CHECK-ENV-MULTIPLE-NOT: FOO

tests/Inputs/shtest-env/env-args-none.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/Inputs/shtest-env/env-calls-env.txt

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/Inputs/shtest-env/env-u.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/Inputs/shtest-env/env.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/Inputs/shtest-env/mixed.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/Inputs/shtest-env/print_environment.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/shtest-env-negative.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Test the env command (failing tests).
2+
3+
# RUN: not %{lit} -a -v %{inputs}/shtest-env-negative \
4+
# RUN: | FileCheck -match-full-lines %s
5+
#
6+
# END.
7+
8+
## Test the env command's expected failures.
9+
10+
# CHECK: -- Testing: 7 tests{{.*}}
11+
12+
# CHECK: FAIL: shtest-env :: env-calls-cd.txt ({{[^)]*}})
13+
# CHECK: env -u FOO BAR=3 cd foobar
14+
# CHECK: # executed command: env -u FOO BAR=3 cd foobar
15+
# CHECK: # error: command failed with exit status: {{.*}}
16+
17+
# CHECK: FAIL: shtest-env :: env-calls-colon.txt ({{[^)]*}})
18+
# CHECK: env -u FOO BAR=3 :
19+
# CHECK: # executed command: env -u FOO BAR=3 :
20+
# CHECK: # error: command failed with exit status: {{.*}}
21+
22+
# CHECK: FAIL: shtest-env :: env-calls-echo.txt ({{[^)]*}})
23+
# CHECK: env -u FOO BAR=3 echo hello world
24+
# CHECK: # executed command: env -u FOO BAR=3 echo hello world
25+
# CHECK: # error: command failed with exit status: {{.*}}
26+
27+
# CHECK: FAIL: shtest-env :: env-calls-export.txt ({{[^)]*}})
28+
# CHECK: env -u FOO BAR=3 export BAZ=3
29+
# CHECK: # executed command: env -u FOO BAR=3 export BAZ=3
30+
# CHECK: # error: command failed with exit status: {{.*}}
31+
32+
# CHECK: FAIL: shtest-env :: env-calls-mkdir.txt ({{[^)]*}})
33+
# CHECK: env -u FOO BAR=3 mkdir foobar
34+
# CHECK: # executed command: env -u FOO BAR=3 mkdir foobar
35+
# CHECK: # error: command failed with exit status: {{.*}}
36+
37+
# CHECK: FAIL: shtest-env :: env-calls-not-builtin.txt ({{[^)]*}})
38+
# CHECK: env -u FOO BAR=3 not rm {{.+}}.no-such-file
39+
# CHECK: # executed command: env -u FOO BAR=3 not rm {{.+}}.no-such-file{{.*}}
40+
# CHECK: # error: command failed with exit status: {{.*}}
41+
42+
# CHECK: FAIL: shtest-env :: env-calls-rm.txt ({{[^)]*}})
43+
# CHECK: env -u FOO BAR=3 rm foobar
44+
# CHECK: # executed command: env -u FOO BAR=3 rm foobar
45+
# CHECK: # error: command failed with exit status: {{.*}}
46+
47+
# CHECK: Total Discovered Tests: 7
48+
# CHECK: Failed: 7 {{\([0-9]*\.[0-9]*%\)}}
49+
# CHECK-NOT: {{.}}

tests/shtest-env-positive.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## Test the env command (passing tests).
2+
3+
# RUN: %{lit} -a -v %{inputs}/shtest-env-positive \
4+
# RUN: | FileCheck -match-full-lines %s
5+
#
6+
# END.
7+
8+
## Test the env command's successful executions.
9+
10+
# CHECK: -- Testing: 9 tests{{.*}}
11+
12+
# CHECK: PASS: shtest-env :: env-args-last-is-assign.txt ({{[^)]*}})
13+
# CHECK: env FOO=1
14+
# CHECK: # executed command: env FOO=1
15+
# CHECK-NOT: # error:
16+
# CHECK: --
17+
18+
# CHECK: PASS: shtest-env :: env-args-last-is-u-arg.txt ({{[^)]*}})
19+
# CHECK: env -u FOO
20+
# CHECK: # executed command: env -u FOO
21+
# CHECK-NOT: # error:
22+
# CHECK: --
23+
24+
# CHECK: PASS: shtest-env :: env-args-last-is-u.txt ({{[^)]*}})
25+
# CHECK: env -u
26+
# CHECK: # executed command: env -u
27+
# CHECK-NOT: # error:
28+
# CHECK: --
29+
30+
# CHECK: PASS: shtest-env :: env-args-nested-none.txt ({{[^)]*}})
31+
# CHECK: env env env
32+
# CHECK: # executed command: env env env
33+
# CHECK-NOT: # error:
34+
# CHECK: --
35+
36+
# CHECK: PASS: shtest-env :: env-calls-env.txt ({{[^)]*}})
37+
# CHECK: env env | {{.*}}
38+
# CHECK: # executed command: env env
39+
# CHECK-NOT: # error:
40+
# CHECK: --
41+
42+
# CHECK: PASS: shtest-env :: env-no-subcommand.txt ({{[^)]*}})
43+
# CHECK: env | {{.*}}
44+
# CHECK: # executed command: env
45+
# CHECK: env FOO=2 BAR=1 | {{.*}}
46+
# CHECK: # executed command: env FOO=2 BAR=1
47+
# CHECK-NOT: # error:
48+
# CHECK: --
49+
50+
# CHECK: PASS: shtest-env :: env-u.txt ({{[^)]*}})
51+
# CHECK: env -u FOO | {{.*}}
52+
# CHECK: # executed command: env -u FOO
53+
# CHECK-NOT: # error:
54+
# CHECK: --
55+
56+
# CHECK: PASS: shtest-env :: env.txt ({{[^)]*}})
57+
# CHECK: env A_FOO=999 | {{.*}}
58+
# CHECK: # executed command: env A_FOO=999
59+
# CHECK-NOT: # error:
60+
# CHECK: --
61+
62+
# CHECK: PASS: shtest-env :: mixed.txt ({{[^)]*}})
63+
# CHECK: env A_FOO=999 -u FOO | {{.*}}
64+
# CHECK: # executed command: env A_FOO=999 -u FOO
65+
# CHECK-NOT: # error:
66+
# CHECK: --
67+
68+
# CHECK: Total Discovered Tests: 9
69+
# CHECK: Passed: 9 {{\([0-9]*\.[0-9]*%\)}}
70+
# CHECK-NOT: {{.}}

0 commit comments

Comments
 (0)