Skip to content

Commit 450fe43

Browse files
Merge pull request #85198 from charles-zablit/charles-zablit/update-checkout/formatting-errors
[NFC][update-checkout] run the black formatter on update-checkout
2 parents e50f3db + b6ef3d8 commit 450fe43

File tree

10 files changed

+417
-264
lines changed

10 files changed

+417
-264
lines changed

utils/update_checkout/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from .update_checkout import main
32

43
__all__ = ["main"]

utils/update_checkout/run_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
UTILS_DIR = os.path.abspath(os.path.join(MODULE_DIR, os.pardir))
2525

2626

27-
if __name__ == '__main__':
27+
if __name__ == "__main__":
2828
# Add the swift/utils directory to the Python path.
2929
sys.path.append(UTILS_DIR)
3030

3131
# Create temp directory and export it for the test suite.
3232
temp_dir = tempfile.mkdtemp()
33-
os.environ['UPDATECHECKOUT_TEST_WORKSPACE_DIR'] = temp_dir
33+
os.environ["UPDATECHECKOUT_TEST_WORKSPACE_DIR"] = temp_dir
3434

3535
# Discover all tests for the module.
3636
module_tests = unittest.defaultTestLoader.discover(MODULE_DIR)

utils/update_checkout/tests/scheme_mock.py

