Skip to content

Commit 4c977c6

Browse files
Merge pull request #85277 from charles-zablit/charles-zablit/update-checkout/add-tests
2 parents f1d564d + 1d9d003 commit 4c977c6

File tree

5 files changed

+99
-82
lines changed

5 files changed

+99
-82
lines changed
Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from pathlib import Path
12
import unittest
23
from unittest.mock import patch
34

45
from update_checkout.update_checkout import UpdateArguments, _is_any_repository_locked
56

7+
FAKE_PATH = Path("/fake_path")
68

7-
def _update_arguments_with_fake_path(repo_name: str, path: str) -> UpdateArguments:
9+
10+
def _update_arguments_with_fake_path(repo_name: str, path: Path) -> UpdateArguments:
811
return UpdateArguments(
912
repo_name=repo_name,
1013
source_root=path,
@@ -23,87 +26,93 @@ def _update_arguments_with_fake_path(repo_name: str, path: str) -> UpdateArgumen
2326

2427

2528
class TestIsAnyRepositoryLocked(unittest.TestCase):
26-
@patch("os.path.exists")
27-
@patch("os.path.isdir")
28-
@patch("os.listdir")
29-
def test_repository_with_lock_file(self, mock_listdir, mock_isdir, mock_exists):
29+
@patch("pathlib.Path.exists", autospec=True)
30+
@patch("pathlib.Path.is_dir", autospec=True)
31+
@patch("pathlib.Path.iterdir", autospec=True)
32+
def test_repository_with_lock_file(self, mock_iterdir, mock_is_dir, mock_exists):
3033
pool_args = [
31-
_update_arguments_with_fake_path("repo1", "/fake_path"),
32-
_update_arguments_with_fake_path("repo2", "/fake_path"),
34+
_update_arguments_with_fake_path("repo1", FAKE_PATH),
35+
_update_arguments_with_fake_path("repo2", FAKE_PATH),
3336
]
3437

35-
def listdir_side_effect(path):
36-
if "repo1" in path:
37-
return ["index.lock", "config"]
38-
elif "repo2" in path:
39-
return ["HEAD", "config"]
38+
def iterdir_side_effect(path: Path):
39+
if "repo1" in path.as_posix():
40+
return [path.joinpath("index.lock"), path.joinpath("config")]
41+
elif "repo2" in path.as_posix():
42+
return [path.joinpath("HEAD"), path.joinpath("config")]
4043
return []
4144

4245
mock_exists.return_value = True
43-
mock_isdir.return_value = True
44-
mock_listdir.side_effect = listdir_side_effect
46+
mock_is_dir.return_value = True
47+
mock_iterdir.side_effect = iterdir_side_effect
4548

4649
result = _is_any_repository_locked(pool_args)
4750
self.assertEqual(result, {"repo1"})
4851

49-
@patch("os.path.exists")
50-
@patch("os.path.isdir")
51-
@patch("os.listdir")
52-
def test_repository_without_git_dir(self, mock_listdir, mock_isdir, mock_exists):
52+
@patch("pathlib.Path.exists")
53+
@patch("pathlib.Path.is_dir")
54+
@patch("pathlib.Path.iterdir")
55+
def test_repository_without_git_dir(self, mock_iterdir, mock_is_dir, mock_exists):
5356
pool_args = [
54-
_update_arguments_with_fake_path("repo1", "/fake_path"),
57+
_update_arguments_with_fake_path("repo1", FAKE_PATH),
5558
]
5659

5760
mock_exists.return_value = False
58-
mock_isdir.return_value = False
59-
mock_listdir.return_value = []
61+
mock_is_dir.return_value = False
62+
mock_iterdir.return_value = []
6063

6164
result = _is_any_repository_locked(pool_args)
6265
self.assertEqual(result, set())
6366

64-
@patch("os.path.exists")
65-
@patch("os.path.isdir")
66-
@patch("os.listdir")
67-
def test_repository_with_git_file(self, mock_listdir, mock_isdir, mock_exists):
67+
@patch("pathlib.Path.exists")
68+
@patch("pathlib.Path.is_dir")
69+
@patch("pathlib.Path.iterdir")
70+
def test_repository_with_git_file(self, mock_iterdir, mock_is_dir, mock_exists):
6871
pool_args = [
69-
_update_arguments_with_fake_path("repo1", "/fake_path"),
72+
_update_arguments_with_fake_path("repo1", FAKE_PATH),
7073
]
7174

7275
mock_exists.return_value = True
73-
mock_isdir.return_value = False
74-
mock_listdir.return_value = []
76+
mock_is_dir.return_value = False
77+
mock_iterdir.return_value = []
7578

7679
result = _is_any_repository_locked(pool_args)
7780
self.assertEqual(result, set())
7881

79-
@patch("os.path.exists")
80-
@patch("os.path.isdir")
81-
@patch("os.listdir")
82+
@patch("pathlib.Path.exists")
83+
@patch("pathlib.Path.is_dir")
84+
@patch("pathlib.Path.iterdir")
8285
def test_repository_with_multiple_lock_files(
83-
self, mock_listdir, mock_isdir, mock_exists
86+
self, mock_iterdir, mock_is_dir, mock_exists
8487
):
8588
pool_args = [
86-
_update_arguments_with_fake_path("repo1", "/fake_path"),
89+
_update_arguments_with_fake_path("repo1", FAKE_PATH),
8790
]
8891

8992
mock_exists.return_value = True
90-
mock_isdir.return_value = True
91-
mock_listdir.return_value = ["index.lock", "merge.lock", "HEAD"]
93+
mock_is_dir.return_value = True
94+
mock_iterdir.return_value = [
95+
FAKE_PATH.joinpath(x) for x in ("index.lock", "merge.lock", "HEAD")
96+
]
9297

9398
result = _is_any_repository_locked(pool_args)
9499
self.assertEqual(result, {"repo1"})
95100

96-
@patch("os.path.exists")
97-
@patch("os.path.isdir")
98-
@patch("os.listdir")
99-
def test_repository_with_no_lock_files(self, mock_listdir, mock_isdir, mock_exists):
101+
@patch("pathlib.Path.exists")
102+
@patch("pathlib.Path.is_dir")
103+
@patch("pathlib.Path.iterdir")
104+
def test_repository_with_no_lock_files(
105+
self, mock_iterdir, mock_is_dir, mock_exists
106+
):
100107
pool_args = [
101-
_update_arguments_with_fake_path("repo1", "/fake_path"),
108+
_update_arguments_with_fake_path("repo1", FAKE_PATH),
102109
]
103110

104111
mock_exists.return_value = True
105-
mock_isdir.return_value = True
106-
mock_listdir.return_value = ["HEAD", "config", "logs"]
112+
mock_is_dir.return_value = True
113+
mock_iterdir.return_value = [
114+
FAKE_PATH.joinpath(x) for x in ("HEAD", "config", "logs")
115+
]
107116

108117
result = _is_any_repository_locked(pool_args)
109118
self.assertEqual(result, set())

utils/update_checkout/update_checkout/cli_arguments.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
from pathlib import Path
23
from typing import List, Optional
34

45
from build_swift.build_swift.constants import SWIFT_SOURCE_ROOT
@@ -22,7 +23,7 @@ class CliArguments(argparse.Namespace):
2223
tag: Optional[str]
2324
match_timestamp: bool
2425
n_processes: int
25-
source_root: str
26+
source_root: Path
2627
use_submodules: bool
2728
verbose: bool
2829

@@ -139,6 +140,7 @@ def parse_args() -> "CliArguments":
139140
"--source-root",
140141
help="The root directory to checkout repositories",
141142
default=SWIFT_SOURCE_ROOT,
143+
type=Path,
142144
)
143145
parser.add_argument(
144146
"--use-submodules",

utils/update_checkout/update_checkout/git_command.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import Path
23
import shlex
34
import subprocess
45
import sys
@@ -44,7 +45,7 @@ def __str__(self):
4445
class Git:
4546
@staticmethod
4647
def run(
47-
repo_path: str,
48+
repo_path: Path,
4849
args: List[str],
4950
echo: bool = False,
5051
env: Optional[Dict[str, Any]] = None,
@@ -77,9 +78,7 @@ def run(
7778
f"command `{command}` terminated with a non-zero exit "
7879
f"status {str(e.returncode)}, aborting"
7980
)
80-
raise GitException(
81-
e.returncode, command, os.path.basename(repo_path), output
82-
)
81+
raise GitException(e.returncode, command, repo_path.name, output)
8382
except OSError as e:
8483
if fatal:
8584
sys.exit(

utils/update_checkout/update_checkout/runner_arguments.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
from pathlib import Path
23
from typing import Any, Dict, List, Optional
34

45
from .cli_arguments import CliArguments
@@ -14,7 +15,7 @@ class RunnerArguments:
1415

1516
@dataclass
1617
class UpdateArguments(RunnerArguments):
17-
source_root: str
18+
source_root: Path
1819
config: Dict[str, Any]
1920
scheme_map: Any
2021
tag: Optional[str]

0 commit comments

Comments
 (0)