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 | |
| 50 | |
Mirko Bonadei | 7de1eb7 | 2017-09-19 10:16:10 +0200 | [diff] [blame] | 51 | class CheckNewlineAtTheEndOfProtoFilesTest(unittest.TestCase): |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 52 | |
| 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 Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame^] | 63 | self._GenerateProtoWithoutNewlineAtTheEnd() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 64 | 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 Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame^] | 73 | self._GenerateProtoWithNewlineAtTheEnd() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 74 | 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 Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame^] | 79 | def _GenerateProtoWithNewlineAtTheEnd(self): |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 80 | 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 Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame^] | 87 | def _GenerateProtoWithoutNewlineAtTheEnd(self): |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 88 | 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 Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame^] | 95 | class 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 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 247 | if __name__ == '__main__': |
| 248 | unittest.main() |