Lines changed: 92 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,58 @@
2121
# For now we only use a config with a single scheme. We should add support for
2222
# handling multiple schemes.
2323
MOCK_REMOTE = {
24-
'repo1': [
24+
"repo1": [
2525
# This is a series of changes to repo1. (File, NewContents)
26-
('A.txt', 'A'),
27-
('B.txt', 'B'),
28-
('A.txt', 'a'),
26+
("A.txt", "A"),
27+
("B.txt", "B"),
28+
("A.txt", "a"),
2929
],
30-
'repo2': [
30+
"repo2": [
3131
# This is a series of changes to repo2. (File, NewContents)
32-
('X.txt', 'X'),
33-
('Y.txt', 'Y'),
34-
('X.txt', 'z'),
32+
("X.txt", "X"),
33+
("Y.txt", "Y"),
34+
("X.txt", "z"),
3535
],
3636
}
3737

3838
MOCK_CONFIG = {
3939
# This is here just b/c we expect it. We should consider consolidating
4040
# clone-patterns into a dictionary where we map protocols (i.e. ['ssh,
4141
# 'https'] to patterns). Then we can define this issue.
42-
'ssh-clone-pattern': 'DO_NOT_USE',
42+
"ssh-clone-pattern": "DO_NOT_USE",
4343
# We reset this value with our remote path when we process
44-
'https-clone-pattern': '',
45-
'repos': {
46-
'repo1': {
47-
'remote': {'id': 'repo1'},
44+
"https-clone-pattern": "",
45+
"repos": {
46+
"repo1": {
47+
"remote": {"id": "repo1"},
4848
},
49-
'repo2': {
50-
'remote': {'id': 'repo2'},
49+
"repo2": {
50+
"remote": {"id": "repo2"},
5151
},
5252
},
53-
'default-branch-scheme': 'main',
54-
'branch-schemes': {
55-
'main': {
56-
'aliases': ['main'],
57-
'repos': {
58-
'repo1': 'main',
59-
'repo2': 'main',
60-
}
53+
"default-branch-scheme": "main",
54+
"branch-schemes": {
55+
"main": {
56+
"aliases": ["main"],
57+
"repos": {
58+
"repo1": "main",
59+
"repo2": "main",
60+
},
6161
}
62-
}
62+
},
6363
}
6464

6565
MOCK_ADDITIONAL_SCHEME = {
66-
'branch-schemes': {
67-
'extra': {
68-
'aliases': ['extra'],
69-
'repos': {
66+
"branch-schemes": {
67+
"extra": {
68+
"aliases": ["extra"],
69+
"repos": {
7070
# Spell this differently just to make it distinguishable in
7171
# test output, even though we only have one branch.
7272
# TODO: Support multiple test branches in the repo instead.
73-
'repo1': 'refs/heads/main',
74-
'repo2': 'refs/heads/main',
75-
}
73+
"repo1": "refs/heads/main",
74+
"repo2": "refs/heads/main",
75+
},
7676
}
7777
}
7878
}
@@ -85,18 +85,21 @@ def __init__(self, command, returncode, output):
8585
self.output = output
8686

8787
def __str__(self):
88-
return f"Command returned a non-zero exit status {self.returncode}:\n"\
89-
f"Command: {' '.join(self.command)}\n" \
90-
f"Output: {self.output.decode('utf-8')}"
88+
return (
89+
f"Command returned a non-zero exit status {self.returncode}:\n"
90+
f"Command: {' '.join(self.command)}\n"
91+
f"Output: {self.output.decode('utf-8')}"
92+
)
9193

9294

9395
def call_quietly(*args, **kwargs):
94-
kwargs['stderr'] = subprocess.STDOUT
96+
kwargs["stderr"] = subprocess.STDOUT
9597
try:
9698
return subprocess.check_output(*args, **kwargs)
9799
except subprocess.CalledProcessError as e:
98-
raise CallQuietlyException(command=e.cmd, returncode=e.returncode,
99-
output=e.stdout) from e
100+
raise CallQuietlyException(
101+
command=e.cmd, returncode=e.returncode, output=e.stdout
102+
) from e
100103

101104

102105
def create_dir(d):
@@ -105,75 +108,82 @@ def create_dir(d):
105108

106109

107110
def teardown_mock_remote(base_dir):
108-
call_quietly(['rm', '-rf', base_dir])
111+
call_quietly(["rm", "-rf", base_dir])
109112

110113

111114
def get_config_path(base_dir):
112-
return os.path.join(base_dir, 'test-config.json')
115+
return os.path.join(base_dir, "test-config.json")
113116

114117

115118
def get_additional_config_path(base_dir):
116-
return os.path.join(base_dir, 'test-additional-config.json')
119+
return os.path.join(base_dir, "test-additional-config.json")
117120

118121

119122
def setup_mock_remote(base_dir, base_config):
120123
create_dir(base_dir)
121124

122125
# We use local as a workspace for creating commits.
123-
LOCAL_PATH = os.path.join(base_dir, 'local')
126+
LOCAL_PATH = os.path.join(base_dir, "local")
124127
# We use remote as a directory that simulates our remote unchecked out
125128
# repo.
126-
REMOTE_PATH = os.path.join(base_dir, 'remote')
129+
REMOTE_PATH = os.path.join(base_dir, "remote")
127130

128131
create_dir(REMOTE_PATH)
129132
create_dir(LOCAL_PATH)
130133

131-
for (k, v) in MOCK_REMOTE.items():
134+
for k, v in MOCK_REMOTE.items():
132135
local_repo_path = os.path.join(LOCAL_PATH, k)
133136
remote_repo_path = os.path.join(REMOTE_PATH, k)
134137
create_dir(remote_repo_path)
135138
create_dir(local_repo_path)
136-
call_quietly(['git', 'init', '--bare', remote_repo_path])
137-
call_quietly(['git', 'symbolic-ref', 'HEAD', 'refs/heads/main'],
138-
cwd=remote_repo_path)
139-
call_quietly(['git', 'clone', '-l', remote_repo_path, local_repo_path])
140-
call_quietly(['git', 'config', '--local', 'user.name', 'swift_test'],
141-
cwd=local_repo_path)
142-
call_quietly(['git', 'config', '--local', 'user.email', 'no-reply@swift.org'],
143-
cwd=local_repo_path)
144-
call_quietly(['git', 'config', '--local', 'commit.gpgsign', 'false'],
145-
cwd=local_repo_path)
146-
call_quietly(['git', 'symbolic-ref', 'HEAD', 'refs/heads/main'],
147-
cwd=local_repo_path)
148-
for (i, (filename, contents)) in enumerate(v):
139+
call_quietly(["git", "init", "--bare", remote_repo_path])
140+
call_quietly(
141+
["git", "symbolic-ref", "HEAD", "refs/heads/main"], cwd=remote_repo_path
142+
)
143+
call_quietly(["git", "clone", "-l", remote_repo_path, local_repo_path])
144+
call_quietly(
145+
["git", "config", "--local", "user.name", "swift_test"], cwd=local_repo_path
146+
)
147+
call_quietly(
148+
["git", "config", "--local", "user.email", "no-reply@swift.org"],
149+
cwd=local_repo_path,
150+
)
151+
call_quietly(
152+
["git", "config", "--local", "commit.gpgsign", "false"], cwd=local_repo_path
153+
)
154+
call_quietly(
155+
["git", "symbolic-ref", "HEAD", "refs/heads/main"], cwd=local_repo_path
156+
)
157+
for i, (filename, contents) in enumerate(v):
149158
filename_path = os.path.join(local_repo_path, filename)
150-
with open(filename_path, 'w') as f:
159+
with open(filename_path, "w") as f:
151160
f.write(contents)
152-
call_quietly(['git', 'add', filename], cwd=local_repo_path)
153-
call_quietly(['git', 'commit', '-m', 'Commit %d' % i],
154-
cwd=local_repo_path)
155-
call_quietly(['git', 'push', 'origin', 'main'],
156-
cwd=local_repo_path)
161+
call_quietly(["git", "add", filename], cwd=local_repo_path)
162+
call_quietly(["git", "commit", "-m", "Commit %d" % i], cwd=local_repo_path)
163+
call_quietly(["git", "push", "origin", "main"], cwd=local_repo_path)
157164

158-
https_clone_pattern = os.path.join('file://%s' % REMOTE_PATH, '%s')
159-
base_config['https-clone-pattern'] = https_clone_pattern
165+
https_clone_pattern = os.path.join("file://%s" % REMOTE_PATH, "%s")
166+
base_config["https-clone-pattern"] = https_clone_pattern
160167

161-
with open(get_config_path(base_dir), 'w') as f:
168+
with open(get_config_path(base_dir), "w") as f:
162169
json.dump(base_config, f)
163170

164-
with open(get_additional_config_path(base_dir), 'w') as f:
171+
with open(get_additional_config_path(base_dir), "w") as f:
165172
json.dump(MOCK_ADDITIONAL_SCHEME, f)
166173

167174
return (LOCAL_PATH, REMOTE_PATH)
168175

169176

170-
BASEDIR_ENV_VAR = 'UPDATECHECKOUT_TEST_WORKSPACE_DIR'
177+
BASEDIR_ENV_VAR = "UPDATECHECKOUT_TEST_WORKSPACE_DIR"
171178
CURRENT_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
172-
UPDATE_CHECKOUT_EXECUTABLE = 'update-checkout.cmd' if os.name == 'nt' else 'update-checkout'
173-
UPDATE_CHECKOUT_PATH = os.path.abspath(os.path.join(CURRENT_FILE_DIR,
174-
os.path.pardir,
175-
os.path.pardir,
176-
UPDATE_CHECKOUT_EXECUTABLE))
179+
UPDATE_CHECKOUT_EXECUTABLE = (
180+
"update-checkout.cmd" if os.name == "nt" else "update-checkout"
181+
)
182+
UPDATE_CHECKOUT_PATH = os.path.abspath(
183+
os.path.join(
184+
CURRENT_FILE_DIR, os.path.pardir, os.path.pardir, UPDATE_CHECKOUT_EXECUTABLE
185+
)
186+
)
177187

178188

179189
class SchemeMockTestCase(unittest.TestCase):
@@ -184,16 +194,19 @@ def __init__(self, *args, **kwargs):
184194
self.config = MOCK_CONFIG.copy()
185195
self.workspace = os.getenv(BASEDIR_ENV_VAR)
186196
if self.workspace is None:
187-
raise RuntimeError('Misconfigured test suite! Environment '
188-
'variable %s must be set!' % BASEDIR_ENV_VAR)
197+
raise RuntimeError(
198+
"Misconfigured test suite! Environment "
199+
"variable %s must be set!" % BASEDIR_ENV_VAR
200+
)
189201
self.config_path = get_config_path(self.workspace)
190202
self.additional_config_path = get_additional_config_path(self.workspace)
191203
self.update_checkout_path = UPDATE_CHECKOUT_PATH
192204
if not os.access(self.update_checkout_path, os.X_OK):
193-
raise RuntimeError('Error! Could not find executable '
194-
'update-checkout at path: %s'
195-
% self.update_checkout_path)
196-
self.source_root = os.path.join(self.workspace, 'source_root')
205+
raise RuntimeError(
206+
"Error! Could not find executable "
207+
"update-checkout at path: %s" % self.update_checkout_path
208+
)
209+
self.source_root = os.path.join(self.workspace, "source_root")
197210

198211
def setUp(self):
199212
create_dir(self.source_root)
@@ -205,7 +218,7 @@ def tearDown(self):
205218
teardown_mock_remote(self.workspace)
206219

207220
def call(self, *args, **kwargs):
208-
kwargs['cwd'] = self.source_root
221+
kwargs["cwd"] = self.source_root
209222
return call_quietly(*args, **kwargs)
210223

211224
def get_all_repos(self):

utils/update_checkout/tests/test_clone.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,58 @@ def __init__(self, *args, **kwargs):
2222
super(CloneTestCase, self).__init__(*args, **kwargs)
2323

2424
def test_simple_clone(self):
25-
self.call([self.update_checkout_path,
26-
'--config', self.config_path,
27-
'--source-root', self.source_root,
28-
'--clone'])
25+
self.call(
26+
[
27+
self.update_checkout_path,
28+
"--config",
29+
self.config_path,
30+
"--source-root",
31+
self.source_root,
32+
"--clone",
33+
]
34+
)
2935

3036
for repo in self.get_all_repos():
3137
repo_path = os.path.join(self.source_root, repo)
3238
self.assertTrue(os.path.isdir(repo_path))
3339

3440
def test_clone_with_additional_scheme(self):
35-
output = self.call([self.update_checkout_path,
36-
'--config', self.config_path,
37-
'--config', self.additional_config_path,
38-
'--source-root', self.source_root,
39-
'--clone',
40-
'--scheme', 'extra',
41-
'--verbose'])
41+
output = self.call(
42+
[
43+
self.update_checkout_path,
44+
"--config",
45+
self.config_path,
46+
"--config",
47+
self.additional_config_path,
48+
"--source-root",
49+
self.source_root,
50+
"--clone",
51+
"--scheme",
52+
"extra",
53+
"--verbose",
54+
]
55+
)
4256

4357
# Test that we're actually checking out the 'extra' scheme based on the output
4458
self.assertIn("git checkout refs/heads/main", output.decode("utf-8"))
4559

4660
def test_manager_not_called_on_long_socket(self):
47-
fake_tmpdir = '/tmp/very/' + '/long' * 20 + '/tmp'
48-
49-
with mock.patch('tempfile.gettempdir', return_value=fake_tmpdir), \
50-
mock.patch('multiprocessing.Manager') as mock_manager:
51-
52-
self.call([self.update_checkout_path,
53-
'--config', self.config_path,
54-
'--source-root', self.source_root,
55-
'--clone'])
61+
fake_tmpdir = "/tmp/very/" + "/long" * 20 + "/tmp"
62+
63+
with mock.patch("tempfile.gettempdir", return_value=fake_tmpdir), mock.patch(
64+
"multiprocessing.Manager"
65+
) as mock_manager:
66+
67+
self.call(
68+
[
69+
self.update_checkout_path,
70+
"--config",
71+
self.config_path,
72+
"--source-root",
73+
self.source_root,
74+
"--clone",
75+
]
76+
)
5677
# Ensure that we do not try to create a Manager when the tempdir
5778
# is too long.
5879
mock_manager.assert_not_called()
@@ -85,9 +106,7 @@ def __init__(self, *args, **kwargs):
85106
def test_clone(self):
86107
self.call(self.base_args + ["--scheme", self.scheme_name, "--clone"])
87108

88-
missing_repo_path = os.path.join(
89-
self.source_root, self.get_all_repos().pop()
90-
)
109+
missing_repo_path = os.path.join(self.source_root, self.get_all_repos().pop())
91110
self.assertFalse(os.path.isdir(missing_repo_path))
92111

93112
# Test that we do not update a repository that is not listed in the given

0 commit comments

Comments
 (0)