2009-11-24 Eric Seidel <eric@webkit.org>
[WebKit-https.git] / WebKitTools / Scripts / modules / commands / queries_unittest.py
index 5e8ddc175d7409ce7cb3f57c3f030b5569195ff6..6bae72222bc3936ac7df2d682dc9fe561a61b7a1 100644 (file)
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 import unittest
+from StringIO import StringIO
 
 from modules.commands.queries import *
 from modules.mock_bugzillatool import *
 
 class QueryCommandsTest(unittest.TestCase):
+    def _capture_output_with_name(output_name):
+        self.saved_outputs[output_name] = getattr(sys, output_name)
+        setattr(sys, output_name, StringIO.StringIO())
+
+    def _release_output_with_name(output_name):
+        captured_output = getattr(sys, output_name).getvalue()
+        setattr(sys, output_name, self.saved_outputs[output_name])
+        del self.saved_outputs[output_name]
+        return captured_output
+
+    def _capture_output(self):
+        self._capture_output_with_name("stdout")
+        self._capture_output_with_name("stderr")
+
+    def _restore_output(self):
+        return (self._release_output_with_name("stdout"), self._release_output_with_name("stderr"))
+
+    def _assert_execute_outputs(self, command, command_args, expected_stdout, expected_stderr = ""):
+        self._capture_output()
+        command.execute(None, command_args, MockBugzillaTool())
+        (stdout_string, stderr_string) = self._restore_output()
+        self.assertEqual(stdout_string, expected_stdout)
+        self.assertEqual(expected_stderr, expected_stderr)
+
     def test_bugs_to_commit(self):
-        BugsToCommit().execute(None, None, MockBugzillaTool())
+        self._assert_execute_outputs(BugsToCommit(), None, "42\n75\n")
 
     def test_patches_to_commit(self):
-        PatchesToCommit().execute(None, None, MockBugzillaTool())
+        expected_stdout = "http://example.com/197\nhttp://example.com/128\n"
+        expected_stderr = "Patches in commit queue:\n"
+        self._assert_execute_outputs(PatchesToCommit(), None, expected_stdout, expected_stderr)
 
     def test_reviewed_patches(self):
-        args = [42]
-        ReviewedPatches().execute(None, args, MockBugzillaTool())
+        expected_stdout = "http://example.com/197\nhttp://example.com/128\n"
+        self._assert_execute_outputs(ReviewedPatches(), [42], expected_stdout)
 
     def test_tree_status(self):
-        TreeStatus().execute(None, None, MockBugzillaTool())
+        expected_stdout = "ok   : Builder1\nok   : Builder2\n"
+        self._assert_execute_outputs(TreeStatus(), None, expected_stdout)