1# Copyright (C) 2009 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#    * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#    * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#    * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29from __future__ import with_statement
30
31import codecs
32import os
33import tempfile
34import unittest
35
36from StringIO import StringIO
37
38from webkitpy.common.checkout.changelog import *
39
40
41class ChangeLogTest(unittest.TestCase):
42
43    _example_entry = u'''2009-08-17  Peter Kasting  <pkasting@google.com>
44
45        Reviewed by Tor Arne Vestb\xf8.
46
47        https://bugs.webkit.org/show_bug.cgi?id=27323
48        Only add Cygwin to the path when it isn't already there.  This avoids
49        causing problems for people who purposefully have non-Cygwin versions of
50        executables like svn in front of the Cygwin ones in their paths.
51
52        * DumpRenderTree/win/DumpRenderTree.vcproj:
53        * DumpRenderTree/win/ImageDiff.vcproj:
54        * DumpRenderTree/win/TestNetscapePlugin/TestNetscapePlugin.vcproj:
55'''
56
57    _rolled_over_footer = '== Rolled over to ChangeLog-2009-06-16 =='
58
59    # More example text than we need.  Eventually we need to support parsing this all and write tests for the parsing.
60    _example_changelog = u"""2009-08-17  Tor Arne Vestb\xf8  <vestbo@webkit.org>
61
62        <http://webkit.org/b/28393> check-webkit-style: add check for use of std::max()/std::min() instead of MAX()/MIN()
63
64        Reviewed by David Levin.
65
66        * Scripts/modules/cpp_style.py:
67        (_ERROR_CATEGORIES): Added 'runtime/max_min_macros'.
68        (check_max_min_macros): Added.  Returns level 4 error when MAX()
69        and MIN() macros are used in header files and C++ source files.
70        (check_style): Added call to check_max_min_macros().
71        * Scripts/modules/cpp_style_unittest.py: Added unit tests.
72        (test_max_macro): Added.
73        (test_min_macro): Added.
74
752009-08-16  David Kilzer  <ddkilzer@apple.com>
76
77        Backed out r47343 which was mistakenly committed
78
79        * Scripts/bugzilla-tool:
80        * Scripts/modules/scm.py:
81
822009-06-18  Darin Adler  <darin@apple.com>
83
84        Rubber stamped by Mark Rowe.
85
86        * DumpRenderTree/mac/DumpRenderTreeWindow.mm:
87        (-[DumpRenderTreeWindow close]): Resolved crashes seen during regression
88        tests. The close method can be called on a window that's already closed
89        so we can't assert here.
90
91== Rolled over to ChangeLog-2009-06-16 ==
92"""
93
94    def test_latest_entry_parse(self):
95        changelog_contents = u"%s\n%s" % (self._example_entry, self._example_changelog)
96        changelog_file = StringIO(changelog_contents)
97        latest_entry = ChangeLog.parse_latest_entry_from_file(changelog_file)
98        self.assertEquals(latest_entry.contents(), self._example_entry)
99        self.assertEquals(latest_entry.author_name(), "Peter Kasting")
100        self.assertEquals(latest_entry.author_email(), "pkasting@google.com")
101        self.assertEquals(latest_entry.reviewer_text(), u"Tor Arne Vestb\xf8")
102        self.assertTrue(latest_entry.reviewer())  # Make sure that our UTF8-based lookup of Tor works.
103
104    def test_latest_entry_parse_single_entry(self):
105        changelog_contents = u"%s\n%s" % (self._example_entry, self._rolled_over_footer)
106        changelog_file = StringIO(changelog_contents)
107        latest_entry = ChangeLog.parse_latest_entry_from_file(changelog_file)
108        self.assertEquals(latest_entry.contents(), self._example_entry)
109        self.assertEquals(latest_entry.author_name(), "Peter Kasting")
110
111    @staticmethod
112    def _write_tmp_file_with_contents(byte_array):
113        assert(isinstance(byte_array, str))
114        (file_descriptor, file_path) = tempfile.mkstemp() # NamedTemporaryFile always deletes the file on close in python < 2.6
115        with os.fdopen(file_descriptor, "w") as file:
116            file.write(byte_array)
117        return file_path
118
119    @staticmethod
120    def _read_file_contents(file_path, encoding):
121        with codecs.open(file_path, "r", encoding) as file:
122            return file.read()
123
124    _new_entry_boilerplate = '''2009-08-19  Eric Seidel  <eric@webkit.org>
125
126        Reviewed by NOBODY (OOPS!).
127
128        Need a short description and bug URL (OOPS!)
129
130        * Scripts/bugzilla-tool:
131'''
132
133    def test_set_reviewer(self):
134        changelog_contents = u"%s\n%s" % (self._new_entry_boilerplate, self._example_changelog)
135        changelog_path = self._write_tmp_file_with_contents(changelog_contents.encode("utf-8"))
136        reviewer_name = 'Test Reviewer'
137        ChangeLog(changelog_path).set_reviewer(reviewer_name)
138        actual_contents = self._read_file_contents(changelog_path, "utf-8")
139        expected_contents = changelog_contents.replace('NOBODY (OOPS!)', reviewer_name)
140        os.remove(changelog_path)
141        self.assertEquals(actual_contents, expected_contents)
142
143    def test_set_short_description_and_bug_url(self):
144        changelog_contents = u"%s\n%s" % (self._new_entry_boilerplate, self._example_changelog)
145        changelog_path = self._write_tmp_file_with_contents(changelog_contents.encode("utf-8"))
146        short_description = "A short description"
147        bug_url = "http://example.com/b/2344"
148        ChangeLog(changelog_path).set_short_description_and_bug_url(short_description, bug_url)
149        actual_contents = self._read_file_contents(changelog_path, "utf-8")
150        expected_message = "%s\n        %s" % (short_description, bug_url)
151        expected_contents = changelog_contents.replace("Need a short description and bug URL (OOPS!)", expected_message)
152        os.remove(changelog_path)
153        self.assertEquals(actual_contents, expected_contents)
154