]> arthur.barton.de Git - bup.git/blob - conftest.py
tests: partially convert to pytest
[bup.git] / conftest.py
1
2 from __future__ import absolute_import
3 from os.path import basename, dirname, realpath
4 from time import tzset
5 from traceback import extract_stack
6 import os
7 import pytest
8 import subprocess
9 import sys
10
11 sys.path[:0] = ['lib']
12
13 from bup import helpers
14 from bup.compat import environ, fsencode
15
16
17 _bup_src_top = realpath(dirname(fsencode(__file__)))
18
19 # The "pwd -P" here may not be appropriate in the long run, but we
20 # need it until we settle the relevant drecurse/exclusion questions:
21 # https://groups.google.com/forum/#!topic/bup-list/9ke-Mbp10Q0
22 os.chdir(realpath(os.getcwd()))
23
24 @pytest.fixture(autouse=True)
25 def no_lingering_errors():
26     def fail_if_errors():
27         if helpers.saved_errors:
28             bt = extract_stack()
29             src_file, src_line, src_func, src_txt = bt[-4]
30             msg = 'saved_errors ' + repr(helpers.saved_errors)
31             assert False, '%s:%-4d %s' % (basename(src_file),
32                                           src_line, msg)
33
34     fail_if_errors()
35     helpers.clear_errors()
36     yield None
37     fail_if_errors()
38     helpers.clear_errors()
39
40 @pytest.fixture(autouse=True)
41 def ephemeral_env_changes():
42     orig_env = environ.copy()
43     yield None
44     for k, orig_v in orig_env.items():
45         v = environ.get(k)
46         if v is not orig_v:
47             environ[k] = orig_v
48             if k == b'TZ':
49                 tzset()
50     for k in environ.keys():
51         if k not in orig_env:
52             del environ[k]
53             if k == b'TZ':
54                 tzset()
55     os.chdir(_bup_src_top)
56
57 @pytest.fixture()
58 def tmpdir(tmp_path):
59     try:
60         yield bytes(tmp_path)
61     finally:
62         subprocess.call([b'chmod', b'-R', b'u+rwX', bytes(tmp_path)])
63         # FIXME: delete only if there are no errors
64         #subprocess.call(['rm', '-rf', tmpdir])