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."""
13 test_dir
= os
.path
.dirname(os
.path
.abspath(__file__
))
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
):
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
, '', [''])
39 'Input """\n%s""", expected output: "%s", actual: "%s"' % \
40 (data
, expected_output
, actual_output
));
42 def testCopyrightedFiles(self
):
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
):
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
):
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
):
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
):
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
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'
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
):
178 SuperMoxTestBase
.setUp(self
)
179 self
.input_api
= self
.mox
.CreateMockAnything()
180 self
.input_api
.os_path
= os
.path
182 def testAnalyzeScanResults(self
):
183 # Tests whitelisted vs. current files state logic.
185 # Whitelisted - in whitelist, and contains 3rd party code => OK
186 # Missing - in whitelist, but doesn't exist
187 # Stale - in whitelist, but is clean
188 # Unknown - not in whitelist, but contains 3rd party code
189 self
.input_api
.os_path
.isfile
= lambda x
: x
!= 'Missing'
191 (['Unknown'], ['Missing'], ['Stale']),
192 copyright_scanner
.AnalyzeScanResults(self
.input_api
, \
193 ['Whitelisted', 'Missing', 'Stale'], ['Whitelisted', 'Unknown']))
196 class ScanAtPresubmitTest(SuperMoxTestBase
):
198 SuperMoxTestBase
.setUp(self
)
199 self
.input_api
= self
.mox
.CreateMockAnything()
200 self
.input_api
.re
= re
201 self
.input_api
.os_path
= os
.path
202 self
.output_api
= self
.mox
.CreateMockAnything()
204 self
.mox
.UnsetStubs()
205 SuperMoxTestBase
.tearDown(self
)
207 class AffectedFileMock(object):
208 def __init__(self
, local_path
, action
):
209 self
._local
_path
= local_path
210 self
._action
= action
212 return self
._local
_path
216 def CreateAffectedFilesFunc(self
, paths_and_actions
):
218 for i
in range(0, len(paths_and_actions
), 2):
219 result
.append(ScanAtPresubmitTest
.AffectedFileMock(
220 paths_and_actions
[i
], paths_and_actions
[i
+ 1]))
221 return lambda: result
223 def CreateDoScanAtPresubmitFunc(self
):
224 self
._whitelisted
_files
= None
225 self
._files
_to
_check
= None
226 def ScanAtPresubmitStub(_
, whitelisted
, to_check
):
227 self
._whitelisted
_files
= whitelisted
228 self
._files
_to
_check
= to_check
230 return ScanAtPresubmitStub
232 def GetWhitelistedFiles(self
):
233 return sorted(self
._whitelisted
_files
)
235 def GetFilesToCheck(self
):
236 return sorted(self
._files
_to
_check
)
238 def testWhitelistedUntouched(self
):
239 # When a change doesn't touch the whitelist file, any updated files
240 # (except deleted) must be checked. The whitelist used for analysis
241 # must be trimmed to the changed files subset.
243 # A_NW.cc - added, not whitelisted => check
244 # A_W.cc - added, whitelisted => check, remain on the trimmed whitelist
245 # D_NW.cc - deleted, not whitelisted => ignore
246 # D_W.cc - deleted and whitelisted => remain on w/l
247 # M_NW.cc - modified, not whitelisted => check
248 # M_W.cc - modified and whitelisted => check, remain on w/l
249 # NM_W.cc - not modified, whitelisted => trim from w/l
250 # W - the whitelist file
252 self
.input_api
.AffectedFiles
= self
.CreateAffectedFilesFunc(
253 ['A_NW.cc', 'A', 'A_W.cc', 'A', 'D_NW.cc', 'D', 'D_W.cc', 'D',
254 'M_NW.cc', 'M', 'M_W.cc', 'M'])
255 self
.mox
.StubOutWithMock(copyright_scanner
, '_GetWhitelistFileName')
256 copyright_scanner
._GetWhitelistFileName
= lambda _
: 'W'
257 self
.mox
.StubOutWithMock(copyright_scanner
, 'LoadWhitelistedFilesList')
258 copyright_scanner
.LoadWhitelistedFilesList
= \
259 lambda _
: ['A_W.cc', 'D_W.cc', 'M_W.cc', 'NM_W.cc']
260 self
.mox
.StubOutWithMock(copyright_scanner
, '_DoScanAtPresubmit')
261 copyright_scanner
._DoScanAtPresubmit
= self
.CreateDoScanAtPresubmitFunc()
263 copyright_scanner
.ScanAtPresubmit(self
.input_api
, self
.output_api
)
265 ['A_W.cc', 'D_W.cc', 'M_W.cc'], self
.GetWhitelistedFiles())
267 ['A_NW.cc', 'A_W.cc', 'M_NW.cc', 'M_W.cc'], self
.GetFilesToCheck())
269 def testWhitelistTouched(self
):
270 # When the whitelist file is touched by the change, all the files listed in
271 # it, including deleted entries, must be re-checked. All modified files
272 # (including the deleted ones) must be checked as well. The current contents
273 # of the whitelist are used for analysis.
274 # Whitelist addition or deletion are not considered.
276 # All the files from names testWhitelistedUntouched are re-used, but now
277 # action for all of them is 'check' (except for the w/l file itself).
278 # A_DW.cc - added, deleted from w/l => check
279 # D_DW.cc - deleted from repo and w/l => check
280 # M_DW.cc - modified, deleted from w/l => check
281 self
.input_api
.AffectedFiles
= self
.CreateAffectedFilesFunc(
282 ['A_DW.cc', 'A', 'A_NW.cc', 'A', 'A_W.cc', 'A',
283 'D_DW.cc', 'D', 'D_NW.cc', 'D', 'D_W.cc', 'D',
284 'M_DW.cc', 'M', 'M_NW.cc', 'M', 'M_W.cc', 'M', 'W', 'M'])
285 self
.mox
.StubOutWithMock(copyright_scanner
, '_GetWhitelistFileName')
286 copyright_scanner
._GetWhitelistFileName
= lambda _
: 'W'
287 self
.mox
.StubOutWithMock(copyright_scanner
, '_GetDeletedContents')
288 def GetDeletedContentsStub(affected_file
):
289 self
.assertEqual('W', affected_file
.LocalPath())
290 return ['A_DW.cc', 'D_DW.cc', 'M_DW.cc']
291 copyright_scanner
._GetDeletedContents
= GetDeletedContentsStub
292 self
.mox
.StubOutWithMock(copyright_scanner
, 'LoadWhitelistedFilesList')
293 copyright_scanner
.LoadWhitelistedFilesList
= \
294 lambda _
: ['A_W.cc', 'D_W.cc', 'M_W.cc', 'NM_W.cc']
295 self
.mox
.StubOutWithMock(copyright_scanner
, '_DoScanAtPresubmit')
296 copyright_scanner
._DoScanAtPresubmit
= self
.CreateDoScanAtPresubmitFunc()
298 copyright_scanner
.ScanAtPresubmit(self
.input_api
, self
.output_api
)
300 ['A_W.cc', 'D_W.cc', 'M_W.cc', 'NM_W.cc'], self
.GetWhitelistedFiles())
302 ['A_DW.cc', 'A_NW.cc', 'A_W.cc', 'D_DW.cc', 'D_NW.cc', 'D_W.cc',
303 'M_DW.cc', 'M_NW.cc', 'M_W.cc', 'NM_W.cc' ], self
.GetFilesToCheck())
305 if __name__
== '__main__':