• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 testVarLookup(self):
86    local_scope = {'foo': 'wrong', 'vars': {'foo': 'bar'}}
87    lookup = roll_deps.VarLookup(local_scope)
88    self.assertEquals(lookup('foo'), 'bar')
89
90  def testUpdateDepsFile(self):
91    new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111'
92
93    current_rev = TEST_DATA_VARS['chromium_revision']
94    UpdateDepsFile(self._libyuv_depsfile, current_rev, new_rev, [])
95    with open(self._libyuv_depsfile) as deps_file:
96      deps_contents = deps_file.read()
97      self.assertTrue(new_rev in deps_contents,
98                      'Failed to find %s in\n%s' % (new_rev, deps_contents))
99
100  def testParseDepsDict(self):
101    with open(self._libyuv_depsfile) as deps_file:
102      deps_contents = deps_file.read()
103    local_scope = ParseDepsDict(deps_contents)
104    vars_dict = local_scope['vars']
105
106    def assertVar(variable_name):
107      self.assertEquals(vars_dict[variable_name], TEST_DATA_VARS[variable_name])
108    assertVar('chromium_git')
109    assertVar('chromium_revision')
110    self.assertEquals(len(local_scope['deps']), 3)
111
112  def testGetMatchingDepsEntriesReturnsPathInSimpleCase(self):
113    entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing/gtest')
114    self.assertEquals(len(entries), 1)
115    self.assertEquals(entries[0], DEPS_ENTRIES['src/testing/gtest'])
116
117  def testGetMatchingDepsEntriesHandlesSimilarStartingPaths(self):
118    entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing')
119    self.assertEquals(len(entries), 2)
120
121  def testGetMatchingDepsEntriesHandlesTwoPathsWithIdenticalFirstParts(self):
122    entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/build')
123    self.assertEquals(len(entries), 1)
124    self.assertEquals(entries[0], DEPS_ENTRIES['src/build'])
125
126  def testCalculateChangedDeps(self):
127    _SetupGitLsRemoteCall(self.fake,
128        'https://chromium.googlesource.com/chromium/src/build', BUILD_NEW_REV)
129    libyuv_deps = ParseLocalDepsFile(self._libyuv_depsfile)
130    new_cr_deps = ParseLocalDepsFile(self._new_cr_depsfile)
131    changed_deps = CalculateChangedDeps(libyuv_deps, new_cr_deps)
132    self.assertEquals(len(changed_deps), 2)
133    self.assertEquals(changed_deps[0].path, 'src/build')
134    self.assertEquals(changed_deps[0].current_rev, BUILD_OLD_REV)
135    self.assertEquals(changed_deps[0].new_rev, BUILD_NEW_REV)
136
137    self.assertEquals(changed_deps[1].path, 'src/buildtools')
138    self.assertEquals(changed_deps[1].current_rev, BUILDTOOLS_OLD_REV)
139    self.assertEquals(changed_deps[1].new_rev, BUILDTOOLS_NEW_REV)
140
141
142def _SetupGitLsRemoteCall(cmd_fake, url, revision):
143  cmd = ['git', 'ls-remote', url, revision]
144  cmd_fake.add_expectation(cmd, _returns=(revision, None))
145
146
147if __name__ == '__main__':
148  unittest.main()
149