Skip to content

Commit 5c8e275

Browse files
fj128nicoddemus
authored andcommitted
add cpp_ignore_py_files option (enabled by default)
1 parent 95df534 commit 5c8e275

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

pytest_cpp/plugin.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ def pytest_collect_file(parent, path):
2323
return
2424

2525
config = parent.config
26-
masks = config.getini('cpp_files') or DEFAULT_MASKS
27-
28-
test_args = config.getini(_ARGUMENTS) or ()
26+
masks = config.getini('cpp_files')
27+
test_args = config.getini('cpp_arguments')
28+
cpp_ignore_py_files = config.getini('cpp_ignore_py_files')
2929

30+
# don't attempt to check *.py files even if they were given as explicit arguments
31+
if cpp_ignore_py_files and path.fnmatch('*.py'):
32+
return
3033
if not parent.session.isinitpath(path):
3134
for pat in masks:
3235
if path.fnmatch(pat):
@@ -39,13 +42,18 @@ def pytest_collect_file(parent, path):
3942

4043

4144
def pytest_addoption(parser):
42-
parser.addini("cpp_files", type="args",
43-
default=DEFAULT_MASKS,
44-
help="glob-style file patterns for C++ test module discovery")
45-
parser.addini(_ARGUMENTS,
46-
type='args',
47-
default='',
48-
help='Additional arguments for test executables')
45+
parser.addini('cpp_files',
46+
type='args',
47+
default=DEFAULT_MASKS,
48+
help="glob-style file patterns for C++ test module discovery")
49+
parser.addini('cpp_arguments',
50+
type='args',
51+
default=(),
52+
help='additional arguments for test executables')
53+
parser.addini('cpp_ignore_py_files',
54+
type='bool',
55+
default=True,
56+
help='ignore *.py files that otherwise match "cpp_files" patterns')
4957

5058

5159
class CppFile(pytest.File):

tests/SConstruct

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ else:
99
LIBS = ['pthread']
1010

1111
env = Environment(
12+
CXX=os.environ.get('CXX'),
1213
CPPPATH=os.environ.get('INCLUDE'),
1314
CCFLAGS=CCFLAGS,
1415
LIBPATH=os.environ.get('LIBPATH'),

tests/test_pytest_cpp.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import pytest
23
import subprocess
34
from pytest_cpp import error
@@ -260,6 +261,36 @@ def test_cpp_files_option(testdir, exes):
260261
])
261262

262263

264+
# skip to avoid dealing with exes.get appending extension
265+
@pytest.mark.skipif(sys.platform.startswith('win'), reason='This is not a problem on Windows')
266+
def test_cpp_ignore_py_files(testdir, exes):
267+
file_name = 'cpptest_success.py'
268+
exes.get('gtest', 'cpptest_success.py')
269+
testdir.makeini('''
270+
[pytest]
271+
cpp_files = cpptest_*
272+
''')
273+
274+
result = testdir.inline_run('--collect-only')
275+
reps = result.getreports()
276+
print(reps)
277+
assert len(reps) == 1
278+
assert reps[0].result == []
279+
280+
result = testdir.inline_run('--collect-only', '-o', 'cpp_ignore_py_files=False')
281+
assert len(result.matchreport(exes.exe_name(file_name), when='collect').result) == 4
282+
283+
# running directly skips out machinery as well.
284+
result = testdir.inline_run('--collect-only', file_name)
285+
assert len(result.matchreport(exes.exe_name(file_name), when='collect').result) == 0
286+
287+
result = testdir.inline_run('--collect-only', '-o', 'cpp_ignore_py_files=False', file_name)
288+
# assert len(result.matchreport(exes.exe_name(file_name)).result) == 4
289+
# workaround for a pytest bug
290+
print([(rep.nodeid.split("::"), len(rep.result)) for rep in result.getreports()])
291+
assert any(file_name in rep.nodeid.split("::") and len(rep.result) == 4 for rep in result.getreports())
292+
293+
263294
def test_google_one_argument(testdir, exes):
264295
testdir.makeini('''
265296
[pytest]

0 commit comments

Comments
 (0)