| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
| 3 # | |
| 4 # Use of this source code is governed by a BSD-style license | |
| 5 # that can be found in the LICENSE file in the root of the source | |
| 6 # tree. An additional intellectual property rights grant can be found | |
| 7 # in the file PATENTS. All contributing project authors may | |
| 8 # be found in the AUTHORS file in the root of the source tree. | |
| 9 | |
| 10 import glob | |
| 11 import os | |
| 12 import shutil | |
| 13 import sys | |
| 14 import tempfile | |
| 15 import unittest | |
| 16 | |
| 17 | |
| 18 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| 19 PARENT_DIR = os.path.join(SCRIPT_DIR, os.pardir) | |
| 20 sys.path.append(PARENT_DIR) | |
| 21 import roll_deps | |
| 22 from roll_deps import CalculateChangedDeps, GetMatchingDepsEntries, \ | |
| 23 ParseDepsDict, ParseLocalDepsFile, UpdateDepsFile | |
| 24 | |
| 25 | |
| 26 TEST_DATA_VARS = { | |
| 27 'chromium_git': 'https://chromium.googlesource.com', | |
| 28 'chromium_revision': '1b9c098a08e40114e44b6c1ec33ddf95c40b901d', | |
| 29 } | |
| 30 | |
| 31 DEPS_ENTRIES = { | |
| 32 'src/build': 'https://build.com', | |
| 33 'src/buildtools': 'https://buildtools.com', | |
| 34 'src/testing/gtest': 'https://gtest.com', | |
| 35 'src/testing/gmock': 'https://gmock.com', | |
| 36 } | |
| 37 | |
| 38 BUILD_OLD_REV = '52f7afeca991d96d68cf0507e20dbdd5b845691f' | |
| 39 BUILD_NEW_REV = 'HEAD' | |
| 40 BUILDTOOLS_OLD_REV = '64e38f0cebdde27aa0cfb405f330063582f9ac76' | |
| 41 BUILDTOOLS_NEW_REV = '55ad626b08ef971fd82a62b7abb325359542952b' | |
| 42 | |
| 43 | |
| 44 class TestError(Exception): | |
| 45 pass | |
| 46 | |
| 47 | |
| 48 class FakeCmd(object): | |
| 49 def __init__(self): | |
| 50 self.expectations = [] | |
| 51 | |
| 52 def AddExpectation(self, *args, **kwargs): | |
| 53 returns = kwargs.pop('_returns', None) | |
| 54 self.expectations.append((args, kwargs, returns)) | |
| 55 | |
| 56 def __call__(self, *args, **kwargs): | |
| 57 if not self.expectations: | |
| 58 raise TestError('Got unexpected\n%s\n%s' % (args, kwargs)) | |
| 59 exp_args, exp_kwargs, exp_returns = self.expectations.pop(0) | |
| 60 if args != exp_args or kwargs != exp_kwargs: | |
| 61 message = 'Expected:\n args: %s\n kwargs: %s\n' % (exp_args, exp_kwargs) | |
| 62 message += 'Got:\n args: %s\n kwargs: %s\n' % (args, kwargs) | |
| 63 raise TestError(message) | |
| 64 return exp_returns | |
| 65 | |
| 66 | |
| 67 class TestRollChromiumRevision(unittest.TestCase): | |
| 68 def setUp(self): | |
| 69 self._output_dir = tempfile.mkdtemp() | |
| 70 for test_file in glob.glob(os.path.join(SCRIPT_DIR, 'testdata', '*')): | |
| 71 shutil.copy(test_file, self._output_dir) | |
| 72 self._webrtc_depsfile = os.path.join(self._output_dir, 'DEPS') | |
| 73 self._old_cr_depsfile = os.path.join(self._output_dir, 'DEPS.chromium.old') | |
| 74 self._new_cr_depsfile = os.path.join(self._output_dir, 'DEPS.chromium.new') | |
| 75 | |
| 76 self.fake = FakeCmd() | |
| 77 self.old_run_command = getattr(roll_deps, '_RunCommand') | |
| 78 setattr(roll_deps, '_RunCommand', self.fake) | |
| 79 | |
| 80 def tearDown(self): | |
| 81 shutil.rmtree(self._output_dir, ignore_errors=True) | |
| 82 self.assertEqual(self.fake.expectations, []) | |
| 83 setattr(roll_deps, '_RunCommand', self.old_run_command) | |
| 84 | |
| 85 def testUpdateDepsFile(self): | |
| 86 new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111' | |
| 87 | |
| 88 current_rev = TEST_DATA_VARS['chromium_revision'] | |
| 89 UpdateDepsFile(self._webrtc_depsfile, current_rev, new_rev, []) | |
| 90 with open(self._webrtc_depsfile) as deps_file: | |
| 91 deps_contents = deps_file.read() | |
| 92 self.assertTrue(new_rev in deps_contents, | |
| 93 'Failed to find %s in\n%s' % (new_rev, deps_contents)) | |
| 94 | |
| 95 def testParseDepsDict(self): | |
| 96 with open(self._webrtc_depsfile) as deps_file: | |
| 97 deps_contents = deps_file.read() | |
| 98 local_scope = ParseDepsDict(deps_contents) | |
| 99 vars_dict = local_scope['vars'] | |
| 100 | |
| 101 def AssertVar(variable_name): | |
| 102 self.assertEquals(vars_dict[variable_name], TEST_DATA_VARS[variable_name]) | |
| 103 AssertVar('chromium_git') | |
| 104 AssertVar('chromium_revision') | |
| 105 self.assertEquals(len(local_scope['deps']), 3) | |
| 106 self.assertEquals(len(local_scope['deps_os']), 1) | |
| 107 | |
| 108 def testGetMatchingDepsEntriesReturnsPathInSimpleCase(self): | |
| 109 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing/gtest') | |
| 110 self.assertEquals(len(entries), 1) | |
| 111 self.assertEquals(entries[0], DEPS_ENTRIES['src/testing/gtest']) | |
| 112 | |
| 113 def testGetMatchingDepsEntriesHandlesSimilarStartingPaths(self): | |
| 114 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing') | |
| 115 self.assertEquals(len(entries), 2) | |
| 116 | |
| 117 def testGetMatchingDepsEntriesHandlesTwoPathsWithIdenticalFirstParts(self): | |
| 118 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/build') | |
| 119 self.assertEquals(len(entries), 1) | |
| 120 self.assertEquals(entries[0], DEPS_ENTRIES['src/build']) | |
| 121 | |
| 122 def testCalculateChangedDeps(self): | |
| 123 _SetupGitLsRemoteCall(self.fake, | |
| 124 'https://chromium.googlesource.com/chromium/src/build', BUILD_NEW_REV) | |
| 125 webrtc_deps = ParseLocalDepsFile(self._webrtc_depsfile) | |
| 126 new_cr_deps = ParseLocalDepsFile(self._new_cr_depsfile) | |
| 127 changed_deps = CalculateChangedDeps(webrtc_deps, new_cr_deps) | |
| 128 self.assertEquals(len(changed_deps), 2) | |
| 129 self.assertEquals(changed_deps[0].path, 'src/build') | |
| 130 self.assertEquals(changed_deps[0].current_rev, BUILD_OLD_REV) | |
| 131 self.assertEquals(changed_deps[0].new_rev, BUILD_NEW_REV) | |
| 132 | |
| 133 self.assertEquals(changed_deps[1].path, 'src/buildtools') | |
| 134 self.assertEquals(changed_deps[1].current_rev, BUILDTOOLS_OLD_REV) | |
| 135 self.assertEquals(changed_deps[1].new_rev, BUILDTOOLS_NEW_REV) | |
| 136 | |
| 137 | |
| 138 def _SetupGitLsRemoteCall(cmd_fake, url, revision): | |
| 139 cmd = ['git', 'ls-remote', url, revision] | |
| 140 cmd_fake.AddExpectation(cmd, _returns=(revision, None)) | |
| 141 | |
| 142 | |
| 143 if __name__ == '__main__': | |
| 144 unittest.main() | |
| OLD | NEW |