1# Copyright (C) 2010 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
29import os
30import unittest
31
32from webkitpy.common.net.buildbot import Builder
33from webkitpy.common.system.outputcapture import OutputCapture
34from webkitpy.thirdparty.mock import Mock
35from webkitpy.tool.bot.sheriff import Sheriff
36from webkitpy.tool.mocktool import MockTool
37
38
39class MockSheriffBot(object):
40    name = "mock-sheriff-bot"
41    watchers = [
42        "watcher@example.com",
43    ]
44
45    def run_webkit_patch(self, args):
46        return "Created bug https://bugs.webkit.org/show_bug.cgi?id=36936\n"
47
48
49class SheriffTest(unittest.TestCase):
50    def test_post_blame_comment_on_bug(self):
51        def run():
52            sheriff = Sheriff(MockTool(), MockSheriffBot())
53            builders = [
54                Builder("Foo", None),
55                Builder("Bar", None),
56            ]
57            commit_info = Mock()
58            commit_info.bug_id = lambda: None
59            commit_info.revision = lambda: 4321
60            # Should do nothing with no bug_id
61            sheriff.post_blame_comment_on_bug(commit_info, builders, [])
62            sheriff.post_blame_comment_on_bug(commit_info, builders, ["mock-test-1", "mock-test-2"])
63            # Should try to post a comment to the bug, but MockTool.bugs does nothing.
64            commit_info.bug_id = lambda: 1234
65            sheriff.post_blame_comment_on_bug(commit_info, builders, [])
66            sheriff.post_blame_comment_on_bug(commit_info, builders, ["mock-test-1"])
67            sheriff.post_blame_comment_on_bug(commit_info, builders, ["mock-test-1", "mock-test-2"])
68
69        expected_stderr = u"""MOCK bug comment: bug_id=1234, cc=['watcher@example.com']
70--- Begin comment ---
71http://trac.webkit.org/changeset/4321 might have broken Foo and Bar
72--- End comment ---
73
74MOCK bug comment: bug_id=1234, cc=['watcher@example.com']
75--- Begin comment ---
76http://trac.webkit.org/changeset/4321 might have broken Foo and Bar
77The following tests are not passing:
78mock-test-1
79--- End comment ---
80
81MOCK bug comment: bug_id=1234, cc=['watcher@example.com']
82--- Begin comment ---
83http://trac.webkit.org/changeset/4321 might have broken Foo and Bar
84The following tests are not passing:
85mock-test-1
86mock-test-2
87--- End comment ---
88
89"""
90        OutputCapture().assert_outputs(self, run, expected_stderr=expected_stderr)
91