1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 18:42:46 +02:00

[lit] support unsetting env variables (again!)

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

llvm-svn: 311180
This commit is contained in:
Ben Dunbobbin 2017-08-18 17:32:57 +00:00
parent eec800fc3a
commit d80ce7788d
7 changed files with 89 additions and 0 deletions

View 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.

View File

@ -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

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,3 @@
# Check the env command
#
# RUN: %{lit} -a -v %{inputs}/shtest-env