2 from __future__ import absolute_import, print_function
3 from os.path import basename, dirname, realpath, relpath
5 from traceback import extract_stack
14 sys.path[:0] = ['lib']
16 from bup import helpers
17 from bup.compat import environ, fsencode
20 _bup_src_top = realpath(dirname(fsencode(__file__)))
22 # The "pwd -P" here may not be appropriate in the long run, but we
23 # need it until we settle the relevant drecurse/exclusion questions:
24 # https://groups.google.com/forum/#!topic/bup-list/9ke-Mbp10Q0
25 os.chdir(realpath(os.getcwd()))
27 # Make the test results available to fixtures
28 @pytest.hookimpl(tryfirst=True, hookwrapper=True)
29 def pytest_runtest_makereport(item, call):
31 report = other_hooks.get_result()
32 bup = item.__dict__.setdefault('bup', {})
33 bup[report.when + '-report'] = report # setup, call, teardown
36 def bup_test_sort_order(item):
37 # Pull some slower tests forward to speed parallel runs
38 if item.fspath.basename in ('test_get.py', 'test-index.sh'):
39 return (0, str(item.fspath))
40 return (1, str(item.fspath))
42 def pytest_collection_modifyitems(session, config, items):
43 items.sort(key=bup_test_sort_order)
45 @pytest.fixture(autouse=True)
46 def no_lingering_errors():
48 if helpers.saved_errors:
50 src_file, src_line, src_func, src_txt = bt[-4]
51 msg = 'saved_errors ' + repr(helpers.saved_errors)
52 assert False, '%s:%-4d %s' % (basename(src_file),
56 helpers.clear_errors()
59 helpers.clear_errors()
61 @pytest.fixture(autouse=True)
62 def ephemeral_env_changes():
63 orig_env = environ.copy()
65 for k, orig_v in orig_env.items():
71 for k in environ.keys():
76 os.chdir(_bup_src_top)
78 # Assumes (of course) this file is at the top-level of the source tree
79 _bup_test_dir = realpath(dirname(fsencode(__file__))) + b'/test'
80 _bup_tmp = _bup_test_dir + b'/tmp'
84 if e.errno != errno.EEXIST:
87 _safe_path_rx = re.compile(br'[^a-zA-Z0-9_-]')
91 if sys.version_info[0] > 2:
92 rp = realpath(fsencode(request.fspath))
94 rp = realpath(str(request.fspath))
95 rp = relpath(rp, _bup_test_dir)
97 rp += b'-' + fsencode(request.function.__name__)
98 safe = _safe_path_rx.sub(b'-', rp)
99 tmpdir = tempfile.mkdtemp(dir=_bup_tmp, prefix=safe)
101 if request.node.bup['call-report'].failed:
102 print('\nPreserving:', b'test/' + relpath(tmpdir, _bup_test_dir),
105 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
106 subprocess.call(['rm', '-rf', tmpdir])