This is an updated version of https://reviews.llvm.org/D22144 by @jlpeyton.
The patch was accepted but not landed.
This is useful functionality and I would like to use this to enable lit tests for environment variable behaviour.
Differential Revision: https://reviews.llvm.org/D36403
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311180
91177308-0d34-0410-b5e6-
96231b3b80d8
# cmd is export or env
def updateEnv(env, cmd):
arg_idx = 1
+ unset_next_env_var = False
for arg_idx, arg in enumerate(cmd.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.
+ if arg == '-u':
+ unset_next_env_var = True
+ continue
+ if unset_next_env_var:
+ unset_next_env_var = False
+ if arg in env.env:
+ del env.env[arg]
+ continue
+
# Partition the string into KEY=VALUE.
key, eq, val = arg.partition('=')
# Stop if there was no equals.
--- /dev/null
+# Check and make sure preset environment variable were set in lit.cfg
+#
+# RUN: %{python} print_environment.py \
+# RUN: | FileCheck --check-prefix=CHECK-ENV-PRESET %s
+#
+# Check single unset of environment variable
+#
+# RUN: env -u FOO %{python} print_environment.py \
+# RUN: | FileCheck --check-prefix=CHECK-ENV-UNSET-1 %s
+#
+# Check multiple unsets of environment variables
+#
+# RUN: env -u FOO -u BAR %{python} print_environment.py \
+# RUN: | FileCheck --check-prefix=CHECK-ENV-UNSET-MULTIPLE %s
+
+# CHECK-ENV-PRESET: BAR = 2
+# CHECK-ENV-PRESET: FOO = 1
+
+# CHECK-ENV-UNSET-1: BAR = 2
+# CHECK-ENV-UNSET-1-NOT: FOO
+
+# CHECK-ENV-UNSET-MULTIPLE-NOT: BAR
+# CHECK-ENV-UNSET-MULTIPLE-NOT: FOO
--- /dev/null
+# Check for simple one environment variable setting
+#
+# RUN: env A_FOO=999 %{python} print_environment.py \
+# RUN: | FileCheck --check-prefix=CHECK-ENV-1 %s
+#
+# Check for multiple environment variable settings
+#
+# RUN: env A_FOO=1 B_BAR=2 C_OOF=3 %{python} print_environment.py \
+# RUN: | FileCheck --check-prefix=CHECK-ENV-MULTIPLE %s
+
+# CHECK-ENV-1: A_FOO = 999
+
+# CHECK-ENV-MULTIPLE: A_FOO = 1
+# CHECK-ENV-MULTIPLE: B_BAR = 2
+# CHECK-ENV-MULTIPLE: C_OOF = 3
--- /dev/null
+import lit.formats
+config.name = 'shtest-env'
+config.suffixes = ['.txt']
+config.test_format = lit.formats.ShTest()
+config.test_source_root = None
+config.test_exec_root = None
+config.environment['FOO'] = '1'
+config.environment['BAR'] = '2'
+config.substitutions.append(('%{python}', sys.executable))
--- /dev/null
+# Check for setting and removing one environment variable
+#
+# RUN: env A_FOO=999 -u FOO %{python} print_environment.py \
+# RUN: | FileCheck --check-prefix=CHECK-ENV-1 %s
+#
+# Check for setting/unsetting multiple environment variables
+#
+# RUN: env A_FOO=1 -u FOO B_BAR=2 -u BAR C_OOF=3 %{python} print_environment.py \
+# RUN: | FileCheck --check-prefix=CHECK-ENV-MULTIPLE %s
+
+# CHECK-ENV-1: A_FOO = 999
+# CHECK-ENV-1-NOT: FOO
+
+# CHECK-ENV-MULTIPLE: A_FOO = 1
+# CHECK-ENV-MULTIPLE-NOT: BAR
+# CHECK-ENV-MULTIPLE: B_BAR = 2
+# CHECK-ENV-MULTIPLE: C_OOF = 3
+# CHECK-ENV-MULTIPLE-NOT: FOO
--- /dev/null
+#!/usr/bin/env python
+
+import os
+
+sorted_environment = sorted(os.environ.items())
+
+for name,value in sorted_environment:
+ print name,'=',value
--- /dev/null
+# Check the env command
+#
+# RUN: %{lit} -a -v %{inputs}/shtest-env