blob: d6e512954a0578b942bb8ffa6c407191de428a9b [file] [log] [blame]
Henrik Kjellandera18a3bf2017-09-15 11:19:10 +02001#!/usr/bin/env python
2
3# Copyright 2017 The WebRTC 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
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020011import os
12import shutil
13import tempfile
14import textwrap
charujain9893e252017-09-14 13:33:22 +020015import unittest
16
17import PRESUBMIT
Mirko Bonadei61880182017-10-12 15:12:35 +020018# pylint: disable=line-too-long
19from presubmit_test_mocks import MockInputApi, MockOutputApi, MockFile, MockChange
charujain9893e252017-09-14 13:33:22 +020020
21
Mirko Bonadei7de1eb72017-09-19 10:16:10 +020022class CheckBugEntryFieldTest(unittest.TestCase):
charujain9893e252017-09-14 13:33:22 +020023 def testCommitMessageBugEntryWithNoError(self):
24 mock_input_api = MockInputApi()
25 mock_output_api = MockOutputApi()
Mirko Bonadei61880182017-10-12 15:12:35 +020026 mock_input_api.change = MockChange([], ['webrtc:1234'])
charujain9893e252017-09-14 13:33:22 +020027 errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
Edward Lemur6d01f6d2017-09-14 17:02:01 +020028 mock_output_api)
charujain9893e252017-09-14 13:33:22 +020029 self.assertEqual(0, len(errors))
30
31 def testCommitMessageBugEntryReturnError(self):
32 mock_input_api = MockInputApi()
33 mock_output_api = MockOutputApi()
Mirko Bonadei61880182017-10-12 15:12:35 +020034 mock_input_api.change = MockChange([], ['webrtc:1234', 'webrtc=4321'])
charujain9893e252017-09-14 13:33:22 +020035 errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
Edward Lemur6d01f6d2017-09-14 17:02:01 +020036 mock_output_api)
charujain9893e252017-09-14 13:33:22 +020037 self.assertEqual(1, len(errors))
Mirko Bonadei61880182017-10-12 15:12:35 +020038 self.assertEqual(('Bogus Bug entry: webrtc=4321. Please specify'
charujain9893e252017-09-14 13:33:22 +020039 ' the issue tracker prefix and the issue number,'
40 ' separated by a colon, e.g. webrtc:123 or'
41 ' chromium:12345.'), str(errors[0]))
42
Mirko Bonadei61880182017-10-12 15:12:35 +020043 def testCommitMessageBugEntryWithoutPrefix(self):
44 mock_input_api = MockInputApi()
45 mock_output_api = MockOutputApi()
46 mock_input_api.change = MockChange([], ['1234'])
47 errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
48 mock_output_api)
49 self.assertEqual(1, len(errors))
50 self.assertEqual(('Bug entry requires issue tracker prefix, '
51 'e.g. webrtc:1234'), str(errors[0]))
52
charujain9893e252017-09-14 13:33:22 +020053 def testCommitMessageBugEntryIsNone(self):
54 mock_input_api = MockInputApi()
55 mock_output_api = MockOutputApi()
Mirko Bonadei61880182017-10-12 15:12:35 +020056 mock_input_api.change = MockChange([], ['None'])
charujain9893e252017-09-14 13:33:22 +020057 errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
Edward Lemur6d01f6d2017-09-14 17:02:01 +020058 mock_output_api)
charujain9893e252017-09-14 13:33:22 +020059 self.assertEqual(0, len(errors))
60
charujain81a58c72017-09-25 13:25:45 +020061 def testCommitMessageBugEntrySupportInternalBugReference(self):
62 mock_input_api = MockInputApi()
63 mock_output_api = MockOutputApi()
64 mock_input_api.change.BUG = 'b/12345'
65 errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
66 mock_output_api)
67 self.assertEqual(0, len(errors))
68 mock_input_api.change.BUG = 'b/12345, webrtc:1234'
69 errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api,
70 mock_output_api)
71 self.assertEqual(0, len(errors))
72
charujain9893e252017-09-14 13:33:22 +020073
Mirko Bonadei7de1eb72017-09-19 10:16:10 +020074class CheckNewlineAtTheEndOfProtoFilesTest(unittest.TestCase):
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020075
76 def setUp(self):
77 self.tmp_dir = tempfile.mkdtemp()
78 self.proto_file_path = os.path.join(self.tmp_dir, 'foo.proto')
79 self.input_api = MockInputApi()
80 self.output_api = MockOutputApi()
81
82 def tearDown(self):
83 shutil.rmtree(self.tmp_dir, ignore_errors=True)
84
85 def testErrorIfProtoFileDoesNotEndWithNewline(self):
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020086 self._GenerateProtoWithoutNewlineAtTheEnd()
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020087 self.input_api.files = [MockFile(self.proto_file_path)]
88 errors = PRESUBMIT.CheckNewlineAtTheEndOfProtoFiles(self.input_api,
Artem Titov739351d2018-05-11 12:21:36 +020089 self.output_api,
90 lambda x: True)
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020091 self.assertEqual(1, len(errors))
92 self.assertEqual(
93 'File %s must end with exactly one newline.' % self.proto_file_path,
94 str(errors[0]))
95
96 def testNoErrorIfProtoFileEndsWithNewline(self):
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020097 self._GenerateProtoWithNewlineAtTheEnd()
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020098 self.input_api.files = [MockFile(self.proto_file_path)]
99 errors = PRESUBMIT.CheckNewlineAtTheEndOfProtoFiles(self.input_api,
Artem Titov739351d2018-05-11 12:21:36 +0200100 self.output_api,
101 lambda x: True)
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200102 self.assertEqual(0, len(errors))
103
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200104 def _GenerateProtoWithNewlineAtTheEnd(self):
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200105 with open(self.proto_file_path, 'w') as f:
106 f.write(textwrap.dedent("""
107 syntax = "proto2";
108 option optimize_for = LITE_RUNTIME;
109 package webrtc.audioproc;
110 """))
111
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200112 def _GenerateProtoWithoutNewlineAtTheEnd(self):
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200113 with open(self.proto_file_path, 'w') as f:
114 f.write(textwrap.dedent("""
115 syntax = "proto2";
116 option optimize_for = LITE_RUNTIME;
117 package webrtc.audioproc;"""))
118
119
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200120class CheckNoMixingSourcesTest(unittest.TestCase):
121
122 def setUp(self):
123 self.tmp_dir = tempfile.mkdtemp()
124 self.file_path = os.path.join(self.tmp_dir, 'BUILD.gn')
125 self.input_api = MockInputApi()
126 self.output_api = MockOutputApi()
127
128 def tearDown(self):
129 shutil.rmtree(self.tmp_dir, ignore_errors=True)
130
131 def testErrorIfCAndCppAreMixed(self):
132 self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.cc', 'bar.h'])
133
134 def testErrorIfCAndObjCAreMixed(self):
135 self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.m', 'bar.h'])
136
137 def testErrorIfCAndObjCppAreMixed(self):
138 self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.mm', 'bar.h'])
139
140 def testErrorIfCppAndObjCAreMixed(self):
141 self._AssertNumberOfErrorsWithSources(1, ['foo.cc', 'bar.m', 'bar.h'])
142
143 def testErrorIfCppAndObjCppAreMixed(self):
144 self._AssertNumberOfErrorsWithSources(1, ['foo.cc', 'bar.mm', 'bar.h'])
145
146 def testNoErrorIfOnlyC(self):
147 self._AssertNumberOfErrorsWithSources(0, ['foo.c', 'bar.c', 'bar.h'])
148
149 def testNoErrorIfOnlyCpp(self):
150 self._AssertNumberOfErrorsWithSources(0, ['foo.cc', 'bar.cc', 'bar.h'])
151
152 def testNoErrorIfOnlyObjC(self):
153 self._AssertNumberOfErrorsWithSources(0, ['foo.m', 'bar.m', 'bar.h'])
154
155 def testNoErrorIfOnlyObjCpp(self):
156 self._AssertNumberOfErrorsWithSources(0, ['foo.mm', 'bar.mm', 'bar.h'])
157
158 def testNoErrorIfObjCAndObjCppAreMixed(self):
159 self._AssertNumberOfErrorsWithSources(0, ['foo.m', 'bar.mm', 'bar.h'])
160
161 def testNoErrorIfSourcesAreInExclusiveIfBranches(self):
162 self._GenerateBuildFile(textwrap.dedent("""
163 rtc_source_set("bar_foo") {
164 if (is_win) {
165 sources = [
166 "bar.cc",
167 ],
168 }
169 if (is_ios) {
170 sources = [
171 "bar.mm",
172 ],
173 }
174 }
175 rtc_source_set("foo_bar") {
176 if (is_win) {
177 sources = [
178 "foo.cc",
179 ],
180 }
181 if (is_ios) {
182 sources = [
183 "foo.mm",
184 ],
185 }
186 }
187 """))
188 self.input_api.files = [MockFile(self.file_path)]
189 errors = PRESUBMIT.CheckNoMixingSources(self.input_api,
190 [MockFile(self.file_path)],
191 self.output_api)
192 self.assertEqual(0, len(errors))
193
194 def testErrorIfSourcesAreNotInExclusiveIfBranches(self):
195 self._GenerateBuildFile(textwrap.dedent("""
196 rtc_source_set("bar_foo") {
197 if (is_win) {
198 sources = [
199 "bar.cc",
200 ],
201 }
202 if (foo_bar) {
203 sources += [
204 "bar.mm",
205 ],
206 }
207 }
208 rtc_source_set("foo_bar") {
209 if (is_win) {
210 sources = [
211 "foo.cc",
212 ],
213 }
214 if (foo_bar) {
215 sources += [
216 "foo.mm",
217 ],
218 }
219 if (is_ios) {
220 sources = [
221 "bar.m",
222 "bar.c",
223 ],
224 }
225 }
226 """))
227 self.input_api.files = [MockFile(self.file_path)]
228 errors = PRESUBMIT.CheckNoMixingSources(self.input_api,
229 [MockFile(self.file_path)],
230 self.output_api)
231 self.assertEqual(1, len(errors))
232 self.assertTrue('bar.cc' in str(errors[0]))
233 self.assertTrue('bar.mm' in str(errors[0]))
234 self.assertTrue('foo.cc' in str(errors[0]))
235 self.assertTrue('foo.mm' in str(errors[0]))
236 self.assertTrue('bar.m' in str(errors[0]))
237 self.assertTrue('bar.c' in str(errors[0]))
238
239 def _AssertNumberOfErrorsWithSources(self, number_of_errors, sources):
Artem Titov5d7a4c62018-07-23 13:58:25 +0200240 assert len(sources) == 3, 'This function accepts a list of 3 source files'
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200241 self._GenerateBuildFile(textwrap.dedent("""
242 rtc_static_library("bar_foo") {
243 sources = [
244 "%s",
245 "%s",
246 "%s",
247 ],
248 }
249 rtc_source_set("foo_bar") {
250 sources = [
251 "%s",
252 "%s",
253 "%s",
254 ],
255 }
256 """ % (tuple(sources) * 2)))
257 self.input_api.files = [MockFile(self.file_path)]
258 errors = PRESUBMIT.CheckNoMixingSources(self.input_api,
259 [MockFile(self.file_path)],
260 self.output_api)
261 self.assertEqual(number_of_errors, len(errors))
262 if number_of_errors == 1:
263 for source in sources:
264 if not source.endswith('.h'):
265 self.assertTrue(source in str(errors[0]))
266
267 def _GenerateBuildFile(self, content):
268 with open(self.file_path, 'w') as f:
269 f.write(content)
270
271
charujain9893e252017-09-14 13:33:22 +0200272if __name__ == '__main__':
273 unittest.main()