blob: 5c4398d07c5b85c67e9d4a9dbacebe8e24d49b8a [file] [log] [blame]
Jon Salz98255932012-08-18 14:48:02 +08001#!/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
7import mox
8import unittest
9
10
11# pylint: disable=W0212
12
13pre_upload = __import__('pre-upload')
14
15
16class 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
25class 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
59
60if __name__ == '__main__':
61 unittest.main()