[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 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. |
| 5 | |
| 6 | """Unit tests for top-level Chromium presubmit script. |
| 7 | """ |
| 8 | |
| 9 | |
| 10 | import PRESUBMIT |
| 11 | import unittest |
| 12 | |
| 13 | |
| 14 | class MockInputApi(object): |
| 15 | def __init__(self): |
| 16 | self.affected_files = [] |
| 17 | |
| 18 | def AffectedTextFiles(self, include_deletes=True): |
| 19 | return self.affected_files |
| 20 | |
| 21 | |
| 22 | class MockAffectedFile(object): |
| 23 | def __init__(self, path): |
| 24 | self.path = path |
| 25 | |
| 26 | def LocalPath(self): |
| 27 | return self.path |
| 28 | |
| 29 | |
| 30 | class MockOutputApi(object): |
| 31 | class PresubmitError(object): |
| 32 | def __init__(self, msg, items): |
| 33 | self.msg = msg |
| 34 | self.items = items |
| 35 | |
| 36 | |
| 37 | class PresubmitUnittest(unittest.TestCase): |
| 38 | def setUp(self): |
| 39 | self.file_contents = '' |
| 40 | def MockReadFile(path): |
| 41 | self.failIf(path.endswith('notsource')) |
| 42 | return self.file_contents |
| 43 | PRESUBMIT._ReadFile = MockReadFile |
| 44 | |
| 45 | def tearDown(self): |
| 46 | PRESUBMIT._ReadFile = PRESUBMIT.ReadFile |
| 47 | |
| 48 | def testCheckNoCrLfOrTabs(self): |
| 49 | api = MockInputApi() |
| 50 | api.affected_files = [ |
| 51 | MockAffectedFile('foo/blat/yoo.notsource'), |
| 52 | MockAffectedFile('foo/blat/source.h'), |
| 53 | MockAffectedFile('foo/blat/source.mm'), |
| 54 | MockAffectedFile('foo/blat/source.py'), |
| 55 | ] |
| 56 | self.file_contents = 'file with\nerror\nhere\r\nyes there' |
| 57 | self.failUnless(len(PRESUBMIT.CheckNoCrOrTabs(api, MockOutputApi)) == 1) |
| 58 | self.failUnless( |
| 59 | len(PRESUBMIT.CheckNoCrOrTabs(api, MockOutputApi)[0].items) == 3) |
| 60 | |
| 61 | self.file_contents = 'file\twith\ttabs' |
| 62 | self.failUnless(len(PRESUBMIT.CheckNoCrOrTabs(api, MockOutputApi)) == 1) |
| 63 | |
| 64 | self.file_contents = 'file\rusing\rCRs' |
| 65 | self.failUnless(len(PRESUBMIT.CheckNoCrOrTabs(api, MockOutputApi)) == 1) |
| 66 | |
| 67 | self.file_contents = 'both\ttabs and\r\nCRLF' |
| 68 | self.failUnless(len(PRESUBMIT.CheckNoCrOrTabs(api, MockOutputApi)) == 2) |
| 69 | |
| 70 | self.file_contents = 'file with\nzero \\t errors \\r\\n' |
| 71 | self.failIf(PRESUBMIT.CheckNoCrOrTabs(api, MockOutputApi)) |
| 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | unittest.main() |