Skip to content

Commit

Permalink
tests: simplify environment handling for subprocesses
Browse files Browse the repository at this point in the history
  • Loading branch information
amezin committed Jan 4, 2025
1 parent cc38bfb commit dfecef3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
29 changes: 18 additions & 11 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

IMAGES_STASH_KEY = pytest.StashKey[list]()

ENV_CLEANUP = [
ENV_FILTER_OUT = [
'DBUS_SESSION_BUS_ADDRESS',
'DBUS_SESSION_BUS_PID',
'DBUS_SESSION_BUS_WINDOWID',
Expand Down Expand Up @@ -271,7 +271,7 @@ def container(tmp_path_factory, request):
'systemd.journald.forward_to_console=0',
])

launcher = procutil.Launcher(os.environ)
launcher = procutil.Launcher()

with contextlib.ExitStack() as stack:
container_lock = request.getfixturevalue('container_lock')
Expand Down Expand Up @@ -360,18 +360,25 @@ def shutdown():

@pytest.fixture(scope='session')
def process_launcher(container):
if container is not None:
return procutil.ContainerExecLauncher(
container_id=container,
user=os.getuid(),
)
if container is None:
return procutil.Launcher()

return procutil.ContainerExecLauncher(
container_id=container,
user=os.getuid(),
)

base_env = dict(os.environ)

for var in ENV_CLEANUP:
base_env.pop(var, None)
@pytest.fixture(scope='session')
def base_environment(container):
if container is not None:
return {}

return procutil.Launcher(base_env)
return {
k: v
for k, v in os.environ.items()
if k not in ENV_FILTER_OUT
}


@pytest.fixture(scope='session')
Expand Down
18 changes: 10 additions & 8 deletions test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,22 @@ def gnome_data_dir(self, xdg_data_home):
@pytest.fixture(scope='class')
def base_environment(
self,
base_environment,
xdg_runtime_dir,
xdg_config_home,
xdg_cache_home,
xdg_state_home,
request,
):
env = {
'XDG_RUNTIME_DIR': str(xdg_runtime_dir),
'XDG_CONFIG_HOME': str(xdg_config_home),
'XDG_CACHE_HOME': str(xdg_cache_home),
'XDG_STATE_HOME': str(xdg_state_home),
'NO_AT_BRIDGE': '1',
'GTK_A11Y': 'none',
}
env = dict(
base_environment,
XDG_RUNTIME_DIR=str(xdg_runtime_dir),
XDG_CONFIG_HOME=str(xdg_config_home),
XDG_CACHE_HOME=str(xdg_cache_home),
XDG_STATE_HOME=str(xdg_state_home),
NO_AT_BRIDGE='1',
GTK_A11Y='none',
)

if not request.config.option.hw_accel:
env['LIBGL_ALWAYS_SOFTWARE'] = 'true'
Expand Down
8 changes: 0 additions & 8 deletions test/procutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@


class Launcher:
def __init__(self, base_env):
self.base_env = dict(base_env)

def tweak(self, args, kwargs):
if env := kwargs.pop('env', None):
new_env = dict(self.base_env)
new_env.update(env)
kwargs['env'] = new_env

return args, kwargs

def run(self, *args, **kwargs):
Expand Down

0 comments on commit dfecef3

Please sign in to comment.