From 7e4d6a77335ff961501f69e43345733551324b3f Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Thu, 17 Nov 2022 10:57:45 +0100 Subject: [PATCH 1/3] TST: simplify pep518 test setup This also makes the test work on Cygwin where a ninja wheel is not available and needs to be built from the source distribution. --- tests/conftest.py | 52 ++++++++++---------------------------------- tests/test_pep518.py | 5 ++--- 2 files changed, 13 insertions(+), 44 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 475cb96fa..dbfe492cf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -126,47 +126,17 @@ def fixture(tmp_dir_session): @pytest.fixture(scope='session') def pep518_wheelhouse(tmpdir_factory): wheelhouse = tmpdir_factory.mktemp('wheelhouse') - dist = tmpdir_factory.mktemp('dist') - subprocess.run( - [sys.executable, '-m', 'build', '--wheel', '--outdir', str(dist)], - cwd=str(package_dir.parent.parent), - check=True, - ) - (wheel_path,) = dist.visit('*.whl') - subprocess.run( - [ - sys.executable, - '-m', - 'pip', - 'download', - '-q', - '-d', - str(wheelhouse), - str(wheel_path), - ], - check=True, - ) - subprocess.run( - [ - sys.executable, - '-m', - 'pip', - 'download', - '-q', - '-d', - str(wheelhouse), - 'build', - 'colorama', - 'meson', - 'ninja', - 'patchelf', - 'pyproject-metadata', - 'tomli', - 'typing-extensions', - 'wheel', - ], - check=True, - ) + meson_python = str(package_dir.parent.parent) + # Populate wheelhouse with wheel for the following packages and + # their dependencies. Wheels are downloaded from PyPI or built + # from the source distribution as needed. Sources or wheels in + # the pip cache are used when available. + packages = [ + meson_python, + 'build', + 'wheel', + ] + subprocess.run([sys.executable, '-m', 'pip', 'wheel', '--wheel-dir', str(wheelhouse), *packages], check=True) return str(wheelhouse) diff --git a/tests/test_pep518.py b/tests/test_pep518.py index a2367fa6c..6498362bf 100644 --- a/tests/test_pep518.py +++ b/tests/test_pep518.py @@ -15,10 +15,9 @@ @pytest.mark.parametrize( 'build_arg', ['', '--wheel'], ids=['sdist_to_wheel', 'wheel_directly'] ) -@pytest.mark.xfail(sys.platform.startswith('cygwin'), reason='ninja pip package not available on cygwin', strict=True) def test_pep518(pep518, package, build_arg, tmp_path): dist = tmp_path / 'dist' - with cd_package(package) as package_dir, in_git_repo_context(): + with cd_package(package), in_git_repo_context(): build_args = [build_arg] if build_arg else [] - subprocess.run([sys.executable, '-m', 'build', f'--outdir={dist}', *build_args], cwd=package_dir, check=True) + subprocess.run([sys.executable, '-m', 'build', '--outdir', str(dist), *build_args], check=True) From f747f777a19f962d2f510bc38cb2e54b300197bc Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Thu, 17 Nov 2022 16:15:31 +0100 Subject: [PATCH 2/3] TST: disable pip version check for all tests --- tests/conftest.py | 8 ++++++++ tests/docs/examples/test_spam.py | 2 +- tests/test_wheel.py | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index dbfe492cf..768615bd2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -123,6 +123,14 @@ def fixture(tmp_dir_session): globals()[f'wheel_{normalized}'] = generate_wheel_fixture(package) +@pytest.fixture(autouse=True, scope='session') +def disable_pip_version_check(): + # Cannot use the 'monkeypatch' fixture because of scope mismatch. + mpatch = pytest.MonkeyPatch() + yield mpatch.setenv('PIP_DISABLE_PIP_VERSION_CHECK', '1') + mpatch.undo() + + @pytest.fixture(scope='session') def pep518_wheelhouse(tmpdir_factory): wheelhouse = tmpdir_factory.mktemp('wheelhouse') diff --git a/tests/docs/examples/test_spam.py b/tests/docs/examples/test_spam.py index cccb8ddb8..d79ad9a4e 100644 --- a/tests/docs/examples/test_spam.py +++ b/tests/docs/examples/test_spam.py @@ -20,7 +20,7 @@ def test_build_and_import(venv, tmp_dir_session): wheel = build_project_wheel(package=examples_dir / 'spam', outdir=tmp_dir_session) subprocess.check_call([ - venv.executable, '-m', 'pip', '--disable-pip-version-check', 'install', wheel + venv.executable, '-m', 'pip', 'install', wheel ]) output = subprocess.check_output([ venv.executable, '-c', 'import spam; print(spam.add(1, 2))' diff --git a/tests/test_wheel.py b/tests/test_wheel.py index 3717a0643..b11a321e3 100644 --- a/tests/test_wheel.py +++ b/tests/test_wheel.py @@ -142,7 +142,7 @@ def test_configure_data(wheel_configure_data): @pytest.mark.skipif(platform.system() != 'Linux', reason='Unsupported on this platform for now') def test_local_lib(venv, wheel_link_against_local_lib): subprocess.check_call([ - venv.executable, '-m', 'pip', '--disable-pip-version-check', 'install', wheel_link_against_local_lib + venv.executable, '-m', 'pip', 'install', wheel_link_against_local_lib ]) assert subprocess.check_output([ venv.executable, '-c', 'import example; print(example.example_sum(1, 2))' From 427b1fced908d3fdaa74cf1de0f484b3f776659a Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Thu, 17 Nov 2022 16:23:10 +0100 Subject: [PATCH 3/3] TST: use the recommended subprocess API --- tests/conftest.py | 10 +++++----- tests/docs/examples/test_spam.py | 12 ++++++------ tests/test_project.py | 2 +- tests/test_wheel.py | 14 ++++++++------ 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 768615bd2..2534bf09e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -48,11 +48,11 @@ def in_git_repo_context(path=os.path.curdir): path = os.path.abspath(path) shutil.rmtree(os.path.join(path, '.git'), ignore_errors=True) try: - subprocess.check_call(['git', 'init', '-b', 'main', path]) - subprocess.check_call(['git', 'config', 'user.email', 'author@example.com'], cwd=path) - subprocess.check_call(['git', 'config', 'user.name', 'A U Thor'], cwd=path) - subprocess.check_call(['git', 'add', '*'], cwd=path) - subprocess.check_call(['git', 'commit', '-q', '-m', 'Test'], cwd=path) + subprocess.run(['git', 'init', '-b', 'main', path], check=True) + subprocess.run(['git', 'config', 'user.email', 'author@example.com'], cwd=path, check=True) + subprocess.run(['git', 'config', 'user.name', 'A U Thor'], cwd=path, check=True) + subprocess.run(['git', 'add', '*'], cwd=path, check=True) + subprocess.run(['git', 'commit', '-q', '-m', 'Test'], cwd=path, check=True) yield finally: # PermissionError raised on Windows. diff --git a/tests/docs/examples/test_spam.py b/tests/docs/examples/test_spam.py index d79ad9a4e..4e6101831 100644 --- a/tests/docs/examples/test_spam.py +++ b/tests/docs/examples/test_spam.py @@ -19,11 +19,11 @@ def test_build_and_import(venv, tmp_dir_session): else: wheel = build_project_wheel(package=examples_dir / 'spam', outdir=tmp_dir_session) - subprocess.check_call([ - venv.executable, '-m', 'pip', 'install', wheel - ]) - output = subprocess.check_output([ - venv.executable, '-c', 'import spam; print(spam.add(1, 2))' - ]) + subprocess.run( + [venv.executable, '-m', 'pip', 'install', wheel], + check=True) + output = subprocess.run( + [venv.executable, '-c', 'import spam; print(spam.add(1, 2))'], + check=True, stdout=subprocess.PIPE).stdout assert int(output) == 3 diff --git a/tests/test_project.py b/tests/test_project.py index 05281cc99..ff7c7bb4a 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -65,7 +65,7 @@ def last_two_meson_args(): # create the build directory ourselves because Project._meson is mocked builddir = str(tmp_dir_session / 'build') - subprocess.check_call(['meson', 'setup', '.', builddir]) + subprocess.run(['meson', 'setup', '.', builddir], check=True) config_settings = { 'builddir': builddir, # use the build directory we created diff --git a/tests/test_wheel.py b/tests/test_wheel.py index b11a321e3..c151ddef7 100644 --- a/tests/test_wheel.py +++ b/tests/test_wheel.py @@ -141,12 +141,14 @@ def test_configure_data(wheel_configure_data): @pytest.mark.skipif(platform.system() != 'Linux', reason='Unsupported on this platform for now') def test_local_lib(venv, wheel_link_against_local_lib): - subprocess.check_call([ - venv.executable, '-m', 'pip', 'install', wheel_link_against_local_lib - ]) - assert subprocess.check_output([ - venv.executable, '-c', 'import example; print(example.example_sum(1, 2))' - ]).decode().strip() == '3' + subprocess.run( + [venv.executable, '-m', 'pip', 'install', wheel_link_against_local_lib], + check=True) + output = subprocess.run( + [venv.executable, '-c', 'import example; print(example.example_sum(1, 2))'], + stdout=subprocess.PIPE, + check=True).stdout + assert int(output) == 3 def test_contents_license_file(wheel_license_file):