14
14
15
15
import pytest
16
16
import os
17
+ from pathlib import Path
17
18
import subprocess
18
19
from mock import patch , ANY
19
20
34
35
35
36
@patch ("subprocess.check_call" )
36
37
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
38
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
37
39
@patch ("os.path.isfile" , return_value = True )
38
40
@patch ("os.path.isdir" , return_value = True )
39
41
@patch ("os.path.exists" , return_value = True )
40
- def test_git_clone_repo_succeed (exists , isdir , isfile , mkdtemp , check_call ):
42
+ def test_git_clone_repo_succeed (exists , isdir , isfile , tempdir , mkdtemp , check_call ):
41
43
git_config = {"repo" : PUBLIC_GIT_REPO , "branch" : PUBLIC_BRANCH , "commit" : PUBLIC_COMMIT }
42
44
entry_point = "entry_point"
43
45
source_dir = "source_dir"
@@ -88,7 +90,8 @@ def test_git_clone_repo_git_argument_wrong_format():
88
90
),
89
91
)
90
92
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
91
- def test_git_clone_repo_clone_fail (mkdtemp , check_call ):
93
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
94
+ def test_git_clone_repo_clone_fail (tempdir , mkdtemp , check_call ):
92
95
git_config = {"repo" : PUBLIC_GIT_REPO , "branch" : PUBLIC_BRANCH , "commit" : PUBLIC_COMMIT }
93
96
entry_point = "entry_point"
94
97
source_dir = "source_dir"
@@ -103,7 +106,8 @@ def test_git_clone_repo_clone_fail(mkdtemp, check_call):
103
106
side_effect = [True , subprocess .CalledProcessError (returncode = 1 , cmd = "git checkout banana" )],
104
107
)
105
108
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
106
- def test_git_clone_repo_branch_not_exist (mkdtemp , check_call ):
109
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
110
+ def test_git_clone_repo_branch_not_exist (tempdir , mkdtemp , check_call ):
107
111
git_config = {"repo" : PUBLIC_GIT_REPO , "branch" : PUBLIC_BRANCH , "commit" : PUBLIC_COMMIT }
108
112
entry_point = "entry_point"
109
113
source_dir = "source_dir"
@@ -122,7 +126,8 @@ def test_git_clone_repo_branch_not_exist(mkdtemp, check_call):
122
126
],
123
127
)
124
128
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
125
- def test_git_clone_repo_commit_not_exist (mkdtemp , check_call ):
129
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
130
+ def test_git_clone_repo_commit_not_exist (tempdir , mkdtemp , check_call ):
126
131
git_config = {"repo" : PUBLIC_GIT_REPO , "branch" : PUBLIC_BRANCH , "commit" : PUBLIC_COMMIT }
127
132
entry_point = "entry_point"
128
133
source_dir = "source_dir"
@@ -134,10 +139,11 @@ def test_git_clone_repo_commit_not_exist(mkdtemp, check_call):
134
139
135
140
@patch ("subprocess.check_call" )
136
141
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
142
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
137
143
@patch ("os.path.isfile" , return_value = False )
138
144
@patch ("os.path.isdir" , return_value = True )
139
145
@patch ("os.path.exists" , return_value = True )
140
- def test_git_clone_repo_entry_point_not_exist (exists , isdir , isfile , mkdtemp , heck_call ):
146
+ def test_git_clone_repo_entry_point_not_exist (exists , isdir , isfile , tempdir , mkdtemp , heck_call ):
141
147
git_config = {"repo" : PUBLIC_GIT_REPO , "branch" : PUBLIC_BRANCH , "commit" : PUBLIC_COMMIT }
142
148
entry_point = "entry_point_that_does_not_exist"
143
149
source_dir = "source_dir"
@@ -149,10 +155,11 @@ def test_git_clone_repo_entry_point_not_exist(exists, isdir, isfile, mkdtemp, he
149
155
150
156
@patch ("subprocess.check_call" )
151
157
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
158
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
152
159
@patch ("os.path.isfile" , return_value = True )
153
160
@patch ("os.path.isdir" , return_value = False )
154
161
@patch ("os.path.exists" , return_value = True )
155
- def test_git_clone_repo_source_dir_not_exist (exists , isdir , isfile , mkdtemp , check_call ):
162
+ def test_git_clone_repo_source_dir_not_exist (exists , isdir , isfile , tempdir , mkdtemp , check_call ):
156
163
git_config = {"repo" : PUBLIC_GIT_REPO , "branch" : PUBLIC_BRANCH , "commit" : PUBLIC_COMMIT }
157
164
entry_point = "entry_point"
158
165
source_dir = "source_dir_that_does_not_exist"
@@ -164,10 +171,11 @@ def test_git_clone_repo_source_dir_not_exist(exists, isdir, isfile, mkdtemp, che
164
171
165
172
@patch ("subprocess.check_call" )
166
173
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
174
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
167
175
@patch ("os.path.isfile" , return_value = True )
168
176
@patch ("os.path.isdir" , return_value = True )
169
177
@patch ("os.path.exists" , side_effect = [True , False ])
170
- def test_git_clone_repo_dependencies_not_exist (exists , isdir , isfile , mkdtemp , check_call ):
178
+ def test_git_clone_repo_dependencies_not_exist (exists , isdir , isfile , tempdir , mkdtemp , check_call ):
171
179
git_config = {"repo" : PUBLIC_GIT_REPO , "branch" : PUBLIC_BRANCH , "commit" : PUBLIC_COMMIT }
172
180
entry_point = "entry_point"
173
181
source_dir = "source_dir"
@@ -179,8 +187,9 @@ def test_git_clone_repo_dependencies_not_exist(exists, isdir, isfile, mkdtemp, c
179
187
180
188
@patch ("subprocess.check_call" )
181
189
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
190
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
182
191
@patch ("os.path.isfile" , return_value = True )
183
- def test_git_clone_repo_with_username_password_no_2fa (isfile , mkdtemp , check_call ):
192
+ def test_git_clone_repo_with_username_password_no_2fa (isfile , tempdir , mkdtemp , check_call ):
184
193
git_config = {
185
194
"repo" : PRIVATE_GIT_REPO ,
186
195
"branch" : PRIVATE_BRANCH ,
@@ -210,8 +219,9 @@ def test_git_clone_repo_with_username_password_no_2fa(isfile, mkdtemp, check_cal
210
219
211
220
@patch ("subprocess.check_call" )
212
221
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
222
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
213
223
@patch ("os.path.isfile" , return_value = True )
214
- def test_git_clone_repo_with_token_no_2fa (isfile , mkdtemp , check_call ):
224
+ def test_git_clone_repo_with_token_no_2fa (isfile , tempdir , mkdtemp , check_call ):
215
225
git_config = {
216
226
"repo" : PRIVATE_GIT_REPO ,
217
227
"branch" : PRIVATE_BRANCH ,
@@ -236,8 +246,9 @@ def test_git_clone_repo_with_token_no_2fa(isfile, mkdtemp, check_call):
236
246
237
247
@patch ("subprocess.check_call" )
238
248
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
249
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
239
250
@patch ("os.path.isfile" , return_value = True )
240
- def test_git_clone_repo_with_token_2fa (isfile , mkdtemp , check_call ):
251
+ def test_git_clone_repo_with_token_2fa (isfile , tempdirm , mkdtemp , check_call ):
241
252
git_config = {
242
253
"repo" : PRIVATE_GIT_REPO ,
243
254
"branch" : PRIVATE_BRANCH ,
@@ -264,8 +275,10 @@ def test_git_clone_repo_with_token_2fa(isfile, mkdtemp, check_call):
264
275
@patch ("subprocess.check_call" )
265
276
@patch ("os.chmod" )
266
277
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
278
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
267
279
@patch ("os.path.isfile" , return_value = True )
268
- def test_git_clone_repo_ssh (isfile , mkdtemp , chmod , check_call ):
280
+ def test_git_clone_repo_ssh (isfile , tempdir , mkdtemp , chmod , check_call ):
281
+ Path (REPO_DIR ).mkdir (parents = True , exist_ok = True )
269
282
git_config = {"repo" : PRIVATE_GIT_REPO_SSH , "branch" : PRIVATE_BRANCH , "commit" : PRIVATE_COMMIT }
270
283
entry_point = "entry_point"
271
284
ret = git_utils .git_clone_repo (git_config , entry_point )
@@ -277,8 +290,11 @@ def test_git_clone_repo_ssh(isfile, mkdtemp, chmod, check_call):
277
290
278
291
@patch ("subprocess.check_call" )
279
292
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
293
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
280
294
@patch ("os.path.isfile" , return_value = True )
281
- def test_git_clone_repo_with_token_no_2fa_unnecessary_creds_provided (isfile , mkdtemp , check_call ):
295
+ def test_git_clone_repo_with_token_no_2fa_unnecessary_creds_provided (
296
+ isfile , tempdir , mkdtemp , check_call
297
+ ):
282
298
git_config = {
283
299
"repo" : PRIVATE_GIT_REPO ,
284
300
"branch" : PRIVATE_BRANCH ,
@@ -309,8 +325,11 @@ def test_git_clone_repo_with_token_no_2fa_unnecessary_creds_provided(isfile, mkd
309
325
310
326
@patch ("subprocess.check_call" )
311
327
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
328
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
312
329
@patch ("os.path.isfile" , return_value = True )
313
- def test_git_clone_repo_with_token_2fa_unnecessary_creds_provided (isfile , mkdtemp , check_call ):
330
+ def test_git_clone_repo_with_token_2fa_unnecessary_creds_provided (
331
+ isfile , tempdir , mkdtemp , check_call
332
+ ):
314
333
git_config = {
315
334
"repo" : PRIVATE_GIT_REPO ,
316
335
"branch" : PRIVATE_BRANCH ,
@@ -346,7 +365,8 @@ def test_git_clone_repo_with_token_2fa_unnecessary_creds_provided(isfile, mkdtem
346
365
),
347
366
)
348
367
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
349
- def test_git_clone_repo_with_username_and_password_wrong_creds (mkdtemp , check_call ):
368
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
369
+ def test_git_clone_repo_with_username_and_password_wrong_creds (tempdir , mkdtemp , check_call ):
350
370
git_config = {
351
371
"repo" : PRIVATE_GIT_REPO ,
352
372
"branch" : PRIVATE_BRANCH ,
@@ -370,7 +390,8 @@ def test_git_clone_repo_with_username_and_password_wrong_creds(mkdtemp, check_ca
370
390
),
371
391
)
372
392
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
373
- def test_git_clone_repo_with_token_wrong_creds (mkdtemp , check_call ):
393
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
394
+ def test_git_clone_repo_with_token_wrong_creds (tempdir , mkdtemp , check_call ):
374
395
git_config = {
375
396
"repo" : PRIVATE_GIT_REPO ,
376
397
"branch" : PRIVATE_BRANCH ,
@@ -393,7 +414,8 @@ def test_git_clone_repo_with_token_wrong_creds(mkdtemp, check_call):
393
414
),
394
415
)
395
416
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
396
- def test_git_clone_repo_with_and_token_2fa_wrong_creds (mkdtemp , check_call ):
417
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
418
+ def test_git_clone_repo_with_and_token_2fa_wrong_creds (tempdir , mkdtemp , check_call ):
397
419
git_config = {
398
420
"repo" : PRIVATE_GIT_REPO ,
399
421
"branch" : PRIVATE_BRANCH ,
@@ -411,8 +433,11 @@ def test_git_clone_repo_with_and_token_2fa_wrong_creds(mkdtemp, check_call):
411
433
412
434
@patch ("subprocess.check_call" )
413
435
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
436
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
414
437
@patch ("os.path.isfile" , return_value = True )
415
- def test_git_clone_repo_codecommit_https_with_username_and_password (isfile , mkdtemp , check_call ):
438
+ def test_git_clone_repo_codecommit_https_with_username_and_password (
439
+ isfile , tempdir , mkdtemp , check_call
440
+ ):
416
441
git_config = {
417
442
"repo" : CODECOMMIT_REPO ,
418
443
"branch" : CODECOMMIT_BRANCH ,
@@ -445,7 +470,9 @@ def test_git_clone_repo_codecommit_https_with_username_and_password(isfile, mkdt
445
470
),
446
471
)
447
472
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
448
- def test_git_clone_repo_codecommit_ssh_passphrase_required (mkdtemp , check_call ):
473
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
474
+ def test_git_clone_repo_codecommit_ssh_passphrase_required (tempdir , mkdtemp , check_call ):
475
+ Path (REPO_DIR ).mkdir (parents = True , exist_ok = True )
449
476
git_config = {"repo" : CODECOMMIT_REPO_SSH , "branch" : CODECOMMIT_BRANCH }
450
477
entry_point = "entry_point"
451
478
with pytest .raises (subprocess .CalledProcessError ) as error :
@@ -460,7 +487,8 @@ def test_git_clone_repo_codecommit_ssh_passphrase_required(mkdtemp, check_call):
460
487
),
461
488
)
462
489
@patch ("tempfile.mkdtemp" , return_value = REPO_DIR )
463
- def test_git_clone_repo_codecommit_https_creds_not_stored_locally (mkdtemp , check_call ):
490
+ @patch ("tempfile.TemporaryDirectory.__enter__" , return_value = REPO_DIR )
491
+ def test_git_clone_repo_codecommit_https_creds_not_stored_locally (tempdir , mkdtemp , check_call ):
464
492
git_config = {"repo" : CODECOMMIT_REPO , "branch" : CODECOMMIT_BRANCH }
465
493
entry_point = "entry_point"
466
494
with pytest .raises (subprocess .CalledProcessError ) as error :
0 commit comments