]> arthur.barton.de Git - bup.git/blob - test/ext/conftest.py
Migrate the remaining (standalone) test/ext tests to pytest
[bup.git] / test / ext / conftest.py
1
2 from subprocess import CalledProcessError
3 import pytest, subprocess, sys
4
5 from bup.compat import fsdecode
6 from bup.io import byte_stream
7
8 # Handle all test-* files as wvtest protocol subprocesses
9 # cf. https://docs.pytest.org/en/latest/example/nonpython.html
10
11 class BupSubprocFailure(Exception):
12     def __init__(self, msg, cmd, status, failures):
13         super(BupSubprocFailure, self).__init__(msg)
14         self.cmd = cmd
15         self.status = status
16         self.failures = failures
17
18 class BupSubprocTestRunner(pytest.Item):
19
20     def __init__(self, name, parent):
21         super(BupSubprocTestRunner, self).__init__(name, parent)
22
23     def runtest(self):
24         cmd = str(self.fspath)
25         p = subprocess.Popen(cmd,
26                              stdout=subprocess.PIPE,
27                              stderr=subprocess.STDOUT)
28         out = p.communicate()[0]
29         sys.stdout.flush()
30         byte_stream(sys.stdout).write(out)
31         failures = [line for line in out.splitlines()
32                     if (line.startswith(b'!')
33                         and line.lower().endswith(b' failed'))]
34         if failures or p.returncode != 0:
35             raise BupSubprocFailure('%s failed (exit %d, %d failures)'
36                                     % (cmd, p.returncode, len(failures)),
37                                     cmd, p.returncode, failures)
38
39     def repr_failure(self, excinfo):
40         ex = excinfo.value
41         if isinstance(ex, BupSubprocFailure):
42             msg = ['Exit status: %d' % ex.status,
43                    'Failures:']
44             msg.extend(fsdecode(s) for s in ex.failures)
45             return '\n'.join(msg)
46
47     def reportinfo(self):
48         # This does not appear to be documented, but is in the
49         # example, and sets the final report header line (at least)
50         # for failures.
51         test_name = str(self.fspath)
52         linenum = None
53         return self.fspath, linenum, test_name
54
55 class BupSubprocTestFile(pytest.File):
56     def collect(self):
57         name = self.fspath.basename
58         # name='' because there's only one test: running the command.
59         # i.e there are no sub-tests.  Otherwise the status messages
60         # duplicate the test name like this:
61         #   test/ext/test-cat-file.sh::test-cat-file.sh PASSED ...
62         try:
63             yield BupSubprocTestRunner.from_parent(self, name='')
64         except AttributeError:
65             yield BupSubprocTestRunner('', self)
66
67 def pytest_collect_file(parent, path):
68     base = path.basename
69     if base.startswith('test-') and not base.endswith('~'):
70         try:
71             item = BupSubprocTestFile.from_parent(parent, fspath=path)
72         except AttributeError:
73             item = BupSubprocTestFile(path, parent)
74         if base == 'test-release-archive.sh':
75             item.add_marker(pytest.mark.release)
76         return item