You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
9.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #!/usr/bin/env python
  2. # Copyright 2017 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. import os
  10. import shutil
  11. import tempfile
  12. import textwrap
  13. import unittest
  14. import PRESUBMIT
  15. # pylint: disable=line-too-long
  16. from presubmit_test_mocks import MockInputApi, MockOutputApi, MockFile, MockChange
  17. class CheckBugEntryFieldTest(unittest.TestCase):
  18. def testCommitMessageBugEntryWithNoError(self):
  19. mock_input_api = MockInputApi()
  20. mock_output_api = MockOutputApi()
  21. mock_input_api.change = MockChange([], ['webrtc:1234'])
  22. errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
  23. mock_output_api)
  24. self.assertEqual(0, len(errors))
  25. def testCommitMessageBugEntryReturnError(self):
  26. mock_input_api = MockInputApi()
  27. mock_output_api = MockOutputApi()
  28. mock_input_api.change = MockChange([], ['webrtc:1234', 'webrtc=4321'])
  29. errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
  30. mock_output_api)
  31. self.assertEqual(1, len(errors))
  32. self.assertEqual(('Bogus Bug entry: webrtc=4321. Please specify'
  33. ' the issue tracker prefix and the issue number,'
  34. ' separated by a colon, e.g. webrtc:123 or'
  35. ' chromium:12345.'), str(errors[0]))
  36. def testCommitMessageBugEntryWithoutPrefix(self):
  37. mock_input_api = MockInputApi()
  38. mock_output_api = MockOutputApi()
  39. mock_input_api.change = MockChange([], ['1234'])
  40. errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
  41. mock_output_api)
  42. self.assertEqual(1, len(errors))
  43. self.assertEqual(('Bug entry requires issue tracker prefix, '
  44. 'e.g. webrtc:1234'), str(errors[0]))
  45. def testCommitMessageBugEntryIsNone(self):
  46. mock_input_api = MockInputApi()
  47. mock_output_api = MockOutputApi()
  48. mock_input_api.change = MockChange([], ['None'])
  49. errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
  50. mock_output_api)
  51. self.assertEqual(0, len(errors))
  52. def testCommitMessageBugEntrySupportInternalBugReference(self):
  53. mock_input_api = MockInputApi()
  54. mock_output_api = MockOutputApi()
  55. mock_input_api.change.BUG = 'b/12345'
  56. errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
  57. mock_output_api)
  58. self.assertEqual(0, len(errors))
  59. mock_input_api.change.BUG = 'b/12345, webrtc:1234'
  60. errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
  61. mock_output_api)
  62. self.assertEqual(0, len(errors))
  63. class CheckNewlineAtTheEndOfProtoFilesTest(unittest.TestCase):
  64. def setUp(self):
  65. self.tmp_dir = tempfile.mkdtemp()
  66. self.proto_file_path = os.path.join(self.tmp_dir, 'foo.proto')
  67. self.input_api = MockInputApi()
  68. self.output_api = MockOutputApi()
  69. def tearDown(self):
  70. shutil.rmtree(self.tmp_dir, ignore_errors=True)
  71. def testErrorIfProtoFileDoesNotEndWithNewline(self):
  72. self._GenerateProtoWithoutNewlineAtTheEnd()
  73. self.input_api.files = [MockFile(self.proto_file_path)]
  74. errors = PRESUBMIT.CheckNewlineAtTheEndOfProtoFiles(self.input_api,
  75. self.output_api,
  76. lambda x: True)
  77. self.assertEqual(1, len(errors))
  78. self.assertEqual(
  79. 'File %s must end with exactly one newline.' % self.proto_file_path,
  80. str(errors[0]))
  81. def testNoErrorIfProtoFileEndsWithNewline(self):
  82. self._GenerateProtoWithNewlineAtTheEnd()
  83. self.input_api.files = [MockFile(self.proto_file_path)]
  84. errors = PRESUBMIT.CheckNewlineAtTheEndOfProtoFiles(self.input_api,
  85. self.output_api,
  86. lambda x: True)
  87. self.assertEqual(0, len(errors))
  88. def _GenerateProtoWithNewlineAtTheEnd(self):
  89. with open(self.proto_file_path, 'w') as f:
  90. f.write(textwrap.dedent("""
  91. syntax = "proto2";
  92. option optimize_for = LITE_RUNTIME;
  93. package webrtc.audioproc;
  94. """))
  95. def _GenerateProtoWithoutNewlineAtTheEnd(self):
  96. with open(self.proto_file_path, 'w') as f:
  97. f.write(textwrap.dedent("""
  98. syntax = "proto2";
  99. option optimize_for = LITE_RUNTIME;
  100. package webrtc.audioproc;"""))
  101. class CheckNoMixingSourcesTest(unittest.TestCase):
  102. def setUp(self):
  103. self.tmp_dir = tempfile.mkdtemp()
  104. self.file_path = os.path.join(self.tmp_dir, 'BUILD.gn')
  105. self.input_api = MockInputApi()
  106. self.output_api = MockOutputApi()
  107. def tearDown(self):
  108. shutil.rmtree(self.tmp_dir, ignore_errors=True)
  109. def testErrorIfCAndCppAreMixed(self):
  110. self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.cc', 'bar.h'])
  111. def testErrorIfCAndObjCAreMixed(self):
  112. self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.m', 'bar.h'])
  113. def testErrorIfCAndObjCppAreMixed(self):
  114. self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.mm', 'bar.h'])
  115. def testErrorIfCppAndObjCAreMixed(self):
  116. self._AssertNumberOfErrorsWithSources(1, ['foo.cc', 'bar.m', 'bar.h'])
  117. def testErrorIfCppAndObjCppAreMixed(self):
  118. self._AssertNumberOfErrorsWithSources(1, ['foo.cc', 'bar.mm', 'bar.h'])
  119. def testNoErrorIfOnlyC(self):
  120. self._AssertNumberOfErrorsWithSources(0, ['foo.c', 'bar.c', 'bar.h'])
  121. def testNoErrorIfOnlyCpp(self):
  122. self._AssertNumberOfErrorsWithSources(0, ['foo.cc', 'bar.cc', 'bar.h'])
  123. def testNoErrorIfOnlyObjC(self):
  124. self._AssertNumberOfErrorsWithSources(0, ['foo.m', 'bar.m', 'bar.h'])
  125. def testNoErrorIfOnlyObjCpp(self):
  126. self._AssertNumberOfErrorsWithSources(0, ['foo.mm', 'bar.mm', 'bar.h'])
  127. def testNoErrorIfObjCAndObjCppAreMixed(self):
  128. self._AssertNumberOfErrorsWithSources(0, ['foo.m', 'bar.mm', 'bar.h'])
  129. def testNoErrorIfSourcesAreInExclusiveIfBranches(self):
  130. self._GenerateBuildFile(textwrap.dedent("""
  131. rtc_library("bar_foo") {
  132. if (is_win) {
  133. sources = [
  134. "bar.cc",
  135. ],
  136. }
  137. if (is_ios) {
  138. sources = [
  139. "bar.mm",
  140. ],
  141. }
  142. }
  143. rtc_library("foo_bar") {
  144. if (is_win) {
  145. sources = [
  146. "foo.cc",
  147. ],
  148. }
  149. if (is_ios) {
  150. sources = [
  151. "foo.mm",
  152. ],
  153. }
  154. }
  155. """))
  156. self.input_api.files = [MockFile(self.file_path)]
  157. errors = PRESUBMIT.CheckNoMixingSources(self.input_api,
  158. [MockFile(self.file_path)],
  159. self.output_api)
  160. self.assertEqual(0, len(errors))
  161. def testErrorIfSourcesAreNotInExclusiveIfBranches(self):
  162. self._GenerateBuildFile(textwrap.dedent("""
  163. rtc_library("bar_foo") {
  164. if (is_win) {
  165. sources = [
  166. "bar.cc",
  167. ],
  168. }
  169. if (foo_bar) {
  170. sources += [
  171. "bar.mm",
  172. ],
  173. }
  174. }
  175. rtc_library("foo_bar") {
  176. if (is_win) {
  177. sources = [
  178. "foo.cc",
  179. ],
  180. }
  181. if (foo_bar) {
  182. sources += [
  183. "foo.mm",
  184. ],
  185. }
  186. if (is_ios) {
  187. sources = [
  188. "bar.m",
  189. "bar.c",
  190. ],
  191. }
  192. }
  193. """))
  194. self.input_api.files = [MockFile(self.file_path)]
  195. errors = PRESUBMIT.CheckNoMixingSources(self.input_api,
  196. [MockFile(self.file_path)],
  197. self.output_api)
  198. self.assertEqual(1, len(errors))
  199. self.assertTrue('bar.cc' in str(errors[0]))
  200. self.assertTrue('bar.mm' in str(errors[0]))
  201. self.assertTrue('foo.cc' in str(errors[0]))
  202. self.assertTrue('foo.mm' in str(errors[0]))
  203. self.assertTrue('bar.m' in str(errors[0]))
  204. self.assertTrue('bar.c' in str(errors[0]))
  205. def _AssertNumberOfErrorsWithSources(self, number_of_errors, sources):
  206. assert len(sources) == 3, 'This function accepts a list of 3 source files'
  207. self._GenerateBuildFile(textwrap.dedent("""
  208. rtc_static_library("bar_foo") {
  209. sources = [
  210. "%s",
  211. "%s",
  212. "%s",
  213. ],
  214. }
  215. rtc_library("foo_bar") {
  216. sources = [
  217. "%s",
  218. "%s",
  219. "%s",
  220. ],
  221. }
  222. """ % (tuple(sources) * 2)))
  223. self.input_api.files = [MockFile(self.file_path)]
  224. errors = PRESUBMIT.CheckNoMixingSources(self.input_api,
  225. [MockFile(self.file_path)],
  226. self.output_api)
  227. self.assertEqual(number_of_errors, len(errors))
  228. if number_of_errors == 1:
  229. for source in sources:
  230. if not source.endswith('.h'):
  231. self.assertTrue(source in str(errors[0]))
  232. def _GenerateBuildFile(self, content):
  233. with open(self.file_path, 'w') as f:
  234. f.write(content)
  235. if __name__ == '__main__':
  236. unittest.main()