Skip to content

Commit 5146877

Browse files
committed
Fix warnings for pytest 7
- Use apt-get update to install boost. - Replace 'tmpdir' by 'tmp_path'. Close #78
1 parent fd62aaf commit 5146877

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
sudo make install
3030
- name: Install Boost.Test
3131
run: |
32+
sudo apt-get update
3233
sudo apt-get install libboost-test-dev valgrind
3334
- name: Compile
3435
run: |

src/pytest_cpp/plugin.py

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import stat
33
import sys
4+
from fnmatch import fnmatch
45

56
import pytest
67

@@ -23,12 +24,12 @@ def matches_any_mask(path, masks):
2324
"""Return True if the given path matches any of the masks given"""
2425
if sys.platform.startswith("win"):
2526
masks = [m + ".exe" for m in masks]
26-
return any(path.fnmatch(m) for m in masks)
27+
return any(fnmatch(path.name, m) for m in masks)
2728

2829

29-
def pytest_collect_file(parent, path):
30+
def pytest_collect_file(parent, file_path):
3031
try:
31-
is_executable = os.stat(str(path)).st_mode & stat.S_IXUSR
32+
is_executable = os.stat(str(file_path)).st_mode & stat.S_IXUSR
3233
except OSError:
3334
# in some situations the file might not be available anymore at this point
3435
is_executable = False
@@ -41,23 +42,30 @@ def pytest_collect_file(parent, path):
4142
cpp_ignore_py_files = config.getini("cpp_ignore_py_files")
4243

4344
# don't attempt to check *.py files even if they were given as explicit arguments
44-
if cpp_ignore_py_files and path.fnmatch("*.py"):
45+
if cpp_ignore_py_files and fnmatch(file_path.name, "*.py"):
4546
return
4647

47-
if not parent.session.isinitpath(path) and not matches_any_mask(path, masks):
48+
if not parent.session.isinitpath(file_path) and not matches_any_mask(
49+
file_path, masks
50+
):
4851
return
4952

5053
for facade_class in FACADES:
51-
if facade_class.is_test_suite(str(path)):
54+
if facade_class.is_test_suite(str(file_path)):
5255
if needs_from_parent:
5356
return CppFile.from_parent(
54-
fspath=path,
57+
path=file_path,
5558
parent=parent,
5659
facade=facade_class(),
5760
arguments=test_args,
5861
)
5962
else:
60-
return CppFile(path, parent, facade_class(), test_args)
63+
return CppFile(
64+
path=file_path,
65+
parent=parent,
66+
facade_class=facade_class(),
67+
arguments=test_args,
68+
)
6169

6270

6371
def pytest_addoption(parser):
@@ -94,16 +102,15 @@ def pytest_addoption(parser):
94102

95103

96104
class CppFile(pytest.File):
97-
def __init__(self, fspath, parent, facade, arguments):
98-
pytest.File.__init__(self, fspath, parent)
105+
def __init__(self, *, path, parent, facade, arguments, **kwargs):
106+
pytest.File.__init__(self, path=path, parent=parent, **kwargs)
99107
self.facade = facade
100108
self._arguments = arguments
101109

102110
@classmethod
103-
def from_parent(cls, parent, fspath, facade, arguments):
104-
# TODO: after dropping python 2, change to keyword only after 'parent'
111+
def from_parent(cls, *, parent, path, facade, arguments, **kwargs):
105112
return super().from_parent(
106-
parent=parent, fspath=fspath, facade=facade, arguments=arguments
113+
parent=parent, path=path, facade=facade, arguments=arguments
107114
)
108115

109116
def collect(self):
@@ -120,16 +127,15 @@ def collect(self):
120127

121128

122129
class CppItem(pytest.Item):
123-
def __init__(self, name, parent, facade, arguments):
124-
pytest.Item.__init__(self, name, parent)
130+
def __init__(self, *, name, parent, facade, arguments, **kwargs):
131+
pytest.Item.__init__(self, name, parent, **kwargs)
125132
self.facade = facade
126133
self._arguments = arguments
127134

128135
@classmethod
129-
def from_parent(cls, parent, name, facade, arguments):
130-
# TODO: after dropping python 2, change to keyword only after 'parent'
136+
def from_parent(cls, *, parent, name, facade, arguments, **kw):
131137
return super().from_parent(
132-
name=name, parent=parent, facade=facade, arguments=arguments
138+
name=name, parent=parent, facade=facade, arguments=arguments, **kw
133139
)
134140

135141
def runtest(self):

