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 | 0c15c53 | 2017-09-19 10:41:02 +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 | 0c15c53 | 2017-09-19 10:41:02 +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 | 0c15c53 | 2017-09-19 10:41:02 +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 | 0c15c53 | 2017-09-19 10:41:02 +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 | 0c15c53 | 2017-09-19 10:41:02 +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 _AssertNumberOfErrorsWithSources(self, number_of_errors, sources): |
| 137 | assert 3 == len(sources), 'This function accepts a list of 3 source files' |
| 138 | self._GenerateBuildFile(textwrap.dedent(""" |
| 139 | rtc_source_set("foo_bar") { |
| 140 | sources = [ |
| 141 | "%s", |
| 142 | "%s", |
| 143 | "%s", |
| 144 | ], |
| 145 | } |
| 146 | """ % tuple(sources))) |
| 147 | self.input_api.files = [MockFile(self.file_path)] |
| 148 | errors = PRESUBMIT.CheckNoMixingSources(self.input_api, |
| 149 | [MockFile(self.file_path)], |
| 150 | self.output_api) |
| 151 | self.assertEqual(number_of_errors, len(errors)) |
| 152 | if number_of_errors == 1: |
| 153 | for source in sources: |
| 154 | if not source.endswith('.h'): |
| 155 | self.assertTrue(source in str(errors[0])) |
| 156 | |
| 157 | def _GenerateBuildFile(self, content): |
| 158 | with open(self.file_path, 'w') as f: |
| 159 | f.write(content) |
| 160 | |
| 161 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 162 | if __name__ == '__main__': |
| 163 | unittest.main() |