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