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