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
104 lines (78 loc) · 3.34 KB
/
test_create_venv.py
File metadata and controls
104 lines (78 loc) · 3.34 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
# 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"
def test_pip_not_installed():
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:
create_venv.main(["--install"])
assert str(e.value) == "CREATE_VENV.PIP_NOT_FOUND"
@pytest.mark.parametrize("env_exists", [True, False])
@pytest.mark.parametrize("git_ignore", [True, False])
@pytest.mark.parametrize("install", [True, False])
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
install_packages_called = False
def install_packages(_name):
nonlocal install_packages_called
install_packages_called = True
create_venv.install_packages = install_packages
run_process_called = False
def run_process(args, error_message):
nonlocal run_process_called
run_process_called = True
if not env_exists:
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:
args.append("--git-ignore")
if install:
args.append("--install")
create_venv.main(args)
assert install_packages_called == install
# run_process is called when the venv does not exist
assert run_process_called != env_exists
# add_gitignore is called when new venv is created and git_ignore is True
assert add_gitignore_called == (not env_exists and git_ignore)
@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", ".[extras]"]:
installing = "pyproject"
assert error_message == "CREATE_VENV.PIP_FAILED_INSTALL_PYPROJECT"
create_venv.run_process = run_process
create_venv.main(["--install"])
assert pip_upgraded
assert installing == install_type