Debian Codegraph @

tools_libyuv/autoroller/unittests/roll_deps_test.py

raw
1 #!/usr/bin/env vpython3
2
3 # Copyright 2017 The LibYuv Project Authors. All rights reserved.
4 #
5 # Use of this source code is governed by a BSD-style license
6 # that can be found in the LICENSE file in the root of the source
7 # tree. An additional intellectual property rights grant can be found
8 # in the file PATENTS. All contributing project authors may
9 # be found in the AUTHORS file in the root of the source tree.
10
11 import glob
12 import os
13 import shutil
14 import sys
15 import tempfile
16 import unittest
17
18 import roll_deps
19 from roll_deps import CalculateChangedDeps, GetMatchingDepsEntries, ParseDepsDict, ParseLocalDepsFile, UpdateDepsFile # pylint: disable=line-too-long
20
21 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
22 PARENT_DIR = os.path.join(SCRIPT_DIR, os.pardir)
23 sys.path.append(PARENT_DIR)
24
25 TEST_DATA_VARS = {
26 'chromium_git': 'https://chromium.googlesource.com',
27 'chromium_revision': '1b9c098a08e40114e44b6c1ec33ddf95c40b901d',
28 }
29
30 DEPS_ENTRIES = {
31 'src/build': 'https://build.com',
32 'src/buildtools': 'https://buildtools.com',
33 'src/testing/gtest': 'https://gtest.com',
34 'src/testing/gmock': 'https://gmock.com',
35 }
36
37 BUILD_OLD_REV = '52f7afeca991d96d68cf0507e20dbdd5b845691f'
38 BUILD_NEW_REV = 'HEAD'
39 BUILDTOOLS_OLD_REV = '64e38f0cebdde27aa0cfb405f330063582f9ac76'
40 BUILDTOOLS_NEW_REV = '55ad626b08ef971fd82a62b7abb325359542952b'
41
42
43 class TestError(Exception):
44 pass
45
46
47 class FakeCmd:
48
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' % (
62 exp_args,
63 exp_kwargs,
64 )
65 message += 'Got:\n args: %s\n kwargs: %s\n' % (args, kwargs)
66 raise TestError(message)
67 return exp_returns
68
69
70 class TestRollChromiumRevision(unittest.TestCase):
71
72 def setUp(self):
73 self._output_dir = tempfile.mkdtemp()
74 for test_file in glob.glob(os.path.join(SCRIPT_DIR, 'testdata', '*')):
75 shutil.copy(test_file, self._output_dir)
76 self._libyuv_depsfile = os.path.join(self._output_dir, 'DEPS')
77 self._old_cr_depsfile = os.path.join(
78 self._output_dir, 'DEPS.chromium.old'
79 )
80 self._new_cr_depsfile = os.path.join(
81 self._output_dir, 'DEPS.chromium.new'
82 )
83
84 self.fake = FakeCmd()
85 self.old_RunCommand = getattr(roll_deps, '_RunCommand')
86 setattr(roll_deps, '_RunCommand', self.fake)
87
88 def tearDown(self):
89 shutil.rmtree(self._output_dir, ignore_errors=True)
90 self.assertEqual(self.fake.expectations, [])
91 setattr(roll_deps, '_RunCommand', self.old_RunCommand)
92
93 def testVarLookup(self):
94 local_scope = {'foo': 'wrong', 'vars': {'foo': 'bar'}}
95 lookup = roll_deps.VarLookup(local_scope)
96 self.assertEqual(lookup('foo'), 'bar')
97
98 def testUpdateDepsFile(self):
99 new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111'
100
101 current_rev = TEST_DATA_VARS['chromium_revision']
102 UpdateDepsFile(self._libyuv_depsfile, current_rev, new_rev, [])
103 with open(self._libyuv_depsfile, 'r') as deps_file:
104 deps_contents = deps_file.read()
105 self.assertTrue(
106 new_rev in deps_contents,
107 'Failed to find %s in\n%s' % (new_rev, deps_contents),
108 )
109
110 def testParseDepsDict(self):
111 with open(self._libyuv_depsfile, 'r') as deps_file:
112 deps_contents = deps_file.read()
113 local_scope = ParseDepsDict(deps_contents)
114 vars_dict = local_scope['vars']
115
116 def assertVar(variable_name):
117 self.assertEqual(
118 vars_dict[variable_name], TEST_DATA_VARS[variable_name]
119 )
120
121 assertVar('chromium_git')
122 assertVar('chromium_revision')
123 self.assertEqual(len(local_scope['deps']), 3)
124
125 def testGetMatchingDepsEntriesReturnsPathInSimpleCase(self):
126 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing/gtest')
127 self.assertEqual(len(entries), 1)
128 self.assertEqual(entries[0], DEPS_ENTRIES['src/testing/gtest'])
129
130 def testGetMatchingDepsEntriesHandlesSimilarStartingPaths(self):
131 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing')
132 self.assertEqual(len(entries), 2)
133
134 def testGetMatchingDepsEntriesHandlesTwoPathsWithIdenticalFirstParts(self):
135 entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/build')
136 self.assertEqual(len(entries), 1)
137 self.assertEqual(entries[0], DEPS_ENTRIES['src/build'])
138
139 def testCalculateChangedDeps(self):
140 _SetupGitLsRemoteCall(
141 self.fake,
142 'https://chromium.googlesource.com/chromium/src/build',
143 BUILD_NEW_REV,
144 )
145 libyuv_deps = ParseLocalDepsFile(self._libyuv_depsfile)
146 new_cr_deps = ParseLocalDepsFile(self._new_cr_depsfile)
147 changed_deps = CalculateChangedDeps(libyuv_deps, new_cr_deps)
148 self.assertEqual(len(changed_deps), 2)
149 self.assertEqual(changed_deps[0].path, 'src/build')
150 self.assertEqual(changed_deps[0].current_rev, BUILD_OLD_REV)
151 self.assertEqual(changed_deps[0].new_rev, BUILD_NEW_REV)
152
153 self.assertEqual(changed_deps[1].path, 'src/buildtools')
154 self.assertEqual(changed_deps[1].current_rev, BUILDTOOLS_OLD_REV)
155 self.assertEqual(changed_deps[1].new_rev, BUILDTOOLS_NEW_REV)
156
157
158 def _SetupGitLsRemoteCall(cmd_fake, url, revision):
159 cmd = ['git', 'ls-remote', url, revision]
160 cmd_fake.add_expectation(cmd, _returns=(revision, None))
161
162
163 if __name__ == '__main__':
164 unittest.main()