2 # Copyright (c) 2009 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 top-level Chromium presubmit script.
16 class MockInputApi(object):
18 self
.affected_files
= []
20 self
.os_path
= os
.path
22 def AffectedFiles(self
):
23 return self
.affected_files
25 def AffectedTextFiles(self
, include_deletes
=True):
26 return self
.affected_files
29 class MockAffectedFile(object):
30 def __init__(self
, path
):
37 class MockOutputApi(object):
38 class PresubmitError(object):
39 def __init__(self
, msg
, items
=[], long_text
=''):
44 class PresubmitUnittest(unittest
.TestCase
):
46 self
.file_contents
= ''
47 def MockReadFile(path
):
48 self
.failIf(path
.endswith('notsource'))
49 return self
.file_contents
50 self
._ReadFile
= PRESUBMIT
.ReadFile
51 PRESUBMIT
.ReadFile
= MockReadFile
54 PRESUBMIT
.ReadFile
= self
._ReadFile
56 def testLocalChecks(self
):
58 api
.affected_files
= [
59 MockAffectedFile('foo/blat/yoo.notsource'),
60 MockAffectedFile('third_party/blat/source.cc'),
61 MockAffectedFile('foo/blat/source.h'),
62 MockAffectedFile('foo/blat/source.mm'),
63 MockAffectedFile('foo/blat/source.py'),
65 self
.file_contents
= 'file with \n\terror\nhere\r\nyes there'
66 # 3 source files, 2 errors by file + 1 global CR error.
67 self
.failUnless(len(PRESUBMIT
.LocalChecks(api
, MockOutputApi
)) == 7)
69 self
.file_contents
= 'file\twith\ttabs'
70 # 3 source files, 1 error by file.
71 self
.failUnless(len(PRESUBMIT
.LocalChecks(api
, MockOutputApi
)) == 3)
73 self
.file_contents
= 'file\rusing\rCRs'
74 # One global CR error.
75 self
.failUnless(len(PRESUBMIT
.LocalChecks(api
, MockOutputApi
)) == 1)
77 len(PRESUBMIT
.LocalChecks(api
, MockOutputApi
)[0].items
) == 3)
79 self
.file_contents
= 'both\ttabs and\r\nCRLF'
80 # 3 source files, 1 error by file + 1 global CR error.
81 self
.failUnless(len(PRESUBMIT
.LocalChecks(api
, MockOutputApi
)) == 4)
83 self
.file_contents
= 'file with\nzero \\t errors \\r\\n'
84 self
.failIf(PRESUBMIT
.LocalChecks(api
, MockOutputApi
))
87 if __name__
== '__main__':