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