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
29import unittest
30
31from webkitpy.common.net.bugzilla import Attachment
32from webkitpy.common.system.outputcapture import OutputCapture
33from webkitpy.common.system.executive import ScriptError
34from webkitpy.thirdparty.mock import Mock
35from webkitpy.tool.commands.stepsequence import StepSequenceErrorHandler
36from webkitpy.tool.mocktool import MockTool
37
38
39class MockQueueEngine(object):
40    def __init__(self, name, queue, wakeup_event):
41        pass
42
43    def run(self):
44        pass
45
46
47class QueuesTest(unittest.TestCase):
48    # This is _patch1 in mocktool.py
49    mock_work_item = MockTool().bugs.fetch_attachment(197)
50
51    def assert_outputs(self, func, func_name, args, expected_stdout, expected_stderr, expected_exceptions):
52        exception = None
53        if expected_exceptions and func_name in expected_exceptions:
54            exception = expected_exceptions[func_name]
55
56        OutputCapture().assert_outputs(self,
57                func,
58                args=args,
59                expected_stdout=expected_stdout.get(func_name, ""),
60                expected_stderr=expected_stderr.get(func_name, ""),
61                expected_exception=exception)
62
63    def _default_begin_work_queue_stderr(self, name, checkout_dir):
64        string_replacements = {"name": name, 'checkout_dir': checkout_dir}
65        return "CAUTION: %(name)s will discard all local changes in \"%(checkout_dir)s\"\nRunning WebKit %(name)s.\nMOCK: update_status: %(name)s Starting Queue\n" % string_replacements
66
67    def assert_queue_outputs(self, queue, args=None, work_item=None, expected_stdout=None, expected_stderr=None, expected_exceptions=None, options=None, tool=None):
68        if not tool:
69            tool = MockTool()
70            # This is a hack to make it easy for callers to not have to setup a custom MockFileSystem just to test the commit-queue
71            # the cq tries to read the layout test results, and will hit a KeyError in MockFileSystem if we don't do this.
72            tool.filesystem.write_text_file('/mock/results.html', "")
73        if not expected_stdout:
74            expected_stdout = {}
75        if not expected_stderr:
76            expected_stderr = {}
77        if not args:
78            args = []
79        if not options:
80            options = Mock()
81            options.port = None
82        if not work_item:
83            work_item = self.mock_work_item
84        tool.user.prompt = lambda message: "yes"
85
86        queue.execute(options, args, tool, engine=MockQueueEngine)
87
88        self.assert_outputs(queue.queue_log_path, "queue_log_path", [], expected_stdout, expected_stderr, expected_exceptions)
89        self.assert_outputs(queue.work_item_log_path, "work_item_log_path", [work_item], expected_stdout, expected_stderr, expected_exceptions)
90        self.assert_outputs(queue.begin_work_queue, "begin_work_queue", [], expected_stdout, expected_stderr, expected_exceptions)
91        self.assert_outputs(queue.should_continue_work_queue, "should_continue_work_queue", [], expected_stdout, expected_stderr, expected_exceptions)
92        self.assert_outputs(queue.next_work_item, "next_work_item", [], expected_stdout, expected_stderr, expected_exceptions)
93        self.assert_outputs(queue.should_proceed_with_work_item, "should_proceed_with_work_item", [work_item], expected_stdout, expected_stderr, expected_exceptions)
94        self.assert_outputs(queue.process_work_item, "process_work_item", [work_item], expected_stdout, expected_stderr, expected_exceptions)
95        self.assert_outputs(queue.handle_unexpected_error, "handle_unexpected_error", [work_item, "Mock error message"], expected_stdout, expected_stderr, expected_exceptions)
96        # Should we have a different function for testing StepSequenceErrorHandlers?
97        if isinstance(queue, StepSequenceErrorHandler):
98            self.assert_outputs(queue.handle_script_error, "handle_script_error", [tool, {"patch": self.mock_work_item}, ScriptError(message="ScriptError error message", script_args="MockErrorCommand")], expected_stdout, expected_stderr, expected_exceptions)
99