| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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 gclient_scm.py.""" | 6 """Unit tests for gclient_scm.py.""" |
| 7 | 7 |
| 8 # Import before super_mox to keep valid references. | 8 # Import before super_mox to keep valid references. |
| 9 from os import rename | 9 from os import rename |
| 10 from shutil import rmtree | 10 from shutil import rmtree |
| 11 from subprocess import Popen, PIPE, STDOUT | 11 from subprocess import Popen, PIPE, STDOUT |
| 12 import tempfile | 12 import tempfile |
| 13 import __builtin__ |
| 13 | 14 |
| 14 # Fixes include path. | 15 # Fixes include path. |
| 15 from super_mox import mox, SuperMoxBaseTestBase | 16 from super_mox import mox, SuperMoxBaseTestBase, SuperMoxTestBase |
| 16 | 17 |
| 17 import gclient_scm | 18 import gclient_scm |
| 18 from gclient_test import BaseTestCase as GCBaseTestCase | 19 from gclient_test import BaseTestCase as GCBaseTestCase |
| 19 | 20 |
| 20 | 21 |
| 21 class BaseTestCase(GCBaseTestCase): | 22 class BaseTestCase(GCBaseTestCase): |
| 22 def setUp(self): | 23 def setUp(self): |
| 23 GCBaseTestCase.setUp(self) | 24 GCBaseTestCase.setUp(self) |
| 24 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') | 25 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') |
| 25 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') | 26 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 try: | 347 try: |
| 347 Popen(['git', 'init'], stdout=PIPE, stderr=STDOUT, | 348 Popen(['git', 'init'], stdout=PIPE, stderr=STDOUT, |
| 348 cwd=path).communicate() | 349 cwd=path).communicate() |
| 349 except OSError: | 350 except OSError: |
| 350 # git is not available, skip this test. | 351 # git is not available, skip this test. |
| 351 return False | 352 return False |
| 352 Popen(['git', 'fast-import'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, | 353 Popen(['git', 'fast-import'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, |
| 353 cwd=path).communicate(input=git_import) | 354 cwd=path).communicate(input=git_import) |
| 354 Popen(['git', 'checkout'], stdout=PIPE, stderr=STDOUT, | 355 Popen(['git', 'checkout'], stdout=PIPE, stderr=STDOUT, |
| 355 cwd=path).communicate() | 356 cwd=path).communicate() |
| 357 Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE, |
| 358 stderr=STDOUT, cwd=path).communicate() |
| 359 Popen(['git', 'checkout', '-b', 'new', 'origin/master'], stdout=PIPE, |
| 360 stderr=STDOUT, cwd=path).communicate() |
| 361 Popen(['git', 'push', 'origin', 'origin/origin:origin/master'], stdout=PIPE, |
| 362 stderr=STDOUT, cwd=path).communicate() |
| 363 Popen(['git', 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE, |
| 364 stderr=STDOUT, cwd=path).communicate() |
| 356 return True | 365 return True |
| 357 | 366 |
| 358 def setUp(self): | 367 def setUp(self): |
| 359 self.args = self.Args() | 368 self.args = self.Args() |
| 360 self.url = 'git://foo' | 369 self.url = 'git://foo' |
| 361 self.root_dir = tempfile.mkdtemp() | 370 self.root_dir = tempfile.mkdtemp() |
| 362 self.relpath = '.' | 371 self.relpath = '.' |
| 363 self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) | 372 self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 364 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) | 373 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
| 365 SuperMoxBaseTestBase.setUp(self) | 374 SuperMoxBaseTestBase.setUp(self) |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 expected_file_list = [gclient_scm.os.path.join(self.base_path, x) | 514 expected_file_list = [gclient_scm.os.path.join(self.base_path, x) |
| 506 for x in ['a', 'b']] | 515 for x in ['a', 'b']] |
| 507 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 516 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 508 relpath=self.relpath) | 517 relpath=self.relpath) |
| 509 file_list = [] | 518 file_list = [] |
| 510 scm.update(options, (), file_list) | 519 scm.update(options, (), file_list) |
| 511 self.assertEquals(file_list, expected_file_list) | 520 self.assertEquals(file_list, expected_file_list) |
| 512 self.assertEquals(scm.revinfo(options, (), None), | 521 self.assertEquals(scm.revinfo(options, (), None), |
| 513 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 522 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 514 | 523 |
| 524 def testUpdateUnstagedConflict(self): |
| 525 if not self.enabled: |
| 526 return |
| 527 options = self.Options() |
| 528 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 529 relpath=self.relpath) |
| 530 file_path = gclient_scm.os.path.join(self.base_path, 'b') |
| 531 f = open(file_path, 'w').writelines('conflict\n') |
| 532 exception = ( |
| 533 "error: Your local changes to 'b' would be overwritten by merge. " |
| 534 "Aborting.\n" |
| 535 "Please, commit your changes or stash them before you can merge.\n") |
| 536 self.assertRaisesError(exception, scm.update, options, (), []) |
| 537 |
| 515 def testUpdateConflict(self): | 538 def testUpdateConflict(self): |
| 516 if not self.enabled: | 539 if not self.enabled: |
| 517 return | 540 return |
| 518 options = self.Options() | 541 options = self.Options() |
| 519 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 542 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 520 relpath=self.relpath) | 543 relpath=self.relpath) |
| 521 file_path = gclient_scm.os.path.join(self.base_path, 'b') | 544 file_path = gclient_scm.os.path.join(self.base_path, 'b') |
| 522 f = open(file_path, 'w').writelines('conflict\n') | 545 f = open(file_path, 'w').writelines('conflict\n') |
| 523 scm._Run(['commit', '-am', 'test']) | 546 scm._Run(['commit', '-am', 'test']) |
| 547 self.mox.StubOutWithMock(__builtin__, 'raw_input') |
| 548 __builtin__.raw_input.__call__(mox.StrContains('Cannot fast-forward merge, ' |
| 549 'attempt to rebase? (y)es / ' |
| 550 '(q)uit / (s)kip : ') |
| 551 ).AndReturn('y') |
| 552 self.mox.ReplayAll() |
| 524 exception = \ | 553 exception = \ |
| 525 '\n____ .\n' \ | 554 'Conflict while rebasing this branch.\n' \ |
| 526 '\nConflict while rebasing this branch.\n' \ | |
| 527 'Fix the conflict and run gclient again.\n' \ | 555 'Fix the conflict and run gclient again.\n' \ |
| 528 'See man git-rebase for details.\n' | 556 "See 'man git-rebase' for details.\n" |
| 529 self.assertRaisesError(exception, scm.update, options, (), []) | 557 self.assertRaisesError(exception, scm.update, options, (), []) |
| 530 exception = \ | 558 exception = \ |
| 531 '\n____ .\n' \ | 559 '\n____ . at refs/heads/master\n' \ |
| 532 '\tAlready in a conflict, i.e. (no branch).\n' \ | 560 '\tAlready in a conflict, i.e. (no branch).\n' \ |
| 533 '\tFix the conflict and run gclient again.\n' \ | 561 '\tFix the conflict and run gclient again.\n' \ |
| 534 '\tOr to abort run:\n\t\tgit-rebase --abort\n' \ | 562 '\tOr to abort run:\n\t\tgit-rebase --abort\n' \ |
| 535 '\tSee man git-rebase for details.\n' | 563 '\tSee man git-rebase for details.\n' |
| 536 self.assertRaisesError(exception, scm.update, options, (), []) | 564 self.assertRaisesError(exception, scm.update, options, (), []) |
| 537 | 565 |
| 538 def testUpdateNotGit(self): | 566 def testUpdateNotGit(self): |
| 539 if not self.enabled: | 567 if not self.enabled: |
| 540 return | 568 return |
| 541 options = self.Options() | 569 options = self.Options() |
| 542 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 570 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 543 relpath=self.relpath) | 571 relpath=self.relpath) |
| 544 git_path = gclient_scm.os.path.join(self.base_path, '.git') | 572 git_path = gclient_scm.os.path.join(self.base_path, '.git') |
| 545 rename(git_path, git_path + 'foo') | 573 rename(git_path, git_path + 'foo') |
| 546 exception = \ | 574 exception = \ |
| 547 '\n____ .\n' \ | 575 '\n____ . at refs/heads/master\n' \ |
| 548 '\tPath is not a git repo. No .git dir.\n' \ | 576 '\tPath is not a git repo. No .git dir.\n' \ |
| 549 '\tTo resolve:\n' \ | 577 '\tTo resolve:\n' \ |
| 550 '\t\trm -rf .\n' \ | 578 '\t\trm -rf .\n' \ |
| 551 '\tAnd run gclient sync again\n' | 579 '\tAnd run gclient sync again\n' |
| 552 self.assertRaisesError(exception, scm.update, options, (), []) | 580 self.assertRaisesError(exception, scm.update, options, (), []) |
| 553 | 581 |
| 554 def testRevinfo(self): | 582 def testRevinfo(self): |
| 555 if not self.enabled: | 583 if not self.enabled: |
| 556 return | 584 return |
| 557 options = self.Options() | 585 options = self.Options() |
| 558 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 586 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 559 relpath=self.relpath) | 587 relpath=self.relpath) |
| 560 rev_info = scm.revinfo(options, (), None) | 588 rev_info = scm.revinfo(options, (), None) |
| 561 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') | 589 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') |
| 562 | 590 |
| 563 | 591 |
| 564 if __name__ == '__main__': | 592 if __name__ == '__main__': |
| 565 import unittest | 593 import unittest |
| 566 unittest.main() | 594 unittest.main() |
| 567 | 595 |
| 568 # vim: ts=2:sw=2:tw=80:et: | 596 # vim: ts=2:sw=2:tw=80:et: |
| OLD | NEW |