Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import mox |
| 8 | import unittest |
| 9 | |
| 10 | |
| 11 | # pylint: disable=W0212 |
| 12 | |
| 13 | pre_upload = __import__('pre-upload') |
| 14 | |
| 15 | |
| 16 | class TryUTF8DecodeTest(unittest.TestCase): |
| 17 | def runTest(self): |
| 18 | self.assertEquals(u'', pre_upload._try_utf8_decode('')) |
| 19 | self.assertEquals(u'abc', pre_upload._try_utf8_decode('abc')) |
| 20 | self.assertEquals(u'你好布萊恩', pre_upload._try_utf8_decode('你好布萊恩')) |
| 21 | # Invalid UTF-8 |
| 22 | self.assertEquals('\x80', pre_upload._try_utf8_decode('\x80')) |
| 23 | |
| 24 | |
| 25 | class CheckNoLongLinesTest(unittest.TestCase): |
| 26 | def setUp(self): |
| 27 | self.mocker = mox.Mox() |
| 28 | self.mocker.StubOutWithMock(pre_upload, '_filter_files') |
| 29 | self.mocker.StubOutWithMock(pre_upload, '_get_affected_files') |
| 30 | self.mocker.StubOutWithMock(pre_upload, '_get_file_diff') |
| 31 | pre_upload._get_affected_files(mox.IgnoreArg()).AndReturn(['x.py']) |
| 32 | pre_upload._filter_files( |
| 33 | ['x.py'], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(['x.py']) |
| 34 | |
| 35 | def tearDown(self): |
| 36 | self.mocker.UnsetStubs() |
| 37 | self.mocker.VerifyAll() |
| 38 | |
| 39 | def runTest(self): |
| 40 | pre_upload._get_file_diff(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 41 | [(1, u"x" * 80), # OK |
| 42 | (2, "\x80" * 80), # OK |
| 43 | (3, u"x" * 81), # Too long |
| 44 | (4, "\x80" * 81), # Too long |
| 45 | (5, u"See http://" + (u"x" * 80)), # OK (URL) |
| 46 | (6, u"See https://" + (u"x" * 80)), # OK (URL) |
| 47 | (7, u"# define " + (u"x" * 80)), # OK (compiler directive) |
| 48 | (8, u"#define" + (u"x" * 74)), # Too long |
| 49 | ]) |
| 50 | self.mocker.ReplayAll() |
| 51 | failure = pre_upload._check_no_long_lines('PROJECT', 'COMMIT') |
| 52 | self.assertTrue(failure) |
| 53 | self.assertEquals('Found lines longer than 80 characters (first 5 shown):', |
| 54 | failure.msg) |
| 55 | self.assertEquals(['x.py, line %d, 81 chars' % line |
| 56 | for line in [3, 4, 8]], |
| 57 | failure.items) |
| 58 | |
Olof Johansson | a96810f | 2012-09-04 16:20:03 -0700 | [diff] [blame] | 59 | class CheckKernelConfig(unittest.TestCase): |
| 60 | def tearDown(self): |
| 61 | self.mocker.UnsetStubs() |
| 62 | |
| 63 | def runTest(self): |
| 64 | self.mocker = mox.Mox(); |
| 65 | |
| 66 | # Mixed changes, should fail |
| 67 | self.mocker.StubOutWithMock(pre_upload, '_get_affected_files') |
| 68 | pre_upload._get_affected_files(mox.IgnoreArg()).AndReturn( |
| 69 | ['/kernel/files/chromeos/config/base.config', |
| 70 | '/kernel/files/arch/arm/mach-exynos/mach-exynos5-dt.c' |
| 71 | ]) |
| 72 | self.mocker.ReplayAll() |
| 73 | failure = pre_upload._kernel_configcheck('PROJECT', 'COMMIT') |
| 74 | self.assertTrue(failure) |
| 75 | |
| 76 | # Code-only changes, should pass |
| 77 | self.mocker.UnsetStubs() |
| 78 | self.mocker.StubOutWithMock(pre_upload, '_get_affected_files') |
| 79 | pre_upload._get_affected_files(mox.IgnoreArg()).AndReturn( |
| 80 | ['/kernel/files/Makefile', |
| 81 | '/kernel/files/arch/arm/mach-exynos/mach-exynos5-dt.c' |
| 82 | ]) |
| 83 | self.mocker.ReplayAll() |
| 84 | failure = pre_upload._kernel_configcheck('PROJECT', 'COMMIT') |
| 85 | self.assertFalse(failure) |
| 86 | |
| 87 | # Config-only changes, should pass |
| 88 | self.mocker.UnsetStubs() |
| 89 | self.mocker.StubOutWithMock(pre_upload, '_get_affected_files') |
| 90 | pre_upload._get_affected_files(mox.IgnoreArg()).AndReturn( |
| 91 | ['/kernel/files/chromeos/config/base.config', |
| 92 | ]) |
| 93 | self.mocker.ReplayAll() |
| 94 | failure = pre_upload._kernel_configcheck('PROJECT', 'COMMIT') |
| 95 | self.assertFalse(failure) |
| 96 | |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 97 | |
| 98 | if __name__ == '__main__': |
| 99 | unittest.main() |