-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_get_git_sources.py
More file actions
264 lines (221 loc) · 7.33 KB
/
test_get_git_sources.py
File metadata and controls
264 lines (221 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# -----------------------------------------------------------------------------
# (C) Crown copyright Met Office. All rights reserved.
# The file LICENCE, distributed with this code, contains details of the terms
# under which the code may be used.
# -----------------------------------------------------------------------------
"""
Unit tests for get_git_sources
"""
import os
import subprocess
from shlex import split
from pathlib import Path
import pytest
from ..get_git_sources import (
validate_dependencies,
determine_mirror_fetch,
set_https,
clone_repo,
clone_repo_mirror,
sync_repo,
check_existing,
merge_source,
)
@pytest.fixture(scope="session")
def setup_sources(tmpdir_factory):
"""
Setup a tempdir for cloning into, a mirror of SimSys_Scripts and a local clone of
SimSys_Scripts
Use SimSys_Scripts as a public repo
"""
location = tmpdir_factory.mktemp("data")
os.chdir(location)
# Setup local mirror
subprocess.run(
split("git clone --mirror https://github.com/MetOffice/SimSys_Scripts.git"),
check=True,
)
# Create local clone
subprocess.run(
split("git clone https://github.com/MetOffice/SimSys_Scripts.git"), check=True
)
subprocess.run(split("git -C SimSys_Scripts checkout 2025.12.1"))
# Create a non-git repo to test check_existing
existing = Path(location) / "empty_dir"
existing.mkdir()
# Create 2 clones with conflicting commits
for i in range(2):
subprocess.run(split(f"cp -r SimSys_Scripts merge{i}"), check=True)
subprocess.run(split(f"git -C merge{i} switch -c merge{i}"), check=True)
with open(f"merge{i}/merge.txt", "w") as f:
f.write(f"merge{i}")
subprocess.run(split(f"git -C merge{i} add merge.txt"), check=True)
subprocess.run(
split(f"git -C merge{i} commit -a -m 'merge conflict'"),
check=True,
)
return Path(location)
def test_clone_repo(setup_sources):
"""
Test cloning from a github source
"""
output_loc = setup_sources / "github_clone"
assert (
clone_repo(
"https://github.com/MetOffice/SimSys_Scripts.git", "2025.12.1", output_loc
)
is None
)
assert Path(output_loc / ".git").is_dir() is True
def test_clone_repo_mirror(setup_sources):
"""
Test rsyncing a local clone
"""
output_loc = setup_sources / "mirror_clone"
mirror_loc = setup_sources / "SimSys_Scripts.git"
assert (
clone_repo_mirror(
"https://github.com/MetOffice/SimSys_Scripts.git",
"2025.12.1",
mirror_loc,
output_loc,
)
is None
)
assert Path(output_loc / ".git").is_dir() is True
def test_sync_repo(setup_sources):
"""
Test cloning from a github source
"""
# Cant test syncing with a hostname in github actions
source_loc = setup_sources / "SimSys_Scripts"
output_loc = setup_sources / "sync_clone"
assert sync_repo(source_loc, "2025.12.1", output_loc) is None
assert Path(output_loc / ".git").is_dir() is True
def test_merge_sources(setup_sources):
"""
Test merge_source
"""
target_clone = setup_sources / "SimSys_Scripts"
# Test Remote Source merges cleanly
assert (
merge_source(
"https://github.com/MetOffice/SimSys_Scripts.git",
"main",
target_clone,
)
is None
)
# Test Local Source Merges Cleanly
assert (
merge_source(setup_sources / "merge0", "merge0", target_clone, "SimSys_Scripts")
is None
)
# Test Local Source Doesn't Merge
with pytest.raises(RuntimeError):
merge_source(setup_sources / "merge1", "merge1", target_clone, "SimSys_Scripts")
# Test Local Source without ref raises error
with pytest.raises(Exception):
merge_source(setup_sources / "merge0", "", target_clone, "SimSys_Scripts")
def test_check_exists(setup_sources):
"""
Test check_existing
"""
assert check_existing(setup_sources / "SimSys_Scripts") is None
with pytest.raises(FileExistsError):
check_existing(setup_sources / "empty_dir")
def test_validate_dependencies():
valid = {
"repo1": {"source": "abc", "ref": "123"},
"repo2": [{"source": "abc", "ref": "123"}, {"source": "abc", "ref": "123"}],
}
assert validate_dependencies(valid) is None
invalid_dependencies = set()
invalid_dependencies.add(1)
with pytest.raises(TypeError):
validate_dependencies(invalid_dependencies)
invalid_repo_type = {"repo1": invalid_dependencies}
with pytest.raises(TypeError):
validate_dependencies(invalid_repo_type)
invalid_list = {"repo1": [invalid_dependencies]}
with pytest.raises(TypeError):
validate_dependencies(invalid_list)
missing_source = {"repo1": {"ref": "123"}}
with pytest.raises(ValueError):
validate_dependencies(missing_source)
missing_ref = {"repo1": {"source": "abc"}}
with pytest.raises(ValueError):
validate_dependencies(missing_ref)
def test_determine_mirror_fetch():
"""
Test determine_mirror_fetch
"""
# Test MetOffice User
assert (
determine_mirror_fetch("git@github.com:MetOffice/SimSys_Scripts.git", "ref")
== "ref"
)
assert (
determine_mirror_fetch("https://github.com/MetOffice/SimSys_Scripts.git", "ref")
== "ref"
)
# Test using hash
commit_hash = "ba965768395de47de064a60ee769471e3868e02d"
assert (
determine_mirror_fetch(
"git@github.com:user_name/SimSys_Scripts.git", commit_hash
)
== commit_hash
)
assert (
determine_mirror_fetch(
"https://github.com/user_name/SimSys_Scripts.git", commit_hash
)
== commit_hash
)
# Test using user and branch
user_name = "user_name"
branch = "branch"
assert (
determine_mirror_fetch(f"git@github.com:{user_name}/SimSys_Scripts.git", branch)
== f"{user_name}/{branch}"
)
assert (
determine_mirror_fetch(
f"https://github.com/{user_name}/SimSys_Scripts.git", branch
)
== f"{user_name}/{branch}"
)
def test_set_https():
"""
Test set_https
"""
input_dict = {
"repo1": {
"source": "git@github.com:MetOffice/SimSys_Scripts.git",
"ref": "123",
},
"repo2": [
{"source": "git@github.com:MetOffice/lfric_apps.git", "ref": "123"},
{"source": "git@github.com:MetOffice/lfric_apps.git", "ref": "456"},
],
"repo3": {
"source": "https://github.com/MetOffice/lfric_core.git",
"ref": "123",
},
"repo4": {"source": "hostname:/path/to/repository", "ref": "123"},
}
output_dict = {
"repo1": [
{"source": "https://github.com/MetOffice/SimSys_Scripts.git", "ref": "123"}
],
"repo2": [
{"source": "https://github.com/MetOffice/lfric_apps.git", "ref": "123"},
{"source": "https://github.com/MetOffice/lfric_apps.git", "ref": "456"},
],
"repo3": [
{"source": "https://github.com/MetOffice/lfric_core.git", "ref": "123"}
],
"repo4": [{"source": "hostname:/path/to/repository", "ref": "123"}],
}
assert set_https(input_dict) == output_dict