]> arthur.barton.de Git - bup.git/blob - conftest.py
0507a79626e6ae544c6b6624b96e0c0625589848
[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 # Make the test results available to fixtures
25 @pytest.hookimpl(tryfirst=True, hookwrapper=True)
26 def pytest_runtest_makereport(item, call):
27     other_hooks = yield
28     report = other_hooks.get_result()
29     bup = item.__dict__.setdefault('bup', {})
30     bup[report.when + '_report'] = report  # setup, call, teardown
31     item.bup = bup
32
33 def bup_test_sort_order(item):
34     # Pull some slower tests forward to speed parallel runs
35     if item.fspath.basename in ('test_get.py', 'test-index.sh'):
36         return (0, str(item.fspath))
37     return (1, str(item.fspath))
38
39 def pytest_collection_modifyitems(session, config, items):
40     items.sort(key=bup_test_sort_order)
41
42 @pytest.fixture(autouse=True)
43 def no_lingering_errors():
44     def fail_if_errors():
45         if helpers.saved_errors:
46             bt = extract_stack()
47             src_file, src_line, src_func, src_txt = bt[-4]
48             msg = 'saved_errors ' + repr(helpers.saved_errors)
49             assert False, '%s:%-4d %s' % (basename(src_file),
50                                           src_line, msg)
51
52     fail_if_errors()
53     helpers.clear_errors()
54     yield None
55     fail_if_errors()
56     helpers.clear_errors()
57
58 @pytest.fixture(autouse=True)
59 def ephemeral_env_changes():
60     orig_env = environ.copy()
61     yield None
62     for k, orig_v in orig_env.items():
63         v = environ.get(k)
64         if v is not orig_v:
65             environ[k] = orig_v
66             if k == b'TZ':
67                 tzset()
68     for k in environ.keys():
69         if k not in orig_env:
70             del environ[k]
71             if k == b'TZ':
72                 tzset()
73     os.chdir(_bup_src_top)
74
75 @pytest.fixture()
76 def tmpdir(tmp_path):
77     try:
78         yield bytes(tmp_path)
79     finally:
80         subprocess.call([b'chmod', b'-R', b'u+rwX', bytes(tmp_path)])
81         # FIXME: delete only if there are no errors
82         #subprocess.call(['rm', '-rf', tmpdir])