2011-03-29 David Levin <levin@chromium.org>
[WebKit-https.git] / Tools / Scripts / webkitpy / style / checkers / changelog_unittest.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2010 Apple Inc. All rights reserved.
4 # Copyright (C) 2011 Patrick Gansterer <paroga@paroga.com>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1.  Redistributions of source code must retain the above copyright
10 #     notice, this list of conditions and the following disclaimer.
11 # 2.  Redistributions in binary form must reproduce the above copyright
12 #     notice, this list of conditions and the following disclaimer in the
13 #     documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
19 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 """Unit test for changelog.py."""
27
28 import changelog
29 import unittest
30
31
32 class ChangeLogCheckerTest(unittest.TestCase):
33     """Tests ChangeLogChecker class."""
34
35     def assert_no_error(self, lines_to_check, changelog_data):
36         def handle_style_error(line_number, category, confidence, message):
37             self.fail('Unexpected error: %d %s %d %s for\n%s' % (line_number, category, confidence, message, changelog_data))
38         self.lines_to_check = set(lines_to_check)
39         checker = changelog.ChangeLogChecker('ChangeLog', handle_style_error, self.mock_should_line_be_checked)
40         checker.check(changelog_data.split('\n'))
41
42     def assert_error(self, expected_line_number, lines_to_check, expected_category, changelog_data):
43         self.had_error = False
44
45         def handle_style_error(line_number, category, confidence, message):
46             self.had_error = True
47             self.assertEquals(expected_line_number, line_number)
48             self.assertEquals(expected_category, category)
49         self.lines_to_check = set(lines_to_check)
50         checker = changelog.ChangeLogChecker('ChangeLog', handle_style_error, self.mock_should_line_be_checked)
51         checker.check(changelog_data.split('\n'))
52         self.assertTrue(self.had_error)
53
54     def mock_handle_style_error(self):
55         pass
56
57     def mock_should_line_be_checked(self, line_number):
58         return line_number in self.lines_to_check
59
60     def test_init(self):
61         checker = changelog.ChangeLogChecker('ChangeLog', self.mock_handle_style_error, self.mock_should_line_be_checked)
62         self.assertEquals(checker.file_path, 'ChangeLog')
63         self.assertEquals(checker.handle_style_error, self.mock_handle_style_error)
64         self.assertEquals(checker.should_line_be_checked, self.mock_should_line_be_checked)
65
66     def test_missing_bug_number(self):
67         self.assert_error(1, range(1, 20), 'changelog/bugnumber',
68                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
69                           '\n'
70                           '        Example bug\n')
71         self.assert_error(1, range(1, 20), 'changelog/bugnumber',
72                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
73                           '\n'
74                           '        Example bug\n'
75                           '        http://bugs.webkit.org/show_bug.cgi?id=\n')
76         self.assert_error(1, range(1, 20), 'changelog/bugnumber',
77                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
78                           '\n'
79                           '        Example bug\n'
80                           '        https://bugs.webkit.org/show_bug.cgi?id=\n')
81         self.assert_error(1, range(1, 20), 'changelog/bugnumber',
82                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
83                           '\n'
84                           '        Example bug\n'
85                           '        http://webkit.org/b/\n')
86         self.assert_error(1, range(1, 20), 'changelog/bugnumber',
87                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
88                           '\n'
89                           '        Example bug'
90                           '\n'
91                           '        http://trac.webkit.org/changeset/12345\n')
92         self.assert_error(2, range(2, 5), 'changelog/bugnumber',
93                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
94                           '        Example bug\n'
95                           '        https://bugs.webkit.org/show_bug.cgi\n'
96                           '\n'
97                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
98                           '        Another change\n')
99         self.assert_error(2, range(2, 6), 'changelog/bugnumber',
100                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
101                           '        Example bug\n'
102                           '        More text about bug.\n'
103                           '\n'
104                           '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
105                           '\n'
106                           '        No bug in this change.\n')
107
108     def test_no_error(self):
109         self.assert_no_error([],
110                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
111                              '\n'
112                              '        Example ChangeLog entry out of range\n'
113                              '        http://example.com/\n')
114         self.assert_no_error([],
115                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
116                              '\n'
117                              '        Example bug\n'
118                              '        http://bugs.webkit.org/show_bug.cgi?id=12345\n')
119         self.assert_no_error(range(1, 20),
120                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
121                              '\n'
122                              '        Example bug\n'
123                              '        http://bugs.webkit.org/show_bug.cgi?id=12345\n')
124         self.assert_no_error(range(1, 20),
125                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
126                              '\n'
127                              '        Example bug\n'
128                              '        https://bugs.webkit.org/show_bug.cgi?id=12345\n')
129         self.assert_no_error(range(1, 20),
130                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
131                              '\n'
132                              '        Example bug\n'
133                              '        http://webkit.org/b/12345\n')
134         self.assert_no_error(range(1, 20),
135                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
136                              '\n'
137                              '        Unreview build fix for r12345.\n')
138         self.assert_no_error(range(1, 20),
139                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
140                              '\n'
141                              '        Fix build after a bad change.\n')
142         self.assert_no_error(range(1, 20),
143                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
144                              '\n'
145                              '        Fix example port build.\n')
146         self.assert_no_error(range(2, 6),
147                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
148                              '        Example bug\n'
149                              '        https://bugs.webkit.org/show_bug.cgi?id=12345\n'
150                              '\n'
151                              '2011-01-01  Patrick Gansterer  <paroga@paroga.com>\n'
152                              '        No bug here!\n')
153
154 if __name__ == '__main__':
155     unittest.main()