OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import doctest |
| 4 import os |
| 5 import sys |
| 6 import unittest |
| 7 |
| 8 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname( |
| 9 os.path.abspath(__file__)))) |
| 10 THIRD_PARTY = os.path.join(ROOT_DIR, 'recipe_engine', 'third_party') |
| 11 sys.path.insert(0, os.path.join(THIRD_PARTY, 'mock-1.0.1')) |
| 12 sys.path.insert(0, THIRD_PARTY) |
| 13 sys.path.insert(0, ROOT_DIR) |
| 14 |
| 15 import mock |
| 16 from recipe_engine import package |
| 17 |
| 18 class MockIOThings(object): |
| 19 def setUp(self): |
| 20 self.mock_os_patcher = mock.patch('recipe_engine.package.os') |
| 21 self.mock_os = self.mock_os_patcher.start() |
| 22 self.mock_os.path.join = os.path.join |
| 23 self.mock_os.path.dirname = os.path.dirname |
| 24 self.mock_os.sep = os.sep |
| 25 |
| 26 self.mock_subprocess_patcher = mock.patch( |
| 27 'recipe_engine.package.subprocess') |
| 28 self.mock_subprocess = self.mock_subprocess_patcher.start() |
| 29 |
| 30 def tearDown(self): |
| 31 self.mock_subprocess_patcher.stop() |
| 32 self.mock_os_patcher.stop() |
| 33 |
| 34 |
| 35 class TestGitRepoSpec(MockIOThings, unittest.TestCase): |
| 36 REPO_URL = 'https://funny.recipes/repo.git' |
| 37 def setUp(self): |
| 38 super(TestGitRepoSpec, self).setUp() |
| 39 |
| 40 self.repo_spec = package.GitRepoSpec( |
| 41 'funny_recipes', |
| 42 self.REPO_URL, |
| 43 'master', |
| 44 'deadbeef', |
| 45 'path/to/recipes', |
| 46 ) |
| 47 self.context = package.PackageContext( |
| 48 recipes_dir='repo/root/recipes', |
| 49 package_dir='repo/root/recipes/.recipe_deps', |
| 50 repo_root='repo/root', |
| 51 ) |
| 52 |
| 53 def test_checkout_nonexistant_package_dir(self): |
| 54 self.mock_os.path.exists.return_value = False |
| 55 self.mock_os.path.isdir.return_value = False |
| 56 |
| 57 self.repo_spec.checkout(self.context) |
| 58 |
| 59 self.mock_subprocess.check_call.assert_any_call( |
| 60 ['git', 'clone', self.REPO_URL, |
| 61 os.path.join(self.context.package_dir, 'funny_recipes')], |
| 62 cwd=None) |
| 63 self.mock_subprocess.check_call.assert_any_call( |
| 64 ['git', 'reset', '-q', '--hard', 'deadbeef'], |
| 65 cwd='repo/root/recipes/.recipe_deps/funny_recipes') |
| 66 |
| 67 |
| 68 class MockProtoFile(package.ProtoFile): |
| 69 def __init__(self, path, text): |
| 70 super(MockProtoFile, self).__init__(path) |
| 71 self._text = text |
| 72 |
| 73 @property |
| 74 def path(self): |
| 75 return self._path |
| 76 |
| 77 def read_text(self): |
| 78 return self._text |
| 79 |
| 80 def write(self, buf): |
| 81 pass |
| 82 |
| 83 |
| 84 class TestPackageSpec(MockIOThings, unittest.TestCase): |
| 85 def setUp(self): |
| 86 super(TestPackageSpec, self).setUp() |
| 87 |
| 88 self.proto_text = """ |
| 89 api_version: 1 |
| 90 project_id: "super_main_package" |
| 91 recipes_path: "path/to/recipes" |
| 92 deps { |
| 93 project_id: "bar" |
| 94 url: "https://repo.com/bar.git" |
| 95 branch: "superbar" |
| 96 revision: "deadd00d" |
| 97 } |
| 98 deps { |
| 99 project_id: "foo" |
| 100 url: "https://repo.com/foo.git" |
| 101 branch: "master" |
| 102 revision: "cafebeef" |
| 103 } |
| 104 """.lstrip() |
| 105 self.proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', |
| 106 self.proto_text) |
| 107 self.context = package.PackageContext.from_proto_file( |
| 108 'repo/root', self.proto_file) |
| 109 |
| 110 # def test_dump_load_inverses(self): |
| 111 # Doubles as a test for equality reflexivity. |
| 112 package_spec = package.PackageSpec.load_proto(self.proto_file) |
| 113 self.assertEqual(self.proto_file.to_text(package_spec.dump()), |
| 114 self.proto_text) |
| 115 self.assertEqual(package.PackageSpec.load_proto(self.proto_file), |
| 116 package_spec) |
| 117 |
| 118 def test_updates_merged(self): |
| 119 """Tests that updates are monotone in each dependency's history and |
| 120 that dep rolls stay in their proper dependency.""" |
| 121 |
| 122 package_spec = package.PackageSpec.load_proto(self.proto_file) |
| 123 foo_revs = [ "aaaaaa", "123456", "cdabfe" ] |
| 124 bar_revs = [ "0156ff", "ffaaff", "aa0000" ] |
| 125 package_spec.deps['bar']._raw_updates = mock.Mock( |
| 126 return_value='\n'.join(bar_revs)) |
| 127 package_spec.deps['foo']._raw_updates = mock.Mock( |
| 128 return_value='\n'.join(foo_revs)) |
| 129 |
| 130 updates = package_spec.updates(self.context) |
| 131 foo_update_ixs = [ |
| 132 (['cafebeef'] + foo_revs).index(update.spec.deps['foo'].revision) |
| 133 for update in updates ] |
| 134 bar_update_ixs = [ |
| 135 (['deadd00d'] + bar_revs).index(update.spec.deps['bar'].revision) |
| 136 for update in updates ] |
| 137 self.assertEqual(len(updates), 6) |
| 138 self.assertEqual(foo_update_ixs, sorted(foo_update_ixs)) |
| 139 self.assertEqual(bar_update_ixs, sorted(bar_update_ixs)) |
| 140 |
| 141 def test_no_version(self): |
| 142 with self.assertRaises(Exception): |
| 143 package.PackageSpec.load({ |
| 144 'id': 'foo', |
| 145 'deps': {}, |
| 146 }) |
| 147 |
| 148 def test_unsupported_version(self): |
| 149 with self.assertRaises(Exception): |
| 150 package.PackageSpec.load({ |
| 151 'api_version': 1, |
| 152 'id': 'fizzbar', |
| 153 'deps': {}, |
| 154 }) |
| 155 |
| 156 def load_tests(loader, tests, ignore): |
| 157 tests.addTests(doctest.DocTestSuite(package)) |
| 158 return tests |
| 159 |
| 160 |
| 161 if __name__ == '__main__': |
| 162 result = unittest.main() |
OLD | NEW |