Skip to content

Commit fb333e0

Browse files
authored
Merge pull request #48 from nicoddemus/pre-commit
2 parents 9223211 + abb2eec commit fb333e0

43 files changed

Lines changed: 560 additions & 453 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
exclude: '^($|.*\.bin)'
2+
repos:
3+
- repo: https://github.com/ambv/black
4+
rev: 19.10b0
5+
hooks:
6+
- id: black
7+
args: [--safe, --quiet]
8+
language_version: python3.7
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v2.5.0
11+
hooks:
12+
- id: trailing-whitespace
13+
- id: end-of-file-fixer
14+
- repo: local
15+
hooks:
16+
- id: rst
17+
name: rst
18+
entry: rst-lint --encoding utf-8
19+
files: README.rst
20+
language: python
21+
additional_dependencies: [pygments, restructuredtext_lint]

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
python: '3.8'
1515
- env: TOXENV=py38-pytest53
1616
python: '3.8'
17+
- env: TOXENV=linting
18+
python: '3.7'
1719
- stage: deploy
1820
python: '3.8'
1921
install: pip install -U setuptools setuptools_scm

CHANGELOG.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# 1.1.0
1616

17-
- New `cpp_arguments` ini option allows customization of the command-line
17+
- New `cpp_arguments` ini option allows customization of the command-line
1818
used to run tests, for example to enable logging and verbosity.
1919
Thanks @elkin for the PR.
2020

@@ -50,22 +50,22 @@
5050
# 0.4.0 #
5151

5252
- Integrated most of the examples found in boost-1.55 for Boost Test into the
53-
`pytest-cpp` test suite. This uncovered a problem when Fatal Errors were
54-
produced and which would break py.test.
55-
53+
`pytest-cpp` test suite. This uncovered a problem when Fatal Errors were
54+
produced and which would break py.test.
55+
5656
- Integrated all the official Google Test examples into the `pytest-cpp` test
57-
suite, ensuring that Google Test is fully covered.
58-
59-
- Fixed #17: supporting Type-Parametrized tests in Google Test. Thanks
60-
@joarbe for the report.
57+
suite, ensuring that Google Test is fully covered.
58+
59+
- Fixed #17: supporting Type-Parametrized tests in Google Test. Thanks
60+
@joarbe for the report.
6161

62-
- Now any executable passed explicitly to py.test in the
62+
- Now any executable passed explicitly to py.test in the
6363
command-line will run as a test, regardless of the `cpp_files` option.
6464

6565
# 0.3.1 #
6666

67-
- Now capturing standard error while collecting tests, otherwise
68-
executable files that are not test suites could write into `stderr`,
67+
- Now capturing standard error while collecting tests, otherwise
68+
executable files that are not test suites could write into `stderr`,
6969
which would mess with `py.test`'s output to console.
7070

7171
# 0.3.0 #
@@ -76,7 +76,7 @@ which would mess with `py.test`'s output to console.
7676
# 0.2.0 #
7777

7878
- `cpp_files` option to `pytest.ini` files.
79-
- `Boost::Test` suites now writes reports to a file, which makes
79+
- `Boost::Test` suites now writes reports to a file, which makes
8080
it behave correctly if a test writes in `std::cout` or `std::cerr`.
8181

8282
# 0.1.0 #

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
include LICENSE
2-
include README.md
2+
include README.md

README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pytest-cpp
44

55
Use `pytest <https://pypi.python.org/pypi/pytest>`_ runner to discover and execute C++ tests.
66

7-
|python| |version| |anaconda| |ci| |coverage|
7+
|python| |version| |anaconda| |ci| |coverage| |black|
88

99
Supports both `Google Test <https://code.google.com/p/googletest>`_ and
1010
`Boost::Test <http://www.boost.org/doc/libs/release/libs/test>`_:
@@ -27,6 +27,9 @@ Supports both `Google Test <https://code.google.com/p/googletest>`_ and
2727
:target: https://pypi.python.org/pypi/pytest-cpp/
2828
:alt: Supported Python versions
2929

