OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import shutil | 5 import shutil |
6 import sys | 6 import sys |
7 import tempfile | 7 import tempfile |
8 | 8 |
9 # 'super on old-style class' - pylint: disable=E1002 | 9 import testing_support |
10 # 'cannot import' - pylint: disable=F0401 | |
11 # 'no __init__ method' - pylint: disable=W0232 | |
12 from testing_support import git_test_utils | |
13 | 10 |
14 from infra.services.gnumbd.support import git, util | 11 from infra.libs import git2 |
15 | 12 |
16 class TestBasis(git_test_utils.GitRepoReadWriteTestBase): | 13 class TestBasis(testing_support.git.unittest_helpers.GitRepoReadWriteTestBase): |
17 REPO_SCHEMA = """ | 14 REPO_SCHEMA = """ |
18 A B C D E F | 15 A B C D E F |
19 D L M N O | 16 D L M N O |
20 O P Q R S | 17 O P Q R S |
21 B G H I J K | 18 B G H I J K |
22 H Z | 19 H Z |
23 O Z | 20 O Z |
24 """ | 21 """ |
25 | 22 |
26 @staticmethod | 23 @staticmethod |
27 def capture_stdio(fn, *args, **kwargs): | 24 def capture_stdio(fn, *args, **kwargs): |
28 stdout = sys.stdout | 25 stdout = sys.stdout |
29 stderr = sys.stderr | 26 stderr = sys.stderr |
30 try: | 27 try: |
31 # "multiple statements on a line" pylint: disable=C0321 | 28 # "multiple statements on a line" pylint: disable=C0321 |
32 with tempfile.TemporaryFile() as out, tempfile.TemporaryFile() as err: | 29 with tempfile.TemporaryFile() as out, tempfile.TemporaryFile() as err: |
33 sys.stdout = out | 30 sys.stdout = out |
34 sys.stderr = err | 31 sys.stderr = err |
35 fn(*args, **kwargs) | 32 fn(*args, **kwargs) |
36 out.seek(0) | 33 out.seek(0) |
37 err.seek(0) | 34 err.seek(0) |
38 return out.read(), err.read() | 35 return out.read(), err.read() |
39 finally: | 36 finally: |
40 sys.stdout = stdout | 37 sys.stdout = stdout |
41 sys.stderr = stderr | 38 sys.stderr = stderr |
42 | 39 |
43 def setUp(self): | 40 def setUp(self): # "super on old-style class" pylint: disable=E1002 |
44 self.repos_dir = tempfile.mkdtemp(suffix='.gnumbd') | 41 self.repos_dir = tempfile.mkdtemp(suffix='.git_test') |
45 super(TestBasis, self).setUp() | 42 super(TestBasis, self).setUp() |
46 self.repo.git('branch', 'branch_O', self.repo['O']) | 43 self.repo.git('branch', 'branch_O', self.repo['O']) |
47 | 44 |
48 def tearDown(self): | 45 def tearDown(self): # "super on old-style class" pylint: disable=E1002 |
49 shutil.rmtree(self.repos_dir) | 46 shutil.rmtree(self.repos_dir) |
50 super(TestBasis, self).tearDown() | 47 super(TestBasis, self).tearDown() |
51 | 48 |
52 def mkRepo(self): | 49 def mkRepo(self): |
53 r = git.Repo(self.repo.repo_path) | 50 r = git2.Repo(self.repo.repo_path) |
54 r.repos_dir = self.repos_dir | 51 r.repos_dir = self.repos_dir |
55 self.capture_stdio(r.reify) | 52 self.capture_stdio(r.reify) |
56 return r | 53 return r |
57 | 54 |
58 | 55 |
59 class TestRepo(TestBasis): | 56 class TestRepo(TestBasis): |
60 def testEmptyRepo(self): | 57 def testEmptyRepo(self): |
61 r = git.Repo('doesnt_exist') | 58 r = git2.Repo('doesnt_exist') |
62 r.repos_dir = self.repos_dir | 59 r.repos_dir = self.repos_dir |
63 | 60 |
64 with self.assertRaises(util.CalledProcessError): | 61 with self.assertRaises(git2.CalledProcessError): |
65 self.capture_stdio(r.reify) | 62 self.capture_stdio(r.reify) |
66 | 63 |
67 with self.assertRaises(AssertionError): | 64 with self.assertRaises(AssertionError): |
68 r.run('show-ref') | 65 r.run('show-ref') |
69 | 66 |
70 def testDefaultRepo(self): | 67 def testDefaultRepo(self): |
71 r = self.mkRepo() | 68 r = self.mkRepo() |
72 r.reify() # covers 'already initialized' portion of reify() | 69 r.reify() # covers 'already initialized' portion of reify() |
73 self.assertEqual(r.run('rev-parse', 'branch_F').strip(), self.repo['F']) | 70 self.assertEqual(r.run('rev-parse', 'branch_F').strip(), self.repo['F']) |
74 _, err = self.capture_stdio(r.run, 'rev-parse', 'rtaitnariostnr', | 71 _, err = self.capture_stdio(r.run, 'rev-parse', 'rtaitnariostnr', |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 self.assertIs(N, r.get_commit(self.repo['N'])) | 127 self.assertIs(N, r.get_commit(self.repo['N'])) |
131 self.assertIs(O, r.get_commit(self.repo['O'])) | 128 self.assertIs(O, r.get_commit(self.repo['O'])) |
132 self.assertEqual(len(r._commit_cache), 2) | 129 self.assertEqual(len(r._commit_cache), 2) |
133 | 130 |
134 self.assertIsNot(L, r.get_commit(self.repo['L'])) | 131 self.assertIsNot(L, r.get_commit(self.repo['L'])) |
135 | 132 |
136 | 133 |
137 class TestRef(TestBasis): | 134 class TestRef(TestBasis): |
138 def testComparison(self): | 135 def testComparison(self): |
139 r = self.mkRepo() | 136 r = self.mkRepo() |
140 O = git.Ref(r, 'refs/heads/branch_O') | 137 O = r['refs/heads/branch_O'] |
141 self.assertEqual(O, O) | 138 self.assertEqual(O, O) |
142 self.assertEqual(O, git.Ref(r, 'refs/heads/branch_O')) | 139 self.assertEqual(O, r['refs/heads/branch_O']) |
143 | 140 |
144 N = git.Ref(r, 'refs/heads/branch_K') | 141 N = r['refs/heads/branch_K'] |
145 self.assertNotEqual(O, N) | 142 self.assertNotEqual(O, N) |
146 | 143 |
147 def testRepr(self): | 144 def testRepr(self): |
148 r = self.mkRepo() | 145 r = self.mkRepo() |
149 O = git.Ref(r, 'refs/heads/branch_O') | 146 O = r['refs/heads/branch_O'] |
150 self.assertEqual("Ref(%r, 'refs/heads/branch_O')" % r, repr(O)) | 147 self.assertEqual("Ref(%r, 'refs/heads/branch_O')" % r, repr(O)) |
151 | 148 |
152 def testCommit(self): | 149 def testCommit(self): |
153 r = self.mkRepo() | 150 r = self.mkRepo() |
154 self.assertEqual( | 151 self.assertEqual( |
155 git.Ref(r, 'refs/heads/branch_O').commit.hsh, | 152 r['refs/heads/branch_O'].commit.hsh, |
156 self.repo['O']) | 153 self.repo['O']) |
157 | 154 |
158 def testCommitBogus(self): | 155 def testCommitBogus(self): |
159 r = self.mkRepo() | 156 r = self.mkRepo() |
160 self.assertIs(git.Ref(r, 'refs/heads/bogus').commit, git.INVALID) | 157 self.assertIs(r['refs/heads/bogus'].commit, git2.INVALID) |
161 # exercise __ne__ and __eq__ | 158 # exercise __ne__ and __eq__ |
162 self.assertNotEqual(git.Ref(r, 'refs/heads/bogus').commit, | 159 self.assertNotEqual(r['refs/heads/bogus'].commit, |
163 git.Ref(r, 'refs/heads/other_bogus').commit) | 160 r['refs/heads/other_bogus'].commit) |
164 self.assertFalse(git.Ref(r, 'refs/heads/bogus').commit == | 161 self.assertFalse(r['refs/heads/bogus'].commit == |
165 git.Ref(r, 'refs/heads/other_bogus').commit) | 162 r['refs/heads/other_bogus'].commit) |
166 | 163 |
167 def testTo(self): | 164 def testTo(self): |
168 r = self.mkRepo() | 165 r = self.mkRepo() |
169 A = git.Ref(r, 'refs/heads/root_A') | 166 A = r['refs/heads/root_A'] |
170 O = git.Ref(r, 'refs/heads/branch_O') | 167 O = r['refs/heads/branch_O'] |
171 self.assertEqual( | 168 self.assertEqual( |
172 list(c.hsh for c in A.to(O)), | 169 list(c.hsh for c in A.to(O)), |
173 [self.repo[c] for c in 'BCDLMNO'] | 170 [self.repo[c] for c in 'BCDLMNO'] |
174 ) | 171 ) |
175 | 172 |
176 def testNonFastForward(self): | 173 def testNonFastForward(self): |
177 r = self.mkRepo() | 174 r = self.mkRepo() |
178 O = git.Ref(r, 'refs/heads/branch_O') | 175 O = r['refs/heads/branch_O'] |
179 D = r.get_commit(self.repo['D']) | 176 D = r.get_commit(self.repo['D']) |
180 with self.assertRaises(git.CalledProcessError): | 177 with self.assertRaises(git2.CalledProcessError): |
181 O.fast_forward_push(D) | 178 O.fast_forward_push(D) |
182 self.assertEqual( | 179 self.assertEqual( |
183 self.repo.git('rev-parse', 'branch_O').stdout.strip(), | 180 self.repo.git('rev-parse', 'branch_O').stdout.strip(), |
184 self.repo['O']) | 181 self.repo['O']) |
185 | 182 |
186 def testFastForward(self): | 183 def testFastForward(self): |
187 r = self.mkRepo() | 184 r = self.mkRepo() |
188 O = git.Ref(r, 'refs/heads/branch_O') | 185 O = r['refs/heads/branch_O'] |
189 S = r.get_commit(self.repo['S']) | 186 S = r.get_commit(self.repo['S']) |
190 self.capture_stdio(O.fast_forward_push, S) | 187 self.capture_stdio(O.fast_forward_push, S) |
191 self.assertEqual(O.commit.hsh, self.repo['S']) | 188 self.assertEqual(O.commit.hsh, self.repo['S']) |
192 self.assertEqual( | 189 self.assertEqual( |
193 self.repo.git('rev-parse', 'branch_O').stdout.strip(), | 190 self.repo.git('rev-parse', 'branch_O').stdout.strip(), |
194 self.repo['S']) | 191 self.repo['S']) |
195 | 192 |
196 | 193 |
197 class TestCommit(TestBasis): | 194 class TestCommit(TestBasis): |
198 def testComparison(self): | 195 def testComparison(self): |
199 r = self.mkRepo() | 196 r = self.mkRepo() |
200 c = git.Ref(r, 'refs/heads/branch_O').commit | 197 c = r['refs/heads/branch_O'].commit |
201 self.assertEqual(c, c) | 198 self.assertEqual(c, c) |
202 self.assertEqual(c, git.Ref(r, 'refs/heads/branch_O').commit) | 199 self.assertEqual(c, r['refs/heads/branch_O'].commit) |
203 self.assertNotEqual(c, git.Ref(r, 'refs/heads/branch_S').commit) | 200 self.assertNotEqual(c, r['refs/heads/branch_S'].commit) |
204 self.assertIs(c.repo, r) | 201 self.assertIs(c.repo, r) |
205 | 202 |
206 def testRepr(self): | 203 def testRepr(self): |
207 r = self.mkRepo() | 204 r = self.mkRepo() |
208 c = git.Ref(r, 'refs/heads/branch_O').commit | 205 c = r['refs/heads/branch_O'].commit |
209 self.assertEqual("Commit(%r, %r)" % (r, self.repo['O']), repr(c)) | 206 self.assertEqual("Commit(%r, %r)" % (r, self.repo['O']), repr(c)) |
210 | 207 |
211 def testData(self): | 208 def testData(self): |
212 r = self.mkRepo() | 209 r = self.mkRepo() |
213 d = git.Ref(r, 'refs/heads/branch_O').commit.data | 210 d = r['refs/heads/branch_O'].commit.data |
214 self.assertEqual(d.committer.email, 'commitish@example.com') | 211 self.assertEqual(d.committer.email, 'commitish@example.com') |
215 | 212 |
216 def testBogus(self): | 213 def testBogus(self): |
217 r = self.mkRepo() | 214 r = self.mkRepo() |
218 d = git.Commit(r, 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef').data | 215 d = git2.Commit(r, 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef').data |
219 self.assertIs(d, git.INVALID) | 216 self.assertIs(d, git2.INVALID) |
220 self.assertIs(d.committer, git.INVALID) | 217 self.assertIs(d.committer, git2.INVALID) |
221 self.assertIs(d.committer.alter(user='tom'), git.INVALID) | 218 self.assertIs(d.committer.alter(user='tom'), git2.INVALID) |
222 | 219 |
223 def testParent(self): | 220 def testParent(self): |
224 r = self.mkRepo() | 221 r = self.mkRepo() |
225 c = git.Ref(r, 'refs/heads/branch_O').commit | 222 c = r['refs/heads/branch_O'].commit |
226 self.assertEqual(c.parent.hsh, self.repo['N']) | 223 self.assertEqual(c.parent.hsh, self.repo['N']) |
227 | 224 |
228 a = git.Ref(r, 'refs/heads/root_A').commit | 225 a = r['refs/heads/root_A'].commit |
229 self.assertIsNone(a.parent) | 226 self.assertIsNone(a.parent) |
230 | 227 |
231 z = git.Ref(r, 'refs/heads/branch_Z').commit | 228 z = r['refs/heads/branch_Z'].commit |
232 self.assertIs(z.parent, git.INVALID) | 229 self.assertIs(z.parent, git2.INVALID) |
233 | 230 |
234 def testAlter(self): | 231 def testAlter(self): |
235 r = self.mkRepo() | 232 r = self.mkRepo() |
236 c = git.Ref(r, 'refs/heads/branch_O').commit | 233 c = r['refs/heads/branch_O'].commit |
237 d = c.data | 234 d = c.data |
238 | 235 |
239 a = c.alter(committer=d.committer.alter(email='bob@dude.example.com')) | 236 a = c.alter(committer=d.committer.alter(email='bob@dude.example.com')) |
240 self.assertEqual(a.hsh, 'fadfbe63d40f60f5313a71a1c9d72a741ee91770') | 237 self.assertEqual(a.hsh, 'fadfbe63d40f60f5313a71a1c9d72a741ee91770') |
241 | 238 |
242 with self.assertRaises(Exception): | 239 with self.assertRaises(Exception): |
243 c.alter(tree='failbeef') | 240 c.alter(tree='failbeef') |
244 | 241 |
OLD | NEW |