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
10import os
11import shutil
12import sys
13import tempfile
14import unittest
15
16
17SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
18PARENT_DIR = os.path.join(SCRIPT_DIR, os.pardir)
19sys.path.append(PARENT_DIR)
20from roll_chromium_revision import ParseDepsDict, UpdateDeps, \
21    GetMatchingDepsEntries
22
23TEST_DATA_VARS = {
24  'extra_gyp_flag': '-Dextra_gyp_flag=0',
25  'chromium_git': 'https://chromium.googlesource.com',
26  'chromium_revision': '1b9c098a08e40114e44b6c1ec33ddf95c40b901d',
27}
28
29DEPS_ENTRIES = {
30  'src/build': 'https://build.com',
31  'src/buildtools': 'https://buildtools.com',
32  'src/testing/gtest': 'https://gtest.com',
33  'src/testing/gmock': 'https://gmock.com',
34}
35
36
37class TestRollChromiumRevision(unittest.TestCase):
38  def setUp(self):
39    self._output_dir = tempfile.mkdtemp()
40    shutil.copy(os.path.join(SCRIPT_DIR, 'DEPS'), self._output_dir)
41    self._deps_filename = os.path.join(self._output_dir, 'DEPS')
42
43  def tearDown(self):
44    shutil.rmtree(self._output_dir, ignore_errors=True)
45
46  def testUpdateDeps(self):
47    new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111'
48
49    current_rev = TEST_DATA_VARS['chromium_revision']
50    UpdateDeps(self._deps_filename, current_rev, new_rev)
51    with open(self._deps_filename) as deps_file:
52      deps_contents = deps_file.read()
53      self.assertTrue(new_rev in deps_contents,
54                      'Failed to find %s in\n%s' % (new_rev, deps_contents))
55
56  def testParseDepsDict(self):
57    with open(self._deps_filename) as deps_file:
58      deps_contents = deps_file.read()
59    local_scope = ParseDepsDict(deps_contents)
60    vars_dict = local_scope['vars']
61
62    def assertVar(variable_name):
63      self.assertEquals(vars_dict[variable_name], TEST_DATA_VARS[variable_name])
64    assertVar('extra_gyp_flag')
65    assertVar('chromium_git')
66    assertVar('chromium_revision')
67
68  def testGetMatchingDepsEntriesReturnsPathInSimpleCase(self):
69    entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing/gtest')
70    self.assertEquals(len(entries), 1)
71    self.assertEquals(entries[0], DEPS_ENTRIES['src/testing/gtest'])
72
73  def testGetMatchingDepsEntriesHandlesSimilarStartingPaths(self):
74    entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing')
75    self.assertEquals(len(entries), 2)
76
77  def testGetMatchingDepsEntriesHandlesTwoPathsWithIdenticalFirstParts(self):
78    entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/build')
79    self.assertEquals(len(entries), 1)
80    self.assertEquals(entries[0], DEPS_ENTRIES['src/build'])
81
82if __name__ == '__main__':
83  unittest.main()
84