Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / android_webview / tools / copyright_scanner_unittest.py
blob57bf8a43f20ab17c9b2839e56431e841a79fdde4
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Unit tests for Copyright Scanner utilities."""
8 import os
9 import re
10 import sys
11 import unittest
13 test_dir = os.path.dirname(os.path.abspath(__file__))
14 sys.path.extend([
15 os.path.normpath(os.path.join(test_dir, '..', '..', 'tools')),
16 os.path.join(test_dir),
19 import find_depot_tools
20 from testing_support.super_mox import SuperMoxTestBase
22 import copyright_scanner
24 class FindCopyrightsTest(SuperMoxTestBase):
25 def setUp(self):
26 SuperMoxTestBase.setUp(self)
27 self.input_api = self.mox.CreateMockAnything()
28 self.input_api.re = re
29 self.input_api.os_path = os.path
30 self.input_api.os_walk = os.walk
32 def ShouldMatchReferenceOutput(self, test_data, expected_output):
33 for data in test_data:
34 self.input_api.ReadFile = lambda _1, _2: data
35 actual_output = copyright_scanner.FindCopyrights(self.input_api, '', [''])
36 self.assertEqual(
37 expected_output,
38 actual_output,
39 'Input """\n%s""", expected output: "%s", actual: "%s"' % \
40 (data, expected_output, actual_output));
42 def testCopyrightedFiles(self):
43 test_data = [
44 '// (c) 2014 Google Inc.\n//\n// (a) One\n//\n// (b) Two\n//\n',
45 'Copyright 2014 Google Inc.\n',
46 'Copr. 2014 Google Inc.',
47 '\xc2\xa9 2014 Google Inc.',
48 'Copyright 2014 Google Inc.'
50 self.ShouldMatchReferenceOutput(test_data, [['2014 Google Inc.']])
52 def testGeneratedFiles(self):
53 test_data = [
54 'ALL CHANGES MADE IN THIS FILE WILL BE LOST\nCopyright 2014 Google\n',
55 'GENERATED FILE. DO NOT EDIT\nCopyright 2014 Google\n',
56 'GENERATED. DO NOT DELETE THIS FILE.\nCopyright 2014 Google\n',
57 'DO NOT EDIT\nCopyright 2014 Google\n',
58 'DO NOT DELETE THIS FILE\nCopyright 2014 Google\n',
59 'All changes made in this file will be lost\nCopyright 2014 Google\n',
60 'Automatically generated file\nCopyright 2014 Google\n',
61 'Synthetically generated dummy file\nCopyright 2014 Google\n',
62 'Generated data (by gnugnu)\nCopyright 2014 Google\n'
64 self.ShouldMatchReferenceOutput(test_data, [['GENERATED FILE']])
66 def testNonCopyrightedFiles(self):
67 test_data = [
68 'std::cout << "Copyright 2014 Google"\n',
69 '// Several points can be made:\n//\n// (a) One\n//\n// (b) Two\n'
70 '//\n// (c) Three\n//\n',
71 'See \'foo\' for copyright information.\n',
72 'See \'foo\' for the copyright notice.\n',
73 'See \'foo\' for the copyright and other things.\n'
75 self.ShouldMatchReferenceOutput(test_data, [['*No copyright*']])
77 def testNonGeneratedFiles(self):
78 test_data = [
79 'This file was prohibited from being generated.\n',
80 'Please do not delete our files! They are valuable to us.\n',
81 'Manually generated from dice rolls.\n',
82 '"""This Python script produces generated data\n"""\n',
83 '\'\'\'This Python script produces generated data\n\'\'\'\n'
85 self.ShouldMatchReferenceOutput(test_data, [['*No copyright*']])
88 class FindFilesTest(SuperMoxTestBase):
89 def setUp(self):
90 SuperMoxTestBase.setUp(self)
91 self.input_api = self.mox.CreateMockAnything()
92 self.input_api.re = re
93 self.input_api.os_path = os.path
95 def testFilesAsStartPaths(self):
96 join = self.input_api.os_path.join
97 self.input_api.os_path.isfile = lambda _: True
98 input_files = [
99 'a',
100 'a.cc',
101 'a.txt',
102 join('foo', 'a'),
103 join('foo', 'a.cc'),
104 join('foo', 'a.txt'),
105 join('third_party', 'a'),
106 join('third_party', 'a.cc'),
107 join('third_party', 'a.txt'),
108 join('foo', 'third_party', 'a'),
109 join('foo', 'third_party', 'a.cc'),
110 join('foo', 'third_party', 'a.txt'),
112 root_dir = os.path.sep + 'src'
113 actual = copyright_scanner.FindFiles(
114 self.input_api, root_dir, input_files, [''])
115 self.assertEqual(['a.cc', join('foo', 'a.cc')], actual)
116 actual = copyright_scanner.FindFiles(
117 self.input_api, root_dir, input_files, ['third_party'])
118 self.assertEqual(['a.cc', join('foo', 'a.cc')], actual)
119 actual = copyright_scanner.FindFiles(
120 self.input_api, root_dir, input_files, ['foo'])
121 self.assertEqual(['a.cc'], actual)
122 actual = copyright_scanner.FindFiles(
123 self.input_api, root_dir, input_files, ['foo', 'third_party'])
124 self.assertEqual(['a.cc'], actual)
125 actual = copyright_scanner.FindFiles(
126 self.input_api, root_dir, input_files, [join('foo', 'third_party')])
127 self.assertEqual(['a.cc', join('foo', 'a.cc')], actual)
129 def testDirAsStartPath(self):
130 self.input_api.os_path.isfile = lambda _: False
131 join = self.input_api.os_path.join
132 normpath = self.input_api.os_path.normpath
133 root_dir = os.path.sep + 'src'
134 scan_from = '.'
135 base_path = join(root_dir, scan_from)
137 def mock_os_walk(path):
138 return lambda _: [(join(base_path, path), [''], ['a', 'a.cc', 'a.txt'])]
140 self.input_api.os_walk = mock_os_walk('')
141 actual = map(normpath, copyright_scanner.FindFiles(
142 self.input_api, root_dir, [scan_from], ['']))
143 self.assertEqual(['a.cc'], actual)
145 self.input_api.os_walk = mock_os_walk('third_party')
146 actual = map(normpath, copyright_scanner.FindFiles(
147 self.input_api, root_dir, [scan_from], ['']))
148 self.assertEqual([], actual)
150 self.input_api.os_walk = mock_os_walk('foo')
151 actual = map(normpath, copyright_scanner.FindFiles(
152 self.input_api, root_dir, [scan_from], ['']))
153 self.assertEqual([join('foo', 'a.cc')], actual)
155 self.input_api.os_walk = mock_os_walk('foo')
156 actual = map(normpath, copyright_scanner.FindFiles(
157 self.input_api, root_dir, [scan_from], ['foo']))
158 self.assertEqual([], actual)
160 self.input_api.os_walk = mock_os_walk(join('foo', 'bar'))
161 actual = map(normpath, copyright_scanner.FindFiles(
162 self.input_api, root_dir, [scan_from], ['foo']))
163 self.assertEqual([], actual)
165 self.input_api.os_walk = mock_os_walk(join('foo', 'third_party'))
166 actual = map(normpath, copyright_scanner.FindFiles(
167 self.input_api, root_dir, [scan_from], ['']))
168 self.assertEqual([], actual)
170 self.input_api.os_walk = mock_os_walk(join('foo', 'third_party'))
171 actual = map(normpath, copyright_scanner.FindFiles(
172 self.input_api, root_dir, [scan_from], [join('foo', 'third_party')]))
173 self.assertEqual([], actual)
176 class AnalyzeScanResultsTest(SuperMoxTestBase):
177 def setUp(self):
178 SuperMoxTestBase.setUp(self)
179 self.input_api = self.mox.CreateMockAnything()
180 self.input_api.os_path = os.path
181 self.input_api.change = self.mox.CreateMockAnything()
182 self.input_api.change.RepositoryRoot = lambda: ''
184 def testAnalyzeScanResults(self):
185 # Tests whitelisted vs. current files state logic.
187 # Whitelisted - in whitelist, and contains 3rd party code => OK
188 # Missing - in whitelist, but doesn't exist
189 # Stale - in whitelist, but is clean
190 # Unknown - not in whitelist, but contains 3rd party code
191 self.input_api.os_path.isfile = lambda x: x != 'Missing'
192 self.assertEqual(
193 (['Unknown'], ['Missing'], ['Stale']),
194 copyright_scanner.AnalyzeScanResults(self.input_api, \
195 ['Whitelisted', 'Missing', 'Stale'], ['Whitelisted', 'Unknown']))
198 class ScanAtPresubmitTest(SuperMoxTestBase):
199 def setUp(self):
200 SuperMoxTestBase.setUp(self)
201 self.input_api = self.mox.CreateMockAnything()
202 self.input_api.re = re
203 self.input_api.os_path = os.path
204 self.output_api = self.mox.CreateMockAnything()
205 def tearDown(self):
206 self.mox.UnsetStubs()
207 SuperMoxTestBase.tearDown(self)
209 class AffectedFileMock(object):
210 def __init__(self, local_path, action):
211 self._local_path = local_path
212 self._action = action
213 def LocalPath(self):
214 return self._local_path
215 def Action(self):
216 return self._action
218 def CreateAffectedFilesFunc(self, paths_and_actions):
219 result = []
220 for i in range(0, len(paths_and_actions), 2):
221 result.append(ScanAtPresubmitTest.AffectedFileMock(
222 paths_and_actions[i], paths_and_actions[i + 1]))
223 return lambda: result
225 def CreateDoScanAtPresubmitFunc(self):
226 self._whitelisted_files = None
227 self._files_to_check = None
228 def ScanAtPresubmitStub(_, whitelisted, to_check):
229 self._whitelisted_files = whitelisted
230 self._files_to_check = to_check
231 return ([], [], [])
232 return ScanAtPresubmitStub
234 def GetWhitelistedFiles(self):
235 return sorted(self._whitelisted_files)
237 def GetFilesToCheck(self):
238 return sorted(self._files_to_check)
240 def testWhitelistedUntouched(self):
241 # When a change doesn't touch the whitelist file, any updated files
242 # (except deleted) must be checked. The whitelist used for analysis
243 # must be trimmed to the changed files subset.
245 # A_NW.cc - added, not whitelisted => check
246 # A_W.cc - added, whitelisted => check, remain on the trimmed whitelist
247 # D_NW.cc - deleted, not whitelisted => ignore
248 # D_W.cc - deleted and whitelisted => remain on w/l
249 # M_NW.cc - modified, not whitelisted => check
250 # M_W.cc - modified and whitelisted => check, remain on w/l
251 # NM_W.cc - not modified, whitelisted => trim from w/l
252 # W - the whitelist file
254 self.input_api.AffectedFiles = self.CreateAffectedFilesFunc(
255 ['A_NW.cc', 'A', 'A_W.cc', 'A', 'D_NW.cc', 'D', 'D_W.cc', 'D',
256 'M_NW.cc', 'M', 'M_W.cc', 'M'])
257 self.mox.StubOutWithMock(copyright_scanner, '_GetWhitelistFileName')
258 copyright_scanner._GetWhitelistFileName = lambda _: 'W'
259 self.mox.StubOutWithMock(copyright_scanner, 'LoadWhitelistedFilesList')
260 copyright_scanner.LoadWhitelistedFilesList = \
261 lambda _: ['A_W.cc', 'D_W.cc', 'M_W.cc', 'NM_W.cc']
262 self.mox.StubOutWithMock(copyright_scanner, '_DoScanAtPresubmit')
263 copyright_scanner._DoScanAtPresubmit = self.CreateDoScanAtPresubmitFunc()
264 self.mox.ReplayAll()
265 copyright_scanner.ScanAtPresubmit(self.input_api, self.output_api)
266 self.assertEqual(
267 ['A_W.cc', 'D_W.cc', 'M_W.cc'], self.GetWhitelistedFiles())
268 self.assertEqual(
269 ['A_NW.cc', 'A_W.cc', 'M_NW.cc', 'M_W.cc'], self.GetFilesToCheck())
271 def testWhitelistTouched(self):
272 # When the whitelist file is touched by the change, all the files listed in
273 # it, including deleted entries, must be re-checked. All modified files
274 # (including the deleted ones) must be checked as well. The current contents
275 # of the whitelist are used for analysis.
276 # Whitelist addition or deletion are not considered.
278 # All the files from names testWhitelistedUntouched are re-used, but now
279 # action for all of them is 'check' (except for the w/l file itself).
280 # A_DW.cc - added, deleted from w/l => check
281 # D_DW.cc - deleted from repo and w/l => check
282 # M_DW.cc - modified, deleted from w/l => check
283 self.input_api.AffectedFiles = self.CreateAffectedFilesFunc(
284 ['A_DW.cc', 'A', 'A_NW.cc', 'A', 'A_W.cc', 'A',
285 'D_DW.cc', 'D', 'D_NW.cc', 'D', 'D_W.cc', 'D',
286 'M_DW.cc', 'M', 'M_NW.cc', 'M', 'M_W.cc', 'M', 'W', 'M'])
287 self.mox.StubOutWithMock(copyright_scanner, '_GetWhitelistFileName')
288 copyright_scanner._GetWhitelistFileName = lambda _: 'W'
289 self.mox.StubOutWithMock(copyright_scanner, '_GetDeletedContents')
290 def GetDeletedContentsStub(affected_file):
291 self.assertEqual('W', affected_file.LocalPath())
292 return ['A_DW.cc', 'D_DW.cc', 'M_DW.cc']
293 copyright_scanner._GetDeletedContents = GetDeletedContentsStub
294 self.mox.StubOutWithMock(copyright_scanner, 'LoadWhitelistedFilesList')
295 copyright_scanner.LoadWhitelistedFilesList = \
296 lambda _: ['A_W.cc', 'D_W.cc', 'M_W.cc', 'NM_W.cc']
297 self.mox.StubOutWithMock(copyright_scanner, '_DoScanAtPresubmit')
298 copyright_scanner._DoScanAtPresubmit = self.CreateDoScanAtPresubmitFunc()
299 self.mox.ReplayAll()
300 copyright_scanner.ScanAtPresubmit(self.input_api, self.output_api)
301 self.assertEqual(
302 ['A_W.cc', 'D_W.cc', 'M_W.cc', 'NM_W.cc'], self.GetWhitelistedFiles())
303 self.assertEqual(
304 ['A_DW.cc', 'A_NW.cc', 'A_W.cc', 'D_DW.cc', 'D_NW.cc', 'D_W.cc',
305 'M_DW.cc', 'M_NW.cc', 'M_W.cc', 'NM_W.cc' ], self.GetFilesToCheck())
307 if __name__ == '__main__':
308 unittest.main()