Skip to content

Commit f185d23

Browse files
committed
Update tests
1 parent 64cf1de commit f185d23

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def pytest_runtest_call(item: pytest.Item):
7575

7676
@pytest.fixture()
7777
def local_pip(monkeypatch):
78-
monkeypatch.setattr(build.env, '_valid_global_pip', lambda: None)
78+
monkeypatch.setattr(build.env._PipEnvBackendStub, '_has_valid_outer_pip', None)
7979

8080

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

130130
@pytest.fixture(autouse=True)
131131
def force_venv(mocker):
132-
mocker.patch.object(build.env, '_should_use_virtualenv', lambda: False)
132+
mocker.patch.object(build.env, '_has_virtualenv', lambda: False)
133133

134134

135135
def pytest_report_header() -> str:

tests/test_env.py

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# SPDX-License-Identifier: MIT
2-
import collections
32
import logging
43
import platform
54
import subprocess
65
import sys
76
import sysconfig
87

8+
from types import SimpleNamespace
9+
910
import pytest
11+
import pytest_mock
1012

1113
from packaging.version import Version
1214

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

2830
@pytest.mark.isolated
2931
@pytest.mark.usefixtures('local_pip')
30-
def test_isolated_environment_install(mocker):
32+
def test_isolated_environment_install(mocker: pytest_mock.MockerFixture):
3133
with build.env.DefaultIsolatedEnv() as env:
32-
mocker.patch('build.env.run_subprocess')
34+
run_subprocess = mocker.patch('build.env.run_subprocess')
3335

3436
env.install([])
35-
build.env.run_subprocess.assert_not_called()
37+
run_subprocess.assert_not_called()
3638

