OLD | NEW |
1 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 import re | 6 import re |
7 import sys | 7 import sys |
8 import unittest | 8 import unittest |
9 import PRESUBMIT | 9 import PRESUBMIT |
10 | 10 |
11 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 11 sys.path.append( |
| 12 os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) |
12 from PRESUBMIT_test_mocks import MockOutputApi, MockChange | 13 from PRESUBMIT_test_mocks import MockOutputApi, MockChange |
13 | 14 |
14 class MockInputApi(object): | 15 class MockInputApi(object): |
15 """ Mocked input api for unit testing of presubmit. | 16 """ Mocked input api for unit testing of presubmit. |
16 This lets us mock things like file system operations and changed files. | 17 This lets us mock things like file system operations and changed files. |
17 """ | 18 """ |
18 def __init__(self): | 19 def __init__(self): |
19 self.re = re | 20 self.re = re |
20 self.os_path = os.path | 21 self.os_path = os.path |
21 self.files = [] | 22 self.files = [] |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 return self._local_path | 57 return self._local_path |
57 # Format string used as the contents of a mock sync.proto in order to | 58 # Format string used as the contents of a mock sync.proto in order to |
58 # test presubmit parsing of EntitySpecifics definition in that file. | 59 # test presubmit parsing of EntitySpecifics definition in that file. |
59 MOCK_PROTOFILE_CONTENTS = ('\n' | 60 MOCK_PROTOFILE_CONTENTS = ('\n' |
60 'message EntitySpecifics {\n' | 61 'message EntitySpecifics {\n' |
61 '//comment\n' | 62 '//comment\n' |
62 '\n' | 63 '\n' |
63 'optional AutofillSpecifics autofill = 123;\n' | 64 'optional AutofillSpecifics autofill = 123;\n' |
64 'optional AppSpecifics app = 456;\n' | 65 'optional AppSpecifics app = 456;\n' |
65 'optional AppSettingSpecifics app_setting = 789;\n' | 66 'optional AppSettingSpecifics app_setting = 789;\n' |
| 67 'optional ExtensionSettingSpecifics extension_setting = 910;\n' |
66 '//comment\n' | 68 '//comment\n' |
67 '}\n') | 69 '}\n') |
68 | 70 |
69 | 71 |
70 # Format string used as the contents of a mock model_type.cc | 72 # Format string used as the contents of a mock model_type.cc |
71 # in order to test presubmit parsing of the ModelTypeInfoMap in that file. | 73 # in order to test presubmit parsing of the ModelTypeInfoMap in that file. |
72 MOCK_MODELTYPE_CONTENTS =('\n' | 74 MOCK_MODELTYPE_CONTENTS =('\n' |
73 'const ModelTypeInfo kModelTypeInfoMap[] = {\n' | 75 'const ModelTypeInfo kModelTypeInfoMap[] = {\n' |
74 '{APP_SETTINGS, "APP_SETTING", "app_settings", "App settings",\n' | 76 '{APP_SETTINGS, "APP_SETTING", "app_settings", "App settings",\n' |
75 'sync_pb::EntitySpecifics::kAppSettingFieldNumber, 13},\n' | 77 'sync_pb::EntitySpecifics::kAppSettingFieldNumber, 13},\n' |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 self.assertEqual(1, len(results)) | 120 self.assertEqual(1, len(results)) |
119 self.assertTrue('root tag' in results[0].message) | 121 self.assertTrue('root tag' in results[0].message) |
120 | 122 |
121 def testInvalidChangeDuplicatedValues(self): | 123 def testInvalidChangeDuplicatedValues(self): |
122 results = self._testChange('{APP_SETTINGS, "APP_SETTING",\n' | 124 results = self._testChange('{APP_SETTINGS, "APP_SETTING",\n' |
123 '"app_settings", "App settings",\n' | 125 '"app_settings", "App settings",\n' |
124 'sync_pb::EntitySpecifics::kAppSettingFieldNumber, 13},\n') | 126 'sync_pb::EntitySpecifics::kAppSettingFieldNumber, 13},\n') |
125 self.assertEqual(6, len(results)) | 127 self.assertEqual(6, len(results)) |
126 self.assertTrue('APP_SETTINGS' in results[0].message) | 128 self.assertTrue('APP_SETTINGS' in results[0].message) |
127 | 129 |
| 130 def testBlacklistedRootTag(self): |
| 131 results = self._testChange('{EXTENSION_SETTING, "EXTENSION_SETTING",\n' |
| 132 '"_mts_schema_descriptor","Extension Setting",\n' |
| 133 'sync_pb::EntitySpecifics::kExtensionSettingFieldNumber, 6},') |
| 134 self.assertEqual(2, len(results)) |
| 135 self.assertTrue('_mts_schema_descriptor' in results[0].message) |
| 136 self.assertTrue("blacklist" in results[0].message) |
| 137 |
128 def _testChange(self, modeltype_literal): | 138 def _testChange(self, modeltype_literal): |
129 mock_input_api = MockInputApi() | 139 mock_input_api = MockInputApi() |
130 mock_input_api.files = [ | 140 mock_input_api.files = [ |
131 MockFile(os.path.abspath('./protocol/sync.proto'), | 141 MockFile(os.path.abspath('./protocol/sync.proto'), |
132 MOCK_PROTOFILE_CONTENTS), | 142 MOCK_PROTOFILE_CONTENTS), |
133 MockFile(os.path.abspath('./syncable/model_type.cc'), | 143 MockFile(os.path.abspath('./syncable/model_type.cc'), |
134 MOCK_MODELTYPE_CONTENTS % (modeltype_literal)) | 144 MOCK_MODELTYPE_CONTENTS % (modeltype_literal)) |
135 ] | 145 ] |
136 | 146 |
137 return PRESUBMIT.CheckChangeOnCommit(mock_input_api, MockOutputApi()) | 147 return PRESUBMIT.CheckChangeOnCommit(mock_input_api, MockOutputApi()) |
138 if __name__ == '__main__': | 148 if __name__ == '__main__': |
139 unittest.main() | 149 unittest.main() |
OLD | NEW |