Skip to content

Commit 3c577bb

Browse files
committedAug 22, 2019
[lit] Diagnose insufficient args to internal env
Without this patch, failing to provide a subcommand to lit's internal `env` results in either a python `IndexError` or an attempt to execute the final `env` argument, such as `FOO=1`, as a command. This patch diagnoses those cases with a more helpful message. Reviewed By: stella.stamenova Differential Revision: https://reviews.llvm.org/D66482 llvm-svn: 369620
1 parent 7d5bc55 commit 3c577bb

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed
 

‎llvm/utils/lit/lit/TestRunner.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def quote_windows_command(seq):
238238
# args are from 'export' or 'env' command.
239239
# Returns copy of args without those commands or their arguments.
240240
def updateEnv(env, args):
241-
arg_idx = 1
241+
arg_idx_next = len(args)
242242
unset_next_env_var = False
243243
for arg_idx, arg in enumerate(args[1:]):
244244
# Support for the -u flag (unsetting) for env command
@@ -257,9 +257,10 @@ def updateEnv(env, args):
257257
key, eq, val = arg.partition('=')
258258
# Stop if there was no equals.
259259
if eq == '':
260+
arg_idx_next = arg_idx + 1
260261
break
261262
env.env[key] = val
262-
return args[arg_idx+1:]
263+
return args[arg_idx_next:]
263264

264265
def executeBuiltinEcho(cmd, shenv):
265266
"""Interpret a redirected echo command"""
@@ -880,6 +881,9 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
880881
# env FOO=1 llc < %s | env BAR=2 llvm-mc | FileCheck %s
881882
cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env)
882883
args = updateEnv(cmd_shenv, j.args)
884+
if not args:
885+
raise InternalShellError(j,
886+
"Error: 'env' requires a subcommand")
883887

884888
stdin, stdout, stderr = processRedirects(j, default_stdin, cmd_shenv,
885889
opened_files)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: env FOO=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: env -u FOO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: env -u
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: env

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
# Check the env command
22
#
3-
# RUN: %{lit} -j 1 -a -v %{inputs}/shtest-env \
3+
# RUN: not %{lit} -j 1 -a -v %{inputs}/shtest-env \
44
# RUN: | FileCheck -match-full-lines %s
55
#
66
# END.
77

88
# Make sure env commands are included in printed commands.
99

10+
# CHECK: -- Testing: 7 tests{{.*}}
11+
12+
# CHECK: FAIL: shtest-env :: env-args-last-is-assign.txt ({{[^)]*}})
13+
# CHECK: Error: 'env' requires a subcommand
14+
# CHECK: error: command failed with exit status: {{.*}}
15+
16+
# CHECK: FAIL: shtest-env :: env-args-last-is-u-arg.txt ({{[^)]*}})
17+
# CHECK: Error: 'env' requires a subcommand
18+
# CHECK: error: command failed with exit status: {{.*}}
19+
20+
# CHECK: FAIL: shtest-env :: env-args-last-is-u.txt ({{[^)]*}})
21+
# CHECK: Error: 'env' requires a subcommand
22+
# CHECK: error: command failed with exit status: {{.*}}
23+
24+
# CHECK: FAIL: shtest-env :: env-args-none.txt ({{[^)]*}})
25+
# CHECK: Error: 'env' requires a subcommand
26+
# CHECK: error: command failed with exit status: {{.*}}
27+
1028
# CHECK: PASS: shtest-env :: env-u.txt ({{[^)]*}})
1129
# CHECK: $ "{{[^"]*}}" "print_environment.py"
1230
# CHECK: $ "env" "-u" "FOO" "{{[^"]*}}" "print_environment.py"
@@ -21,3 +39,5 @@
2139
# CHECK: $ "env" "A_FOO=1" "-u" "FOO" "B_BAR=2" "-u" "BAR" "C_OOF=3" "{{[^"]*}}" "print_environment.py"
2240

2341
# CHECK: Expected Passes : 3
42+
# CHECK: Unexpected Failures: 4
43+
# CHECK-NOT: {{.}}

0 commit comments

Comments
 (0)