From 84093b78ec61ad47a2a0dea9f1be8d94fa0d485e Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Mon, 26 Jun 2017 16:32:43 +0200 Subject: fix `test` command when run with Python 2 When using Python 2, `python2 -m unittest` is not equivalent to `python2 -m unittest discover`. --- setuptools/command/test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setuptools/command/test.py b/setuptools/command/test.py index 638d0c56..523407fd 100644 --- a/setuptools/command/test.py +++ b/setuptools/command/test.py @@ -101,6 +101,8 @@ class test(Command): return list(self._test_args()) def _test_args(self): + if not self.test_suite: + yield 'discover' if self.verbose: yield '--verbose' if self.test_suite: -- cgit v1.2.3 From dbff2e7ed421be9ec96029366479a8627691e7f3 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Mon, 26 Jun 2017 16:33:36 +0200 Subject: fix `test` command running tests twice --- setuptools/command/test.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/setuptools/command/test.py b/setuptools/command/test.py index 523407fd..bfa71496 100644 --- a/setuptools/command/test.py +++ b/setuptools/command/test.py @@ -18,6 +18,11 @@ from setuptools.py31compat import unittest_main class ScanningLoader(TestLoader): + + def __init__(self): + TestLoader.__init__(self) + self._visited = set() + def loadTestsFromModule(self, module, pattern=None): """Return a suite of all tests cases contained in the given module @@ -25,6 +30,10 @@ class ScanningLoader(TestLoader): If the module has an ``additional_tests`` function, call it and add the return value to the tests. """ + if module in self._visited: + return None + self._visited.add(module) + tests = [] tests.append(TestLoader.loadTestsFromModule(self, module)) @@ -101,7 +110,7 @@ class test(Command): return list(self._test_args()) def _test_args(self): - if not self.test_suite: + if not self.test_suite and sys.version_info >= (2, 7): yield 'discover' if self.verbose: yield '--verbose' -- cgit v1.2.3 From b5d00314293e400bc72f3699e428f168ac74d824 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Mon, 26 Jun 2017 17:58:09 +0200 Subject: tests: improve `test` command test - cleanup test: we're not installing, so no need to override the user site, or skip the test when run with a virtual environment - use pytest support for capturing output (`context.quiet` does not work with Python 2), and check the output --- setuptools/tests/test_test.py | 51 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/setuptools/tests/test_test.py b/setuptools/tests/test_test.py index 7ea43c57..02cba00d 100644 --- a/setuptools/tests/test_test.py +++ b/setuptools/tests/test_test.py @@ -2,9 +2,8 @@ from __future__ import unicode_literals +from distutils import log import os -import site -from distutils.errors import DistutilsError import pytest @@ -66,26 +65,28 @@ def sample_test(tmpdir_cwd): f.write(TEST_PY) -@pytest.mark.skipif('hasattr(sys, "real_prefix")') -@pytest.mark.usefixtures('user_override') -@pytest.mark.usefixtures('sample_test') -class TestTestTest: - def test_test(self): - params = dict( - name='foo', - packages=['name', 'name.space', 'name.space.tests'], - namespace_packages=['name'], - test_suite='name.space.tests.test_suite', - use_2to3=True, - ) - dist = Distribution(params) - dist.script_name = 'setup.py' - cmd = test(dist) - cmd.user = 1 - cmd.ensure_finalized() - cmd.install_dir = site.USER_SITE - cmd.user = 1 - with contexts.quiet(): - # The test runner calls sys.exit - with contexts.suppress_exceptions(SystemExit): - cmd.run() +@pytest.fixture +def silent_log(): + # Running some of the other tests will automatically + # change the log level to info, messing our output. + log.set_verbosity(0) + + +@pytest.mark.usefixtures('sample_test', 'silent_log') +def test_test(capfd): + params = dict( + name='foo', + packages=['name', 'name.space', 'name.space.tests'], + namespace_packages=['name'], + test_suite='name.space.tests.test_suite', + use_2to3=True, + ) + dist = Distribution(params) + dist.script_name = 'setup.py' + cmd = test(dist) + cmd.ensure_finalized() + # The test runner calls sys.exit + with contexts.suppress_exceptions(SystemExit): + cmd.run() + out, err = capfd.readouterr() + assert out == 'Foo\n' -- cgit v1.2.3 From 803707a68f228f452703333cbf75708938c2eb9e Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Mon, 26 Jun 2017 18:28:07 +0200 Subject: tests: check `test` command run tests only once --- setuptools/tests/test_test.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/setuptools/tests/test_test.py b/setuptools/tests/test_test.py index 02cba00d..960527bc 100644 --- a/setuptools/tests/test_test.py +++ b/setuptools/tests/test_test.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals from distutils import log import os +import sys import pytest @@ -66,13 +67,13 @@ def sample_test(tmpdir_cwd): @pytest.fixture -def silent_log(): +def quiet_log(): # Running some of the other tests will automatically # change the log level to info, messing our output. log.set_verbosity(0) -@pytest.mark.usefixtures('sample_test', 'silent_log') +@pytest.mark.usefixtures('sample_test', 'quiet_log') def test_test(capfd): params = dict( name='foo', @@ -90,3 +91,41 @@ def test_test(capfd): cmd.run() out, err = capfd.readouterr() assert out == 'Foo\n' + + +@pytest.mark.xfail( + sys.version_info < (2, 7), + reason="No discover support for unittest on Python 2.6", +) +@pytest.mark.usefixtures('tmpdir_cwd', 'quiet_log') +def test_tests_are_run_once(capfd): + params = dict( + name='foo', + packages=['dummy'], + ) + with open('setup.py', 'wt') as f: + f.write('from setuptools import setup; setup(\n') + for k, v in sorted(params.items()): + f.write(' %s=%r,\n' % (k, v)) + f.write(')\n') + os.makedirs('dummy') + with open('dummy/__init__.py', 'wt'): + pass + with open('dummy/test_dummy.py', 'wt') as f: + f.write(DALS( + """ + from __future__ import print_function + import unittest + class TestTest(unittest.TestCase): + def test_test(self): + print('Foo') + """)) + dist = Distribution(params) + dist.script_name = 'setup.py' + cmd = test(dist) + cmd.ensure_finalized() + # The test runner calls sys.exit + with contexts.suppress_exceptions(SystemExit): + cmd.run() + out, err = capfd.readouterr() + assert out == 'Foo\n' -- cgit v1.2.3 From b4f2df191e0f9de47a71b3c9fba9f44447e017b5 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 13 Nov 2017 10:01:08 -0500 Subject: Update changelog --- CHANGES.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 6b84c03f..fcacd8ea 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +v36.6.2 +------- + +* #701: Fixed duplicate test discovery on Python 3. + v36.6.1 ------- -- cgit v1.2.3