1# Copyright (C) 2012 Google, Inc.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1.  Redistributions of source code must retain the above copyright
7#     notice, this list of conditions and the following disclaimer.
8# 2.  Redistributions in binary form must reproduce the above copyright
9#     notice, this list of conditions and the following disclaimer in the
10#     documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23import StringIO
24import logging
25import re
26import unittest
27
28from webkitpy.common.system.filesystem import FileSystem
29from webkitpy.common.webkit_finder import WebKitFinder
30
31from webkitpy.tool.mocktool import MockOptions
32from webkitpy.test.printer import Printer
33from webkitpy.test.runner import Runner
34
35
36class FakeModuleSuite(object):
37    def __init__(self, name, result, msg):
38        self.name = name
39        self.result = result
40        self.msg = msg
41
42    def __str__(self):
43        return self.name
44
45    def run(self, result):
46        result.testsRun += 1
47        if self.result == 'F':
48            result.failures.append((self.name, self.msg))
49        elif self.result == 'E':
50            result.errors.append((self.name, self.msg))
51
52
53class FakeTopSuite(object):
54    def __init__(self, tests):
55        self._tests = tests
56
57
58class FakeLoader(object):
59    def __init__(self, *test_triples):
60        self.triples = test_triples
61        self._tests = []
62        self._results = {}
63        for test_name, result, msg in self.triples:
64            self._tests.append(test_name)
65            m = re.match("(\w+) \(([\w.]+)\)", test_name)
66            self._results['%s.%s' % (m.group(2), m.group(1))] = tuple([test_name, result, msg])
67
68    def top_suite(self):
69        return FakeTopSuite(self._tests)
70
71    def loadTestsFromName(self, name, _):
72        return FakeModuleSuite(*self._results[name])
73
74
75class RunnerTest(unittest.TestCase):
76    def setUp(self):
77        # Here we have to jump through a hoop to make sure test-webkitpy doesn't log
78        # any messages from these tests :(.
79        self.root_logger = logging.getLogger()
80        self.log_levels = []
81        self.log_handlers = self.root_logger.handlers[:]
82        for handler in self.log_handlers:
83            self.log_levels.append(handler.level)
84            handler.level = logging.CRITICAL
85
86    def tearDown(self):
87        for handler in self.log_handlers:
88            handler.level = self.log_levels.pop(0)
89
90    def test_run(self, verbose=0, timing=False, child_processes=1, quiet=False):
91        options = MockOptions(verbose=verbose, timing=timing, child_processes=child_processes, quiet=quiet, pass_through=False)
92        stream = StringIO.StringIO()
93        loader = FakeLoader(('test1 (Foo)', '.', ''),
94                            ('test2 (Foo)', 'F', 'test2\nfailed'),
95                            ('test3 (Foo)', 'E', 'test3\nerred'))
96        runner = Runner(Printer(stream, options), loader, WebKitFinder(FileSystem()))
97        runner.run(['Foo.test1', 'Foo.test2', 'Foo.test3'], 1)
98        self.assertEqual(runner.tests_run, 3)
99        self.assertEqual(len(runner.failures), 1)
100        self.assertEqual(len(runner.errors), 1)
101