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