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