3739
env.install(['some', 'requirements'])
38-
build.env.run_subprocess.assert_called()
39-
args = build.env.run_subprocess.call_args[0][0][:-1]
40+
run_subprocess.assert_called()
41+
args = run_subprocess.call_args[0][0][:-1]
4042
assert args == [
4143
env.python_executable,
4244
'-Im',
@@ -50,15 +52,19 @@ def test_isolated_environment_install(mocker):
5052

5153
@pytest.mark.skipif(IS_PYPY3, reason='PyPy3 uses get path to create and provision venv')
5254
@pytest.mark.skipif(sys.platform != 'darwin', reason='workaround for Apple Python')
53-
def test_can_get_venv_paths_with_conflicting_default_scheme(mocker):
55+
def test_can_get_venv_paths_with_conflicting_default_scheme(
56+
mocker: pytest_mock.MockerFixture,
57+
):
5458
get_scheme_names = mocker.patch('sysconfig.get_scheme_names', return_value=('osx_framework_library',))
5559
with build.env.DefaultIsolatedEnv():
5660
pass
5761
assert get_scheme_names.call_count == 1
5862

5963

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

7076

71-
def test_executable_missing_post_creation(mocker):
77+
def test_executable_missing_post_creation(
78+
mocker: pytest_mock.MockerFixture,
79+
):
7280
venv_create = mocker.patch('venv.EnvBuilder.create')
7381
with pytest.raises(RuntimeError, match='Virtual environment creation failed, executable .* missing'):
7482
with build.env.DefaultIsolatedEnv():
@@ -80,36 +88,36 @@ def test_isolated_env_abstract():
8088
with pytest.raises(TypeError):
8189
build.env.IsolatedEnv()
8290

83-
84-
def test_isolated_env_has_executable_still_abstract():
85-
class Env(build.env.IsolatedEnv):
91+
class PartialEnv(build.env.IsolatedEnv):
8692
@property
8793
def executable(self):
8894
raise NotImplementedError
8995

9096
with pytest.raises(TypeError):
91-
Env()
97+
PartialEnv()
9298

93-
94-
def test_isolated_env_has_install_still_abstract():
95-
class Env(build.env.IsolatedEnv):
96-
def install(self, requirements):
97-
raise NotImplementedError
99+
class PartialEnv(build.env.IsolatedEnv):
100+
def make_extra_environ(self):
101+
return super().make_extra_environ()
98102

99103
with pytest.raises(TypeError):
100-
Env()
104+
PartialEnv()
101105

102106

103107
@pytest.mark.pypy3323bug
104-
def test_isolated_env_log(mocker, caplog, package_test_flit):
105-
mocker.patch('build.env.run_subprocess')
108+
@pytest.mark.usefixtures('package_test_flit')
109+
def test_isolated_env_log(
110+
caplog: pytest.LogCaptureFixture,
111+
mocker: pytest_mock.MockerFixture,
112+
):
106113
caplog.set_level(logging.DEBUG)
114+
mocker.patch('build.env.run_subprocess')
107115

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

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

@@ -130,32 +138,33 @@ def test_default_pip_is_never_too_old():
130138
@pytest.mark.parametrize('pip_version', ['20.2.0', '20.3.0', '21.0.0', '21.0.1'])
131139
@pytest.mark.parametrize('arch', ['x86_64', 'arm64'])
132140
@pytest.mark.usefixtures('local_pip')
133-
def test_pip_needs_upgrade_mac_os_11(mocker, pip_version, arch):
134-
SimpleNamespace = collections.namedtuple('SimpleNamespace', 'version')
135-
136-
_subprocess = mocker.patch('build.env.run_subprocess')
141+
def test_pip_needs_upgrade_mac_os_11(
142+
mocker: pytest_mock.MockerFixture,
143+
pip_version: str,
144+
arch: str,
145+
):
146+
run_subprocess = mocker.patch('build.env.run_subprocess')
137147
mocker.patch('platform.system', return_value='Darwin')
138-
mocker.patch('platform.machine', return_value=arch)
139-
mocker.patch('platform.mac_ver', return_value=('11.0', ('', '', ''), ''))
148+
mocker.patch('platform.mac_ver', return_value=('11.0', ('', '', ''), arch))
140149
mocker.patch('build._compat.importlib.metadata.distributions', return_value=(SimpleNamespace(version=pip_version),))
141150

142151
min_version = Version('20.3' if arch == 'x86_64' else '21.0.1')
143152
with build.env.DefaultIsolatedEnv():
144153
if Version(pip_version) < min_version:
145-
print(_subprocess.call_args_list)
146-
upgrade_call, uninstall_call = _subprocess.call_args_list
154+
upgrade_call, uninstall_call = run_subprocess.call_args_list
147155
answer = 'pip>=20.3.0' if arch == 'x86_64' else 'pip>=21.0.1'
148-
assert upgrade_call[0][0][1:] == ['-m', 'pip', 'install', answer]
149-
assert uninstall_call[0][0][1:] == ['-m', 'pip', 'uninstall', 'setuptools', '-y']
156+
assert upgrade_call[0][0][1:] == ['-Im', 'pip', 'install', answer]
157+
assert uninstall_call[0][0][1:] == ['-Im', 'pip', 'uninstall', '-y', 'setuptools']
150158
else:
151-
(uninstall_call,) = _subprocess.call_args_list
152-
assert uninstall_call[0][0][1:] == ['-m', 'pip', 'uninstall', 'setuptools', '-y']
159+
(uninstall_call,) = run_subprocess.call_args_list
160+
assert uninstall_call[0][0][1:] == ['-Im', 'pip', 'uninstall', '-y', 'setuptools']
153161

154162

155-
@pytest.mark.isolated
156-
@pytest.mark.skipif(IS_PYPY3 and sys.platform.startswith('win'), reason='Isolated tests not supported on PyPy3 + Windows')
157163
@pytest.mark.parametrize('has_symlink', [True, False] if sys.platform.startswith('win') else [True])
158-
def test_venv_symlink(mocker, has_symlink):
164+
def test_venv_symlink(
165+
mocker: pytest_mock.MockerFixture,
166+
has_symlink: bool,
167+
):
159168
if has_symlink:
160169
mocker.patch('os.symlink')
161170
mocker.patch('os.unlink')

tests/test_main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ def test_build_package_via_sdist_invalid_distribution(tmp_dir, package_test_setu
231231
pytest.param(
232232
[],
233233
[
234-
'* Creating venv isolated environment...',
234+
'* Creating isolated environment: venv...',
235235
'* Installing packages in isolated environment:',
236236
' - setuptools >= 42.0.0',
237237
'* Getting build dependencies for sdist...',
238238
'* Building sdist...',
239239
'* Building wheel from sdist',
240-
'* Creating venv isolated environment...',
240+
'* Creating isolated environment: venv...',
241241
'* Installing packages in isolated environment:',
242242
' - setuptools >= 42.0.0',
243243
'* Getting build dependencies for wheel...',
@@ -264,7 +264,7 @@ def test_build_package_via_sdist_invalid_distribution(tmp_dir, package_test_setu
264264
pytest.param(
265265
['--wheel'],
266266
[
267-
'* Creating venv isolated environment...',
267+
'* Creating isolated environment: venv...',
268268
'* Installing packages in isolated environment:',
269269
' - setuptools >= 42.0.0',
270270
'* Getting build dependencies for wheel...',
@@ -322,7 +322,7 @@ def test_output(package_test_setuptools, tmp_dir, capsys, args, output):
322322
False,
323323
'ERROR ',
324324
[
325-
'* Creating venv isolated environment...',
325+
'* Creating isolated environment: venv...',
326326
'* Installing packages in isolated environment:',
327327
' - setuptools >= 42.0.0',
328328
' - this is invalid',
@@ -332,7 +332,7 @@ def test_output(package_test_setuptools, tmp_dir, capsys, args, output):
332332
True,
333333
'\33[91mERROR\33[0m ',
334334
[
335-
'\33[1m* Creating venv isolated environment...\33[0m',
335+
'\33[1m* Creating isolated environment: venv...\33[0m',
336336
'\33[1m* Installing packages in isolated environment:\33[0m',
337337
' - setuptools >= 42.0.0',
338338
' - this is invalid',
@@ -424,7 +424,7 @@ def raise_called_process_err(*args, **kwargs):
424424
assert (
425425
stdout
426426
== """\
427-
* Creating venv isolated environment...
427+
* Creating isolated environment: venv...
428428
> test args
429429
< stdoutput
430430
ERROR Failed to create venv. Maybe try installing virtualenv.

0 commit comments

Comments
 (0)