30+
.. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg
31+
:target: https://github.com/psf/black
32+
3033
This brings several benefits:
3134

3235
* Allows you to run all your tests in multi-language projects with a single

pytest_cpp/boost.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@ class BoostTestFacade(object):
1515
@classmethod
1616
def is_test_suite(cls, executable):
1717
try:
18-
output = subprocess.check_output([executable, '--help'],
19-
stderr=subprocess.STDOUT,
20-
universal_newlines=True)
18+
output = subprocess.check_output(
19+
[executable, "--help"],
20+
stderr=subprocess.STDOUT,
21+
universal_newlines=True,
22+
)
2123
except (subprocess.CalledProcessError, OSError):
2224
return False
2325
else:
24-
return '--output_format' in output and 'log_format' in output
26+
return "--output_format" in output and "log_format" in output
2527

2628
def list_tests(self, executable):
2729
# unfortunately boost doesn't provide us with a way to list the tests
2830
# inside the executable, so the test_id is a dummy placeholder :(
2931
return [os.path.basename(os.path.splitext(executable)[0])]
3032

3133
def run_test(self, executable, test_id, test_args=()):
32-
3334
def read_file(name):
3435
try:
3536
with io.open(name) as f:
@@ -38,13 +39,13 @@ def read_file(name):
3839
return None
3940

4041
temp_dir = tempfile.mkdtemp()
41-
log_xml = os.path.join(temp_dir, 'log.xml')
42-
report_xml = os.path.join(temp_dir, 'report.xml')
42+
log_xml = os.path.join(temp_dir, "log.xml")
43+
report_xml = os.path.join(temp_dir, "report.xml")
4344
args = [
4445
executable,
45-
'--output_format=XML',
46-
'--log_sink=%s' % log_xml,
47-
'--report_sink=%s' % report_xml,
46+
"--output_format=XML",
47+
"--log_sink=%s" % log_xml,
48+
"--report_sink=%s" % report_xml,
4849
]
4950
args.extend(test_args)
5051
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
@@ -54,27 +55,33 @@ def read_file(name):
5455
report = read_file(report_xml)
5556

5657
if p.returncode not in (0, 200, 201):
57-
msg = ('Internal Error: calling {executable} '
58-
'for test {test_id} failed (returncode={returncode}):\n'
59-
'output:{stdout}\n'
60-
'log:{log}\n'
61-
'report:{report}')
58+
msg = (
59+
"Internal Error: calling {executable} "
60+
"for test {test_id} failed (returncode={returncode}):\n"
61+
"output:{stdout}\n"
62+
"log:{log}\n"
63+
"report:{report}"
64+
)
6265
failure = BoostTestFailure(
63-
'<no source file>',
66+
"<no source file>",
6467
linenum=0,
65-
contents=msg.format(executable=executable,
66-
test_id=test_id,
67-
stdout=stdout,
68-
log=log,
69-
report=report,
70-
returncode=p.returncode))
68+
contents=msg.format(
69+
executable=executable,
70+
test_id=test_id,
71+
stdout=stdout,
72+
log=log,
73+
report=report,
74+
returncode=p.returncode,
75+
),
76+
)
7177
return [failure]
7278

7379
if report is not None and (
74-
report.startswith('Boost.Test framework internal error: ') or
75-
report.startswith('Test setup error: ')):
80+
report.startswith("Boost.Test framework internal error: ")
81+
or report.startswith("Test setup error: ")
82+
):
7683
# boost.test doesn't do XML output on fatal-enough errors.
77-
failure = BoostTestFailure('unknown location', 0, report)
84+
failure = BoostTestFailure("unknown location", 0, report)
7885
return [failure]
7986

