lint_test_expectations_unittest.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (C) 2012 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. import optparse
  29. import StringIO
  30. import unittest2 as unittest
  31. from webkitpy.common.host_mock import MockHost
  32. from webkitpy.layout_tests import lint_test_expectations
  33. class FakePort(object):
  34. def __init__(self, host, name, path):
  35. self.host = host
  36. self.name = name
  37. self.path = path
  38. def test_configuration(self):
  39. return None
  40. def expectations_dict(self):
  41. self.host.ports_parsed.append(self.name)
  42. return {self.path: ''}
  43. def skipped_layout_tests(self, _):
  44. return set([])
  45. def all_test_configurations(self):
  46. return []
  47. def configuration_specifier_macros(self):
  48. return []
  49. def get_option(self, _, val):
  50. return val
  51. def path_to_generic_test_expectations_file(self):
  52. return ''
  53. class FakeFactory(object):
  54. def __init__(self, host, ports):
  55. self.host = host
  56. self.ports = {}
  57. for port in ports:
  58. self.ports[port.name] = port
  59. def get(self, port_name, *args, **kwargs): # pylint: disable=W0613,E0202
  60. return self.ports[port_name]
  61. def all_port_names(self, platform=None): # pylint: disable=W0613,E0202
  62. return sorted(self.ports.keys())
  63. class LintTest(unittest.TestCase):
  64. def test_all_configurations(self):
  65. host = MockHost()
  66. host.ports_parsed = []
  67. host.port_factory = FakeFactory(host, (FakePort(host, 'a', 'path-to-a'),
  68. FakePort(host, 'b', 'path-to-b'),
  69. FakePort(host, 'b-win', 'path-to-b')))
  70. logging_stream = StringIO.StringIO()
  71. options = optparse.Values({'platform': None})
  72. res = lint_test_expectations.lint(host, options, logging_stream)
  73. self.assertEqual(res, 0)
  74. self.assertEqual(host.ports_parsed, ['a', 'b', 'b-win'])
  75. def test_lint_test_files(self):
  76. logging_stream = StringIO.StringIO()
  77. options = optparse.Values({'platform': 'test-mac-leopard'})
  78. host = MockHost()
  79. # pylint appears to complain incorrectly about the method overrides pylint: disable=E0202,C0322
  80. # FIXME: incorrect complaints about spacing pylint: disable=C0322
  81. host.port_factory.all_port_names = lambda platform=None: [platform]
  82. res = lint_test_expectations.lint(host, options, logging_stream)
  83. self.assertEqual(res, 0)
  84. self.assertIn('Lint succeeded', logging_stream.getvalue())
  85. def test_lint_test_files__errors(self):
  86. options = optparse.Values({'platform': 'test', 'debug_rwt_logging': False})
  87. host = MockHost()
  88. # FIXME: incorrect complaints about spacing pylint: disable=C0322
  89. port = host.port_factory.get(options.platform, options=options)
  90. port.expectations_dict = lambda: {'foo': '-- syntax error1', 'bar': '-- syntax error2'}
  91. host.port_factory.get = lambda platform, options=None: port
  92. host.port_factory.all_port_names = lambda platform=None: [port.name()]
  93. logging_stream = StringIO.StringIO()
  94. res = lint_test_expectations.lint(host, options, logging_stream)
  95. self.assertEqual(res, -1)
  96. self.assertIn('Lint failed', logging_stream.getvalue())
  97. self.assertIn('foo:1', logging_stream.getvalue())
  98. self.assertIn('bar:1', logging_stream.getvalue())
  99. class MainTest(unittest.TestCase):
  100. def test_success(self):
  101. orig_lint_fn = lint_test_expectations.lint
  102. # unused args pylint: disable=W0613
  103. def interrupting_lint(host, options, logging_stream):
  104. raise KeyboardInterrupt
  105. def successful_lint(host, options, logging_stream):
  106. return 0
  107. def exception_raising_lint(host, options, logging_stream):
  108. assert False
  109. stdout = StringIO.StringIO()
  110. stderr = StringIO.StringIO()
  111. try:
  112. lint_test_expectations.lint = interrupting_lint
  113. res = lint_test_expectations.main([], stdout, stderr)
  114. self.assertEqual(res, lint_test_expectations.INTERRUPTED_EXIT_STATUS)
  115. lint_test_expectations.lint = successful_lint
  116. res = lint_test_expectations.main(['--platform', 'test'], stdout, stderr)
  117. self.assertEqual(res, 0)
  118. lint_test_expectations.lint = exception_raising_lint
  119. res = lint_test_expectations.main([], stdout, stderr)
  120. self.assertEqual(res, lint_test_expectations.EXCEPTIONAL_EXIT_STATUS)
  121. finally:
  122. lint_test_expectations.lint = orig_lint_fn