Skip to content

Commit af0284f

Browse files
committed
Update tests
1 parent 5612349 commit af0284f

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/sagemaker/git_utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import absolute_import
1515

1616
import os
17+
from pathlib import Path
1718
import subprocess
1819
import tempfile
1920
import warnings
@@ -280,6 +281,7 @@ def _run_clone_command(repo_url, dest_dir):
280281
elif repo_url.startswith("git@") or repo_url.startswith("ssh://"):
281282
try:
282283
with tempfile.TemporaryDirectory() as tmp_dir:
284+
assert Path(tmp_dir).exists()
283285
custom_ssh_executable = Path(tmp_dir) / "ssh_batch"
284286
with open(custom_ssh_executable, "w") as pipe:
285287
print("#!/bin/sh", file=pipe)

tests/unit/test_git_utils.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import pytest
1616
import os
17+
from pathlib import Path
1718
import subprocess
1819
from mock import patch, ANY
1920

@@ -33,7 +34,7 @@
3334

3435

3536
@patch("subprocess.check_call")
36-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
37+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
3738
@patch("os.path.isfile", return_value=True)
3839
@patch("os.path.isdir", return_value=True)
3940
@patch("os.path.exists", return_value=True)
@@ -87,7 +88,7 @@ def test_git_clone_repo_git_argument_wrong_format():
8788
returncode=1, cmd="git clone {} {}".format(PUBLIC_GIT_REPO, REPO_DIR)
8889
),
8990
)
90-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
91+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
9192
def test_git_clone_repo_clone_fail(mkdtemp, check_call):
9293
git_config = {"repo": PUBLIC_GIT_REPO, "branch": PUBLIC_BRANCH, "commit": PUBLIC_COMMIT}
9394
entry_point = "entry_point"
@@ -102,7 +103,7 @@ def test_git_clone_repo_clone_fail(mkdtemp, check_call):
102103
"subprocess.check_call",
103104
side_effect=[True, subprocess.CalledProcessError(returncode=1, cmd="git checkout banana")],
104105
)
105-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
106+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
106107
def test_git_clone_repo_branch_not_exist(mkdtemp, check_call):
107108
git_config = {"repo": PUBLIC_GIT_REPO, "branch": PUBLIC_BRANCH, "commit": PUBLIC_COMMIT}
108109
entry_point = "entry_point"
@@ -121,7 +122,7 @@ def test_git_clone_repo_branch_not_exist(mkdtemp, check_call):
121122
subprocess.CalledProcessError(returncode=1, cmd="git checkout {}".format(PUBLIC_COMMIT)),
122123
],
123124
)
124-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
125+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
125126
def test_git_clone_repo_commit_not_exist(mkdtemp, check_call):
126127
git_config = {"repo": PUBLIC_GIT_REPO, "branch": PUBLIC_BRANCH, "commit": PUBLIC_COMMIT}
127128
entry_point = "entry_point"
@@ -133,7 +134,7 @@ def test_git_clone_repo_commit_not_exist(mkdtemp, check_call):
133134

134135

