From f8285cac4b95e6b43869e27093ef04552e665681 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 1 Jan 2015 23:31:22 -0500 Subject: Move fixture to a fixtures module and make that fixture available globally. --- setuptools/tests/fixtures.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 setuptools/tests/fixtures.py (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py new file mode 100644 index 00000000..6b0e53f3 --- /dev/null +++ b/setuptools/tests/fixtures.py @@ -0,0 +1,16 @@ +import mock +import pytest + +from . import contexts + +@pytest.yield_fixture +def user_override(): + """ + Override site.USER_BASE and site.USER_SITE with temporary directories in + a context. + """ + with contexts.tempdir() as user_base: + with mock.patch('site.USER_BASE', user_base): + with contexts.tempdir() as user_site: + with mock.patch('site.USER_SITE', user_site): + yield -- cgit v1.2.1 From bb7a25abe65fba3fa3e8ae3973321f1fedab8e37 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 1 Jan 2015 23:47:12 -0500 Subject: Also save the ENABLE_USER_SITE setting in the user_override. --- setuptools/tests/fixtures.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 6b0e53f3..225c2ea3 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -13,4 +13,5 @@ def user_override(): with mock.patch('site.USER_BASE', user_base): with contexts.tempdir() as user_site: with mock.patch('site.USER_SITE', user_site): - yield + with contexts.save_user_site_setting(): + yield -- cgit v1.2.1 From bbb68a8b848f9dbd4d44419fdae93b5e057763e5 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 2 Jan 2015 09:49:58 -0500 Subject: Rewrite test using pytest. --- setuptools/tests/fixtures.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 225c2ea3..0b1eaf5f 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -3,6 +3,7 @@ import pytest from . import contexts + @pytest.yield_fixture def user_override(): """ @@ -15,3 +16,9 @@ def user_override(): with mock.patch('site.USER_SITE', user_site): with contexts.save_user_site_setting(): yield + + +@pytest.yield_fixture +def tmpdir_cwd(tmpdir): + with tmpdir.as_cwd() as orig: + yield orig -- cgit v1.2.1 From cde1eb84bd1813ff4425fe6f05cbdeba8e410c13 Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Thu, 15 Jan 2015 23:25:04 +0100 Subject: Use unittest.mock from standard library instead of external mock with Python >=3.3. --- setuptools/tests/fixtures.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 0b1eaf5f..c70c38cb 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -1,4 +1,7 @@ -import mock +try: + from unittest import mock +except ImportError: + import mock import pytest from . import contexts -- cgit v1.2.1 From 849b034ac5440def5327172fa799fbbb3d40db98 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 30 Jul 2016 08:11:33 -0400 Subject: Don't rely on mock in the plugin --- setuptools/tests/fixtures.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index c70c38cb..1e81ee26 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -1,22 +1,18 @@ -try: - from unittest import mock -except ImportError: - import mock import pytest from . import contexts @pytest.yield_fixture -def user_override(): +def user_override(monkeypatch): """ Override site.USER_BASE and site.USER_SITE with temporary directories in a context. """ with contexts.tempdir() as user_base: - with mock.patch('site.USER_BASE', user_base): + monkeypatch.setattr('site.USER_BASE', user_base) with contexts.tempdir() as user_site: - with mock.patch('site.USER_SITE', user_site): + monkeypatch.setattr('site.USER_SITE', user_site) with contexts.save_user_site_setting(): yield -- cgit v1.2.1 From 1074affc4a94224f30692db4ab0d45ed29ac8c8d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 30 Jul 2016 08:12:18 -0400 Subject: Reindent --- setuptools/tests/fixtures.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 1e81ee26..5204c8d1 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -10,11 +10,11 @@ def user_override(monkeypatch): a context. """ with contexts.tempdir() as user_base: - monkeypatch.setattr('site.USER_BASE', user_base) - with contexts.tempdir() as user_site: - monkeypatch.setattr('site.USER_SITE', user_site) - with contexts.save_user_site_setting(): - yield + monkeypatch.setattr('site.USER_BASE', user_base) + with contexts.tempdir() as user_site: + monkeypatch.setattr('site.USER_SITE', user_site) + with contexts.save_user_site_setting(): + yield @pytest.yield_fixture -- cgit v1.2.1 From 701eee9e53dcbfe200bef46420da420052699e68 Mon Sep 17 00:00:00 2001 From: Chih-Hsuan Yen Date: Sun, 13 Dec 2020 18:01:50 +0800 Subject: Fix tests with pytest 6.2 The latest pytest deprecates pytest.yield_fixture in favor of pytest.fixture [1]. The changelog [2] says that both are the same. [1] https://github.com/pytest-dev/pytest/pull/7988 [2] https://docs.pytest.org/en/stable/changelog.html#pytest-6-2-0-2020-12-12 --- setuptools/tests/fixtures.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 5204c8d1..e8cb7f52 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -3,7 +3,7 @@ import pytest from . import contexts -@pytest.yield_fixture +@pytest.fixture def user_override(monkeypatch): """ Override site.USER_BASE and site.USER_SITE with temporary directories in @@ -17,7 +17,7 @@ def user_override(monkeypatch): yield -@pytest.yield_fixture +@pytest.fixture def tmpdir_cwd(tmpdir): with tmpdir.as_cwd() as orig: yield orig -- cgit v1.2.1 From 2e077b73e8a391f460b365382408d70439494d43 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Fri, 1 Jan 2021 23:10:35 +0100 Subject: Make `test_pip_upgrade_from_source` xdist-friendly --- setuptools/tests/fixtures.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index e8cb7f52..0480033c 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -1,8 +1,14 @@ +import pathlib +import shutil + import pytest from . import contexts +SRC_DIR = pathlib.Path(__file__).parents[2] + + @pytest.fixture def user_override(monkeypatch): """ @@ -21,3 +27,25 @@ def user_override(monkeypatch): def tmpdir_cwd(tmpdir): with tmpdir.as_cwd() as orig: yield orig + + +@pytest.fixture +def src_dir(): + """The project source directory available via fixture.""" + return SRC_DIR + + +@pytest.fixture +def tmp_src(src_dir, tmp_path): + """Make a copy of the source dir under `$tmp/src`. + + This fixture is useful whenever it's necessary to run `setup.py` + or `pip install` against the source directory when there's no + control over the number of simultaneous invocations. Such + concurrent runs create and delete directories with the same names + under the target directory and so they influence each other's runs + when they are not being executed sequentially. + """ + tmp_src_path = tmp_path / 'src' + shutil.copytree(src_dir, tmp_src_path) + return tmp_src_path -- cgit v1.2.1 From 3c8e758caa11abe63040058ba136a68d2b620ea3 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 17 Jan 2021 21:49:57 -0500 Subject: Avoid indirection in src_dir --- setuptools/tests/fixtures.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 0480033c..4a990eb9 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -6,9 +6,6 @@ import pytest from . import contexts -SRC_DIR = pathlib.Path(__file__).parents[2] - - @pytest.fixture def user_override(monkeypatch): """ @@ -32,7 +29,7 @@ def tmpdir_cwd(tmpdir): @pytest.fixture def src_dir(): """The project source directory available via fixture.""" - return SRC_DIR + return pathlib.Path(__file__).parents[2] @pytest.fixture -- cgit v1.2.1 From 31b0896bba77f21984dfad5a0b82fcd57bda9658 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 18 Jan 2021 10:30:11 -0500 Subject: Rely on rootdir to determine the source. Avoids coupling with position in the test suite. --- setuptools/tests/fixtures.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 4a990eb9..d975c0fc 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -1,4 +1,3 @@ -import pathlib import shutil import pytest @@ -27,13 +26,7 @@ def tmpdir_cwd(tmpdir): @pytest.fixture -def src_dir(): - """The project source directory available via fixture.""" - return pathlib.Path(__file__).parents[2] - - -@pytest.fixture -def tmp_src(src_dir, tmp_path): +def tmp_src(request, tmp_path): """Make a copy of the source dir under `$tmp/src`. This fixture is useful whenever it's necessary to run `setup.py` @@ -44,5 +37,5 @@ def tmp_src(src_dir, tmp_path): when they are not being executed sequentially. """ tmp_src_path = tmp_path / 'src' - shutil.copytree(src_dir, tmp_src_path) + shutil.copytree(request.config.rootdir, tmp_src_path) return tmp_src_path -- cgit v1.2.1 From f767b4f59b14aa94d7c085173cb9360ba2d187eb Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 18 Jan 2021 10:48:38 -0500 Subject: Extract workaround for pytest-dev/pytest-xdist#376 as a fixture. Invoke the repair at the session level and only when xdist is present. --- setuptools/tests/fixtures.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index d975c0fc..d74b5f03 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -1,3 +1,5 @@ +import contextlib +import sys import shutil import pytest @@ -39,3 +41,20 @@ def tmp_src(request, tmp_path): tmp_src_path = tmp_path / 'src' shutil.copytree(request.config.rootdir, tmp_src_path) return tmp_src_path + + +@pytest.fixture(autouse=True, scope="session") +def workaround_xdist_376(request): + """ + Workaround pytest-dev/pytest-xdist#376 + + ``pytest-xdist`` tends to inject '' into ``sys.path``, + which may break certain isolation expectations. + Remove the entry so the import + machinery behaves the same irrespective of xdist. + """ + if not request.config.pluginmanager.has_plugin('xdist'): + return + + with contextlib.suppress(ValueError): + sys.path.remove('') -- cgit v1.2.1 From b4d8e4755eafc786a9848333ca73bff381747745 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 20 Mar 2021 21:19:49 -0400 Subject: Add test capturing missed expectation. Ref #2612. --- setuptools/tests/fixtures.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'setuptools/tests/fixtures.py') diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index d74b5f03..a5a172e0 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -1,6 +1,7 @@ import contextlib import sys import shutil +import subprocess import pytest @@ -58,3 +59,16 @@ def workaround_xdist_376(request): with contextlib.suppress(ValueError): sys.path.remove('') + + +@pytest.fixture +def sample_project(tmp_path): + """ + Clone the 'sampleproject' and return a path to it. + """ + cmd = ['git', 'clone', 'https://github.com/pypa/sampleproject'] + try: + subprocess.check_call(cmd, cwd=str(tmp_path)) + except Exception: + pytest.skip("Unable to clone sampleproject") + return tmp_path / 'sampleproject' -- cgit v1.2.1