Chromium Code Reviews| Index: recipe_engine/unittests/fetch_test.py |
| diff --git a/recipe_engine/unittests/fetch_test.py b/recipe_engine/unittests/fetch_test.py |
| index b3c7260d9ea92a8bef0b77f73e76d89388f592ef..b2259251d1f3ed99c21fa32bea3dc1f67a4c357f 100755 |
| --- a/recipe_engine/unittests/fetch_test.py |
| +++ b/recipe_engine/unittests/fetch_test.py |
| @@ -21,20 +21,35 @@ from recipe_engine import requests_ssl |
| class TestGit(unittest.TestCase): |
|
martiniss
2016/09/26 22:58:02
Can you add a test case like in the bug? :)
dnj
2016/09/26 23:48:57
Done.
|
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_fresh_clone(self, run_git): |
| + |
| + def setUp(self): |
| + self._patchers = [ |
| + mock.patch('logging.warning'), |
| + mock.patch('recipe_engine.fetch.GitBackend.Git._resolve_git', |
| + return_value='GIT'), |
| + ] |
| + for p in self._patchers: |
| + p.start() |
| + |
| + def tearDown(self): |
| + for p in reversed(self._patchers): |
| + p.stop() |
| + |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_fresh_clone(self, git): |
| fetch.GitBackend().checkout( |
| 'repo', 'revision', 'dir', allow_fetch=True) |
| - run_git.assert_has_calls([ |
| - mock.call(None, 'clone', '-q', 'repo', 'dir'), |
| - mock.call('dir', 'config', 'remote.origin.url', 'repo'), |
| - mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| - mock.call('dir', 'reset', '-q', '--hard', 'revision'), |
| + git.assert_has_calls([ |
| + mock.call('GIT', 'clone', '-q', 'repo', 'dir'), |
| + mock.call('GIT', '-C', 'dir', 'config', 'remote.origin.url', 'repo'), |
| + mock.call('GIT', '-C', 'dir', 'rev-parse', '-q', '--verify', |
| + 'revision^{commit}'), |
| + mock.call('GIT', '-C', 'dir', 'reset', '-q', '--hard', 'revision'), |
| ]) |
| @mock.patch('os.path.isdir') |
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_existing_checkout(self, run_git, isdir): |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_existing_checkout(self, git, isdir): |
| isdir.return_value = True |
| fetch.GitBackend().checkout( |
| 'repo', 'revision', 'dir', allow_fetch=True) |
| @@ -42,21 +57,22 @@ class TestGit(unittest.TestCase): |
| mock.call('dir'), |
| mock.call('dir/.git'), |
| ]) |
| - run_git.assert_has_calls([ |
| - mock.call('dir', 'config', 'remote.origin.url', 'repo'), |
| - mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| - mock.call('dir', 'reset', '-q', '--hard', 'revision'), |
| + git.assert_has_calls([ |
| + mock.call('GIT', '-C', 'dir', 'config', 'remote.origin.url', 'repo'), |
| + mock.call('GIT', '-C', 'dir', 'rev-parse', '-q', '--verify', |
| + 'revision^{commit}'), |
| + mock.call('GIT', '-C', 'dir', 'reset', '-q', '--hard', 'revision'), |
| ]) |
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_clone_not_allowed(self, run_git): |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_clone_not_allowed(self, git): |
| with self.assertRaises(fetch.FetchNotAllowedError): |
| fetch.GitBackend().checkout( |
| 'repo', 'revision', 'dir', allow_fetch=False) |
| @mock.patch('os.path.isdir') |
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_unclean_filesystem(self, run_git, isdir): |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_unclean_filesystem(self, git, isdir): |
| isdir.side_effect = [True, False] |
| with self.assertRaises(fetch.UncleanFilesystemError): |
| fetch.GitBackend().checkout( |
| @@ -67,9 +83,9 @@ class TestGit(unittest.TestCase): |
| ]) |
| @mock.patch('os.path.isdir') |
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_origin_mismatch(self, run_git, isdir): |
| - run_git.return_value = 'not-repo' |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_origin_mismatch(self, git, isdir): |
| + git.return_value = 'not-repo' |
| isdir.return_value = True |
| # This should not raise UncleanFilesystemError, but instead |
| @@ -81,14 +97,14 @@ class TestGit(unittest.TestCase): |
| mock.call('dir'), |
| mock.call('dir/.git'), |
| ]) |
| - run_git.assert_has_calls([ |
| - mock.call('dir', 'config', 'remote.origin.url', 'repo'), |
| + git.assert_has_calls([ |
| + mock.call('GIT', '-C', 'dir', 'config', 'remote.origin.url', 'repo'), |
| ]) |
| @mock.patch('os.path.isdir') |
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_rev_parse_fail(self, run_git, isdir): |
| - run_git.side_effect = [ |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_rev_parse_fail(self, git, isdir): |
| + git.side_effect = [ |
| None, |
| subprocess42.CalledProcessError(1, ['fakecmd']), |
| None, |
| @@ -101,17 +117,18 @@ class TestGit(unittest.TestCase): |
| mock.call('dir'), |
| mock.call('dir/.git'), |
| ]) |
| - run_git.assert_has_calls([ |
| - mock.call('dir', 'config', 'remote.origin.url', 'repo'), |
| - mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| - mock.call('dir', 'fetch'), |
| - mock.call('dir', 'reset', '-q', '--hard', 'revision'), |
| + git.assert_has_calls([ |
| + mock.call('GIT', '-C', 'dir', 'config', 'remote.origin.url', 'repo'), |
| + mock.call('GIT', '-C', 'dir', 'rev-parse', '-q', '--verify', |
| + 'revision^{commit}'), |
| + mock.call('GIT', '-C', 'dir', 'fetch'), |
| + mock.call('GIT', '-C', 'dir', 'reset', '-q', '--hard', 'revision'), |
| ]) |
| @mock.patch('os.path.isdir') |
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_rev_parse_fetch_not_allowed(self, run_git, isdir): |
| - run_git.side_effect = [ |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_rev_parse_fetch_not_allowed(self, git, isdir): |
| + git.side_effect = [ |
| None, |
| subprocess42.CalledProcessError(1, ['fakecmd']), |
| ] |
| @@ -123,23 +140,24 @@ class TestGit(unittest.TestCase): |
| mock.call('dir'), |
| mock.call('dir/.git'), |
| ]) |
| - run_git.assert_has_calls([ |
| - mock.call('dir', 'config', 'remote.origin.url', 'repo'), |
| - mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| + git.assert_has_calls([ |
| + mock.call('GIT', '-C', 'dir', 'config', 'remote.origin.url', 'repo'), |
| + mock.call('GIT', '-C', 'dir', 'rev-parse', '-q', '--verify', |
| + 'revision^{commit}'), |
| ]) |
| - @mock.patch('recipe_engine.fetch._run_git') |
| - def test_commit_metadata(self, run_git): |
| - run_git.side_effect = ['author', 'message'] |
| + @mock.patch('recipe_engine.fetch.GitBackend.Git._execute') |
| + def test_commit_metadata(self, git): |
| + git.side_effect = ['author', 'message'] |
| result = fetch.GitBackend().commit_metadata( |
| 'repo', 'revision', 'dir', allow_fetch=True) |
| self.assertEqual(result, { |
| 'author': 'author', |
| 'message': 'message', |
| }) |
| - run_git.assert_has_calls([ |
| - mock.call('dir', 'show', '-s', '--pretty=%aE', 'revision'), |
| - mock.call('dir', 'show', '-s', '--pretty=%B', 'revision'), |
| + git.assert_has_calls([ |
| + mock.call('GIT', '-C', 'dir', 'show', '-s', '--pretty=%aE', 'revision'), |
| + mock.call('GIT', '-C', 'dir', 'show', '-s', '--pretty=%B', 'revision'), |
| ]) |