6bae72222bc3936ac7df2d682dc9fe561a61b7a1
[WebKit-https.git] / WebKitTools / Scripts / modules / commands / queries_unittest.py
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
29 import unittest
30 from StringIO import StringIO
31
32 from modules.commands.queries import *
33 from modules.mock_bugzillatool import *
34
35 class QueryCommandsTest(unittest.TestCase):
36     def _capture_output_with_name(output_name):
37         self.saved_outputs[output_name] = getattr(sys, output_name)
38         setattr(sys, output_name, StringIO.StringIO())
39
40     def _release_output_with_name(output_name):
41         captured_output = getattr(sys, output_name).getvalue()
42         setattr(sys, output_name, self.saved_outputs[output_name])
43         del self.saved_outputs[output_name]
44         return captured_output
45
46     def _capture_output(self):
47         self._capture_output_with_name("stdout")
48         self._capture_output_with_name("stderr")
49
50     def _restore_output(self):
51         return (self._release_output_with_name("stdout"), self._release_output_with_name("stderr"))
52
53     def _assert_execute_outputs(self, command, command_args, expected_stdout, expected_stderr = ""):
54         self._capture_output()
55         command.execute(None, command_args, MockBugzillaTool())
56         (stdout_string, stderr_string) = self._restore_output()
57         self.assertEqual(stdout_string, expected_stdout)
58         self.assertEqual(expected_stderr, expected_stderr)
59
60     def test_bugs_to_commit(self):
61         self._assert_execute_outputs(BugsToCommit(), None, "42\n75\n")
62
63     def test_patches_to_commit(self):
64         expected_stdout = "http://example.com/197\nhttp://example.com/128\n"
65         expected_stderr = "Patches in commit queue:\n"
66         self._assert_execute_outputs(PatchesToCommit(), None, expected_stdout, expected_stderr)
67
68     def test_reviewed_patches(self):
69         expected_stdout = "http://example.com/197\nhttp://example.com/128\n"
70         self._assert_execute_outputs(ReviewedPatches(), [42], expected_stdout)
71
72     def test_tree_status(self):
73         expected_stdout = "ok   : Builder1\nok   : Builder2\n"
74         self._assert_execute_outputs(TreeStatus(), None, expected_stdout)