Skip to content

Commit 827f5ac

Browse files
thorsten-kleinpdgendt
authored andcommitted
Improve test isolation
Extend and rename current autouse pytest fixture to `setup_teardown_test_environment`. The fixture ensures an isolated test environment. It creates a new temporary directory which is used as working directory. This ensures a clean start for each test and prevents tests from affecting another one through changes to the working directory. The fixture ensures that the user's actual configuration files are neither used nor touched during test, as WEST_CONFIG_* env variables are set, whereby no config files are created at these locations. The fixture sets ZEPHYR_BASE (to avoid complaints in subcommand stderr), but to a spurious location (so that attempts to read from inside of it are caught here). The fixture also ensures that any environment modifications made by a test do not leak into subsequent tests, as the environment is restored when the `update_env` with-block exits.
1 parent bb1c7bf commit 827f5ac

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

tests/conftest.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,43 @@ def _check_git_capabilities(tmpdir_factory):
158158

159159

160160
@pytest.fixture(autouse=True)
161-
def independent_global_and_system_config(tmpdir_factory):
162-
# Fixture to ensure that the user's actual configuration
163-
# files are neither used nor touched during test.
164-
#
165-
# We also set ZEPHYR_BASE (to avoid complaints in subcommand
166-
# stderr), but to a spurious location (so that attempts to read
167-
# from inside of it are caught here).
168-
#
161+
def setup_teardown_test_environment(tmpdir_factory):
162+
"""
163+
The fixture ensures an isolated test environment.
164+
165+
It creates a new temporary directory which is used as working directory.
166+
This ensures a clean start for each test and prevents tests from affecting
167+
another one through changes to the working directory.
168+
169+
The fixture ensures that the user's actual configuration files are neither
170+
used nor touched during test, as WEST_CONFIG_* env variables are set,
171+
whereby no config files are created at these locations.
172+
173+
The fixture sets ZEPHYR_BASE (to avoid complaints in subcommand stderr),
174+
but to a spurious location (so that attempts to read from inside of it are
175+
caught here).
176+
177+
The fixture also ensures that any environment modifications made by a test
178+
do not leak into subsequent tests, as the environment is restored when the
179+
`update_env` with-block exits.
180+
"""
169181
tmpdir = Path(tmpdir_factory.mktemp("test-configs"))
182+
tmp_cwd = tmpdir_factory.mktemp("tmp-cwd")
170183

171184
# config paths
172185
system = tmpdir / 'config.system'
173186
glbl = tmpdir / 'config.global'
174187

175188
# run with environment variables set
176-
with update_env({
177-
'WEST_CONFIG_SYSTEM': str(system),
178-
'WEST_CONFIG_GLOBAL': str(glbl),
179-
'WEST_CONFIG_LOCAL': None,
180-
'ZEPHYR_BASE': str(tmpdir / 'no-zephyr-here'),
181-
}):
189+
with (
190+
chdir(tmp_cwd),
191+
update_env({
192+
'WEST_CONFIG_SYSTEM': str(system),
193+
'WEST_CONFIG_GLOBAL': str(glbl),
194+
'WEST_CONFIG_LOCAL': None,
195+
'ZEPHYR_BASE': str(tmpdir / 'no-zephyr-here'),
196+
}),
197+
):
182198
yield
183199

184200

0 commit comments

Comments
 (0)