1 # Copyright (C) 2012 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
31 import unittest2 as unittest
33 from webkitpy.common.host_mock import MockHost
34 from webkitpy.layout_tests import lint_test_expectations
37 class FakePort(object):
38 def __init__(self, host, name, path):
43 def test_configuration(self):
46 def expectations_dict(self):
47 self.host.ports_parsed.append(self.name)
48 return {self.path: ''}
50 def skipped_layout_tests(self, _):
53 def all_test_configurations(self):
56 def configuration_specifier_macros(self):
59 def get_option(self, _, val):
62 def path_to_generic_test_expectations_file(self):
65 class FakeFactory(object):
66 def __init__(self, host, ports):
70 self.ports[port.name] = port
72 def get(self, port_name, *args, **kwargs): # pylint: disable=W0613,E0202
73 return self.ports[port_name]
75 def all_port_names(self, platform=None): # pylint: disable=W0613,E0202
76 return sorted(self.ports.keys())
79 class LintTest(unittest.TestCase):
80 def test_all_configurations(self):
82 host.ports_parsed = []
83 host.port_factory = FakeFactory(host, (FakePort(host, 'a', 'path-to-a'),
84 FakePort(host, 'b', 'path-to-b'),
85 FakePort(host, 'b-win', 'path-to-b')))
87 logging_stream = StringIO.StringIO()
88 options = optparse.Values({'platform': None})
89 res = lint_test_expectations.lint(host, options, logging_stream)
90 self.assertEqual(res, 0)
91 self.assertEqual(host.ports_parsed, ['a', 'b', 'b-win'])
93 def test_lint_test_files(self):
94 logging_stream = StringIO.StringIO()
95 options = optparse.Values({'platform': 'test-mac-leopard'})
98 # pylint appears to complain incorrectly about the method overrides pylint: disable=E0202,C0322
99 # FIXME: incorrect complaints about spacing pylint: disable=C0322
100 host.port_factory.all_port_names = lambda platform=None: [platform]
102 res = lint_test_expectations.lint(host, options, logging_stream)
104 self.assertEqual(res, 0)
105 self.assertIn('Lint succeeded', logging_stream.getvalue())
107 def test_lint_test_files__errors(self):
108 options = optparse.Values({'platform': 'test', 'debug_rwt_logging': False})
111 # FIXME: incorrect complaints about spacing pylint: disable=C0322
112 port = host.port_factory.get(options.platform, options=options)
113 port.expectations_dict = lambda: {'foo': '-- syntax error1', 'bar': '-- syntax error2'}
115 host.port_factory.get = lambda platform, options=None: port
116 host.port_factory.all_port_names = lambda platform=None: [port.name()]
118 logging_stream = StringIO.StringIO()
120 res = lint_test_expectations.lint(host, options, logging_stream)
122 self.assertEqual(res, -1)
123 self.assertIn('Lint failed', logging_stream.getvalue())
124 self.assertIn('foo:1', logging_stream.getvalue())
125 self.assertIn('bar:1', logging_stream.getvalue())
128 class MainTest(unittest.TestCase):
129 def test_success(self):
130 orig_lint_fn = lint_test_expectations.lint
132 # unused args pylint: disable=W0613
133 def interrupting_lint(host, options, logging_stream):
134 raise KeyboardInterrupt
136 def successful_lint(host, options, logging_stream):
139 def exception_raising_lint(host, options, logging_stream):
142 stdout = StringIO.StringIO()
143 stderr = StringIO.StringIO()
145 lint_test_expectations.lint = interrupting_lint
146 res = lint_test_expectations.main([], stdout, stderr)
147 self.assertEqual(res, lint_test_expectations.INTERRUPTED_EXIT_STATUS)
149 lint_test_expectations.lint = successful_lint
150 res = lint_test_expectations.main(['--platform', 'test'], stdout, stderr)
151 self.assertEqual(res, 0)
153 lint_test_expectations.lint = exception_raising_lint
154 res = lint_test_expectations.main([], stdout, stderr)
155 self.assertEqual(res, lint_test_expectations.EXCEPTIONAL_EXIT_STATUS)
157 lint_test_expectations.lint = orig_lint_fn