OSDN Git Service

[lit] support unsetting env variables (again!)
authorBen Dunbobbin <bd1976llvm@gmail.com>
Fri, 18 Aug 2017 17:32:57 +0000 (17:32 +0000)
committerBen Dunbobbin <bd1976llvm@gmail.com>
Fri, 18 Aug 2017 17:32:57 +0000 (17:32 +0000)
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

utils/lit/lit/TestRunner.py
utils/lit/tests/Inputs/shtest-env/env-u.txt [new file with mode: 0644]
utils/lit/tests/Inputs/shtest-env/env.txt [new file with mode: 0644]
utils/lit/tests/Inputs/shtest-env/lit.cfg [new file with mode: 0644]
utils/lit/tests/Inputs/shtest-env/mixed.txt [new file with mode: 0644]
utils/lit/tests/Inputs/shtest-env/print_environment.py [new file with mode: 0644]
utils/lit/tests/Inputs/shtest-env/shtest-env.py [new file with mode: 0644]

index c2fbb84..dc56ad6 100644 (file)
@@ -217,7 +217,20 @@ def quote_windows_command(seq):
 # 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.
diff --git a/utils/lit/tests/Inputs/shtest-env/env-u.txt b/utils/lit/tests/Inputs/shtest-env/env-u.txt
new file mode 100644 (file)
index 0000000..9cdf9d0
--- /dev/null
@@ -0,0 +1,23 @@
+# 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
diff --git a/utils/lit/tests/Inputs/shtest-env/env.txt b/utils/lit/tests/Inputs/shtest-env/env.txt
new file mode 100644 (file)
index 0000000..aa697b0
--- /dev/null
@@ -0,0 +1,15 @@
+# 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
diff --git a/utils/lit/tests/Inputs/shtest-env/lit.cfg b/utils/lit/tests/Inputs/shtest-env/lit.cfg
new file mode 100644 (file)
index 0000000..23ef60a
--- /dev/null
@@ -0,0 +1,9 @@
+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))
diff --git a/utils/lit/tests/Inputs/shtest-env/mixed.txt b/utils/lit/tests/Inputs/shtest-env/mixed.txt
new file mode 100644 (file)
index 0000000..be32d45
--- /dev/null
@@ -0,0 +1,18 @@
+# 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
diff --git a/utils/lit/tests/Inputs/shtest-env/print_environment.py b/utils/lit/tests/Inputs/shtest-env/print_environment.py
new file mode 100644 (file)
index 0000000..1add407
--- /dev/null
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+
+import os
+
+sorted_environment = sorted(os.environ.items())
+
+for name,value in sorted_environment:
+    print name,'=',value
diff --git a/utils/lit/tests/Inputs/shtest-env/shtest-env.py b/utils/lit/tests/Inputs/shtest-env/shtest-env.py
new file mode 100644 (file)
index 0000000..fc5f973
--- /dev/null
@@ -0,0 +1,3 @@
+# Check the env command
+#
+# RUN: %{lit} -a -v %{inputs}/shtest-env