Henrik Kjellander | a18a3bf | 2017-09-15 09:19:10 | [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 09:33:13 | [diff] [blame] | 11 | import os |
| 12 | import shutil |
| 13 | import tempfile |
| 14 | import textwrap |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 15 | import unittest |
| 16 | |
| 17 | import PRESUBMIT |
Mirko Bonadei | a730c1c | 2017-09-18 09:33:13 | [diff] [blame] | 18 | from presubmit_test_mocks import MockInputApi, MockOutputApi, MockFile |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 19 | |
| 20 | |
Mirko Bonadei | 7de1eb7 | 2017-09-19 08:16:10 | [diff] [blame^] | 21 | class CheckBugEntryFieldTest(unittest.TestCase): |
charujain | 9893e25 | 2017-09-14 11:33:22 | [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 15:02:01 | [diff] [blame] | 27 | mock_output_api) |
charujain | 9893e25 | 2017-09-14 11:33:22 | [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 15:02:01 | [diff] [blame] | 35 | mock_output_api) |
charujain | 9893e25 | 2017-09-14 11:33:22 | [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 15:02:01 | [diff] [blame] | 47 | mock_output_api) |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 48 | self.assertEqual(0, len(errors)) |
| 49 | |
| 50 | |
Mirko Bonadei | 7de1eb7 | 2017-09-19 08:16:10 | [diff] [blame^] | 51 | class CheckNewlineAtTheEndOfProtoFilesTest(unittest.TestCase): |
Mirko Bonadei | a730c1c | 2017-09-18 09:33:13 | [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): |
| 63 | self.__GenerateProtoWithoutNewlineAtTheEnd() |
| 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): |
| 73 | self.__GenerateProtoWithNewlineAtTheEnd() |
| 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 | |
| 79 | def __GenerateProtoWithNewlineAtTheEnd(self): |
| 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 | |
| 87 | def __GenerateProtoWithoutNewlineAtTheEnd(self): |
| 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 | |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 95 | if __name__ == '__main__': |
| 96 | unittest.main() |