| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 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 import doctest | 6 import doctest |
| 7 import os | 7 import os |
| 8 import subprocess |
| 8 import sys | 9 import sys |
| 9 import unittest | 10 import unittest |
| 10 | 11 |
| 11 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 12 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 12 THIRD_PARTY = os.path.join(ROOT_DIR, 'recipe_engine', 'third_party') | 13 THIRD_PARTY = os.path.join(ROOT_DIR, 'recipe_engine', 'third_party') |
| 13 sys.path.insert(0, os.path.join(THIRD_PARTY, 'mock-1.0.1')) | 14 sys.path.insert(0, os.path.join(THIRD_PARTY, 'mock-1.0.1')) |
| 14 sys.path.insert(0, THIRD_PARTY) | 15 sys.path.insert(0, THIRD_PARTY) |
| 15 sys.path.insert(0, ROOT_DIR) | 16 sys.path.insert(0, ROOT_DIR) |
| 16 | 17 |
| 17 import mock | 18 import mock |
| 18 from recipe_engine import package | 19 from recipe_engine import package |
| 19 | 20 |
| 20 class MockIOThings(object): | 21 class MockIOThings(object): |
| 21 def setUp(self): | 22 def setUp(self): |
| 22 self.mock_os_patcher = mock.patch('recipe_engine.package.os') | 23 self.mock_os_patcher = mock.patch('recipe_engine.package.os') |
| 23 self.mock_os = self.mock_os_patcher.start() | 24 self.mock_os = self.mock_os_patcher.start() |
| 24 self.mock_os.path.join = os.path.join | 25 self.mock_os.path.join = os.path.join |
| 25 self.mock_os.path.dirname = os.path.dirname | 26 self.mock_os.path.dirname = os.path.dirname |
| 26 self.mock_os.sep = os.sep | 27 self.mock_os.sep = os.sep |
| 27 | 28 |
| 29 self.orig_subprocess = subprocess |
| 28 self.mock_subprocess_patcher = mock.patch( | 30 self.mock_subprocess_patcher = mock.patch( |
| 29 'recipe_engine.package.subprocess') | 31 'recipe_engine.package.subprocess') |
| 30 self.mock_subprocess = self.mock_subprocess_patcher.start() | 32 self.mock_subprocess = self.mock_subprocess_patcher.start() |
| 33 self.mock_subprocess.PIPE = self.orig_subprocess.PIPE |
| 31 | 34 |
| 32 def tearDown(self): | 35 def tearDown(self): |
| 33 self.mock_subprocess_patcher.stop() | 36 self.mock_subprocess_patcher.stop() |
| 34 self.mock_os_patcher.stop() | 37 self.mock_os_patcher.stop() |
| 35 | 38 |
| 36 | 39 |
| 37 class TestGitRepoSpec(MockIOThings, unittest.TestCase): | 40 class TestGitRepoSpec(MockIOThings, unittest.TestCase): |
| 38 REPO_URL = 'https://funny.recipes/repo.git' | 41 REPO_URL = 'https://funny.recipes/repo.git' |
| 39 def setUp(self): | 42 def setUp(self): |
| 40 super(TestGitRepoSpec, self).setUp() | 43 super(TestGitRepoSpec, self).setUp() |
| (...skipping 11 matching lines...) Expand all Loading... |
| 52 repo_root='repo/root', | 55 repo_root='repo/root', |
| 53 allow_fetch=False, | 56 allow_fetch=False, |
| 54 ) | 57 ) |
| 55 | 58 |
| 56 def test_checkout_nonexistant_package_dir(self): | 59 def test_checkout_nonexistant_package_dir(self): |
| 57 self.mock_os.path.exists.return_value = False | 60 self.mock_os.path.exists.return_value = False |
| 58 self.mock_os.path.isdir.return_value = False | 61 self.mock_os.path.isdir.return_value = False |
| 59 | 62 |
| 60 self.repo_spec.checkout(self.context) | 63 self.repo_spec.checkout(self.context) |
| 61 | 64 |
| 62 self.mock_subprocess.check_call.assert_any_call( | 65 self.mock_subprocess.check_output.assert_any_call( |
| 63 ['git', 'clone', self.REPO_URL, | 66 ['git', 'clone', self.REPO_URL, |
| 64 os.path.join(self.context.package_dir, 'funny_recipes')], | 67 os.path.join(self.context.package_dir, 'funny_recipes')]) |
| 65 cwd=None) | 68 self.mock_subprocess.check_output.assert_any_call( |
| 66 self.mock_subprocess.check_call.assert_any_call( | 69 ['git', |
| 67 ['git', 'reset', '-q', '--hard', 'deadbeef'], | 70 '--git-dir', 'repo/root/recipes/.recipe_deps/funny_recipes/.git', |
| 68 cwd='repo/root/recipes/.recipe_deps/funny_recipes') | 71 'reset', '-q', '--hard', 'deadbeef']) |
| 69 | 72 |
| 70 | 73 |
| 71 class MockProtoFile(package.ProtoFile): | 74 class MockProtoFile(package.ProtoFile): |
| 72 def __init__(self, path, text): | 75 def __init__(self, path, text): |
| 73 super(MockProtoFile, self).__init__(path) | 76 super(MockProtoFile, self).__init__(path) |
| 74 self._text = text | 77 self._text = text |
| 75 | 78 |
| 76 @property | 79 @property |
| 77 def path(self): | 80 def path(self): |
| 78 return self._path | 81 return self._path |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 self.assertEqual(foo_deps.repo_spec.path, '/path/to/local/foo') | 212 self.assertEqual(foo_deps.repo_spec.path, '/path/to/local/foo') |
| 210 | 213 |
| 211 | 214 |
| 212 def load_tests(_loader, tests, _ignore): | 215 def load_tests(_loader, tests, _ignore): |
| 213 tests.addTests(doctest.DocTestSuite(package)) | 216 tests.addTests(doctest.DocTestSuite(package)) |
| 214 return tests | 217 return tests |
| 215 | 218 |
| 216 | 219 |
| 217 if __name__ == '__main__': | 220 if __name__ == '__main__': |
| 218 result = unittest.main() | 221 result = unittest.main() |
| OLD | NEW |