Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
layday committed Mar 5, 2024
1 parent 64cf1de commit f185d23
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def pytest_runtest_call(item: pytest.Item):

@pytest.fixture()
def local_pip(monkeypatch):
monkeypatch.setattr(build.env, '_valid_global_pip', lambda: None)
monkeypatch.setattr(build.env._PipEnvBackendStub, '_has_valid_outer_pip', None)


@pytest.fixture(scope='session', autouse=True)
Expand Down Expand Up @@ -129,7 +129,7 @@ def tmp_dir():

@pytest.fixture(autouse=True)
def force_venv(mocker):
mocker.patch.object(build.env, '_should_use_virtualenv', lambda: False)
mocker.patch.object(build.env, '_has_virtualenv', lambda: False)


def pytest_report_header() -> str:
Expand Down
83 changes: 46 additions & 37 deletions tests/test_env.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# SPDX-License-Identifier: MIT
import collections
import logging
import platform
import subprocess
import sys
import sysconfig

from types import SimpleNamespace

import pytest
import pytest_mock

from packaging.version import Version

Expand All @@ -27,16 +29,16 @@ def test_isolation():

@pytest.mark.isolated
@pytest.mark.usefixtures('local_pip')
def test_isolated_environment_install(mocker):
def test_isolated_environment_install(mocker: pytest_mock.MockerFixture):
with build.env.DefaultIsolatedEnv() as env:
mocker.patch('build.env.run_subprocess')
run_subprocess = mocker.patch('build.env.run_subprocess')

env.install([])
build.env.run_subprocess.assert_not_called()
run_subprocess.assert_not_called()