tests/test_pytest_cpp.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ def test_list_tests(facade, name, expected, exes):
7070
(Catch2Facade(), "catch2_success", "gtest"),
7171
],
7272
)
73-
def test_is_test_suite(facade, name, other_name, exes, tmpdir):
73+
def test_is_test_suite(facade, name, other_name, exes, tmp_path):
7474
assert facade.is_test_suite(exes.get(name))
7575
assert not facade.is_test_suite(exes.get(other_name))
76-
tmpdir.ensure("foo.txt")
77-
assert not facade.is_test_suite(str(tmpdir.join("foo.txt")))
76+
tmp_path.joinpath("foo.txt").touch()
77+
assert not facade.is_test_suite(str(tmp_path.joinpath("foo.txt")))
7878

7979

8080
@pytest.mark.parametrize(
@@ -228,7 +228,7 @@ def test_unknown_error(testdir, exes, mocker):
228228
assert "unknown error" in str(rep.longrepr)
229229

230230

231-
def test_google_internal_errors(mocker, testdir, exes, tmpdir):
231+
def test_google_internal_errors(mocker, testdir, exes, tmp_path):
232232
mocker.patch.object(GoogleTestFacade, "is_test_suite", return_value=True)
233233
mocker.patch.object(
234234
GoogleTestFacade, "list_tests", return_value=["FooTest.test_success"]
@@ -246,8 +246,8 @@ def raise_error(*args, **kwargs):
246246
assert "Internal Error: calling" in str(rep.longrepr)
247247

248248
mocked.side_effect = None
249-
xml_file = tmpdir.join("results.xml")
250-
xml_file.write("<empty/>")
249+
xml_file = tmp_path.joinpath("results.xml")
250+
xml_file.write_text("<empty/>")
251251
mocker.patch.object(
252252
GoogleTestFacade, "_get_temp_xml_filename", return_value=str(xml_file)
253253
)
@@ -496,7 +496,7 @@ def test_passing_files_directly_in_command_line(testdir, exes):
496496
result.stdout.fnmatch_lines(["*1 passed*"])
497497

498498

499-
def test_race_condition_on_collect(tmpdir):
499+
def test_race_condition_on_collect(tmp_path):
500500
"""
501501
Check that collection correctly handles when a path no longer is valid.
502502
@@ -510,24 +510,29 @@ def test_race_condition_on_collect(tmpdir):
510510
"""
511511
import pytest_cpp.plugin
512512

513-
assert pytest_cpp.plugin.pytest_collect_file(None, tmpdir / "invalid-file") is None
513+
assert (
514+
pytest_cpp.plugin.pytest_collect_file(None, tmp_path / "invalid-file") is None
515+
)
514516

515517

516-
def test_exe_mask_on_windows(tmpdir, monkeypatch):
518+
def test_exe_mask_on_windows(tmp_path, monkeypatch):
517519
"""
518520
Test for #45: C++ tests not collected due to '*_test' mask on Windows
519521
"""
520522
import pytest_cpp.plugin
521523

522524
monkeypatch.setattr(sys, "platform", "win32")
523525

524-
fn = tmpdir.join("generator_demo_test.exe").ensure(file=1)
526+
fn = tmp_path.joinpath("generator_demo_test.exe")
527+
fn.touch()
525528
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
526529

527-
fn = tmpdir.join("test_generator_demo.exe").ensure(file=1)
530+
fn = tmp_path.joinpath("test_generator_demo.exe")
531+
fn.touch()
528532
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
529533

530-
fn = tmpdir.join("my_generator_test_demo.exe").ensure(file=1)
534+
fn = tmp_path.joinpath("my_generator_test_demo.exe")
535+
fn.touch()
531536
assert not pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
532537

533538

@@ -591,9 +596,9 @@ def test_get_whitespace(self):
591596
assert error.get_left_whitespace(" foo") == " "
592597
assert error.get_left_whitespace("\t\t foo") == "\t\t "
593598

594-
def test_get_code_context_around_line(self, tmpdir):
595-
f = tmpdir.join("foo.py")
596-
f.write("line1\nline2\nline3\nline4\nline5")
599+
def test_get_code_context_around_line(self, tmp_path):
600+
f = tmp_path.joinpath("foo.py")
601+
f.write_text("line1\nline2\nline3\nline4\nline5")
597602

598603
assert error.get_code_context_around_line(str(f), 1) == ["line1"]
599604
assert error.get_code_context_around_line(str(f), 2) == ["line1", "line2"]
@@ -613,5 +618,5 @@ def test_get_code_context_around_line(self, tmpdir):
613618
"line5",
614619
]
615620

616-
invalid = str(tmpdir.join("invalid"))
621+
invalid = str(tmp_path.joinpath("invalid"))
617622
assert error.get_code_context_around_line(invalid, 10) == []

0 commit comments

Comments
 (0)