4
4
5
5
import pytest
6
6
7
+ from poetry .packages import Locker
8
+
7
9
8
10
if TYPE_CHECKING :
11
+ import httpretty
12
+
9
13
from cleo .testers .command_tester import CommandTester
10
14
from pytest_mock import MockerFixture
11
15
16
+ from poetry .poetry import Poetry
12
17
from tests .types import CommandTesterFactory
13
18
from tests .types import FixtureDirGetter
19
+ from tests .types import ProjectFactory
14
20
15
21
16
22
@pytest .fixture ()
17
23
def tester (command_tester_factory : CommandTesterFactory ) -> CommandTester :
18
24
return command_tester_factory ("check" )
19
25
20
26
27
+ def _project_factory (
28
+ fixture_name : str ,
29
+ project_factory : ProjectFactory ,
30
+ fixture_dir : FixtureDirGetter ,
31
+ ) -> Poetry :
32
+ source = fixture_dir (fixture_name )
33
+ pyproject_content = (source / "pyproject.toml" ).read_text (encoding = "utf-8" )
34
+ poetry_lock_content = (source / "poetry.lock" ).read_text (encoding = "utf-8" )
35
+ return project_factory (
36
+ name = "foobar" ,
37
+ pyproject_content = pyproject_content ,
38
+ poetry_lock_content = poetry_lock_content ,
39
+ source = source ,
40
+ )
41
+
42
+
43
+ @pytest .fixture
44
+ def poetry_with_outdated_lockfile (
45
+ project_factory : ProjectFactory , fixture_dir : FixtureDirGetter
46
+ ) -> Poetry :
47
+ return _project_factory ("outdated_lock" , project_factory , fixture_dir )
48
+
49
+
50
+ @pytest .fixture
51
+ def poetry_with_up_to_date_lockfile (
52
+ project_factory : ProjectFactory , fixture_dir : FixtureDirGetter
53
+ ) -> Poetry :
54
+ return _project_factory ("up_to_date_lock" , project_factory , fixture_dir )
55
+
56
+
21
57
def test_check_valid (tester : CommandTester ) -> None :
22
58
tester .execute ()
23
59
@@ -39,12 +75,13 @@ def test_check_invalid(
39
75
new_callable = mocker .PropertyMock ,
40
76
)
41
77
42
- tester .execute ()
78
+ tester .execute ("--lock" )
43
79
44
80
expected = """\
45
81
Error: 'description' is a required property
46
82
Error: Project name (invalid) is same as one of its dependencies
47
83
Error: Unrecognized classifiers: ['Intended Audience :: Clowns'].
84
+ Error: poetry.lock was not found.
48
85
Warning: A wildcard Python dependency is ambiguous.\
49
86
Consider specifying a more explicit one.
50
87
Warning: The "pendulum" dependency specifies the "allows-prereleases" property,\
@@ -74,3 +111,88 @@ def test_check_private(
74
111
"""
75
112
76
113
assert tester .io .fetch_output () == expected
114
+
115
+
116
+ @pytest .mark .parametrize (
117
+ ("options" , "expected" , "expected_status" ),
118
+ [
119
+ ("" , "All set!\n " , 0 ),
120
+ ("--lock" , "Error: poetry.lock was not found.\n " , 1 ),
121
+ ],
122
+ )
123
+ def test_check_lock_missing (
124
+ mocker : MockerFixture ,
125
+ tester : CommandTester ,
126
+ fixture_dir : FixtureDirGetter ,
127
+ options : str ,
128
+ expected : str ,
129
+ expected_status : int ,
130
+ ) -> None :
131
+ from poetry .toml import TOMLFile
132
+
133
+ mocker .patch (
134
+ "poetry.poetry.Poetry.file" ,
135
+ return_value = TOMLFile (fixture_dir ("private_pyproject" ) / "pyproject.toml" ),
136
+ new_callable = mocker .PropertyMock ,
137
+ )
138
+
139
+ status_code = tester .execute (options )
140
+
141
+ assert status_code == expected_status
142
+
143
+ if status_code == 0 :
144
+ assert tester .io .fetch_output () == expected
145
+ else :
146
+ assert tester .io .fetch_error () == expected
147
+
148
+
149
+ @pytest .mark .parametrize ("options" , ["" , "--lock" ])
150
+ def test_check_lock_outdated (
151
+ command_tester_factory : CommandTesterFactory ,
152
+ poetry_with_outdated_lockfile : Poetry ,
153
+ http : type [httpretty .httpretty ],
154
+ options : str ,
155
+ ) -> None :
156
+ http .disable ()
157
+
158
+ locker = Locker (
159
+ lock = poetry_with_outdated_lockfile .pyproject .file .path .parent / "poetry.lock" ,
160
+ local_config = poetry_with_outdated_lockfile .locker ._local_config ,
161
+ )
162
+ poetry_with_outdated_lockfile .set_locker (locker )
163
+
164
+ tester = command_tester_factory ("check" , poetry = poetry_with_outdated_lockfile )
165
+ status_code = tester .execute (options )
166
+ expected = (
167
+ "Error: poetry.lock is not consistent with pyproject.toml. "
168
+ "Run `poetry lock [--no-update]` to fix it.\n "
169
+ )
170
+
171
+ assert tester .io .fetch_error () == expected
172
+
173
+ # exit with an error
174
+ assert status_code == 1
175
+
176
+
177
+ @pytest .mark .parametrize ("options" , ["" , "--lock" ])
178
+ def test_check_lock_up_to_date (
179
+ command_tester_factory : CommandTesterFactory ,
180
+ poetry_with_up_to_date_lockfile : Poetry ,
181
+ http : type [httpretty .httpretty ],
182
+ options : str ,
183
+ ) -> None :
184
+ http .disable ()
185
+
186
+ locker = Locker (
187
+ lock = poetry_with_up_to_date_lockfile .pyproject .file .path .parent / "poetry.lock" ,
188
+ local_config = poetry_with_up_to_date_lockfile .locker ._local_config ,
189
+ )
190
+ poetry_with_up_to_date_lockfile .set_locker (locker )
191
+
192
+ tester = command_tester_factory ("check" , poetry = poetry_with_up_to_date_lockfile )
193
+ status_code = tester .execute (options )
194
+ expected = "All set!\n "
195
+ assert tester .io .fetch_output () == expected
196
+
197
+ # exit with an error
198
+ assert status_code == 0
0 commit comments