finder_unittest.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. import logging
  23. import unittest2 as unittest
  24. from webkitpy.common.system.filesystem_mock import MockFileSystem
  25. from webkitpy.common.system.outputcapture import OutputCapture
  26. from webkitpy.test.finder import Finder
  27. class FinderTest(unittest.TestCase):
  28. def setUp(self):
  29. files = {
  30. '/foo/bar/baz.py': '',
  31. '/foo/bar/baz_unittest.py': '',
  32. '/foo2/bar2/baz2.py': '',
  33. '/foo2/bar2/baz2.pyc': '',
  34. '/foo2/bar2/baz2_integrationtest.py': '',
  35. '/foo2/bar2/missing.pyc': '',
  36. '/tmp/another_unittest.py': '',
  37. }
  38. self.fs = MockFileSystem(files)
  39. self.finder = Finder(self.fs)
  40. self.finder.add_tree('/foo', 'bar')
  41. self.finder.add_tree('/foo2')
  42. # Here we have to jump through a hoop to make sure test-webkitpy doesn't log
  43. # any messages from these tests :(.
  44. self.root_logger = logging.getLogger()
  45. self.log_levels = []
  46. self.log_handlers = self.root_logger.handlers[:]
  47. for handler in self.log_handlers:
  48. self.log_levels.append(handler.level)
  49. handler.level = logging.CRITICAL
  50. def tearDown(self):
  51. for handler in self.log_handlers:
  52. handler.level = self.log_levels.pop(0)
  53. def test_additional_system_paths(self):
  54. self.assertEqual(self.finder.additional_paths(['/usr']),
  55. ['/foo', '/foo2'])
  56. def test_is_module(self):
  57. self.assertTrue(self.finder.is_module('bar.baz'))
  58. self.assertTrue(self.finder.is_module('bar2.baz2'))
  59. self.assertTrue(self.finder.is_module('bar2.baz2_integrationtest'))
  60. # Missing the proper namespace.
  61. self.assertFalse(self.finder.is_module('baz'))
  62. def test_to_module(self):
  63. self.assertEqual(self.finder.to_module('/foo/test.py'), 'test')
  64. self.assertEqual(self.finder.to_module('/foo/bar/test.py'), 'bar.test')
  65. self.assertEqual(self.finder.to_module('/foo/bar/pytest.py'), 'bar.pytest')
  66. def test_clean(self):
  67. self.assertTrue(self.fs.exists('/foo2/bar2/missing.pyc'))
  68. self.finder.clean_trees()
  69. self.assertFalse(self.fs.exists('/foo2/bar2/missing.pyc'))
  70. def check_names(self, names, expected_names, find_all=True):
  71. self.assertEqual(self.finder.find_names(names, find_all), expected_names)
  72. def test_default_names(self):
  73. self.check_names([], ['bar.baz_unittest', 'bar2.baz2_integrationtest'], find_all=True)
  74. self.check_names([], ['bar.baz_unittest', 'bar2.baz2_integrationtest'], find_all=False)
  75. # Should return the names given it, even if they don't exist.
  76. self.check_names(['foobar'], ['foobar'], find_all=False)
  77. def test_paths(self):
  78. self.fs.chdir('/foo/bar')
  79. self.check_names(['baz_unittest.py'], ['bar.baz_unittest'])
  80. self.check_names(['./baz_unittest.py'], ['bar.baz_unittest'])
  81. self.check_names(['/foo/bar/baz_unittest.py'], ['bar.baz_unittest'])
  82. self.check_names(['.'], ['bar.baz_unittest'])
  83. self.check_names(['../../foo2/bar2'], ['bar2.baz2_integrationtest'])
  84. self.fs.chdir('/')
  85. self.check_names(['bar'], ['bar.baz_unittest'])
  86. self.check_names(['/foo/bar/'], ['bar.baz_unittest'])
  87. # This works 'by accident' since it maps onto a package.
  88. self.check_names(['bar/'], ['bar.baz_unittest'])
  89. # This should log an error, since it's outside the trees.
  90. oc = OutputCapture()
  91. oc.set_log_level(logging.ERROR)
  92. oc.capture_output()
  93. try:
  94. self.check_names(['/tmp/another_unittest.py'], [])
  95. finally:
  96. _, _, logs = oc.restore_output()
  97. self.assertIn('another_unittest.py', logs)
  98. # Paths that don't exist are errors.
  99. oc.capture_output()
  100. try:
  101. self.check_names(['/foo/bar/notexist_unittest.py'], [])
  102. finally:
  103. _, _, logs = oc.restore_output()
  104. self.assertIn('notexist_unittest.py', logs)
  105. # Names that don't exist are caught later, at load time.
  106. self.check_names(['bar.notexist_unittest'], ['bar.notexist_unittest'])