config_unittest.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright (C) 2010 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 os
  29. import sys
  30. import unittest2 as unittest
  31. from webkitpy.common.system.executive import Executive, ScriptError
  32. from webkitpy.common.system.executive_mock import MockExecutive2
  33. from webkitpy.common.system.filesystem import FileSystem
  34. from webkitpy.common.system.filesystem_mock import MockFileSystem
  35. from webkitpy.common.system.outputcapture import OutputCapture
  36. from webkitpy.common.webkit_finder import WebKitFinder
  37. import config
  38. class ConfigTest(unittest.TestCase):
  39. def setUp(self):
  40. config.clear_cached_configuration()
  41. def tearDown(self):
  42. config.clear_cached_configuration()
  43. def make_config(self, output='', files=None, exit_code=0, exception=None, run_command_fn=None, stderr='', port_implementation=None):
  44. e = MockExecutive2(output=output, exit_code=exit_code, exception=exception, run_command_fn=run_command_fn, stderr=stderr)
  45. fs = MockFileSystem(files)
  46. return config.Config(e, fs, port_implementation=port_implementation)
  47. def assert_configuration(self, contents, expected):
  48. # This tests that a configuration file containing
  49. # _contents_ ends up being interpreted as _expected_.
  50. output = 'foo\nfoo/%s' % contents
  51. c = self.make_config(output, {'foo/Configuration': contents})
  52. self.assertEqual(c.default_configuration(), expected)
  53. def test_build_directory(self):
  54. # --top-level
  55. def mock_webkit_build_directory(arg_list):
  56. if arg_list == ['--top-level']:
  57. return '/WebKitBuild/'
  58. elif arg_list == ['--configuration', '--debug']:
  59. return '/WebKitBuild/Debug'
  60. elif arg_list == ['--configuration', '--release']:
  61. return '/WebKitBuild/Release'
  62. elif arg_list == []:
  63. return '/WebKitBuild/\n/WebKitBuild//Debug\n'
  64. return 'Error'
  65. def mock_run_command(arg_list):
  66. if 'webkit-build-directory' in arg_list[1]:
  67. return mock_webkit_build_directory(arg_list[2:])
  68. return 'Error'
  69. c = self.make_config(run_command_fn=mock_run_command)
  70. self.assertEqual(c.build_directory(None), '/WebKitBuild/')
  71. # Test again to check caching
  72. self.assertEqual(c.build_directory(None), '/WebKitBuild/')
  73. # Test other values
  74. self.assertTrue(c.build_directory('Release').endswith('/Release'))
  75. self.assertTrue(c.build_directory('Debug').endswith('/Debug'))
  76. self.assertRaises(KeyError, c.build_directory, 'Unknown')
  77. # Test that stderr output from webkit-build-directory won't mangle the build dir
  78. c = self.make_config(output='/WebKitBuild/', stderr="mock stderr output from webkit-build-directory")
  79. self.assertEqual(c.build_directory(None), '/WebKitBuild/')
  80. def test_build_directory_passes_port_implementation(self):
  81. def mock_run_command(arg_list):
  82. self.assetEquals('--gtk' in arg_list)
  83. return '/tmp'
  84. c = self.make_config(run_command_fn=mock_run_command, port_implementation='gtk')
  85. def test_default_configuration__release(self):
  86. self.assert_configuration('Release', 'Release')
  87. def test_default_configuration__debug(self):
  88. self.assert_configuration('Debug', 'Debug')
  89. def test_default_configuration__deployment(self):
  90. self.assert_configuration('Deployment', 'Release')
  91. def test_default_configuration__development(self):
  92. self.assert_configuration('Development', 'Debug')
  93. def test_default_configuration__notfound(self):
  94. # This tests what happens if the default configuration file doesn't exist.
  95. c = self.make_config(output='foo\nfoo/Release', files={'foo/Configuration': None})
  96. self.assertEqual(c.default_configuration(), "Release")
  97. def test_default_configuration__unknown(self):
  98. # Ignore the warning about an unknown configuration value.
  99. oc = OutputCapture()
  100. oc.capture_output()
  101. self.assert_configuration('Unknown', 'Unknown')
  102. oc.restore_output()
  103. def test_default_configuration__standalone(self):
  104. # FIXME: This test runs a standalone python script to test
  105. # reading the default configuration to work around any possible
  106. # caching / reset bugs. See https://bugs.webkit.org/show_bug.cgi?id=49360
  107. # for the motivation. We can remove this test when we remove the
  108. # global configuration cache in config.py.
  109. e = Executive()
  110. fs = FileSystem()
  111. c = config.Config(e, fs)
  112. script = WebKitFinder(fs).path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'port', 'config_standalone.py')
  113. # Note: don't use 'Release' here, since that's the normal default.
  114. expected = 'Debug'
  115. args = [sys.executable, script, '--mock', expected]
  116. actual = e.run_command(args).rstrip()
  117. self.assertEqual(actual, expected)
  118. def test_default_configuration__no_perl(self):
  119. # We need perl to run webkit-build-directory to find out where the
  120. # default configuration file is. See what happens if perl isn't
  121. # installed. (We should get the default value, 'Release').
  122. c = self.make_config(exception=OSError)
  123. actual = c.default_configuration()
  124. self.assertEqual(actual, 'Release')
  125. def test_default_configuration__scripterror(self):
  126. # We run webkit-build-directory to find out where the default
  127. # configuration file is. See what happens if that script fails.
  128. # (We should get the default value, 'Release').
  129. c = self.make_config(exception=ScriptError())
  130. actual = c.default_configuration()
  131. self.assertEqual(actual, 'Release')