Skip to content

Commit

Permalink
[lit] Print internal env commands
Browse files Browse the repository at this point in the history
Without this patch, the internal `env` command removes `env` and its
args from the command line while parsing it.  This patch modifies a
copy instead so that the original command line is printed.

Reviewed By: stella.stamenova, rnk

Differential Revision: https://reviews.llvm.org/D65624

llvm-svn: 367752
  • Loading branch information
jdenny-ornl committed Aug 3, 2019
1 parent 2c5a60f commit a67d81e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
17 changes: 9 additions & 8 deletions llvm/utils/lit/lit/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,12 @@ def quote_windows_command(seq):

return ''.join(result)

# cmd is export or env
def updateEnv(env, cmd):
# args are from 'export' or 'env' command.
# Returns copy of args without those commands or their arguments.
def updateEnv(env, args):
arg_idx = 1
unset_next_env_var = False
for arg_idx, arg in enumerate(cmd.args[1:]):
for arg_idx, arg in enumerate(args[1:]):
# Support for the -u flag (unsetting) for env command
# e.g., env -u FOO -u BAR will remove both FOO and BAR
# from the environment.
Expand All @@ -258,7 +259,7 @@ def updateEnv(env, cmd):
if eq == '':
break
env.env[key] = val
cmd.args = cmd.args[arg_idx+1:]
return args[arg_idx+1:]

def executeBuiltinEcho(cmd, shenv):
"""Interpret a redirected echo command"""
Expand Down Expand Up @@ -825,7 +826,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
raise ValueError("'export' cannot be part of a pipeline")
if len(cmd.commands[0].args) != 2:
raise ValueError("'export' supports only one argument")
updateEnv(shenv, cmd.commands[0])
updateEnv(shenv, cmd.commands[0].args)
return 0

if cmd.commands[0].args[0] == 'mkdir':
Expand Down Expand Up @@ -872,12 +873,13 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
for i,j in enumerate(cmd.commands):
# Reference the global environment by default.
cmd_shenv = shenv
args = list(j.args)
if j.args[0] == 'env':
# Create a copy of the global environment and modify it for this one
# command. There might be multiple envs in a pipeline:
# env FOO=1 llc < %s | env BAR=2 llvm-mc | FileCheck %s
cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env)
updateEnv(cmd_shenv, j)
args = updateEnv(cmd_shenv, j.args)

stdin, stdout, stderr = processRedirects(j, default_stdin, cmd_shenv,
opened_files)
Expand All @@ -899,7 +901,6 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
stderrTempFiles.append((i, stderr))

# Resolve the executable path ourselves.
args = list(j.args)
executable = None
is_builtin_cmd = args[0] in builtin_commands;
if not is_builtin_cmd:
Expand All @@ -911,7 +912,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
if not executable:
executable = lit.util.which(args[0], cmd_shenv.env['PATH'])
if not executable:
raise InternalShellError(j, '%r: command not found' % j.args[0])
raise InternalShellError(j, '%r: command not found' % args[0])

# Replace uses of /dev/null with temporary files.
if kAvoidDevNull:
Expand Down
22 changes: 21 additions & 1 deletion llvm/utils/lit/tests/shtest-env.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Check the env command
#
# RUN: %{lit} -a -v %{inputs}/shtest-env
# RUN: %{lit} -j 1 -a -v %{inputs}/shtest-env \
# RUN: | FileCheck -match-full-lines %s
#
# END.

# Make sure env commands are included in printed commands.

# CHECK: PASS: shtest-env :: env-u.txt ({{[^)]*}})
# CHECK: $ "{{[^"]*}}" "print_environment.py"
# CHECK: $ "env" "-u" "FOO" "{{[^"]*}}" "print_environment.py"
# CHECK: $ "env" "-u" "FOO" "-u" "BAR" "{{[^"]*}}" "print_environment.py"

# CHECK: PASS: shtest-env :: env.txt ({{[^)]*}})
# CHECK: $ "env" "A_FOO=999" "{{[^"]*}}" "print_environment.py"
# CHECK: $ "env" "A_FOO=1" "B_BAR=2" "C_OOF=3" "{{[^"]*}}" "print_environment.py"

# CHECK: PASS: shtest-env :: mixed.txt ({{[^)]*}})
# CHECK: $ "env" "A_FOO=999" "-u" "FOO" "{{[^"]*}}" "print_environment.py"
# CHECK: $ "env" "A_FOO=1" "-u" "FOO" "B_BAR=2" "-u" "BAR" "C_OOF=3" "{{[^"]*}}" "print_environment.py"

# CHECK: Expected Passes : 3

0 comments on commit a67d81e

Please sign in to comment.