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 |
| 22 self.mock_subprocess_patcher = mock.patch( |
| 23 'recipe_engine.package.subprocess') |
| 24 self.mock_subprocess = self.mock_subprocess_patcher.start() |
| 25 |
| 26 def tearDown(self): |
| 27 self.mock_subprocess_patcher.stop() |
| 28 self.mock_os_patcher.stop() |
| 29 |
| 30 |
| 31 class TestGitRepoSpec(MockIOThings, unittest.TestCase): |
| 32 REPO_URL = 'https://funny.recipes/repo.git' |
| 33 def setUp(self): |
| 34 super(TestGitRepoSpec, self).setUp() |
| 35 |
| 36 self.repo_spec = package.GitRepoSpec( |
| 37 'funny_recipes', |
| 38 self.REPO_URL, |
| 39 'master', |
| 40 'deadbeef', |
| 41 'path/to/recipes', |
| 42 ) |
| 43 self.context = package.PackageContext.from_pyl_path( |
| 44 os.path.join('path', 'to', 'package.pyl')) |
| 45 |
| 46 def test_checkout_nonexistant_package_dir(self): |
| 47 self.mock_os.path.exists.return_value = False |
| 48 self.mock_os.path.isdir.return_value = False |
| 49 |
| 50 self.repo_spec.checkout(self.context) |
| 51 |
| 52 self.mock_subprocess.check_call.assert_any_call( |
| 53 ['git', 'clone', self.REPO_URL, |
| 54 os.path.join(self.context.package_dir, 'funny_recipes')]) |
| 55 self.mock_subprocess.check_call.assert_any_call( |
| 56 ['git', 'checkout', '-q', 'deadbeef']) |
| 57 |
| 58 @mock.patch('recipe_engine.package.logging') |
| 59 def test_checkout_dont_mess_with_this(self, mock_logging): |
| 60 self.mock_os.path.exists.wraps = lambda path: ( |
| 61 path == os.path.join(self.context.package_dir, '.dont_mess_with_this')) |
| 62 self.repo_spec.checkout(self.context) |
| 63 self.mock_subprocess.check_call.assert_has_calls([]) |
| 64 self.assertRegexpMatches(mock_logging.warn.call_args[0][0], |
| 65 r'\.dont_mess_with_this') |
| 66 |
| 67 |
| 68 class TestPackageSpec(MockIOThings, unittest.TestCase): |
| 69 def setUp(self): |
| 70 super(TestPackageSpec, self).setUp() |
| 71 |
| 72 self.package_pyl = { |
| 73 'api_version': 0, |
| 74 'id': 'super_main_package', |
| 75 'deps': { |
| 76 'foo': { |
| 77 'repo': 'https://repo.com/foo.git', |
| 78 'branch': 'master', |
| 79 'revision': 'cafebeef', |
| 80 'path': 'foos/recipes', |
| 81 }, |
| 82 'bar': { |
| 83 'repo': 'https://repo.com/bar.git', |
| 84 'branch': 'superbar', |
| 85 'revision': 'deadd00d', |
| 86 'path': 'bars/recipes', |
| 87 }, |
| 88 }, |
| 89 } |
| 90 self.context = package.PackageContext.from_pyl_path( |
| 91 os.path.join('path', 'to', 'package.pyl')) |
| 92 |
| 93 def test_dump_load_inverses(self): |
| 94 # Doubles as a test for equality reflexivity. |
| 95 package_spec = package.PackageSpec.load(self.package_pyl) |
| 96 self.assertEqual(package_spec.dump(), self.package_pyl) |
| 97 self.assertEqual(package.PackageSpec.load(self.package_pyl), package_spec) |
| 98 |
| 99 def test_updates_merged(self): |
| 100 package_spec = package.PackageSpec.load(self.package_pyl) |
| 101 package_spec.deps['foo']._raw_updates = mock.Mock(return_value='\n'.join([ |
| 102 "2015-07-02T16:41:46+00:00 aaaaaa", |
| 103 "2015-07-05T13:38:26+00:00 bbbbbb", |
| 104 "2015-07-07T15:52:20-07:00 cccccc", |
| 105 ])) |
| 106 package_spec.deps['bar']._raw_updates = mock.Mock(return_value='\n'.join([ |
| 107 "2015-07-01T17:09:06+00:00 dddddd", |
| 108 "2015-07-03T10:12:30+00:00 eeeeee", |
| 109 "2015-07-06T21:16:36-07:00 ffffff", |
| 110 ])) |
| 111 updates = package_spec.updates(self.context) |
| 112 self.assertEqual( |
| 113 [ (spec.deps['foo'].revision, spec.deps['bar'].revision) |
| 114 for _date, spec in updates ], |
| 115 [ ('cafebeef', 'dddddd'), |
| 116 ('aaaaaa', 'dddddd'), |
| 117 ('aaaaaa', 'eeeeee'), |
| 118 ('bbbbbb', 'eeeeee'), |
| 119 ('bbbbbb', 'ffffff'), |
| 120 ('cccccc', 'ffffff') ]) |
| 121 |
| 122 def test_no_version(self): |
| 123 with self.assertRaises(Exception): |
| 124 package.PackageSpec.load({ |
| 125 'id': 'foo', |
| 126 'deps': {}, |
| 127 }) |
| 128 |
| 129 def test_unsupported_version(self): |
| 130 with self.assertRaises(Exception): |
| 131 package.PackageSpec.load({ |
| 132 'api_version': 1, |
| 133 'id': 'fizzbar', |
| 134 'deps': {}, |
| 135 }) |
| 136 |
| 137 def load_tests(loader, tests, ignore): |
| 138 tests.addTests(doctest.DocTestSuite(package)) |
| 139 return tests |
| 140 |
| 141 |
| 142 if __name__ == '__main__': |
| 143 result = unittest.main() |
OLD | NEW |