Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: PRESUBMIT_test.py

Issue 2598173002: Add a Presubmit to check if a file must be included or imported.
Patch Set: feedback Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import re 6 import re
7 import subprocess 7 import subprocess
8 import unittest 8 import unittest
9 9
10 import PRESUBMIT 10 import PRESUBMIT
11 from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile 11 from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile
12 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi 12 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
13 13
14 _TEST_DATA_DIR = 'base/test/data/presubmit' 14 _TEST_DATA_DIR = 'base/test/data/presubmit'
15 15
16 class IncludeImportTest(unittest.TestCase):
17 def testDetectObjectiveCHeaderInterface(self):
18 content = ['# Copyright line',
19 '',
20 '@interface Class']
21 mock_input_api = MockInputApi()
22 mock_name = 'some/ios/foo.h'
23 mock_file = MockFile(mock_name, content)
24 (detection_ok, is_objc) = \
25 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
26 self.assertEqual(detection_ok, True)
27 self.assertEqual(is_objc, True)
28
29 def testDetectObjectiveCNotIosMacFile(self):
30 content = ['# Copyright line',
31 '',
32 '@interface Class']
33 mock_input_api = MockInputApi()
34 mock_name = 'some/bar/foo.h'
35 mock_file = MockFile(mock_name, content)
36 (detection_ok, is_objc) = \
37 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
38 self.assertEqual(detection_ok, False)
39 self.assertEqual(is_objc, False)
40
41
42 def testDetectObjectiveCBlock(self):
43 content = ['# Copyright line',
44 '',
45 'typedef void (^ProceduralBlock)(void);']
46 mock_input_api = MockInputApi()
47 mock_name = 'some/path/foo_mac.h'
48 mock_file = MockFile(mock_name, content)
49 (detection_ok, is_objc) = \
50 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
51 self.assertEqual(detection_ok, True)
52 self.assertEqual(is_objc, True)
53
54 def testDetectObjectiveCImport(self):
55 content = ['# Copyright line',
56 '',
57 '#include "foo.h',
58 '#include "bar.h',
59 '#import "example.h',
60 '#include "chromium.h']
61 mock_input_api = MockInputApi()
62 mock_name = 'some/mac/foo.h'
63 mock_file = MockFile(mock_name, content)
64 (detection_ok, is_objc) = \
65 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
66 self.assertEqual(detection_ok, True)
67 self.assertEqual(is_objc, True)
68
69 def testDetectObjectiveCHeaderCPlusPlus(self):
70 content = ['# Copyright line',
71 '',
72 '#include <cstdio>',
73 'class Foo']
74 mock_input_api = MockInputApi()
75 mock_name = 'some/path/foo_ios.h'
76 mock_file = MockFile(mock_name, content)
77 (detection_ok, is_objc) = \
78 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
79 self.assertEqual(detection_ok, True)
80 self.assertEqual(is_objc, False)
81
82 def testDetectObjectiveCHeaderCPlusPlus(self):
83 content = ['# Copyright line',
84 '',
85 '#if !defined(__OBJC__)',
86 '#error "ObjectiveC required"',
87 '#endif',
88 '',
89 '#include <cstdio>',
90 'class Foo']
91 mock_input_api = MockInputApi()
92 mock_name = 'ios/path/foo.h'
93 mock_file = MockFile(mock_name, content)
94 (detection_ok, is_objc) = \
95 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
96 self.assertEqual(detection_ok, True)
97 self.assertEqual(is_objc, True)
98
99 def testDetectObjectiveCNoObjcGuard(self):
100 content = ['# Copyright line',
101 '',
102 '#if !defined(__OBJC__)',
103 '#include "foo.h"',
104 '#else',
105 '#import "foo.h"',
106 '#endif']
107 mock_input_api = MockInputApi()
108 mock_name = 'mac/path/foo.h'
109 mock_file = MockFile(mock_name, content)
110 (detection_ok, is_objc) = \
111 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
112 self.assertEqual(detection_ok, False)
113
114
115 def testDetectObjectiveCNoObjcGuard(self):
116 content = ['# Copyright line',
117 '',
118 '#if defined(__OBJC__)',
119 '#import "foo.h"',
120 '#else',
121 '#include "foo.h"',
122 '#endif']
123 mock_input_api = MockInputApi()
124 mock_name = 'some/path_ios/foo.h'
125 mock_file = MockFile(mock_name, content)
126 (detection_ok, is_objc) = \
127 PRESUBMIT._CheckHeaderIsObjectiveC(mock_input_api, mock_name, mock_file)
128 self.assertEqual(detection_ok, False)
129
16 class IncludeOrderTest(unittest.TestCase): 130 class IncludeOrderTest(unittest.TestCase):
17 def testSystemHeaderOrder(self): 131 def testSystemHeaderOrder(self):
18 scope = [(1, '#include <csystem.h>'), 132 scope = [(1, '#include <csystem.h>'),
19 (2, '#include <cppsystem>'), 133 (2, '#include <cppsystem>'),
20 (3, '#include "acustom.h"')] 134 (3, '#include "acustom.h"')]
21 all_linenums = [linenum for (linenum, _) in scope] 135 all_linenums = [linenum for (linenum, _) in scope]
22 mock_input_api = MockInputApi() 136 mock_input_api = MockInputApi()
23 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, 137 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
24 '', all_linenums) 138 '', all_linenums)
25 self.assertEqual(0, len(warnings)) 139 self.assertEqual(0, len(warnings))
(...skipping 1270 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 mock_input_api.files = [ 1410 mock_input_api.files = [
1297 MockAffectedFile('chrome/browser/resources/blah.js', 'arrow => OK here'), 1411 MockAffectedFile('chrome/browser/resources/blah.js', 'arrow => OK here'),
1298 ] 1412 ]
1299 warnings = PRESUBMIT._CheckForRiskyJsFeatures( 1413 warnings = PRESUBMIT._CheckForRiskyJsFeatures(
1300 mock_input_api, MockOutputApi()) 1414 mock_input_api, MockOutputApi())
1301 self.assertEqual(0, len(warnings)) 1415 self.assertEqual(0, len(warnings))
1302 1416
1303 1417
1304 if __name__ == '__main__': 1418 if __name__ == '__main__':
1305 unittest.main() 1419 unittest.main()
OLDNEW
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698