135136
@patch("subprocess.check_call")
136-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
137+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
137138
@patch("os.path.isfile", return_value=False)
138139
@patch("os.path.isdir", return_value=True)
139140
@patch("os.path.exists", return_value=True)
@@ -148,7 +149,7 @@ def test_git_clone_repo_entry_point_not_exist(exists, isdir, isfile, mkdtemp, he
148149

149150

150151
@patch("subprocess.check_call")
151-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
152+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
152153
@patch("os.path.isfile", return_value=True)
153154
@patch("os.path.isdir", return_value=False)
154155
@patch("os.path.exists", return_value=True)
@@ -163,7 +164,7 @@ def test_git_clone_repo_source_dir_not_exist(exists, isdir, isfile, mkdtemp, che
163164

164165

165166
@patch("subprocess.check_call")
166-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
167+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
167168
@patch("os.path.isfile", return_value=True)
168169
@patch("os.path.isdir", return_value=True)
169170
@patch("os.path.exists", side_effect=[True, False])
@@ -178,7 +179,7 @@ def test_git_clone_repo_dependencies_not_exist(exists, isdir, isfile, mkdtemp, c
178179

179180

180181
@patch("subprocess.check_call")
181-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
182+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
182183
@patch("os.path.isfile", return_value=True)
183184
def test_git_clone_repo_with_username_password_no_2fa(isfile, mkdtemp, check_call):
184185
git_config = {
@@ -209,7 +210,7 @@ def test_git_clone_repo_with_username_password_no_2fa(isfile, mkdtemp, check_cal
209210

210211

211212
@patch("subprocess.check_call")
212-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
213+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
213214
@patch("os.path.isfile", return_value=True)
214215
def test_git_clone_repo_with_token_no_2fa(isfile, mkdtemp, check_call):
215216
git_config = {
@@ -235,7 +236,7 @@ def test_git_clone_repo_with_token_no_2fa(isfile, mkdtemp, check_call):
235236

236237

237238
@patch("subprocess.check_call")
238-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
239+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
239240
@patch("os.path.isfile", return_value=True)
240241
def test_git_clone_repo_with_token_2fa(isfile, mkdtemp, check_call):
241242
git_config = {
@@ -263,9 +264,10 @@ def test_git_clone_repo_with_token_2fa(isfile, mkdtemp, check_call):
263264

264265
@patch("subprocess.check_call")
265266
@patch("os.chmod")
266-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
267+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
267268
@patch("os.path.isfile", return_value=True)
268269
def test_git_clone_repo_ssh(isfile, mkdtemp, chmod, check_call):
270+
Path(REPO_DIR).mkdir(parents=True, exist_ok=True)
269271
git_config = {"repo": PRIVATE_GIT_REPO_SSH, "branch": PRIVATE_BRANCH, "commit": PRIVATE_COMMIT}
270272
entry_point = "entry_point"
271273
ret = git_utils.git_clone_repo(git_config, entry_point)
@@ -276,7 +278,7 @@ def test_git_clone_repo_ssh(isfile, mkdtemp, chmod, check_call):
276278

277279

278280
@patch("subprocess.check_call")
279-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
281+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
280282
@patch("os.path.isfile", return_value=True)
281283
def test_git_clone_repo_with_token_no_2fa_unnecessary_creds_provided(isfile, mkdtemp, check_call):
282284
git_config = {
@@ -308,7 +310,7 @@ def test_git_clone_repo_with_token_no_2fa_unnecessary_creds_provided(isfile, mkd
308310

309311

310312
@patch("subprocess.check_call")
311-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
313+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
312314
@patch("os.path.isfile", return_value=True)
313315
def test_git_clone_repo_with_token_2fa_unnecessary_creds_provided(isfile, mkdtemp, check_call):
314316
git_config = {
@@ -345,7 +347,7 @@ def test_git_clone_repo_with_token_2fa_unnecessary_creds_provided(isfile, mkdtem
345347
returncode=1, cmd="git clone {} {}".format(PRIVATE_GIT_REPO, REPO_DIR)
346348
),
347349
)
348-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
350+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
349351
def test_git_clone_repo_with_username_and_password_wrong_creds(mkdtemp, check_call):
350352
git_config = {
351353
"repo": PRIVATE_GIT_REPO,
@@ -369,7 +371,7 @@ def test_git_clone_repo_with_username_and_password_wrong_creds(mkdtemp, check_ca
369371
returncode=1, cmd="git clone {} {}".format(PRIVATE_GIT_REPO, REPO_DIR)
370372
),
371373
)
372-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
374+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
373375
def test_git_clone_repo_with_token_wrong_creds(mkdtemp, check_call):
374376
git_config = {
375377
"repo": PRIVATE_GIT_REPO,
@@ -392,7 +394,7 @@ def test_git_clone_repo_with_token_wrong_creds(mkdtemp, check_call):
392394
returncode=1, cmd="git clone {} {}".format(PRIVATE_GIT_REPO, REPO_DIR)
393395
),
394396
)
395-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
397+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
396398
def test_git_clone_repo_with_and_token_2fa_wrong_creds(mkdtemp, check_call):
397399
git_config = {
398400
"repo": PRIVATE_GIT_REPO,
@@ -410,7 +412,7 @@ def test_git_clone_repo_with_and_token_2fa_wrong_creds(mkdtemp, check_call):
410412

411413

412414
@patch("subprocess.check_call")
413-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
415+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
414416
@patch("os.path.isfile", return_value=True)
415417
def test_git_clone_repo_codecommit_https_with_username_and_password(isfile, mkdtemp, check_call):
416418
git_config = {
@@ -444,7 +446,7 @@ def test_git_clone_repo_codecommit_https_with_username_and_password(isfile, mkdt
444446
returncode=128, cmd="git clone {} {}".format(CODECOMMIT_REPO_SSH, REPO_DIR)
445447
),
446448
)
447-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
449+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
448450
def test_git_clone_repo_codecommit_ssh_passphrase_required(mkdtemp, check_call):
449451
git_config = {"repo": CODECOMMIT_REPO_SSH, "branch": CODECOMMIT_BRANCH}
450452
entry_point = "entry_point"
@@ -459,7 +461,7 @@ def test_git_clone_repo_codecommit_ssh_passphrase_required(mkdtemp, check_call):
459461
returncode=128, cmd="git clone {} {}".format(CODECOMMIT_REPO, REPO_DIR)
460462
),
461463
)
462-
@patch("tempfile.mkdtemp", return_value=REPO_DIR)
464+
@patch("tempfile.TemporaryDirectory", return_value=REPO_DIR)
463465
def test_git_clone_repo_codecommit_https_creds_not_stored_locally(mkdtemp, check_call):
464466
git_config = {"repo": CODECOMMIT_REPO, "branch": CODECOMMIT_BRANCH}
465467
entry_point = "entry_point"

0 commit comments

Comments
 (0)