OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import hashlib |
| 6 import shutil |
| 7 import sys |
| 8 |
| 9 from infra.libs import git2 |
| 10 from infra.libs.git2.test import test_util |
| 11 |
| 12 |
| 13 class TestRepo(test_util.TestBasis): |
| 14 def testEmptyRepo(self): |
| 15 r = git2.Repo('doesnt_exist') |
| 16 r.repos_dir = self.repos_dir |
| 17 |
| 18 with self.assertRaises(git2.CalledProcessError): |
| 19 self.capture_stdio(r.reify) |
| 20 |
| 21 with self.assertRaises(AssertionError): |
| 22 r.run('show-ref') |
| 23 |
| 24 def testDefaultRepo(self): |
| 25 r = self.mkRepo() |
| 26 r.reify() # covers 'already initialized' portion of reify() |
| 27 self.assertEqual(r.run('rev-parse', 'branch_F').strip(), self.repo['F']) |
| 28 _, err = self.capture_stdio(r.run, 'rev-parse', 'rtaitnariostnr', |
| 29 ok_ret={128}) |
| 30 self.assertIn('fatal', err) |
| 31 |
| 32 def testDefaultRepoGit(self): |
| 33 shutil.move(self.repo.repo_path, self.repo.repo_path + '.git') |
| 34 self.repo.repo_path += '.git' |
| 35 r = self.mkRepo() |
| 36 self.assertFalse(r._repo_path.endswith('.git')) # pylint: disable=W0212 |
| 37 self.assertIn('refs/heads/branch_F', r.run('show-ref')) |
| 38 |
| 39 def testRefglob(self): |
| 40 r = self.mkRepo() |
| 41 self.assertEqual( |
| 42 {rf.ref for rf in r.refglob('*branch_*')}, |
| 43 {'refs/heads/branch_'+l for l in 'FOKSZ'}) |
| 44 self.assertIs(next(r.refglob('*branch_O')).repo, r) |
| 45 self.assertEqual(list(r.refglob('*atritaosrtientsaroitna*')), []) |
| 46 |
| 47 def testDryRun(self): |
| 48 r = self.mkRepo() |
| 49 r.dry_run = True |
| 50 self.assertIsNone(r.run('push', 'origin', 'HEAD')) |
| 51 |
| 52 def testRunIndata(self): |
| 53 r = self.mkRepo() |
| 54 with self.assertRaises(AssertionError): |
| 55 r.run('bogus', indata='spam', stdin=sys.stdin) |
| 56 self.assertEqual( |
| 57 r.run('rev-list', '--stdin', indata='branch_O~').splitlines()[0], |
| 58 self.repo['N']) |
| 59 |
| 60 def testGetCommit(self): |
| 61 # pylint: disable=W0212 |
| 62 r = self.mkRepo() |
| 63 c = r.get_commit(self.repo['L']) |
| 64 self.assertEqual(c.hsh, self.repo['L']) |
| 65 self.assertEqual([self.repo['L']], r._commit_cache.keys()) |
| 66 |
| 67 c2 = r.get_commit(self.repo['L']) |
| 68 self.assertIs(c, c2) |
| 69 |
| 70 def testGetCommitEviction(self): |
| 71 # pylint: disable=W0212 |
| 72 r = self.mkRepo() |
| 73 r.MAX_CACHE_SIZE = 2 |
| 74 L = r.get_commit(self.repo['L']) |
| 75 self.assertIs(L, r.get_commit(self.repo['L'])) |
| 76 self.assertEqual(len(r._commit_cache), 1) |
| 77 |
| 78 O = r.get_commit(self.repo['O']) |
| 79 self.assertIs(L, r.get_commit(self.repo['L'])) |
| 80 self.assertIs(O, r.get_commit(self.repo['O'])) |
| 81 self.assertEqual(len(r._commit_cache), 2) |
| 82 |
| 83 N = r.get_commit(self.repo['N']) |
| 84 self.assertIs(N, r.get_commit(self.repo['N'])) |
| 85 self.assertIs(O, r.get_commit(self.repo['O'])) |
| 86 self.assertEqual(len(r._commit_cache), 2) |
| 87 |
| 88 self.assertIsNot(L, r.get_commit(self.repo['L'])) |
| 89 |
| 90 def testIntern(self): |
| 91 r = self.mkRepo() |
| 92 hsh = r.intern('catfood') |
| 93 self.assertEqual(hsh, hashlib.sha1('blob 7\0catfood').hexdigest()) |
| 94 self.assertEqual('catfood', r.run('cat-file', 'blob', hsh)) |
| 95 |
| 96 def testGetRef(self): |
| 97 r = self.mkRepo() |
| 98 self.assertEqual(r['refs/heads/branch_Z'].commit.hsh, |
| 99 'c9813df312aae7cadb91b0d4eb89ad1a4d1b22c9') |
OLD | NEW |