]>
Commit | Line | Data |
---|---|---|
50685beb SW |
1 | # git-cache: Cache git content locally |
2 | # | |
3 | # This program is free software: you can redistribute it and/or modify it | |
4 | # under the terms of the GNU General Public License as published by the | |
5 | # Free Software Foundation, version 3. | |
6 | ||
7 | ||
bef7ce53 SW |
8 | import os.path |
9 | import tempfile | |
10 | import shutil | |
11 | import subprocess | |
12 | import unittest | |
13 | ||
8d251eca SW |
14 | from typing import Optional |
15 | ||
bef7ce53 SW |
16 | import git_cache |
17 | ||
18 | ||
8d251eca SW |
19 | def _setenv(var: str, value: Optional[str]) -> None: |
20 | if value is None: | |
21 | del os.environ[var] | |
22 | else: | |
23 | os.environ[var] = value | |
24 | ||
25 | ||
bef7ce53 SW |
26 | def _git(directory: str, *args: str) -> bytes: |
27 | p = subprocess.run(['git', '-C', directory] + list(args), | |
28 | stdout=subprocess.PIPE, check=True) | |
5dd71f12 | 29 | return bytes(p.stdout) |
bef7ce53 SW |
30 | |
31 | ||
32 | def _commit_file( | |
33 | directory: str, | |
34 | filename: str, | |
35 | contents: str, | |
36 | commit_message: str) -> None: | |
a28f4bb7 | 37 | with open(os.path.join(directory, filename), 'w', encoding='utf-8') as f: |
bef7ce53 SW |
38 | f.write(contents) |
39 | _git(directory, 'add', filename) | |
40 | _git(directory, 'commit', '-m', commit_message) | |
41 | ||
42 | ||
43 | # pylint: disable=too-many-public-methods | |
44 | class TestGitCache(unittest.TestCase): | |
45 | ||
46 | def setUp(self) -> None: | |
7f9b928b | 47 | self.xdgcache = tempfile.TemporaryDirectory( # pylint: disable=consider-using-with |
35000f72 | 48 | prefix='git_cache_test-cache-') |
7f9b928b | 49 | self.xdgdata = tempfile.TemporaryDirectory( # pylint: disable=consider-using-with |
35000f72 | 50 | prefix='git_cache_test-data-') |
bef7ce53 | 51 | self.old_XDG_CACHE_HOME = os.environ.get('XDG_CACHE_HOME') |
35000f72 | 52 | self.old_XDG_DATA_HOME = os.environ.get('XDG_DATA_HOME') |
8d251eca | 53 | _setenv('XDG_CACHE_HOME', self.xdgcache.name) |
35000f72 | 54 | _setenv('XDG_DATA_HOME', self.xdgdata.name) |
bef7ce53 SW |
55 | |
56 | os.environ['GIT_AUTHOR_NAME'] = 'test_git_cache' | |
57 | os.environ['GIT_COMMITTER_NAME'] = 'test_git_cache' | |
58 | os.environ['GIT_AUTHOR_EMAIL'] = 'test_git_cache@example.com' | |
59 | os.environ['GIT_COMMITTER_EMAIL'] = 'test_git_cache@example.com' | |
60 | ||
f36d5c6f | 61 | os.environ['BACKOFF_MAX_TIME'] = '0' |
083b90e7 | 62 | os.environ['FORCE_WARNING_TIME'] = '0' # ONLY FOR TEST USE! |
f36d5c6f | 63 | |
7f9b928b SW |
64 | self.tempdir = tempfile.TemporaryDirectory( # pylint: disable=consider-using-with |
65 | prefix='git_cache_test-') | |
bef7ce53 | 66 | self.upstream = os.path.join(self.tempdir.name, 'upstream') |
fc9c09b3 | 67 | subprocess.run(['git', '-c', 'init.defaultBranch=main', |
21971f7f | 68 | 'init', self.upstream], check=True) |
bef7ce53 SW |
69 | _commit_file(self.upstream, 'file', 'Contents', 'First commit') |
70 | ||
71 | def tearDown(self) -> None: | |
8d251eca | 72 | _setenv('XDG_CACHE_HOME', self.old_XDG_CACHE_HOME) |
35000f72 | 73 | _setenv('XDG_DATA_HOME', self.old_XDG_DATA_HOME) |
bef7ce53 SW |
74 | |
75 | self.tempdir.cleanup() | |
76 | self.xdgcache.cleanup() | |
77 | ||
78 | def test_fetch(self) -> None: | |
fc9c09b3 | 79 | d, rev = git_cache.fetch(self.upstream, 'main') |
f580771a | 80 | self.assertEqual(_git(d, 'show', f'{rev}:file'), b'Contents') |
bef7ce53 SW |
81 | |
82 | def test_fetch_twice(self) -> None: | |
fc9c09b3 | 83 | d1, rev1 = git_cache.fetch(self.upstream, 'main') |
f580771a | 84 | self.assertEqual(_git(d1, 'show', f'{rev1}:file'), b'Contents') |
fc9c09b3 | 85 | d2, rev2 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
86 | self.assertEqual(d1, d2) |
87 | self.assertEqual(rev1, rev2) | |
f580771a | 88 | self.assertEqual(_git(d2, 'show', f'{rev2}:file'), b'Contents') |
bef7ce53 SW |
89 | |
90 | def test_fetch_then_ensure(self) -> None: | |
fc9c09b3 | 91 | d1, rev = git_cache.fetch(self.upstream, 'main') |
f580771a | 92 | self.assertEqual(_git(d1, 'show', f'{rev}:file'), b'Contents') |
fc9c09b3 | 93 | d2 = git_cache.ensure_rev_available(self.upstream, 'main', rev) |
bef7ce53 | 94 | self.assertEqual(d1, d2) |
f580771a | 95 | self.assertEqual(_git(d2, 'show', f'{rev}:file'), b'Contents') |
bef7ce53 SW |
96 | |
97 | def test_ensure_then_fetch(self) -> None: | |
98 | rev1 = _git( | |
99 | self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
fc9c09b3 | 100 | d1 = git_cache.ensure_rev_available(self.upstream, 'main', rev1) |
f580771a | 101 | self.assertEqual(_git(d1, 'show', f'{rev1}:file'), b'Contents') |
fc9c09b3 | 102 | d2, rev2 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
103 | self.assertEqual(d1, d2) |
104 | self.assertEqual(rev1, rev2) | |
f580771a | 105 | self.assertEqual(_git(d2, 'show', f'{rev2}:file'), b'Contents') |
bef7ce53 SW |
106 | |
107 | def test_fetch_new_file(self) -> None: | |
fc9c09b3 | 108 | d1, rev1 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 | 109 | _commit_file(self.upstream, 'foofile', 'foo', 'Foo') |
fc9c09b3 | 110 | d2, rev2 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
111 | self.assertEqual(d1, d2) |
112 | self.assertNotEqual(rev1, rev2) | |
f580771a | 113 | self.assertEqual(_git(d2, 'show', f'{rev2}:foofile'), b'foo') |
bef7ce53 SW |
114 | |
115 | def test_ensure_doesnt_fetch_new_file(self) -> None: | |
fc9c09b3 | 116 | d1, rev1 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
117 | _commit_file(self.upstream, 'foofile', 'foo', 'Foo') |
118 | rev2 = _git( | |
119 | self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
120 | self.assertNotEqual(rev1, rev2) | |
fc9c09b3 | 121 | d2 = git_cache.ensure_rev_available(self.upstream, 'main', rev1) |
bef7ce53 SW |
122 | self.assertEqual(d1, d2) |
123 | p = subprocess.run( | |
f580771a | 124 | ['git', '-C', d2, 'show', f'{rev2}:foofile'], check=False) |
bef7ce53 SW |
125 | self.assertNotEqual(p.returncode, 0) |
126 | ||
127 | def test_ensure_doesnt_fetch_from_deleted_upstream(self) -> None: | |
fc9c09b3 | 128 | d1, rev = git_cache.fetch(self.upstream, 'main') |
bef7ce53 | 129 | self.tempdir.cleanup() |
fc9c09b3 | 130 | d2 = git_cache.ensure_rev_available(self.upstream, 'main', rev) |
bef7ce53 SW |
131 | self.assertEqual(d1, d2) |
132 | ||
133 | def test_ensure_fetches_new_file(self) -> None: | |
fc9c09b3 | 134 | d1, rev1 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
135 | _commit_file(self.upstream, 'foofile', 'foo', 'Foo') |
136 | rev2 = _git( | |
137 | self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
138 | self.assertNotEqual(rev1, rev2) | |
fc9c09b3 | 139 | d2 = git_cache.ensure_rev_available(self.upstream, 'main', rev2) |
bef7ce53 | 140 | self.assertEqual(d1, d2) |
f580771a | 141 | self.assertEqual(_git(d2, 'show', f'{rev2}:foofile'), b'foo') |
bef7ce53 SW |
142 | |
143 | def test_fetch_raises_on_invalid_repo(self) -> None: | |
144 | self.tempdir.cleanup() | |
145 | with self.assertRaises(Exception): | |
fc9c09b3 | 146 | git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
147 | |
148 | def test_ensure_raises_on_invalid_repo(self) -> None: | |
149 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
150 | self.tempdir.cleanup() | |
151 | with self.assertRaises(Exception): | |
fc9c09b3 | 152 | git_cache.ensure_rev_available(self.upstream, 'main', rev) |
bef7ce53 SW |
153 | |
154 | def test_fetch_raises_on_invalid_ref(self) -> None: | |
155 | with self.assertRaises(Exception): | |
156 | git_cache.fetch(self.upstream, 'nobranch') | |
157 | ||
158 | def test_ensure_raises_on_invalid_ref(self) -> None: | |
159 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
160 | with self.assertRaises(Exception): | |
161 | git_cache.ensure_rev_available(self.upstream, 'nobranch', rev) | |
162 | ||
163 | def test_ensure_raises_on_invalid_rev(self) -> None: | |
164 | with self.assertRaises(Exception): | |
165 | git_cache.ensure_rev_available( | |
166 | self.upstream, | |
167 | 'nobranch', | |
168 | '1234567890abcdef01234567890abcdef1234567') | |
169 | ||
170 | def test_ensure_raises_on_rev_from_other_branch(self) -> None: | |
171 | _git(self.upstream, 'checkout', '-b', 'otherbranch') | |
172 | _commit_file(self.upstream, 'foofile', 'foo', 'Foo') | |
173 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
174 | with self.assertRaises(Exception): | |
fc9c09b3 | 175 | git_cache.ensure_rev_available(self.upstream, 'main', rev) |
bef7ce53 SW |
176 | |
177 | def test_ensure_other_branch(self) -> None: | |
178 | _git(self.upstream, 'checkout', '-b', 'otherbranch') | |
179 | _commit_file(self.upstream, 'foofile', 'foo', 'Foo') | |
180 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
181 | d = git_cache.ensure_rev_available(self.upstream, 'otherbranch', rev) | |
f580771a | 182 | self.assertEqual(_git(d, 'show', f'{rev}:foofile'), b'foo') |
bef7ce53 | 183 | |
eb638847 SW |
184 | def test_catch_up(self) -> None: |
185 | _git(self.upstream, 'checkout', '-b', 'otherbranch') | |
186 | _commit_file(self.upstream, 'foofile', 'foo', 'Foo') | |
187 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
188 | d = git_cache.ensure_rev_available(self.upstream, 'otherbranch', rev) | |
f580771a | 189 | self.assertEqual(_git(d, 'show', f'{rev}:foofile'), b'foo') |
fc9c09b3 | 190 | _git(self.upstream, 'checkout', 'main') |
eb638847 | 191 | _git(self.upstream, 'merge', '--ff-only', 'otherbranch') |
fc9c09b3 | 192 | d = git_cache.ensure_rev_available(self.upstream, 'main', rev) |
f580771a | 193 | self.assertEqual(_git(d, 'show', f'{rev}:foofile'), b'foo') |
eb638847 | 194 | |
bef7ce53 | 195 | def test_fetch_after_cache_deleted(self) -> None: |
fc9c09b3 | 196 | d1, rev1 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 | 197 | shutil.rmtree(d1) |
fc9c09b3 | 198 | d2, rev2 = git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
199 | self.assertEqual(d1, d2) |
200 | self.assertEqual(rev1, rev2) | |
f580771a | 201 | self.assertEqual(_git(d2, 'show', f'{rev2}:file'), b'Contents') |
bef7ce53 SW |
202 | |
203 | def test_ensure_after_cache_deleted(self) -> None: | |
fc9c09b3 | 204 | d1, rev = git_cache.fetch(self.upstream, 'main') |
bef7ce53 | 205 | shutil.rmtree(d1) |
fc9c09b3 | 206 | d2 = git_cache.ensure_rev_available(self.upstream, 'main', rev) |
bef7ce53 | 207 | self.assertEqual(d1, d2) |
f580771a | 208 | self.assertEqual(_git(d2, 'show', f'{rev}:file'), b'Contents') |
bef7ce53 SW |
209 | |
210 | def test_fetch_raises_on_amend(self) -> None: | |
fc9c09b3 | 211 | git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
212 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') |
213 | with self.assertRaises(Exception): | |
fc9c09b3 | 214 | git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
215 | |
216 | def test_ensure_raises_on_amend(self) -> None: | |
fc9c09b3 | 217 | git_cache.fetch(self.upstream, 'main') |
bef7ce53 SW |
218 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') |
219 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
220 | with self.assertRaises(Exception): | |
fc9c09b3 | 221 | git_cache.ensure_rev_available(self.upstream, 'main', rev) |
bef7ce53 | 222 | |
35000f72 | 223 | def test_fetch_raises_on_amend_after_cache_deleted(self) -> None: |
fc9c09b3 | 224 | d, _ = git_cache.fetch(self.upstream, 'main') |
35000f72 SW |
225 | shutil.rmtree(d) |
226 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') | |
227 | with self.assertRaises(Exception): | |
fc9c09b3 | 228 | git_cache.fetch(self.upstream, 'main') |
35000f72 SW |
229 | |
230 | def test_ensure_raises_on_amend_after_cache_deleted(self) -> None: | |
fc9c09b3 | 231 | d, _ = git_cache.fetch(self.upstream, 'main') |
35000f72 SW |
232 | shutil.rmtree(d) |
233 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') | |
234 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
235 | with self.assertRaises(Exception): | |
fc9c09b3 | 236 | git_cache.ensure_rev_available(self.upstream, 'main', rev) |
35000f72 | 237 | |
083b90e7 | 238 | def test_force_fetch_after_amend(self) -> None: |
fc9c09b3 | 239 | git_cache.fetch(self.upstream, 'main') |
083b90e7 | 240 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') |
fc9c09b3 | 241 | git_cache.fetch(self.upstream, 'main', force=True) |
083b90e7 SW |
242 | |
243 | def test_force_ensure_after_amend(self) -> None: | |
fc9c09b3 | 244 | git_cache.fetch(self.upstream, 'main') |
083b90e7 SW |
245 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') |
246 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
247 | git_cache.ensure_rev_available( | |
fc9c09b3 | 248 | self.upstream, 'main', rev, force=True) |
083b90e7 SW |
249 | |
250 | def test_force_fetch_after_amend_and_cache_delete(self) -> None: | |
fc9c09b3 | 251 | d, _ = git_cache.fetch(self.upstream, 'main') |
083b90e7 SW |
252 | shutil.rmtree(d) |
253 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') | |
fc9c09b3 | 254 | git_cache.fetch(self.upstream, 'main', force=True) |
083b90e7 SW |
255 | |
256 | def test_force_ensure_after_amend_and_cache_delete(self) -> None: | |
fc9c09b3 | 257 | d, _ = git_cache.fetch(self.upstream, 'main') |
083b90e7 SW |
258 | shutil.rmtree(d) |
259 | _git(self.upstream, 'commit', '--amend', '-m', 'Amended') | |
260 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
261 | git_cache.ensure_rev_available( | |
fc9c09b3 | 262 | self.upstream, 'main', rev, force=True) |
083b90e7 | 263 | |
2fa3bb5b SW |
264 | def test_fetch_tag(self) -> None: |
265 | _git(self.upstream, 'tag', 'v1.2.3') | |
266 | d, rev = git_cache.fetch(self.upstream, 'tag v1.2.3') | |
267 | self.assertEqual(_git(d, 'show', f'{rev}:file'), b'Contents') | |
268 | ||
269 | def test_ensure_tag(self) -> None: | |
270 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
271 | _git(self.upstream, 'tag', 'v1.2.3') | |
272 | d = git_cache.ensure_rev_available(self.upstream, 'tag v1.2.3', rev) | |
273 | self.assertEqual(_git(d, 'show', f'{rev}:file'), b'Contents') | |
274 | ||
275 | def test_fetch_annotated_tag(self) -> None: | |
276 | _git(self.upstream, 'tag', '--annotate', '-m', 'Tag', 'v1.2.3') | |
277 | d, rev = git_cache.fetch(self.upstream, 'tag v1.2.3') | |
278 | self.assertEqual(_git(d, 'show', f'{rev}:file'), b'Contents') | |
279 | ||
280 | def test_ensure_annotated_tag(self) -> None: | |
281 | rev = _git(self.upstream, 'log', '--format=%H', '-n1').strip().decode() | |
282 | _git(self.upstream, 'tag', '--annotate', '-m', 'Tag', 'v1.2.3') | |
283 | d = git_cache.ensure_rev_available(self.upstream, 'tag v1.2.3', rev) | |
284 | self.assertEqual(_git(d, 'show', f'{rev}:file'), b'Contents') | |
285 | ||
bef7ce53 SW |
286 | |
287 | if __name__ == '__main__': | |
288 | unittest.main() |