Skip to content

Commit 407df50

Browse files
authored
add cpp_ignore_py_files option (enabled by default) (#44)
add cpp_ignore_py_files option (enabled by default)
2 parents 95df534 + 08d0326 commit 407df50

5 files changed

Lines changed: 78 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 1.2.0
22

33
- `pytest-cpp` no longer supports Python 3.4.
4+
- New `cpp_ignore_py_files` option that makes the plugin ignore `*.py` files even if they
5+
would otherwise match the `cpp_files` option (defaults to `True`).
46

57
# 1.1.0
68

README.rst

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,34 @@ Once installed, when py.test runs it will search and run tests
4545
found in executable files, detecting if the suites are
4646
Google or Boost tests automatically.
4747

48+
Configuration Options
49+
~~~~~~~~~~~~~~~~~~~~~
50+
51+
**cpp_files**
52+
4853
You can configure which files are tested for suites by using the ``cpp_files``
49-
ini configuration:
54+
ini configuration option:
5055

5156
.. code-block:: ini
5257
5358
[pytest]
54-
cpp_files=test_suite*
59+
cpp_files = test_suite*
5560
5661
By default matches ``test_*`` and ``*_test`` executable files.
5762

58-
Additional arguments to the C++ tests can be provided with the
59-
``cpp_arguments`` ini configuration.
63+
**cpp_arguments**
6064

6165
*New in version 1.1*.
6266

67+
Arguments to the C++ tests can be provided with the
68+
``cpp_arguments`` ini configuration option.
69+
6370
For example:
6471

6572
.. code-block:: ini
6673
6774
[pytest]
68-
cpp_arguments=-v --log-dir=logs
75+
cpp_arguments =-v --log-dir=logs
6976
7077
You can change this option directly in the command-line using
7178
pytest's ``-o`` option:
@@ -74,12 +81,19 @@ pytest's ``-o`` option:
7481
7582
$ pytest -o cpp_arguments='-v --log-dir=logs'
7683
84+
**cpp_ignore_py_files**
85+
86+
*New in version 1.2*.
7787

78-
Requirements
79-
============
88+
This option defaults to ``True`` and configures the plugin to ignore ``*.py`` files that
89+
would otherwise match the ``cpp_files`` option.
8090

81-
* Python 2.7+, Python 3.4+
82-
* pytest
91+
Set it to ``False`` if you have C++ executable files that end with the ``*.py`` extension.
92+
93+
.. code-block:: ini
94+
95+
[pytest]
96+
cpp_ignore_py_files = False
8397
8498
Install
8599
=======

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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ else:
88
CCFLAGS = ''
99
LIBS = ['pthread']
1010

11-
env = Environment(
12-
CPPPATH=os.environ.get('INCLUDE'),
13-
CCFLAGS=CCFLAGS,
14-
LIBPATH=os.environ.get('LIBPATH'),
15-
LIBS=LIBS,
16-
)
11+
kwargs = {
12+
'CPPPATH': os.environ.get('INCLUDE'),
13+
'CCFLAGS': CCFLAGS,
14+
'LIBPATH': os.environ.get('LIBPATH'),
15+
'LIBS': LIBS,
16+
}
17+
if 'CXX' in os.environ:
18+
kwargs['CXX'] = os.environ['CXX']
19+
20+
env = Environment(**kwargs)
1721
genv = env.Clone(LIBS=['gtest'] + LIBS)
1822

1923
Export('env genv')

tests/test_pytest_cpp.py

Lines changed: 25 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,30 @@ 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.runpytest('--collect-only')
275+
result.stdout.fnmatch_lines('* no tests ran *')
276+
277+
result = testdir.runpytest('--collect-only', '-o', 'cpp_ignore_py_files=False')
278+
result.stdout.fnmatch_lines("*CppFile cpptest_success.py*")
279+
280+
# running directly skips out machinery as well.
281+
result = testdir.runpytest('--collect-only', file_name)
282+
result.stdout.fnmatch_lines('*1 error in*')
283+
284+
result = testdir.runpytest('--collect-only', '-o', 'cpp_ignore_py_files=False', file_name)
285+
result.stdout.fnmatch_lines("*CppFile cpptest_success.py*")
286+
287+
263288
def test_google_one_argument(testdir, exes):
264289
testdir.makeini('''
265290
[pytest]

0 commit comments

Comments
 (0)