8087
results = self._parse_log(log=log)
@@ -93,22 +100,22 @@ def _parse_log(self, log):
93100
# <FatalError>...</FatalError><TestLog>...</TestLog>
94101
# so we have to manually split it into two xmls if that's the case.
95102
parsed_elements = []
96-
if log.startswith('<FatalError'):
97-
fatal, log = log.split('</FatalError>')
98-
fatal += '</FatalError>' # put it back, removed by split()
103+
if log.startswith("<FatalError"):
104+
fatal, log = log.split("</FatalError>")
105+
fatal += "</FatalError>" # put it back, removed by split()
99106
fatal_root = ElementTree.fromstring(fatal)
100-
fatal_root.text = 'Fatal Error: %s' % fatal_root.text
107+
fatal_root.text = "Fatal Error: %s" % fatal_root.text
101108
parsed_elements.append(fatal_root)
102109

103110
log_root = ElementTree.fromstring(log)
104-
parsed_elements.extend(log_root.findall('Exception'))
105-
parsed_elements.extend(log_root.findall('Error'))
106-
parsed_elements.extend(log_root.findall('FatalError'))
111+
parsed_elements.extend(log_root.findall("Exception"))
112+
parsed_elements.extend(log_root.findall("Error"))
113+
parsed_elements.extend(log_root.findall("FatalError"))
107114

108115
result = []
109116
for elem in parsed_elements:
110-
filename = elem.attrib['file']
111-
linenum = int(elem.attrib['line'])
117+
filename = elem.attrib["file"]
118+
linenum = int(elem.attrib["line"])
112119
result.append(BoostTestFailure(filename, linenum, elem.text))
113120
return result
114121

@@ -120,7 +127,7 @@ def __init__(self, filename, linenum, contents):
120127
self.lines = contents.splitlines()
121128

122129
def get_lines(self):
123-
m = ('red', 'bold')
130+
m = ("red", "bold")
124131
return [(x, m) for x in self.lines]
125132

126133
def get_file_reference(self):

pytest_cpp/error.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CppFailureError(Exception):
1010
1111
Contains a list of `CppFailure` instances.
1212
"""
13+
1314
def __init__(self, failures):
1415
self.failures = failures
1516

@@ -45,22 +46,23 @@ class CppFailureRepr(object):
4546
"repr" object for pytest that knows how to print a CppFailure instance
4647
into both terminal and files.
4748
"""
48-
failure_sep = '---'
49+
50+
failure_sep = "---"
4951

5052
def __init__(self, failures):
5153
self.failures = failures
5254

5355
def __str__(self):
5456
reprs = []
5557
for failure in self.failures:
56-
pure_lines = '\n'.join(x[0] for x in failure.get_lines())
58+
pure_lines = "\n".join(x[0] for x in failure.get_lines())
5759
repr_loc = self._get_repr_file_location(failure)
5860
reprs.append("%s\n%s" % (pure_lines, repr_loc))
5961
return self.failure_sep.join(reprs)
6062

6163
def _get_repr_file_location(self, failure):
6264
filename, linenum = failure.get_file_reference()
63-
return ReprFileLocation(filename, linenum, 'C++ failure')
65+
return ReprFileLocation(filename, linenum, "C++ failure")
6466

6567
def toterminal(self, tw):
6668
for index, failure in enumerate(self.failures):
@@ -69,7 +71,7 @@ def toterminal(self, tw):
6971
for line in code_lines:
7072
tw.line(line, white=True, bold=True) # pragma: no cover
7173

72-
indent = get_left_whitespace(code_lines[-1]) if code_lines else ''
74+
indent = get_left_whitespace(code_lines[-1]) if code_lines else ""
7375

7476
for line, markup in failure.get_lines():
7577
markup_params = {m: True for m in markup}
@@ -92,18 +94,15 @@ def get_code_context_around_line(filename, linenum):
9294
with open(filename) as f:
9395
index_above = index - 2
9496
index_above = index_above if index_above >= 0 else 0
95-
return [x.rstrip() for x in f.readlines()[index_above:index + 1]]
97+
return [x.rstrip() for x in f.readlines()[index_above : index + 1]]
9698
return []
9799

98100

99101
def get_left_whitespace(line):
100-
result = ''
102+
result = ""
101103
for c in line:
102104
if c in string.whitespace:
103105
result += c
104106
else:
105107
break
106108
return result
107-
108-
109-

0 commit comments

Comments
 (0)