env.install(['some', 'requirements'])
build.env.run_subprocess.assert_called()
args = build.env.run_subprocess.call_args[0][0][:-1]
run_subprocess.assert_called()
args = run_subprocess.call_args[0][0][:-1]
assert args == [
env.python_executable,
'-Im',
Expand All @@ -50,15 +52,19 @@ def test_isolated_environment_install(mocker):

@pytest.mark.skipif(IS_PYPY3, reason='PyPy3 uses get path to create and provision venv')
@pytest.mark.skipif(sys.platform != 'darwin', reason='workaround for Apple Python')
def test_can_get_venv_paths_with_conflicting_default_scheme(mocker):
def test_can_get_venv_paths_with_conflicting_default_scheme(
mocker: pytest_mock.MockerFixture,
):
get_scheme_names = mocker.patch('sysconfig.get_scheme_names', return_value=('osx_framework_library',))
with build.env.DefaultIsolatedEnv():
pass
assert get_scheme_names.call_count == 1


@pytest.mark.skipif('posix_local' not in sysconfig.get_scheme_names(), reason='workaround for Debian/Ubuntu Python')
def test_can_get_venv_paths_with_posix_local_default_scheme(mocker):
def test_can_get_venv_paths_with_posix_local_default_scheme(
mocker: pytest_mock.MockerFixture,
):
get_paths = mocker.spy(sysconfig, 'get_paths')
# We should never call this, but we patch it to ensure failure if we do
get_default_scheme = mocker.patch('sysconfig.get_default_scheme', return_value='posix_local')
Expand All @@ -68,7 +74,9 @@ def test_can_get_venv_paths_with_posix_local_default_scheme(mocker):
assert get_default_scheme.call_count == 0


def test_executable_missing_post_creation(mocker):
def test_executable_missing_post_creation(
mocker: pytest_mock.MockerFixture,
):
venv_create = mocker.patch('venv.EnvBuilder.create')
with pytest.raises(RuntimeError, match='Virtual environment creation failed, executable .* missing'):
with build.env.DefaultIsolatedEnv():
Expand All @@ -80,36 +88,36 @@ def test_isolated_env_abstract():
with pytest.raises(TypeError):
build.env.IsolatedEnv()


def test_isolated_env_has_executable_still_abstract():
class Env(build.env.IsolatedEnv):
class PartialEnv(build.env.IsolatedEnv):
@property
def executable(self):
raise NotImplementedError

with pytest.raises(TypeError):
Env()
PartialEnv()


def test_isolated_env_has_install_still_abstract():
class Env(build.env.IsolatedEnv):
def install(self, requirements):
raise NotImplementedError
class PartialEnv(build.env.IsolatedEnv):
def make_extra_environ(self):
return super().make_extra_environ()

with pytest.raises(TypeError):
Env()
PartialEnv()


@pytest.mark.pypy3323bug
def test_isolated_env_log(mocker, caplog, package_test_flit):
mocker.patch('build.env.run_subprocess')
@pytest.mark.usefixtures('package_test_flit')
def test_isolated_env_log(
caplog: pytest.LogCaptureFixture,
mocker: pytest_mock.MockerFixture,
):
caplog.set_level(logging.DEBUG)
mocker.patch('build.env.run_subprocess')

with build.env.DefaultIsolatedEnv() as env:
env.install(['something'])

assert [(record.levelname, record.message) for record in caplog.records] == [
('INFO', 'Creating venv isolated environment...'),
('INFO', 'Creating isolated environment: venv...'),
('INFO', 'Installing packages in isolated environment:\n- something'),
]

Expand All @@ -130,32 +138,33 @@ def test_default_pip_is_never_too_old():
@pytest.mark.parametrize('pip_version', ['20.2.0', '20.3.0', '21.0.0', '21.0.1'])
@pytest.mark.parametrize('arch', ['x86_64', 'arm64'])
@pytest.mark.usefixtures('local_pip')
def test_pip_needs_upgrade_mac_os_11(mocker, pip_version, arch):
SimpleNamespace = collections.namedtuple('SimpleNamespace', 'version')

_subprocess = mocker.patch('build.env.run_subprocess')
def test_pip_needs_upgrade_mac_os_11(
mocker: pytest_mock.MockerFixture,
pip_version: str,
arch: str,
):
run_subprocess = mocker.patch('build.env.run_subprocess')
mocker.patch('platform.system', return_value='Darwin')
mocker.patch('platform.machine', return_value=arch)
mocker.patch('platform.mac_ver', return_value=('11.0', ('', '', ''), ''))
mocker.patch('platform.mac_ver', return_value=('11.0', ('', '', ''), arch))
mocker.patch('build._compat.importlib.metadata.distributions', return_value=(SimpleNamespace(version=pip_version),))

min_version = Version('20.3' if arch == 'x86_64' else '21.0.1')
with build.env.DefaultIsolatedEnv():
if Version(pip_version) < min_version:
print(_subprocess.call_args_list)
upgrade_call, uninstall_call = _subprocess.call_args_list
upgrade_call, uninstall_call = run_subprocess.call_args_list
answer = 'pip>=20.3.0' if arch == 'x86_64' else 'pip>=21.0.1'
assert upgrade_call[0][0][1:] == ['-m', 'pip', 'install', answer]
assert uninstall_call[0][0][1:] == ['-m', 'pip', 'uninstall', 'setuptools', '-y']
assert upgrade_call[0][0][1:] == ['-Im', 'pip', 'install', answer]
assert uninstall_call[0][0][1:] == ['-Im', 'pip', 'uninstall', '-y', 'setuptools']
else:
(uninstall_call,) = _subprocess.call_args_list
assert uninstall_call[0][0][1:] == ['-m', 'pip', 'uninstall', 'setuptools', '-y']
(uninstall_call,) = run_subprocess.call_args_list
assert uninstall_call[0][0][1:] == ['-Im', 'pip', 'uninstall', '-y', 'setuptools']


@pytest.mark.isolated
@pytest.mark.skipif(IS_PYPY3 and sys.platform.startswith('win'), reason='Isolated tests not supported on PyPy3 + Windows')
@pytest.mark.parametrize('has_symlink', [True, False] if sys.platform.startswith('win') else [True])
def test_venv_symlink(mocker, has_symlink):
def test_venv_symlink(
mocker: pytest_mock.MockerFixture,
has_symlink: bool,
):
if has_symlink:
mocker.patch('os.symlink')
mocker.patch('os.unlink')
Expand Down
12 changes: 6 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ def test_build_package_via_sdist_invalid_distribution(tmp_dir, package_test_setu
pytest.param(
[],
[
'* Creating venv isolated environment...',
'* Creating isolated environment: venv...',
'* Installing packages in isolated environment:',
' - setuptools >= 42.0.0',
'* Getting build dependencies for sdist...',
'* Building sdist...',
'* Building wheel from sdist',
'* Creating venv isolated environment...',
'* Creating isolated environment: venv...',
'* Installing packages in isolated environment:',
' - setuptools >= 42.0.0',
'* Getting build dependencies for wheel...',
Expand All @@ -264,7 +264,7 @@ def test_build_package_via_sdist_invalid_distribution(tmp_dir, package_test_setu
pytest.param(
['--wheel'],
[
'* Creating venv isolated environment...',
'* Creating isolated environment: venv...',
'* Installing packages in isolated environment:',
' - setuptools >= 42.0.0',
'* Getting build dependencies for wheel...',
Expand Down Expand Up @@ -322,7 +322,7 @@ def test_output(package_test_setuptools, tmp_dir, capsys, args, output):
False,
'ERROR ',
[
'* Creating venv isolated environment...',
'* Creating isolated environment: venv...',
'* Installing packages in isolated environment:',
' - setuptools >= 42.0.0',
' - this is invalid',
Expand All @@ -332,7 +332,7 @@ def test_output(package_test_setuptools, tmp_dir, capsys, args, output):
True,
'\33[91mERROR\33[0m ',
[
'\33[1m* Creating venv isolated environment...\33[0m',
'\33[1m* Creating isolated environment: venv...\33[0m',
'\33[1m* Installing packages in isolated environment:\33[0m',
' - setuptools >= 42.0.0',
' - this is invalid',
Expand Down Expand Up @@ -424,7 +424,7 @@ def raise_called_process_err(*args, **kwargs):
assert (
stdout
== """\
* Creating venv isolated environment...
* Creating isolated environment: venv...
> test args
< stdoutput
ERROR Failed to create venv. Maybe try installing virtualenv.
Expand Down

0 comments on commit f185d23

Please sign in to comment.