1#!/usr/bin/env python 2# Copyright 2017 The LibYuv 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 10import glob 11import os 12import shutil 13import sys 14import tempfile 15import unittest 16 17 18SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) 19PARENT_DIR = os.path.join(SCRIPT_DIR, os.pardir) 20sys.path.append(PARENT_DIR) 21import roll_deps 22from roll_deps import CalculateChangedDeps, GetMatchingDepsEntries, \ 23 ParseDepsDict, ParseLocalDepsFile, UpdateDepsFile 24 25 26TEST_DATA_VARS = { 27 'chromium_git': 'https://chromium.googlesource.com', 28 'chromium_revision': '1b9c098a08e40114e44b6c1ec33ddf95c40b901d', 29} 30 31DEPS_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 38BUILD_OLD_REV = '52f7afeca991d96d68cf0507e20dbdd5b845691f' 39BUILD_NEW_REV = 'HEAD' 40BUILDTOOLS_OLD_REV = '64e38f0cebdde27aa0cfb405f330063582f9ac76' 41BUILDTOOLS_NEW_REV = '55ad626b08ef971fd82a62b7abb325359542952b' 42 43 44class TestError(Exception): 45 pass 46 47 48class FakeCmd(object): 49 def __init__(self): 50 self.expectations = [] 51 52 def add_expectation(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 67class 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._libyuv_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_RunCommand = 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_RunCommand) 84 85 def testUpdateDepsFile(self): 86 new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111' 87 88 current_rev = TEST_DATA_VARS['chromium_revision'] 89 UpdateDepsFile(self._libyuv_depsfile, current_rev, new_rev, []) 90 with open(self._libyuv_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._libyuv_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 107 def testGetMatchingDepsEntriesReturnsPathInSimpleCase(self): 108 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing/gtest') 109 self.assertEquals(len(entries), 1) 110 self.assertEquals(entries[0], DEPS_ENTRIES['src/testing/gtest']) 111 112 def testGetMatchingDepsEntriesHandlesSimilarStartingPaths(self): 113 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing') 114 self.assertEquals(len(entries), 2) 115 116 def testGetMatchingDepsEntriesHandlesTwoPathsWithIdenticalFirstParts(self): 117 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/build') 118 self.assertEquals(len(entries), 1) 119 self.assertEquals(entries[0], DEPS_ENTRIES['src/build']) 120 121 def testCalculateChangedDeps(self): 122 _SetupGitLsRemoteCall(self.fake, 123 'https://chromium.googlesource.com/chromium/src/build', BUILD_NEW_REV) 124 libyuv_deps = ParseLocalDepsFile(self._libyuv_depsfile) 125 new_cr_deps = ParseLocalDepsFile(self._new_cr_depsfile) 126 changed_deps = CalculateChangedDeps(libyuv_deps, new_cr_deps) 127 self.assertEquals(len(changed_deps), 2) 128 self.assertEquals(changed_deps[0].path, 'src/build') 129 self.assertEquals(changed_deps[0].current_rev, BUILD_OLD_REV) 130 self.assertEquals(changed_deps[0].new_rev, BUILD_NEW_REV) 131 132 self.assertEquals(changed_deps[1].path, 'src/buildtools') 133 self.assertEquals(changed_deps[1].current_rev, BUILDTOOLS_OLD_REV) 134 self.assertEquals(changed_deps[1].new_rev, BUILDTOOLS_NEW_REV) 135 136 137def _SetupGitLsRemoteCall(cmd_fake, url, revision): 138 cmd = ['git', 'ls-remote', url, revision] 139 cmd_fake.add_expectation(cmd, _returns=(revision, None)) 140 141 142if __name__ == '__main__': 143 unittest.main() 144