From b29a9711c4cb236f9515fca8f36d7733a8301738 Mon Sep 17 00:00:00 2001 From: je Date: Fri, 13 Oct 2017 18:57:52 +0800 Subject: [PATCH 1/3] ignore valid setup.py during --doctest-modules --- _pytest/doctest.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index cc505c8d0..c1b6a81a8 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -50,12 +50,20 @@ def pytest_addoption(parser): def pytest_collect_file(path, parent): config = parent.config if path.ext == ".py": - if config.option.doctestmodules: + if config.option.doctestmodules and not _is_setup_py(config, path, parent): return DoctestModule(path, parent) elif _is_doctest(config, path, parent): return DoctestTextfile(path, parent) +def _is_setup_py(config, path, parent): + if path.basename != "setup.py": + return False + with open(path.strpath, 'r') as f: + contents = f.read() + return 'setuptools' in contents or 'distutils' in contents + + def _is_doctest(config, path, parent): if path.ext in ('.txt', '.rst') and parent.session.isinitpath(path): return True From eaf38c72391efcfde8efc852c0921eb6374787de Mon Sep 17 00:00:00 2001 From: je Date: Sat, 14 Oct 2017 00:47:02 +0800 Subject: [PATCH 2/3] call path.read(), add tests, add news fragment --- AUTHORS | 1 + _pytest/doctest.py | 3 +-- changelog/502.feature | 1 + testing/test_doctest.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 changelog/502.feature diff --git a/AUTHORS b/AUTHORS index f1769116d..026c40fb7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -82,6 +82,7 @@ Jason R. Coombs Javier Domingo Cansino Javier Romero Jeff Widman +John Eddie Ayson John Towler Jon Sonesen Jonas Obrist diff --git a/_pytest/doctest.py b/_pytest/doctest.py index c1b6a81a8..6016265a5 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -59,8 +59,7 @@ def pytest_collect_file(path, parent): def _is_setup_py(config, path, parent): if path.basename != "setup.py": return False - with open(path.strpath, 'r') as f: - contents = f.read() + contents = path.read() return 'setuptools' in contents or 'distutils' in contents diff --git a/changelog/502.feature b/changelog/502.feature new file mode 100644 index 000000000..f768b650e --- /dev/null +++ b/changelog/502.feature @@ -0,0 +1 @@ +Implement feature to skip valid setup.py files when ran with --doctest-modules diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 8a81ea0ed..b8fa1fb77 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -561,6 +561,34 @@ class TestDoctests(object): reportinfo = items[0].reportinfo() assert reportinfo[1] == 1 + def test_valid_setup_py(self, testdir): + ''' + Test to make sure that pytest ignores valid setup.py files when ran + with --doctest-modules + ''' + p = testdir.makepyfile(setup=""" + from setuptools import setup, find_packages + setup(name='sample', + version='0.0', + description='description', + packages=find_packages() + ) + """) + result = testdir.runpytest(p, '--doctest-modules') + result.stdout.fnmatch_lines(['*collected 0 items*']) + + def test_invalid_setup_py(self, testdir): + ''' + Test to make sure that pytest reads setup.py files that are not used + for python packages when ran with --doctest-modules + ''' + p = testdir.makepyfile(setup=""" + def test_foo(): + return 'bar' + """) + result = testdir.runpytest(p, '--doctest-modules') + result.stdout.fnmatch_lines(['*collected 1 item*']) + class TestLiterals(object): From 843872b501721d53704097931472a33fef1d2813 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 16 Oct 2017 21:07:57 -0200 Subject: [PATCH 3/3] Improve formatting in 502.feature file --- changelog/502.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/502.feature b/changelog/502.feature index f768b650e..eb61640b9 100644 --- a/changelog/502.feature +++ b/changelog/502.feature @@ -1 +1 @@ -Implement feature to skip valid setup.py files when ran with --doctest-modules +Implement feature to skip ``setup.py`` files when ran with ``--doctest-modules``.