From ed481c54a523d52dfd575c5f4065c5ddfdab8146 Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Sun, 1 Nov 2020 14:03:41 -0600 Subject: [PATCH] Add pytest context manager to save/restore test environment Without this, test_git.py could fail because test_ftp had left its GIT_DIR lying around, and other tests could fail because it changed and didn't restore the working directory. Make sure to call tzset() after any TZ changes. Signed-off-by: Rob Browning Tested-by: Rob Browning --- HACKING | 5 +++++ conftest.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 conftest.py diff --git a/HACKING b/HACKING index 773f187..96747f4 100644 --- a/HACKING +++ b/HACKING @@ -79,6 +79,11 @@ and external tests that test bup from the outside, typically by running the executable, are located in test/ext. +Some aspects of the environment are automatically restored after each +test via fixtures in conftest.py, including the state of the +environment variables and the working directory; the latter is reset +to the top of the source tree. + Submitting patches ================== diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..75a39c3 --- /dev/null +++ b/conftest.py @@ -0,0 +1,31 @@ + +from os.path import dirname, realpath +from time import tzset +import os +import pytest +import sys + +sys.path[:0] = ['lib'] + +from bup import helpers +from bup.compat import environ, fsencode + + +_bup_src_top = realpath(dirname(fsencode(__file__))) + +@pytest.fixture(autouse=True) +def ephemeral_env_changes(): + orig_env = environ.copy() + yield None + for k, orig_v in orig_env.items(): + v = environ.get(k) + if v is not orig_v: + environ[k] = orig_v + if k == b'TZ': + tzset() + for k in environ.keys(): + if k not in orig_env: + del environ[k] + if k == b'TZ': + tzset() + os.chdir(_bup_src_top) -- 2.39.2