Added missing <stdlib.h>, needed on ubuntu 8.10.
[google-url.git] / PRESUBMIT_unittest.py
blob19231db57082b9112f3e1989097884b1b152ca56
1 #!/usr/bin/python
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.
7 """
10 import os
11 import PRESUBMIT
12 import re
13 import unittest
16 class MockInputApi(object):
17 def __init__(self):
18 self.affected_files = []
19 self.re = re
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):
31 self.path = path
33 def LocalPath(self):
34 return self.path
37 class MockOutputApi(object):
38 class PresubmitError(object):
39 def __init__(self, msg, items=[], long_text=''):
40 self.msg = msg
41 self.items = items
44 class PresubmitUnittest(unittest.TestCase):
45 def setUp(self):
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
53 def tearDown(self):
54 PRESUBMIT.ReadFile = self._ReadFile
56 def testLocalChecks(self):
57 api = MockInputApi()
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)
76 self.failUnless(
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__':
88 unittest.main()