OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Unit tests for git_common.py""" |
| 7 |
| 8 import binascii |
| 9 import collections |
| 10 import os |
| 11 import signal |
| 12 import sys |
| 13 import tempfile |
| 14 import time |
| 15 import unittest |
| 16 |
| 17 DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 18 sys.path.insert(0, DEPOT_TOOLS_ROOT) |
| 19 |
| 20 from testing_support import git_test_utils |
| 21 from testing_support import coverage_utils |
| 22 |
| 23 |
| 24 class GitCommonTestBase(unittest.TestCase): |
| 25 @classmethod |
| 26 def setUpClass(cls): |
| 27 super(GitCommonTestBase, cls).setUpClass() |
| 28 import git_common |
| 29 cls.gc = git_common |
| 30 |
| 31 |
| 32 class Support(GitCommonTestBase): |
| 33 def testMemoizeOne(self): |
| 34 calls = collections.defaultdict(int) |
| 35 def double_if_even(val): |
| 36 calls[val] += 1 |
| 37 if val % 2 == 0: |
| 38 return val * 2 |
| 39 else: |
| 40 return None |
| 41 # Use this explicitly as a wrapper fn instead of a decorator. Otherwise |
| 42 # pylint crashes (!!) |
| 43 double_if_even = self.gc.memoize_one(double_if_even) |
| 44 self.assertEqual(double_if_even(2), 4) |
| 45 self.assertEqual(double_if_even(2), 4) |
| 46 self.assertEqual(double_if_even(1), None) |
| 47 self.assertEqual(double_if_even(1), None) |
| 48 self.assertDictEqual(calls, {1: 2, 2: 1}) |
| 49 |
| 50 double_if_even.cache[10] = 20 |
| 51 self.assertEqual(double_if_even(10), 20) |
| 52 self.assertDictEqual(calls, {1: 2, 2: 1}) |
| 53 |
| 54 double_if_even.cache.clear() |
| 55 self.assertEqual(double_if_even(2), 4) |
| 56 self.assertEqual(double_if_even(2), 4) |
| 57 self.assertEqual(double_if_even(1), None) |
| 58 self.assertEqual(double_if_even(1), None) |
| 59 self.assertEqual(double_if_even(10), 20) |
| 60 self.assertDictEqual(calls, {1: 4, 2: 2, 10: 1}) |
| 61 |
| 62 |
| 63 def slow_square(i): |
| 64 """Helper for ScopedPoolTest. |
| 65 |
| 66 Must be global because non top-level functions aren't pickleable. |
| 67 """ |
| 68 time.sleep(0.2) |
| 69 return i ** 2 |
| 70 |
| 71 |
| 72 class ScopedPoolTest(GitCommonTestBase): |
| 73 if sys.platform.startswith('win'): |
| 74 CTRL_C = signal.CTRL_C_EVENT |
| 75 else: |
| 76 CTRL_C = signal.SIGINT |
| 77 |
| 78 def testThreads(self): |
| 79 result = [] |
| 80 with self.gc.ScopedPool(kind='threads') as pool: |
| 81 for i in pool.imap(slow_square, xrange(10)): |
| 82 result.append(i) |
| 83 self.assertEqual(result, [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]) |
| 84 |
| 85 def testThreadsCtrlC(self): |
| 86 result = [] |
| 87 with self.assertRaises(KeyboardInterrupt): |
| 88 with self.gc.ScopedPool(kind='threads') as pool: |
| 89 # Make sure this pool is interrupted in mid-swing |
| 90 for i in pool.imap(slow_square, xrange(1000000)): |
| 91 if i > 32: |
| 92 os.kill(os.getpid(), self.CTRL_C) |
| 93 result.append(i) |
| 94 self.assertEqual(result, [0, 1, 4, 9, 16, 25]) |
| 95 |
| 96 def testProcs(self): |
| 97 result = [] |
| 98 with self.gc.ScopedPool() as pool: |
| 99 for i in pool.imap(slow_square, xrange(10)): |
| 100 result.append(i) |
| 101 self.assertEqual(result, [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]) |
| 102 |
| 103 def testProcsCtrlC(self): |
| 104 result = [] |
| 105 with self.assertRaises(KeyboardInterrupt): |
| 106 with self.gc.ScopedPool() as pool: |
| 107 # Make sure this pool is interrupted in mid-swing |
| 108 for i in pool.imap(slow_square, xrange(1000000)): |
| 109 if i > 32: |
| 110 os.kill(os.getpid(), self.CTRL_C) |
| 111 result.append(i) |
| 112 self.assertEqual(result, [0, 1, 4, 9, 16, 25]) |
| 113 |
| 114 |
| 115 class ProgressPrinterTest(GitCommonTestBase): |
| 116 class FakeStream(object): |
| 117 def __init__(self): |
| 118 self.data = set() |
| 119 self.count = 0 |
| 120 |
| 121 def write(self, line): |
| 122 self.data.add(line) |
| 123 |
| 124 def flush(self): |
| 125 self.count += 1 |
| 126 |
| 127 # This test is probably racy, but I don't have a better alternative. |
| 128 @unittest.expectedFailure |
| 129 def testBasic(self): |
| 130 fmt = '%(count)d/10' |
| 131 stream = self.FakeStream() |
| 132 |
| 133 pp = self.gc.ProgressPrinter(fmt, enabled=True, stream=stream, period=0.01) |
| 134 with pp as inc: |
| 135 for _ in xrange(10): |
| 136 time.sleep(0.02) |
| 137 inc() |
| 138 |
| 139 filtered = set(x.strip() for x in stream.data) |
| 140 rslt = set(fmt % {'count': i} for i in xrange(11)) |
| 141 self.assertSetEqual(filtered, rslt) |
| 142 self.assertGreaterEqual(stream.count, 10) |
| 143 |
| 144 |
| 145 class GitReadOnlyFunctionsTest(git_test_utils.GitRepoReadOnlyTestBase, |
| 146 GitCommonTestBase): |
| 147 REPO = """ |
| 148 A B C D |
| 149 B E D |
| 150 """ |
| 151 |
| 152 COMMIT_A = { |
| 153 'some/files/file1': {'data': 'file1'}, |
| 154 'some/files/file2': {'data': 'file2'}, |
| 155 'some/files/file3': {'data': 'file3'}, |
| 156 'some/other/file': {'data': 'otherfile'}, |
| 157 } |
| 158 |
| 159 COMMIT_C = { |
| 160 'some/files/file2': { |
| 161 'mode': 0755, |
| 162 'data': 'file2 - vanilla'}, |
| 163 } |
| 164 |
| 165 COMMIT_E = { |
| 166 'some/files/file2': {'data': 'file2 - merged'}, |
| 167 } |
| 168 |
| 169 COMMIT_D = { |
| 170 'some/files/file2': {'data': 'file2 - vanilla\nfile2 - merged'}, |
| 171 } |
| 172 |
| 173 def testHashes(self): |
| 174 ret = self.repo.run( |
| 175 self.gc.hashes, *[ |
| 176 'master', |
| 177 'master~3', |
| 178 self.repo['E']+'~', |
| 179 self.repo['D']+'^2', |
| 180 'tag_C^{}', |
| 181 ] |
| 182 ) |
| 183 self.assertEqual(ret, [ |
| 184 self.repo['D'], |
| 185 self.repo['A'], |
| 186 self.repo['B'], |
| 187 self.repo['E'], |
| 188 self.repo['C'], |
| 189 ]) |
| 190 |
| 191 def testParseCommittishes(self): |
| 192 ret = self.repo.run( |
| 193 self.gc.parse_committishes, *[ |
| 194 'master', |
| 195 'master~3', |
| 196 self.repo['E']+'~', |
| 197 self.repo['D']+'^2', |
| 198 'tag_C^{}', |
| 199 ] |
| 200 ) |
| 201 self.assertEqual(ret, map(binascii.unhexlify, [ |
| 202 self.repo['D'], |
| 203 self.repo['A'], |
| 204 self.repo['B'], |
| 205 self.repo['E'], |
| 206 self.repo['C'], |
| 207 ])) |
| 208 |
| 209 with self.assertRaisesRegexp(Exception, r"one of \('master', 'bananas'\)"): |
| 210 self.repo.run(self.gc.parse_committishes, 'master', 'bananas') |
| 211 |
| 212 def testTree(self): |
| 213 tree = self.repo.run(self.gc.tree, 'master:some/files') |
| 214 file1 = self.COMMIT_A['some/files/file1']['data'] |
| 215 file2 = self.COMMIT_D['some/files/file2']['data'] |
| 216 file3 = self.COMMIT_A['some/files/file3']['data'] |
| 217 self.assertEquals(tree['file1'], |
| 218 ('100644', 'blob', git_test_utils.git_hash_data(file1))) |
| 219 self.assertEquals(tree['file2'], |
| 220 ('100755', 'blob', git_test_utils.git_hash_data(file2))) |
| 221 self.assertEquals(tree['file3'], |
| 222 ('100644', 'blob', git_test_utils.git_hash_data(file3))) |
| 223 |
| 224 tree = self.repo.run(self.gc.tree, 'master:some') |
| 225 self.assertEquals(len(tree), 2) |
| 226 # Don't check the tree hash because we're lazy :) |
| 227 self.assertEquals(tree['files'][:2], ('040000', 'tree')) |
| 228 |
| 229 tree = self.repo.run(self.gc.tree, 'master:wat') |
| 230 self.assertEqual(tree, None) |
| 231 |
| 232 def testTreeRecursive(self): |
| 233 tree = self.repo.run(self.gc.tree, 'master:some', recurse=True) |
| 234 file1 = self.COMMIT_A['some/files/file1']['data'] |
| 235 file2 = self.COMMIT_D['some/files/file2']['data'] |
| 236 file3 = self.COMMIT_A['some/files/file3']['data'] |
| 237 other = self.COMMIT_A['some/other/file']['data'] |
| 238 self.assertEquals(tree['files/file1'], |
| 239 ('100644', 'blob', git_test_utils.git_hash_data(file1))) |
| 240 self.assertEquals(tree['files/file2'], |
| 241 ('100755', 'blob', git_test_utils.git_hash_data(file2))) |
| 242 self.assertEquals(tree['files/file3'], |
| 243 ('100644', 'blob', git_test_utils.git_hash_data(file3))) |
| 244 self.assertEquals(tree['other/file'], |
| 245 ('100644', 'blob', git_test_utils.git_hash_data(other))) |
| 246 |
| 247 def testError(self): |
| 248 with self.assertRaisesRegexp(self.gc.CalledProcessError, 'status 1'): |
| 249 self.gc.run('rev-parse', 'bananas') |
| 250 |
| 251 |
| 252 class GitMutableFunctionsTest(git_test_utils.GitRepoReadWriteTestBase, |
| 253 GitCommonTestBase): |
| 254 REPO = '' |
| 255 |
| 256 def _intern_data(self, data): |
| 257 with tempfile.TemporaryFile() as f: |
| 258 f.write(data) |
| 259 f.seek(0) |
| 260 return self.repo.run(self.gc.intern_f, f) |
| 261 |
| 262 def testIndata(self): |
| 263 data = 'WayCOOL SuperTest!' |
| 264 data_hash = self.repo.run( |
| 265 self.gc.run, 'hash-object', '-t', 'blob', '--stdin', indata=data) |
| 266 self.assertEqual(data_hash, git_test_utils.git_hash_data(data)) |
| 267 |
| 268 def testInternF(self): |
| 269 data = "CoolBobcatsBro" |
| 270 data_hash = self._intern_data(data) |
| 271 self.assertEqual(data_hash, git_test_utils.git_hash_data(data)) |
| 272 self.assertEqual(self.repo.git('cat-file', 'blob', data_hash).stdout, data) |
| 273 |
| 274 def testMkTree(self): |
| 275 tree = {} |
| 276 for i in 1, 2, 3: |
| 277 name = 'file%d' % i |
| 278 tree[name] = ('100644', 'blob', self._intern_data(name)) |
| 279 tree_hash = self.repo.run(self.gc.mktree, tree) |
| 280 self.assertEqual(tree_hash, '37b61866d6e061c4ba478e7eb525be7b5752737d') |
| 281 |
| 282 |
| 283 if __name__ == '__main__': |
| 284 sys.exit(coverage_utils.covered_main( |
| 285 os.path.join(DEPOT_TOOLS_ROOT, 'git_common.py') |
| 286 )) |
OLD | NEW |