roll_deps_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import glob
  10. import os
  11. import shutil
  12. import sys
  13. import tempfile
  14. import unittest
  15. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  16. PARENT_DIR = os.path.join(SCRIPT_DIR, os.pardir)
  17. sys.path.append(PARENT_DIR)
  18. import roll_deps
  19. from roll_deps import CalculateChangedDeps, GetMatchingDepsEntries, \
  20. ParseDepsDict, ParseLocalDepsFile, UpdateDepsFile
  21. TEST_DATA_VARS = {
  22. 'chromium_git': 'https://chromium.googlesource.com',
  23. 'chromium_revision': '1b9c098a08e40114e44b6c1ec33ddf95c40b901d',
  24. }
  25. DEPS_ENTRIES = {
  26. 'src/build': 'https://build.com',
  27. 'src/buildtools': 'https://buildtools.com',
  28. 'src/testing/gtest': 'https://gtest.com',
  29. 'src/testing/gmock': 'https://gmock.com',
  30. }
  31. BUILD_OLD_REV = '52f7afeca991d96d68cf0507e20dbdd5b845691f'
  32. BUILD_NEW_REV = 'HEAD'
  33. BUILDTOOLS_OLD_REV = '64e38f0cebdde27aa0cfb405f330063582f9ac76'
  34. BUILDTOOLS_NEW_REV = '55ad626b08ef971fd82a62b7abb325359542952b'
  35. class TestError(Exception):
  36. pass
  37. class FakeCmd(object):
  38. def __init__(self):
  39. self.expectations = []
  40. def add_expectation(self, *args, **kwargs):
  41. returns = kwargs.pop('_returns', None)
  42. self.expectations.append((args, kwargs, returns))
  43. def __call__(self, *args, **kwargs):
  44. if not self.expectations:
  45. raise TestError('Got unexpected\n%s\n%s' % (args, kwargs))
  46. exp_args, exp_kwargs, exp_returns = self.expectations.pop(0)
  47. if args != exp_args or kwargs != exp_kwargs:
  48. message = 'Expected:\n args: %s\n kwargs: %s\n' % (exp_args, exp_kwargs)
  49. message += 'Got:\n args: %s\n kwargs: %s\n' % (args, kwargs)
  50. raise TestError(message)
  51. return exp_returns
  52. class TestRollChromiumRevision(unittest.TestCase):
  53. def setUp(self):
  54. self._output_dir = tempfile.mkdtemp()
  55. for test_file in glob.glob(os.path.join(SCRIPT_DIR, 'testdata', '*')):
  56. shutil.copy(test_file, self._output_dir)
  57. self._libyuv_depsfile = os.path.join(self._output_dir, 'DEPS')
  58. self._old_cr_depsfile = os.path.join(self._output_dir, 'DEPS.chromium.old')
  59. self._new_cr_depsfile = os.path.join(self._output_dir, 'DEPS.chromium.new')
  60. self.fake = FakeCmd()
  61. self.old_RunCommand = getattr(roll_deps, '_RunCommand')
  62. setattr(roll_deps, '_RunCommand', self.fake)
  63. def tearDown(self):
  64. shutil.rmtree(self._output_dir, ignore_errors=True)
  65. self.assertEqual(self.fake.expectations, [])
  66. setattr(roll_deps, '_RunCommand', self.old_RunCommand)
  67. def testVarLookup(self):
  68. local_scope = {'foo': 'wrong', 'vars': {'foo': 'bar'}}
  69. lookup = roll_deps.VarLookup(local_scope)
  70. self.assertEquals(lookup('foo'), 'bar')
  71. def testUpdateDepsFile(self):
  72. new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111'
  73. current_rev = TEST_DATA_VARS['chromium_revision']
  74. UpdateDepsFile(self._libyuv_depsfile, current_rev, new_rev, [])
  75. with open(self._libyuv_depsfile) as deps_file:
  76. deps_contents = deps_file.read()
  77. self.assertTrue(new_rev in deps_contents,
  78. 'Failed to find %s in\n%s' % (new_rev, deps_contents))
  79. def testParseDepsDict(self):
  80. with open(self._libyuv_depsfile) as deps_file:
  81. deps_contents = deps_file.read()
  82. local_scope = ParseDepsDict(deps_contents)
  83. vars_dict = local_scope['vars']
  84. def assertVar(variable_name):
  85. self.assertEquals(vars_dict[variable_name], TEST_DATA_VARS[variable_name])
  86. assertVar('chromium_git')
  87. assertVar('chromium_revision')
  88. self.assertEquals(len(local_scope['deps']), 3)
  89. def testGetMatchingDepsEntriesReturnsPathInSimpleCase(self):
  90. entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing/gtest')
  91. self.assertEquals(len(entries), 1)
  92. self.assertEquals(entries[0], DEPS_ENTRIES['src/testing/gtest'])
  93. def testGetMatchingDepsEntriesHandlesSimilarStartingPaths(self):
  94. entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/testing')
  95. self.assertEquals(len(entries), 2)
  96. def testGetMatchingDepsEntriesHandlesTwoPathsWithIdenticalFirstParts(self):
  97. entries = GetMatchingDepsEntries(DEPS_ENTRIES, 'src/build')
  98. self.assertEquals(len(entries), 1)
  99. self.assertEquals(entries[0], DEPS_ENTRIES['src/build'])
  100. def testCalculateChangedDeps(self):
  101. _SetupGitLsRemoteCall(self.fake,
  102. 'https://chromium.googlesource.com/chromium/src/build', BUILD_NEW_REV)
  103. libyuv_deps = ParseLocalDepsFile(self._libyuv_depsfile)
  104. new_cr_deps = ParseLocalDepsFile(self._new_cr_depsfile)
  105. changed_deps = CalculateChangedDeps(libyuv_deps, new_cr_deps)
  106. self.assertEquals(len(changed_deps), 2)
  107. self.assertEquals(changed_deps[0].path, 'src/build')
  108. self.assertEquals(changed_deps[0].current_rev, BUILD_OLD_REV)
  109. self.assertEquals(changed_deps[0].new_rev, BUILD_NEW_REV)
  110. self.assertEquals(changed_deps[1].path, 'src/buildtools')
  111. self.assertEquals(changed_deps[1].current_rev, BUILDTOOLS_OLD_REV)
  112. self.assertEquals(changed_deps[1].new_rev, BUILDTOOLS_NEW_REV)
  113. def _SetupGitLsRemoteCall(cmd_fake, url, revision):
  114. cmd = ['git', 'ls-remote', url, revision]
  115. cmd_fake.add_expectation(cmd, _returns=(revision, None))
  116. if __name__ == '__main__':
  117. unittest.main()