]> arthur.barton.de Git - bup.git/commitdiff
Add pytest context manager to save/restore test environment
authorRob Browning <rlb@defaultvalue.org>
Sun, 1 Nov 2020 20:03:41 +0000 (14:03 -0600)
committerRob Browning <rlb@defaultvalue.org>
Thu, 26 Nov 2020 21:53:09 +0000 (15:53 -0600)
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 <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
HACKING
conftest.py [new file with mode: 0644]

diff --git a/HACKING b/HACKING
index 773f187ee4e037735e91fdf1474eebba74051d8f..96747f4c0a78df07eca72c546b49a61ee6ae5677 100644 (file)
--- 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 (file)
index 0000000..75a39c3
--- /dev/null
@@ -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)