Skip to content

Commit 9223211

Browse files
authored
Merge pull request #46 from nicoddemus/fix-exe-windows
2 parents 48da9c1 + 0752c24 commit 9223211

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# 1.2.1
22

3-
- Remove `from_parent()` warnings in pytest 5.4.2+.
3+
- Remove `from_parent()`-related warnings in pytest 5.4.2+.
4+
5+
- Masks like `*_test` now work correctly on Windows by automatically appending the
6+
expected `".exe"` suffix (#45).
7+
Thanks @1fabrism for the report.
48

59
# 1.2.0
610

pytest_cpp/plugin.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import stat
3+
import sys
34

45
import pytest
56

@@ -16,6 +17,12 @@
1617
# pytest 5.4 introduced the 'from_parent' constructor
1718
needs_from_parent = hasattr(pytest.Item, "from_parent")
1819

20+
def matches_any_mask(path, masks):
21+
"""Return True if the given path matches any of the masks given"""
22+
if sys.platform.startswith("win"):
23+
masks = [m + ".exe" for m in masks]
24+
return any(path.fnmatch(m) for m in masks)
25+
1926

2027
def pytest_collect_file(parent, path):
2128
try:
@@ -34,12 +41,10 @@ def pytest_collect_file(parent, path):
3441
# don't attempt to check *.py files even if they were given as explicit arguments
3542
if cpp_ignore_py_files and path.fnmatch('*.py'):
3643
return
37-
if not parent.session.isinitpath(path):
38-
for pat in masks:
39-
if path.fnmatch(pat):
40-
break
41-
else:
42-
return
44+
45+
if not parent.session.isinitpath(path) and not matches_any_mask(path, masks):
46+
return
47+
4348
for facade_class in FACADES:
4449
if facade_class.is_test_suite(str(path)):
4550
if needs_from_parent:

tests/test_pytest_cpp.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ def test_race_condition_on_collect(tmpdir):
400400
assert pytest_cpp.plugin.pytest_collect_file(None, tmpdir / 'invalid-file') is None
401401

402402

403+
def test_exe_mask_on_windows(tmpdir, monkeypatch):
404+
"""
405+
Test for #45: C++ tests not collected due to '*_test' mask on Windows
406+
"""
407+
import pytest_cpp.plugin
408+
monkeypatch.setattr(sys, "platform", "win32")
409+
410+
fn = tmpdir.join("generator_demo_test.exe").ensure(file=1)
411+
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
412+
413+
fn = tmpdir.join("test_generator_demo.exe").ensure(file=1)
414+
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
415+
416+
fn = tmpdir.join("my_generator_test_demo.exe").ensure(file=1)
417+
assert not pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
418+
419+
403420
class TestError:
404421

405422
def test_get_whitespace(self):

0 commit comments

Comments
 (0)