OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 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 import os |
| 7 import sys |
| 8 import unittest |
| 9 |
| 10 BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname( |
| 11 os.path.abspath(__file__)))) |
| 12 THIRD_PARTY = os.path.join(BASE_DIR, 'recipe_engine', 'third_party') |
| 13 sys.path.insert(0, os.path.join(THIRD_PARTY, 'mock-1.0.1')) |
| 14 sys.path.insert(0, BASE_DIR) |
| 15 sys.path.insert(0, THIRD_PARTY) |
| 16 |
| 17 import mock |
| 18 import subprocess42 |
| 19 |
| 20 from recipe_engine import fetch |
| 21 |
| 22 |
| 23 class TestGit(unittest.TestCase): |
| 24 @mock.patch('recipe_engine.fetch._run_git') |
| 25 def test_fresh_clone(self, run_git): |
| 26 run_git.side_effect = [ |
| 27 None, |
| 28 'repo\n', |
| 29 None, |
| 30 None, |
| 31 ] |
| 32 fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=True) |
| 33 run_git.assert_has_calls([ |
| 34 mock.call(None, 'clone', '-q', 'repo', 'dir'), |
| 35 mock.call('dir', 'config', 'remote.origin.url'), |
| 36 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| 37 mock.call('dir', 'reset', '-q', '--hard', 'revision'), |
| 38 ]) |
| 39 |
| 40 @mock.patch('os.path.isdir') |
| 41 @mock.patch('recipe_engine.fetch._run_git') |
| 42 def test_existing_checkout(self, run_git, isdir): |
| 43 run_git.side_effect = [ |
| 44 'repo\n', |
| 45 None, |
| 46 None, |
| 47 ] |
| 48 isdir.return_value = True |
| 49 fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=True) |
| 50 isdir.assert_has_calls([ |
| 51 mock.call('dir'), |
| 52 mock.call('dir/.git'), |
| 53 ]) |
| 54 run_git.assert_has_calls([ |
| 55 mock.call('dir', 'config', 'remote.origin.url'), |
| 56 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| 57 mock.call('dir', 'reset', '-q', '--hard', 'revision'), |
| 58 ]) |
| 59 |
| 60 @mock.patch('recipe_engine.fetch._run_git') |
| 61 def test_clone_not_allowed(self, run_git): |
| 62 with self.assertRaises(fetch.FetchNotAllowedError): |
| 63 fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=False) |
| 64 |
| 65 @mock.patch('os.path.isdir') |
| 66 @mock.patch('recipe_engine.fetch._run_git') |
| 67 def test_unclean_filesystem(self, run_git, isdir): |
| 68 isdir.side_effect = [True, False] |
| 69 with self.assertRaises(fetch.UncleanFilesystemError): |
| 70 fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=False) |
| 71 isdir.assert_has_calls([ |
| 72 mock.call('dir'), |
| 73 mock.call('dir/.git'), |
| 74 ]) |
| 75 |
| 76 @mock.patch('os.path.isdir') |
| 77 @mock.patch('recipe_engine.fetch._run_git') |
| 78 def test_origin_mismatch(self, run_git, isdir): |
| 79 run_git.return_value = 'not-repo' |
| 80 isdir.return_value = True |
| 81 with self.assertRaises(fetch.UncleanFilesystemError): |
| 82 fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=False) |
| 83 isdir.assert_has_calls([ |
| 84 mock.call('dir'), |
| 85 mock.call('dir/.git'), |
| 86 ]) |
| 87 run_git.assert_has_calls([ |
| 88 mock.call('dir', 'config', 'remote.origin.url'), |
| 89 ]) |
| 90 |
| 91 @mock.patch('os.path.isdir') |
| 92 @mock.patch('recipe_engine.fetch._run_git') |
| 93 def test_rev_parse_fail(self, run_git, isdir): |
| 94 run_git.side_effect = [ |
| 95 'repo', |
| 96 subprocess42.CalledProcessError(1, ['fakecmd']), |
| 97 None, |
| 98 None, |
| 99 ] |
| 100 isdir.return_value = True |
| 101 fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=True) |
| 102 isdir.assert_has_calls([ |
| 103 mock.call('dir'), |
| 104 mock.call('dir/.git'), |
| 105 ]) |
| 106 run_git.assert_has_calls([ |
| 107 mock.call('dir', 'config', 'remote.origin.url'), |
| 108 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| 109 mock.call('dir', 'fetch'), |
| 110 mock.call('dir', 'reset', '-q', '--hard', 'revision'), |
| 111 ]) |
| 112 |
| 113 @mock.patch('os.path.isdir') |
| 114 @mock.patch('recipe_engine.fetch._run_git') |
| 115 def test_rev_parse_fetch_not_allowed(self, run_git, isdir): |
| 116 run_git.side_effect = [ |
| 117 'repo', |
| 118 subprocess42.CalledProcessError(1, ['fakecmd']), |
| 119 ] |
| 120 isdir.return_value = True |
| 121 with self.assertRaises(fetch.FetchNotAllowedError): |
| 122 fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=False) |
| 123 isdir.assert_has_calls([ |
| 124 mock.call('dir'), |
| 125 mock.call('dir/.git'), |
| 126 ]) |
| 127 run_git.assert_has_calls([ |
| 128 mock.call('dir', 'config', 'remote.origin.url'), |
| 129 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'), |
| 130 ]) |
| 131 |
| 132 |
| 133 if __name__ == '__main__': |
| 134 unittest.main() |
OLD | NEW |