Skip to content

Commit

Permalink
Fix warnings for pytest 7
Browse files Browse the repository at this point in the history
- Use apt-get update to install boost.
- Replace 'tmpdir' by 'tmp_path'.

Close #78
  • Loading branch information
nicoddemus committed Mar 18, 2022
1 parent fd62aaf commit 5146877
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
sudo make install
- name: Install Boost.Test
run: |
sudo apt-get update
sudo apt-get install libboost-test-dev valgrind
- name: Compile
run: |
Expand Down
42 changes: 24 additions & 18 deletions src/pytest_cpp/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import stat
import sys
from fnmatch import fnmatch

import pytest

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


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

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

if not parent.session.isinitpath(path) and not matches_any_mask(path, masks):
if not parent.session.isinitpath(file_path) and not matches_any_mask(
file_path, masks
):
return

for facade_class in FACADES:
if facade_class.is_test_suite(str(path)):
if facade_class.is_test_suite(str(file_path)):
if needs_from_parent:
return CppFile.from_parent(
fspath=path,
path=file_path,
parent=parent,
facade=facade_class(),
arguments=test_args,
)
else:
return CppFile(path, parent, facade_class(), test_args)
return CppFile(
path=file_path,
parent=parent,
facade_class=facade_class(),
arguments=test_args,
)


def pytest_addoption(parser):
Expand Down Expand Up @@ -94,16 +102,15 @@ def pytest_addoption(parser):


class CppFile(pytest.File):
def __init__(self, fspath, parent, facade, arguments):
pytest.File.__init__(self, fspath, parent)
def __init__(self, *, path, parent, facade, arguments, **kwargs):
pytest.File.__init__(self, path=path, parent=parent, **kwargs)
self.facade = facade
self._arguments = arguments

@classmethod
def from_parent(cls, parent, fspath, facade, arguments):
# TODO: after dropping python 2, change to keyword only after 'parent'
def from_parent(cls, *, parent, path, facade, arguments, **kwargs):
return super().from_parent(
parent=parent, fspath=fspath, facade=facade, arguments=arguments
parent=parent, path=path, facade=facade, arguments=arguments
)

def collect(self):
Expand All @@ -120,16 +127,15 @@ def collect(self):


class CppItem(pytest.Item):
def __init__(self, name, parent, facade, arguments):
pytest.Item.__init__(self, name, parent)
def __init__(self, *, name, parent, facade, arguments, **kwargs):
pytest.Item.__init__(self, name, parent, **kwargs)
self.facade = facade
self._arguments = arguments

@classmethod
def from_parent(cls, parent, name, facade, arguments):
# TODO: after dropping python 2, change to keyword only after 'parent'
def from_parent(cls, *, parent, name, facade, arguments, **kw):
return super().from_parent(
name=name, parent=parent, facade=facade, arguments=arguments
name=name, parent=parent, facade=facade, arguments=arguments, **kw
)

def runtest(self):
Expand Down
37 changes: 21 additions & 16 deletions tests/test_pytest_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ def test_list_tests(facade, name, expected, exes):
(Catch2Facade(), "catch2_success", "gtest"),
],
)
def test_is_test_suite(facade, name, other_name, exes, tmpdir):
def test_is_test_suite(facade, name, other_name, exes, tmp_path):
assert facade.is_test_suite(exes.get(name))
assert not facade.is_test_suite(exes.get(other_name))
tmpdir.ensure("foo.txt")
assert not facade.is_test_suite(str(tmpdir.join("foo.txt")))
tmp_path.joinpath("foo.txt").touch()
assert not facade.is_test_suite(str(tmp_path.joinpath("foo.txt")))


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


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

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


def test_race_condition_on_collect(tmpdir):
def test_race_condition_on_collect(tmp_path):
"""
Check that collection correctly handles when a path no longer is valid.
Expand All @@ -510,24 +510,29 @@ def test_race_condition_on_collect(tmpdir):
"""
import pytest_cpp.plugin

assert pytest_cpp.plugin.pytest_collect_file(None, tmpdir / "invalid-file") is None
assert (
pytest_cpp.plugin.pytest_collect_file(None, tmp_path / "invalid-file") is None
)


def test_exe_mask_on_windows(tmpdir, monkeypatch):
def test_exe_mask_on_windows(tmp_path, monkeypatch):
"""
Test for #45: C++ tests not collected due to '*_test' mask on Windows
"""
import pytest_cpp.plugin

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

fn = tmpdir.join("generator_demo_test.exe").ensure(file=1)
fn = tmp_path.joinpath("generator_demo_test.exe")
fn.touch()
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])

fn = tmpdir.join("test_generator_demo.exe").ensure(file=1)
fn = tmp_path.joinpath("test_generator_demo.exe")
fn.touch()
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])

fn = tmpdir.join("my_generator_test_demo.exe").ensure(file=1)
fn = tmp_path.joinpath("my_generator_test_demo.exe")
fn.touch()
assert not pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])


Expand Down Expand Up @@ -591,9 +596,9 @@ def test_get_whitespace(self):
assert error.get_left_whitespace(" foo") == " "
assert error.get_left_whitespace("\t\t foo") == "\t\t "

def test_get_code_context_around_line(self, tmpdir):
f = tmpdir.join("foo.py")
f.write("line1\nline2\nline3\nline4\nline5")
def test_get_code_context_around_line(self, tmp_path):
f = tmp_path.joinpath("foo.py")
f.write_text("line1\nline2\nline3\nline4\nline5")

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

invalid = str(tmpdir.join("invalid"))
invalid = str(tmp_path.joinpath("invalid"))
assert error.get_code_context_around_line(invalid, 10) == []

0 comments on commit 5146877

Please sign in to comment.