forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_create_venv.py
More file actions
182 lines (140 loc) · 5.61 KB
/
test_create_venv.py
File metadata and controls
182 lines (140 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import importlib
import sys
import create_venv
import pytest
def test_venv_not_installed():
importlib.reload(create_venv)
create_venv.is_installed = lambda module: module != "venv"
with pytest.raises(create_venv.VenvError) as e:
create_venv.main()
assert str(e.value) == "CREATE_VENV.VENV_NOT_FOUND"
@pytest.mark.parametrize("install", ["requirements", "toml"])
def test_pip_not_installed(install):
importlib.reload(create_venv)
create_venv.venv_exists = lambda _n: True
create_venv.is_installed = lambda module: module != "pip"
create_venv.run_process = lambda _args, _error_message: None
with pytest.raises(create_venv.VenvError) as e:
if install == "requirements":
create_venv.main(["--requirements", "requirements-for-test.txt"])
elif install == "toml":
create_venv.main(["--toml", "pyproject.toml", "--extras", "test"])
assert str(e.value) == "CREATE_VENV.PIP_NOT_FOUND"
@pytest.mark.parametrize("env_exists", ["hasEnv", "noEnv"])
@pytest.mark.parametrize("git_ignore", ["useGitIgnore", "skipGitIgnore"])
@pytest.mark.parametrize("install", ["requirements", "toml", "skipInstall"])
def test_create_env(env_exists, git_ignore, install):
importlib.reload(create_venv)
create_venv.is_installed = lambda _x: True
create_venv.venv_exists = lambda _n: env_exists == "hasEnv"
create_venv.upgrade_pip = lambda _x: None
install_packages_called = False
def install_packages(_env, _name):
nonlocal install_packages_called
install_packages_called = True
create_venv.install_requirements = install_packages
create_venv.install_toml = install_packages
run_process_called = False
def run_process(args, error_message):
nonlocal run_process_called
run_process_called = True
if env_exists == "noEnv":
assert args == [sys.executable, "-m", "venv", create_venv.VENV_NAME]
assert error_message == "CREATE_VENV.VENV_FAILED_CREATION"
create_venv.run_process = run_process
add_gitignore_called = False
def add_gitignore(_name):
nonlocal add_gitignore_called
add_gitignore_called = True
create_venv.add_gitignore = add_gitignore
args = []
if git_ignore == "useGitIgnore":
args += ["--git-ignore"]
if install == "requirements":
args += ["--requirements", "requirements-for-test.txt"]
elif install == "toml":
args += ["--toml", "pyproject.toml", "--extras", "test"]
create_venv.main(args)
assert install_packages_called == (install != "skipInstall")
# run_process is called when the venv does not exist
assert run_process_called == (env_exists == "noEnv")
# add_gitignore is called when new venv is created and git_ignore is True
assert add_gitignore_called == (
(env_exists == "noEnv") and (git_ignore == "useGitIgnore")
)
@pytest.mark.parametrize("install_type", ["requirements", "pyproject"])
def test_install_packages(install_type):
importlib.reload(create_venv)
create_venv.is_installed = lambda _x: True
create_venv.file_exists = lambda x: install_type in x
pip_upgraded = False
installing = None
def run_process(args, error_message):
nonlocal pip_upgraded, installing
if args[1:] == ["-m", "pip", "install", "--upgrade", "pip"]:
pip_upgraded = True
assert error_message == "CREATE_VENV.PIP_UPGRADE_FAILED"
elif args[1:-1] == ["-m", "pip", "install", "-r"]:
installing = "requirements"
assert error_message == "CREATE_VENV.PIP_FAILED_INSTALL_REQUIREMENTS"
elif args[1:] == ["-m", "pip", "install", "-e", ".[test]"]:
installing = "pyproject"
assert error_message == "CREATE_VENV.PIP_FAILED_INSTALL_PYPROJECT"
create_venv.run_process = run_process
if install_type == "requirements":
create_venv.main(["--requirements", "requirements-for-test.txt"])
elif install_type == "pyproject":
create_venv.main(["--toml", "pyproject.toml", "--extras", "test"])
assert pip_upgraded
assert installing == install_type
@pytest.mark.parametrize(
("extras", "expected"),
[
([], ["-m", "pip", "install", "-e", "."]),
(["test"], ["-m", "pip", "install", "-e", ".[test]"]),
(["test", "doc"], ["-m", "pip", "install", "-e", ".[test,doc]"]),
],
)
def test_toml_args(extras, expected):
importlib.reload(create_venv)
actual = []
def run_process(args, error_message):
nonlocal actual
actual = args[1:]
create_venv.run_process = run_process
create_venv.install_toml(sys.executable, extras)
assert actual == expected
@pytest.mark.parametrize(
("extras", "expected"),
[
([], None),
(
["requirements/test.txt"],
[sys.executable, "-m", "pip", "install", "-r", "requirements/test.txt"],
),
(
["requirements/test.txt", "requirements/doc.txt"],
[
sys.executable,
"-m",
"pip",
"install",
"-r",
"requirements/test.txt",
"-r",
"requirements/doc.txt",
],
),
],
)
def test_requirements_args(extras, expected):
importlib.reload(create_venv)
actual = None
def run_process(args, error_message):
nonlocal actual
actual = args
create_venv.run_process = run_process
create_venv.install_requirements(sys.executable, extras)
assert actual == expected