-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_cli_sync.py
176 lines (131 loc) · 5.17 KB
/
test_cli_sync.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
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
import sys
import mock
import pytest
from .utils import invoke
from piptools.scripts.sync import cli
def test_run_as_module_sync():
"""piptools can be run as ``python -m piptools ...``."""
status, output = invoke([sys.executable, "-m", "piptools", "sync", "--help"])
# Should have run pip-compile successfully.
output = output.decode("utf-8")
assert output.startswith("Usage:")
assert "Synchronize virtual environment with" in output
assert status == 0
@mock.patch("piptools.sync.check_call")
def test_quiet_option(check_call, runner):
"""sync command can be run with `--quiet` or `-q` flag."""
with open("requirements.txt", "w") as req_in:
req_in.write("six==1.10.0")
out = runner.invoke(cli, ["-q"])
assert not out.stderr_bytes
assert out.exit_code == 0
# for every call to pip ensure the `-q` flag is set
assert check_call.call_count == 2
for call in check_call.call_args_list:
assert "-q" in call[0][0]
@mock.patch("piptools.sync.check_call")
def test_quiet_option_when_up_to_date(check_call, runner):
"""
Sync should output nothing when everything is up to date and quiet option is set.
"""
with open("requirements.txt", "w"):
pass
with mock.patch("piptools.sync.diff", return_value=(set(), set())):
out = runner.invoke(cli, ["-q"])
assert not out.stderr_bytes
assert out.exit_code == 0
check_call.assert_not_called()
def test_no_requirements_file(runner):
"""
It should raise an error if there are no input files
or a requirements.txt file does not exist.
"""
out = runner.invoke(cli)
assert "No requirement files given" in out.stderr
assert out.exit_code == 2
def test_input_files_with_dot_in_extension(runner):
"""
It should raise an error if some of the input files have .in extension.
"""
with open("requirements.in", "w") as req_in:
req_in.write("six==1.10.0")
out = runner.invoke(cli, ["requirements.in"])
assert "ERROR: Some input files have the .in extension" in out.stderr
assert out.exit_code == 2
def test_force_files_with_dot_in_extension(runner):
"""
It should print a warning and sync anyway if some of the input files
have .in extension.
"""
with open("requirements.in", "w") as req_in:
req_in.write("six==1.10.0")
with mock.patch("piptools.sync.check_call"):
out = runner.invoke(cli, ["requirements.in", "--force"])
assert "WARNING: Some input files have the .in extension" in out.stderr
assert out.exit_code == 0
def test_merge_error(runner):
"""
Sync command should raise an error if there are merge errors.
"""
with open("requirements.txt", "w") as req_in:
req_in.write("six>1.10.0\n")
# Add incompatible package
req_in.write("six<1.10.0")
with mock.patch("piptools.sync.check_call"):
out = runner.invoke(cli, ["-n"])
assert out.exit_code == 2
assert "Incompatible requirements found" in out.stderr
@pytest.mark.parametrize(
("cli_flags", "expected_install_flags"),
[
(["--find-links", "./libs"], ["-f", "./libs"]),
(["--no-index"], ["--no-index"]),
(["--index-url", "https://example.com"], ["-i", "https://example.com"]),
(
["--extra-index-url", "https://foo", "--extra-index-url", "https://bar"],
["--extra-index-url", "https://foo", "--extra-index-url", "https://bar"],
),
(
["--trusted-host", "https://foo", "--trusted-host", "https://bar"],
["--trusted-host", "https://foo", "--trusted-host", "https://bar"],
),
(
["--extra-index-url", "https://foo", "--trusted-host", "https://bar"],
["--extra-index-url", "https://foo", "--trusted-host", "https://bar"],
),
(["--user"], ["--user"]),
(["--cert", "foo.crt"], ["--cert", "foo.crt"]),
(["--client-cert", "foo.pem"], ["--client-cert", "foo.pem"]),
],
)
@mock.patch("piptools.sync.check_call")
def test_pip_install_flags(check_call, cli_flags, expected_install_flags, runner):
"""
Test the cli flags have to be passed to the pip install command.
"""
with open("requirements.txt", "w") as req_in:
req_in.write("six==1.10.0")
runner.invoke(cli, cli_flags)
call_args = [call[0][0] for call in check_call.call_args_list]
assert [args[6:] for args in call_args if args[3] == "install"] == [
expected_install_flags
]
@mock.patch("piptools.sync.check_call")
def test_sync_ask_declined(check_call, runner):
"""
Make sure nothing is installed if the confirmation is declined
"""
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
runner.invoke(cli, ["--ask"], input="n\n")
check_call.assert_not_called()
@mock.patch("piptools.sync.check_call")
def test_sync_ask_accepted(check_call, runner):
"""
Make sure pip is called when the confirmation is accepted (even if
--dry-run is given)
"""
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
runner.invoke(cli, ["--ask", "--dry-run"], input="y\n")
assert check_call.call_count == 2