Skip to content

Commit 38e9f3a

Browse files
authored
Sandbox environment in tests to fix repeated job failures (#609)
Signed-off-by: Shane Loretz <[email protected]>
1 parent 40cf5df commit 38e9f3a

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

launch/test/launch/actions/test_append_environment_variable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
from launch.substitutions import EnvironmentVariable
2222
from launch.substitutions import TextSubstitution
2323

24+
from temporary_environment import sandbox_environment_variables
2425

26+
27+
@sandbox_environment_variables
2528
def test_append_environment_variable_constructor():
2629
"""Test the constructor for the AppendEnvironmentVariable class."""
2730
AppendEnvironmentVariable('name', 'value')
@@ -31,6 +34,7 @@ def test_append_environment_variable_constructor():
3134
AppendEnvironmentVariable('name', 'value', prepend=True, separator='|')
3235

3336

37+
@sandbox_environment_variables
3438
def test_append_environment_variable_execute():
3539
"""Test the execute() of the AppendEnvironmentVariable class."""
3640
context = LaunchContext()

launch/test/launch/actions/test_group_action.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from launch.actions import SetLaunchConfiguration
2727
from launch.substitutions import LaunchConfiguration
2828

29+
from temporary_environment import sandbox_environment_variables
30+
2931

3032
def test_group_action_constructors():
3133
"""Test the constructors for the GroupAction class."""
@@ -36,6 +38,7 @@ def test_group_action_constructors():
3638
GroupAction([Action()], scoped=False, forwarding=False, launch_configurations={'foo': 'FOO'})
3739

3840

41+
@sandbox_environment_variables
3942
def test_group_action_execute():
4043
"""Test the execute() of the the GroupAction class."""
4144
lc1 = LaunchContext()

launch/test/launch/actions/test_set_and_unset_environment_variable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
from launch.actions import UnsetEnvironmentVariable
2020
from launch.substitutions import EnvironmentVariable
2121

22+
from temporary_environment import sandbox_environment_variables
2223

24+
25+
@sandbox_environment_variables
2326
def test_set_and_unset_environment_variable_constructors():
2427
"""Test the constructor for SetEnvironmentVariable and UnsetEnvironmentVariable classes."""
2528
SetEnvironmentVariable('name', 'value')
2629
UnsetEnvironmentVariable('name')
2730

2831

32+
@sandbox_environment_variables
2933
def test_set_and_unset_environment_variable_execute():
3034
"""Test the execute() of the SetEnvironmentVariable and UnsetEnvironmentVariable classes."""
3135
context = LaunchContext()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2022 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import functools
16+
import os
17+
18+
19+
class TemporaryEnvironment:
20+
"""Allow temporary changes to environment variables."""
21+
22+
def __init__(self):
23+
self.__old_env = None
24+
25+
def __enter__(self):
26+
self.__old_env = os.environ.copy()
27+
return self
28+
29+
def __exit__(self, t, v, tb):
30+
# Update keys manually to make sure putenv gets called
31+
for key, value in self.__old_env.items():
32+
os.environ[key] = value
33+
# Remove added keys
34+
env_keys = tuple(os.environ)
35+
for key in env_keys:
36+
if key not in self.__old_env:
37+
del os.environ[key]
38+
self.__old_env = None
39+
40+
41+
def sandbox_environment_variables(func):
42+
"""Decorate a function to give it a temporary environment."""
43+
44+
@functools.wraps(func)
45+
def wrapper_func(*args, **kwargs):
46+
with TemporaryEnvironment():
47+
func(*args, **kwargs)
48+
49+
return wrapper_func

0 commit comments

Comments
 (0)