5
5
6
6
import pip ._internal .configuration
7
7
from pip ._internal import main
8
+ from pip ._internal .commands import DownloadCommand
8
9
from tests .lib .options_helpers import AddFakeCommandMixin
9
10
10
11
12
+ @contextmanager
13
+ def temp_environment_variable (name , value ):
14
+ not_set = object ()
15
+ original = os .environ [name ] if name in os .environ else not_set
16
+ os .environ [name ] = value
17
+
18
+ try :
19
+ yield
20
+ finally :
21
+ # Return the environment variable to its original state.
22
+ if original is not_set :
23
+ if name in os .environ :
24
+ del os .environ [name ]
25
+ else :
26
+ os .environ [name ] = original
27
+
28
+
11
29
@contextmanager
12
30
def assert_raises_message (exc_class , expected ):
13
31
"""
@@ -19,6 +37,22 @@ def assert_raises_message(exc_class, expected):
19
37
assert str (excinfo .value ) == expected
20
38
21
39
40
+ @contextmanager
41
+ def assert_option_error (capsys , expected ):
42
+ """
43
+ Assert that a SystemExit occurred because of a parsing error.
44
+
45
+ Args:
46
+ expected: an expected substring of stderr.
47
+ """
48
+ with pytest .raises (SystemExit ) as excinfo :
49
+ yield
50
+
51
+ assert excinfo .value .code == 2
52
+ stderr = capsys .readouterr ().err
53
+ assert expected in stderr
54
+
55
+
22
56
def assert_is_default_cache_dir (value ):
23
57
# This path looks different on different platforms, but the path always
24
58
# has the substring "pip".
@@ -157,6 +191,89 @@ def test_cache_dir__PIP_NO_CACHE_DIR_invalid__with_no_cache_dir(self):
157
191
main (['--no-cache-dir' , 'fake' ])
158
192
159
193
194
+ class TestUsePEP517Options (object ):
195
+
196
+ """
197
+ Test options related to using --use-pep517.
198
+ """
199
+
200
+ def parse_args (self , args ):
201
+ # We use DownloadCommand since that is one of the few Command
202
+ # classes with the use_pep517 options.
203
+ command = DownloadCommand ()
204
+ options , args = command .parse_args (args )
205
+
206
+ return options
207
+
208
+ def test_no_option (self ):
209
+ """
210
+ Test passing no option.
211
+ """
212
+ options = self .parse_args ([])
213
+ assert options .use_pep517 is None
214
+
215
+ def test_use_pep517 (self ):
216
+ """
217
+ Test passing --use-pep517.
218
+ """
219
+ options = self .parse_args (['--use-pep517' ])
220
+ assert options .use_pep517 is True
221
+
222
+ def test_no_use_pep517 (self ):
223
+ """
224
+ Test passing --no-use-pep517.
225
+ """
226
+ options = self .parse_args (['--no-use-pep517' ])
227
+ assert options .use_pep517 is False
228
+
229
+ def test_PIP_USE_PEP517_true (self ):
230
+ """
231
+ Test setting PIP_USE_PEP517 to "true".
232
+ """
233
+ with temp_environment_variable ('PIP_USE_PEP517' , 'true' ):
234
+ options = self .parse_args ([])
235
+ # This is an int rather than a boolean because strtobool() in pip's
236
+ # configuration code returns an int.
237
+ assert options .use_pep517 == 1
238
+
239
+ def test_PIP_USE_PEP517_false (self ):
240
+ """
241
+ Test setting PIP_USE_PEP517 to "false".
242
+ """
243
+ with temp_environment_variable ('PIP_USE_PEP517' , 'false' ):
244
+ options = self .parse_args ([])
245
+ # This is an int rather than a boolean because strtobool() in pip's
246
+ # configuration code returns an int.
247
+ assert options .use_pep517 == 0
248
+
249
+ def test_use_pep517_and_PIP_USE_PEP517_false (self ):
250
+ """
251
+ Test passing --use-pep517 and setting PIP_USE_PEP517 to "false".
252
+ """
253
+ with temp_environment_variable ('PIP_USE_PEP517' , 'false' ):
254
+ options = self .parse_args (['--use-pep517' ])
255
+ assert options .use_pep517 is True
256
+
257
+ def test_no_use_pep517_and_PIP_USE_PEP517_true (self ):
258
+ """
259
+ Test passing --no-use-pep517 and setting PIP_USE_PEP517 to "true".
260
+ """
261
+ with temp_environment_variable ('PIP_USE_PEP517' , 'true' ):
262
+ options = self .parse_args (['--no-use-pep517' ])
263
+ assert options .use_pep517 is False
264
+
265
+ def test_PIP_NO_USE_PEP517 (self , capsys ):
266
+ """
267
+ Test setting PIP_NO_USE_PEP517, which isn't allowed.
268
+ """
269
+ expected_err = (
270
+ '--no-use-pep517 error: A value was passed for --no-use-pep517,\n '
271
+ )
272
+ with temp_environment_variable ('PIP_NO_USE_PEP517' , 'true' ):
273
+ with assert_option_error (capsys , expected = expected_err ):
274
+ self .parse_args ([])
275
+
276
+
160
277
class TestOptionsInterspersed (AddFakeCommandMixin ):
161
278
162
279
def test_general_option_after_subcommand (self ):
0 commit comments