-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_environment_pip.py
79 lines (65 loc) · 2.39 KB
/
test_environment_pip.py
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
import os
from unittest import mock
import pytest
from hipercow import root
from hipercow.environment import environment_engine, environment_new
from hipercow.environment_engines import Pip
from hipercow.util import file_create, transient_working_directory
def test_pip_environment_does_not_exist_until_created(tmp_path):
root.init(tmp_path)
r = root.open_root(tmp_path)
env = Pip(r, "default")
assert not env.exists()
env.path().mkdir(parents=True)
assert env.exists()
def test_pip_environment_can_be_created(tmp_path, mocker):
mock_run = mock.MagicMock()
mocker.patch("subprocess.run", mock_run)
root.init(tmp_path)
file_create(tmp_path / "requirements.txt")
r = root.open_root(tmp_path)
environment_new(r, "default", "pip")
env = environment_engine(r, "default")
assert isinstance(env, Pip)
venv_path = str(env.path())
envvars = env._envvars()
env.create()
assert mock_run.call_count == 1
assert mock_run.mock_calls[0] == mock.call(
["python", "-m", "venv", venv_path], check=True, env=os.environ
)
with transient_working_directory(tmp_path):
env.provision(None)
assert mock_run.call_count == 2
assert mock_run.mock_calls[1] == mock.call(
["pip", "install", "--verbose", "-r", "requirements.txt"],
env=os.environ | envvars,
check=True,
)
def test_pip_can_determine_sensible_default_cmd(tmp_path):
root.init(tmp_path)
root.init(tmp_path)
r = root.open_root(tmp_path)
env = Pip(r, "default")
with transient_working_directory(tmp_path):
with pytest.raises(Exception, match="Can't determine install"):
env._auto()
file_create(tmp_path / "requirements.txt")
assert env._auto() == [
"pip",
"install",
"--verbose",
"-r",
"requirements.txt",
]
file_create(tmp_path / "pyproject.toml")
assert env._auto() == ["pip", "install", "--verbose", "."]
assert env._check_args(None) == ["pip", "install", "--verbose", "."]
assert env._check_args([]) == ["pip", "install", "--verbose", "."]
assert env._check_args(["pip", "install", "x"]) == [
"pip",
"install",
"x",
]
with pytest.raises(Exception, match="Expected first element"):
assert env._check_args(["pwd"])