Henrik Kjellander | a18a3bf | 2017-09-15 11:19:10 +0200 | [diff] [blame] | 1 | #!/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 Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 11 | import os |
| 12 | import shutil |
| 13 | import tempfile |
| 14 | import textwrap |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 15 | import unittest |
| 16 | |
| 17 | import PRESUBMIT |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 18 | from presubmit_test_mocks import MockInputApi, MockOutputApi, MockFile |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 19 | |
| 20 | |
Mirko Bonadei | 7de1eb7 | 2017-09-19 10:16:10 +0200 | [diff] [blame] | 21 | class CheckBugEntryFieldTest(unittest.TestCase): |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 22 | 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 Lemur | 6d01f6d | 2017-09-14 17:02:01 +0200 | [diff] [blame] | 27 | mock_output_api) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 28 | 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 Lemur | 6d01f6d | 2017-09-14 17:02:01 +0200 | [diff] [blame] | 35 | mock_output_api) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 36 | 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 Lemur | 6d01f6d | 2017-09-14 17:02:01 +0200 | [diff] [blame] | 47 | mock_output_api) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 48 | self.assertEqual(0, len(errors)) |
| 49 | |
charujain | 81a58c7 | 2017-09-25 13:25:45 +0200 | [diff] [blame^] | 50 | def testCommitMessageBugEntrySupportInternalBugReference(self): |
| 51 | mock_input_api = MockInputApi() |
| 52 | mock_output_api = MockOutputApi() |
| 53 | mock_input_api.change.BUG = 'b/12345' |
| 54 | errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api, |
| 55 | mock_output_api) |
| 56 | self.assertEqual(0, len(errors)) |
| 57 | mock_input_api.change.BUG = 'b/12345, webrtc:1234' |
| 58 | errors = PRESUBMIT.CheckCommitMessageBugEntry(mock_input_api, |
| 59 | mock_output_api) |
| 60 | self.assertEqual(0, len(errors)) |
| 61 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 62 | |
Mirko Bonadei | 7de1eb7 | 2017-09-19 10:16:10 +0200 | [diff] [blame] | 63 | class CheckNewlineAtTheEndOfProtoFilesTest(unittest.TestCase): |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 64 | |
| 65 | def setUp(self): |
| 66 | self.tmp_dir = tempfile.mkdtemp() |
| 67 | self.proto_file_path = os.path.join(self.tmp_dir, 'foo.proto') |
| 68 | self.input_api = MockInputApi() |
| 69 | self.output_api = MockOutputApi() |
| 70 | |
| 71 | def tearDown(self): |
| 72 | shutil.rmtree(self.tmp_dir, ignore_errors=True) |
| 73 | |
| 74 | def testErrorIfProtoFileDoesNotEndWithNewline(self): |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 75 | self._GenerateProtoWithoutNewlineAtTheEnd() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 76 | self.input_api.files = [MockFile(self.proto_file_path)] |
| 77 | errors = PRESUBMIT.CheckNewlineAtTheEndOfProtoFiles(self.input_api, |
| 78 | self.output_api) |
| 79 | self.assertEqual(1, len(errors)) |
| 80 | self.assertEqual( |
| 81 | 'File %s must end with exactly one newline.' % self.proto_file_path, |
| 82 | str(errors[0])) |
| 83 | |
| 84 | def testNoErrorIfProtoFileEndsWithNewline(self): |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 85 | self._GenerateProtoWithNewlineAtTheEnd() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 86 | self.input_api.files = [MockFile(self.proto_file_path)] |
| 87 | errors = PRESUBMIT.CheckNewlineAtTheEndOfProtoFiles(self.input_api, |
| 88 | self.output_api) |
| 89 | self.assertEqual(0, len(errors)) |
| 90 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 91 | def _GenerateProtoWithNewlineAtTheEnd(self): |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 92 | with open(self.proto_file_path, 'w') as f: |
| 93 | f.write(textwrap.dedent(""" |
| 94 | syntax = "proto2"; |
| 95 | option optimize_for = LITE_RUNTIME; |
| 96 | package webrtc.audioproc; |
| 97 | """)) |
| 98 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 99 | def _GenerateProtoWithoutNewlineAtTheEnd(self): |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 100 | with open(self.proto_file_path, 'w') as f: |
| 101 | f.write(textwrap.dedent(""" |
| 102 | syntax = "proto2"; |
| 103 | option optimize_for = LITE_RUNTIME; |
| 104 | package webrtc.audioproc;""")) |
| 105 | |
| 106 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 107 | class CheckNoMixingSourcesTest(unittest.TestCase): |
| 108 | |
| 109 | def setUp(self): |
| 110 | self.tmp_dir = tempfile.mkdtemp() |
| 111 | self.file_path = os.path.join(self.tmp_dir, 'BUILD.gn') |
| 112 | self.input_api = MockInputApi() |
| 113 | self.output_api = MockOutputApi() |
| 114 | |
| 115 | def tearDown(self): |
| 116 | shutil.rmtree(self.tmp_dir, ignore_errors=True) |
| 117 | |
| 118 | def testErrorIfCAndCppAreMixed(self): |
| 119 | self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.cc', 'bar.h']) |
| 120 | |
| 121 | def testErrorIfCAndObjCAreMixed(self): |
| 122 | self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.m', 'bar.h']) |
| 123 | |
| 124 | def testErrorIfCAndObjCppAreMixed(self): |
| 125 | self._AssertNumberOfErrorsWithSources(1, ['foo.c', 'bar.mm', 'bar.h']) |
| 126 | |
| 127 | def testErrorIfCppAndObjCAreMixed(self): |
| 128 | self._AssertNumberOfErrorsWithSources(1, ['foo.cc', 'bar.m', 'bar.h']) |
| 129 | |
| 130 | def testErrorIfCppAndObjCppAreMixed(self): |
| 131 | self._AssertNumberOfErrorsWithSources(1, ['foo.cc', 'bar.mm', 'bar.h']) |
| 132 | |
| 133 | def testNoErrorIfOnlyC(self): |
| 134 | self._AssertNumberOfErrorsWithSources(0, ['foo.c', 'bar.c', 'bar.h']) |
| 135 | |
| 136 | def testNoErrorIfOnlyCpp(self): |
| 137 | self._AssertNumberOfErrorsWithSources(0, ['foo.cc', 'bar.cc', 'bar.h']) |
| 138 | |
| 139 | def testNoErrorIfOnlyObjC(self): |
| 140 | self._AssertNumberOfErrorsWithSources(0, ['foo.m', 'bar.m', 'bar.h']) |
| 141 | |
| 142 | def testNoErrorIfOnlyObjCpp(self): |
| 143 | self._AssertNumberOfErrorsWithSources(0, ['foo.mm', 'bar.mm', 'bar.h']) |
| 144 | |
| 145 | def testNoErrorIfObjCAndObjCppAreMixed(self): |
| 146 | self._AssertNumberOfErrorsWithSources(0, ['foo.m', 'bar.mm', 'bar.h']) |
| 147 | |
| 148 | def testNoErrorIfSourcesAreInExclusiveIfBranches(self): |
| 149 | self._GenerateBuildFile(textwrap.dedent(""" |
| 150 | rtc_source_set("bar_foo") { |
| 151 | if (is_win) { |
| 152 | sources = [ |
| 153 | "bar.cc", |
| 154 | ], |
| 155 | } |
| 156 | if (is_ios) { |
| 157 | sources = [ |
| 158 | "bar.mm", |
| 159 | ], |
| 160 | } |
| 161 | } |
| 162 | rtc_source_set("foo_bar") { |
| 163 | if (is_win) { |
| 164 | sources = [ |
| 165 | "foo.cc", |
| 166 | ], |
| 167 | } |
| 168 | if (is_ios) { |
| 169 | sources = [ |
| 170 | "foo.mm", |
| 171 | ], |
| 172 | } |
| 173 | } |
| 174 | """)) |
| 175 | self.input_api.files = [MockFile(self.file_path)] |
| 176 | errors = PRESUBMIT.CheckNoMixingSources(self.input_api, |
| 177 | [MockFile(self.file_path)], |
| 178 | self.output_api) |
| 179 | self.assertEqual(0, len(errors)) |
| 180 | |
| 181 | def testErrorIfSourcesAreNotInExclusiveIfBranches(self): |
| 182 | self._GenerateBuildFile(textwrap.dedent(""" |
| 183 | rtc_source_set("bar_foo") { |
| 184 | if (is_win) { |
| 185 | sources = [ |
| 186 | "bar.cc", |
| 187 | ], |
| 188 | } |
| 189 | if (foo_bar) { |
| 190 | sources += [ |
| 191 | "bar.mm", |
| 192 | ], |
| 193 | } |
| 194 | } |
| 195 | rtc_source_set("foo_bar") { |
| 196 | if (is_win) { |
| 197 | sources = [ |
| 198 | "foo.cc", |
| 199 | ], |
| 200 | } |
| 201 | if (foo_bar) { |
| 202 | sources += [ |
| 203 | "foo.mm", |
| 204 | ], |
| 205 | } |
| 206 | if (is_ios) { |
| 207 | sources = [ |
| 208 | "bar.m", |
| 209 | "bar.c", |
| 210 | ], |
| 211 | } |
| 212 | } |
| 213 | """)) |
| 214 | self.input_api.files = [MockFile(self.file_path)] |
| 215 | errors = PRESUBMIT.CheckNoMixingSources(self.input_api, |
| 216 | [MockFile(self.file_path)], |
| 217 | self.output_api) |
| 218 | self.assertEqual(1, len(errors)) |
| 219 | self.assertTrue('bar.cc' in str(errors[0])) |
| 220 | self.assertTrue('bar.mm' in str(errors[0])) |
| 221 | self.assertTrue('foo.cc' in str(errors[0])) |
| 222 | self.assertTrue('foo.mm' in str(errors[0])) |
| 223 | self.assertTrue('bar.m' in str(errors[0])) |
| 224 | self.assertTrue('bar.c' in str(errors[0])) |
| 225 | |
| 226 | def _AssertNumberOfErrorsWithSources(self, number_of_errors, sources): |
| 227 | assert 3 == len(sources), 'This function accepts a list of 3 source files' |
| 228 | self._GenerateBuildFile(textwrap.dedent(""" |
| 229 | rtc_static_library("bar_foo") { |
| 230 | sources = [ |
| 231 | "%s", |
| 232 | "%s", |
| 233 | "%s", |
| 234 | ], |
| 235 | } |
| 236 | rtc_source_set("foo_bar") { |
| 237 | sources = [ |
| 238 | "%s", |
| 239 | "%s", |
| 240 | "%s", |
| 241 | ], |
| 242 | } |
| 243 | """ % (tuple(sources) * 2))) |
| 244 | self.input_api.files = [MockFile(self.file_path)] |
| 245 | errors = PRESUBMIT.CheckNoMixingSources(self.input_api, |
| 246 | [MockFile(self.file_path)], |
| 247 | self.output_api) |
| 248 | self.assertEqual(number_of_errors, len(errors)) |
| 249 | if number_of_errors == 1: |
| 250 | for source in sources: |
| 251 | if not source.endswith('.h'): |
| 252 | self.assertTrue(source in str(errors[0])) |
| 253 | |
| 254 | def _GenerateBuildFile(self, content): |
| 255 | with open(self.file_path, 'w') as f: |
| 256 | f.write(content) |
| 257 | |
| 258 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 259 | if __name__ == '__main__': |
| 260 | unittest.main() |