OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for git_common.py""" | 6 """Unit tests for git_common.py""" |
7 | 7 |
8 import binascii | 8 import binascii |
9 import collections | 9 import collections |
10 import os | 10 import os |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 COMMIT_A = { | 169 COMMIT_A = { |
170 'some/files/file1': {'data': 'file1'}, | 170 'some/files/file1': {'data': 'file1'}, |
171 'some/files/file2': {'data': 'file2'}, | 171 'some/files/file2': {'data': 'file2'}, |
172 'some/files/file3': {'data': 'file3'}, | 172 'some/files/file3': {'data': 'file3'}, |
173 'some/other/file': {'data': 'otherfile'}, | 173 'some/other/file': {'data': 'otherfile'}, |
174 } | 174 } |
175 | 175 |
176 COMMIT_C = { | 176 COMMIT_C = { |
177 'some/files/file2': { | 177 'some/files/file2': { |
178 'mode': 0755, | 178 'mode': 0755, |
179 'data': 'file2 - vanilla'}, | 179 'data': 'file2 - vanilla\n'}, |
180 } | 180 } |
181 | 181 |
182 COMMIT_E = { | 182 COMMIT_E = { |
183 'some/files/file2': {'data': 'file2 - merged'}, | 183 'some/files/file2': {'data': 'file2 - merged\n'}, |
184 } | 184 } |
185 | 185 |
186 COMMIT_D = { | 186 COMMIT_D = { |
187 'some/files/file2': {'data': 'file2 - vanilla\nfile2 - merged'}, | 187 'some/files/file2': {'data': 'file2 - vanilla\nfile2 - merged\n'}, |
188 } | 188 } |
189 | 189 |
190 def testHashes(self): | 190 def testHashes(self): |
191 ret = self.repo.run( | 191 ret = self.repo.run( |
192 self.gc.hash_multi, *[ | 192 self.gc.hash_multi, *[ |
193 'master', | 193 'master', |
194 'master~3', | 194 'master~3', |
195 self.repo['E']+'~', | 195 self.repo['E']+'~', |
196 self.repo['D']+'^2', | 196 self.repo['D']+'^2', |
197 'tag_C^{}', | 197 'tag_C^{}', |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 def testBranches(self): | 252 def testBranches(self): |
253 # This check fails with git 2.4 (see crbug.com/487172) | 253 # This check fails with git 2.4 (see crbug.com/487172) |
254 self.assertEqual(self.repo.run(set, self.gc.branches()), | 254 self.assertEqual(self.repo.run(set, self.gc.branches()), |
255 {'master', 'branch_D', 'root_A'}) | 255 {'master', 'branch_D', 'root_A'}) |
256 | 256 |
257 def testDormant(self): | 257 def testDormant(self): |
258 self.assertFalse(self.repo.run(self.gc.is_dormant, 'master')) | 258 self.assertFalse(self.repo.run(self.gc.is_dormant, 'master')) |
259 self.repo.git('config', 'branch.master.dormant', 'true') | 259 self.repo.git('config', 'branch.master.dormant', 'true') |
260 self.assertTrue(self.repo.run(self.gc.is_dormant, 'master')) | 260 self.assertTrue(self.repo.run(self.gc.is_dormant, 'master')) |
261 | 261 |
| 262 def testBlame(self): |
| 263 def get_porcelain_for_commit(commit_name, lines): |
| 264 format_string = ('%H {}\nauthor %an\nauthor-mail <%ae>\nauthor-time %at\n' |
| 265 'author-tz +0000\ncommitter %cn\ncommitter-mail <%ce>\n' |
| 266 'committer-time %ct\ncommitter-tz +0000\nsummary {}') |
| 267 format_string = format_string.format(lines, commit_name) |
| 268 info = self.repo.show_commit(commit_name, format_string=format_string) |
| 269 return info.split('\n') |
| 270 |
| 271 # Expect to blame line 1 on C, line 2 on E. |
| 272 c_short = self.repo['C'][:8] |
| 273 c_author = self.repo.show_commit('C', format_string='%an %ai') |
| 274 e_short = self.repo['E'][:8] |
| 275 e_author = self.repo.show_commit('E', format_string='%an %ai') |
| 276 expected_output = ['%s (%s 1) file2 - vanilla' % (c_short, c_author), |
| 277 '%s (%s 2) file2 - merged' % (e_short, e_author)] |
| 278 self.assertEqual(expected_output, |
| 279 self.repo.run(self.gc.blame, 'some/files/file2', |
| 280 'tag_D').split('\n')) |
| 281 |
| 282 # Test porcelain. |
| 283 expected_output = [] |
| 284 expected_output.extend(get_porcelain_for_commit('C', '1 1 1')) |
| 285 expected_output.append('previous %s some/files/file2' % self.repo['B']) |
| 286 expected_output.append('filename some/files/file2') |
| 287 expected_output.append('\tfile2 - vanilla') |
| 288 expected_output.extend(get_porcelain_for_commit('E', '1 2 1')) |
| 289 expected_output.append('previous %s some/files/file2' % self.repo['B']) |
| 290 expected_output.append('filename some/files/file2') |
| 291 expected_output.append('\tfile2 - merged') |
| 292 self.assertEqual(expected_output, |
| 293 self.repo.run(self.gc.blame, 'some/files/file2', |
| 294 'tag_D', porcelain=True).split('\n')) |
| 295 |
262 def testParseCommitrefs(self): | 296 def testParseCommitrefs(self): |
263 ret = self.repo.run( | 297 ret = self.repo.run( |
264 self.gc.parse_commitrefs, *[ | 298 self.gc.parse_commitrefs, *[ |
265 'master', | 299 'master', |
266 'master~3', | 300 'master~3', |
267 self.repo['E']+'~', | 301 self.repo['E']+'~', |
268 self.repo['D']+'^2', | 302 self.repo['D']+'^2', |
269 'tag_C^{}', | 303 'tag_C^{}', |
270 ] | 304 ] |
271 ) | 305 ) |
272 self.assertEqual(ret, map(binascii.unhexlify, [ | 306 self.assertEqual(ret, map(binascii.unhexlify, [ |
273 self.repo['D'], | 307 self.repo['D'], |
274 self.repo['A'], | 308 self.repo['A'], |
275 self.repo['B'], | 309 self.repo['B'], |
276 self.repo['E'], | 310 self.repo['E'], |
277 self.repo['C'], | 311 self.repo['C'], |
278 ])) | 312 ])) |
279 | 313 |
280 with self.assertRaisesRegexp(Exception, r"one of \('master', 'bananas'\)"): | 314 with self.assertRaisesRegexp(Exception, r"one of \('master', 'bananas'\)"): |
281 self.repo.run(self.gc.parse_commitrefs, 'master', 'bananas') | 315 self.repo.run(self.gc.parse_commitrefs, 'master', 'bananas') |
282 | 316 |
| 317 def testRepoRoot(self): |
| 318 def cd_and_repo_root(path): |
| 319 print(os.getcwd()) |
| 320 os.chdir(path) |
| 321 return self.gc.repo_root() |
| 322 |
| 323 self.assertEqual(self.repo.repo_path, self.repo.run(self.gc.repo_root)) |
| 324 # cd to a subdirectory; repo_root should still return the root dir. |
| 325 self.assertEqual(self.repo.repo_path, |
| 326 self.repo.run(cd_and_repo_root, 'some/files')) |
| 327 |
283 def testTags(self): | 328 def testTags(self): |
284 self.assertEqual(set(self.repo.run(self.gc.tags)), | 329 self.assertEqual(set(self.repo.run(self.gc.tags)), |
285 {'tag_'+l for l in 'ABCDE'}) | 330 {'tag_'+l for l in 'ABCDE'}) |
286 | 331 |
287 def testTree(self): | 332 def testTree(self): |
288 tree = self.repo.run(self.gc.tree, 'master:some/files') | 333 tree = self.repo.run(self.gc.tree, 'master:some/files') |
289 file1 = self.COMMIT_A['some/files/file1']['data'] | 334 file1 = self.COMMIT_A['some/files/file1']['data'] |
290 file2 = self.COMMIT_D['some/files/file2']['data'] | 335 file2 = self.COMMIT_D['some/files/file2']['data'] |
291 file3 = self.COMMIT_A['some/files/file3']['data'] | 336 file3 = self.COMMIT_A['some/files/file3']['data'] |
292 self.assertEquals( | 337 self.assertEquals( |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 self.repo.show_commit('A', format_string='%cn %ci')) | 841 self.repo.show_commit('A', format_string='%cn %ci')) |
797 self.assertEquals('Author McAuthorly 1970-01-03 00:00:00 +0000', | 842 self.assertEquals('Author McAuthorly 1970-01-03 00:00:00 +0000', |
798 self.repo.show_commit('B', format_string='%an %ai')) | 843 self.repo.show_commit('B', format_string='%an %ai')) |
799 self.assertEquals('Charles Committish 1970-01-04 00:00:00 +0000', | 844 self.assertEquals('Charles Committish 1970-01-04 00:00:00 +0000', |
800 self.repo.show_commit('B', format_string='%cn %ci')) | 845 self.repo.show_commit('B', format_string='%cn %ci')) |
801 | 846 |
802 | 847 |
803 if __name__ == '__main__': | 848 if __name__ == '__main__': |
804 sys.exit(coverage_utils.covered_main( | 849 sys.exit(coverage_utils.covered_main( |
805 os.path.join(DEPOT_TOOLS_ROOT, 'git_common.py'))) | 850 os.path.join(DEPOT_TOOLS_ROOT, 'git_common.py'))) |
OLD | NEW |