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 29import os 30import sys 31import unittest 32 33from webkitpy.common.system import executive 34from webkitpy.common.system import executive_mock 35from webkitpy.common.system import filesystem 36from webkitpy.common.system import filesystem_mock 37from webkitpy.common.system import outputcapture 38 39import config 40 41 42def mock_run_command(arg_list): 43 # Set this to True to test actual output (where possible). 44 integration_test = False 45 if integration_test: 46 return executive.Executive().run_command(arg_list) 47 48 if 'webkit-build-directory' in arg_list[1]: 49 return mock_webkit_build_directory(arg_list[2:]) 50 return 'Error' 51 52 53def mock_webkit_build_directory(arg_list): 54 if arg_list == ['--top-level']: 55 return '/WebKitBuild' 56 elif arg_list == ['--configuration', '--debug']: 57 return '/WebKitBuild/Debug' 58 elif arg_list == ['--configuration', '--release']: 59 return '/WebKitBuild/Release' 60 return 'Error' 61 62 63class ConfigTest(unittest.TestCase): 64 def tearDown(self): 65 config.clear_cached_configuration() 66 67 def make_config(self, output='', files={}, exit_code=0, exception=None, 68 run_command_fn=None): 69 e = executive_mock.MockExecutive2(output=output, exit_code=exit_code, 70 exception=exception, 71 run_command_fn=run_command_fn) 72 fs = filesystem_mock.MockFileSystem(files) 73 return config.Config(e, fs) 74 75 def assert_configuration(self, contents, expected): 76 # This tests that a configuration file containing 77 # _contents_ ends up being interpreted as _expected_. 78 c = self.make_config('foo', {'foo/Configuration': contents}) 79 self.assertEqual(c.default_configuration(), expected) 80 81 def test_build_directory(self): 82 # --top-level 83 c = self.make_config(run_command_fn=mock_run_command) 84 self.assertTrue(c.build_directory(None).endswith('WebKitBuild')) 85 86 # Test again to check caching 87 self.assertTrue(c.build_directory(None).endswith('WebKitBuild')) 88 89 # Test other values 90 self.assertTrue(c.build_directory('Release').endswith('/Release')) 91 self.assertTrue(c.build_directory('Debug').endswith('/Debug')) 92 self.assertRaises(KeyError, c.build_directory, 'Unknown') 93 94 def test_build_dumprendertree__success(self): 95 c = self.make_config(exit_code=0) 96 self.assertTrue(c.build_dumprendertree("Debug")) 97 self.assertTrue(c.build_dumprendertree("Release")) 98 self.assertRaises(KeyError, c.build_dumprendertree, "Unknown") 99 100 def test_build_dumprendertree__failure(self): 101 c = self.make_config(exit_code=-1) 102 103 # FIXME: Build failures should log errors. However, the message we 104 # get depends on how we're being called; as a standalone test, 105 # we'll get the "no handlers found" message. As part of 106 # test-webkitpy, we get the actual message. Really, we need 107 # outputcapture to install its own handler. 108 oc = outputcapture.OutputCapture() 109 oc.capture_output() 110 self.assertFalse(c.build_dumprendertree('Debug')) 111 oc.restore_output() 112 113 oc.capture_output() 114 self.assertFalse(c.build_dumprendertree('Release')) 115 oc.restore_output() 116 117 def test_default_configuration__release(self): 118 self.assert_configuration('Release', 'Release') 119 120 def test_default_configuration__debug(self): 121 self.assert_configuration('Debug', 'Debug') 122 123 def test_default_configuration__deployment(self): 124 self.assert_configuration('Deployment', 'Release') 125 126 def test_default_configuration__development(self): 127 self.assert_configuration('Development', 'Debug') 128 129 def test_default_configuration__notfound(self): 130 # This tests what happens if the default configuration file 131 # doesn't exist. 132 c = self.make_config(output='foo', files={'foo/Configuration': None}) 133 self.assertEqual(c.default_configuration(), "Release") 134 135 def test_default_configuration__unknown(self): 136 # Ignore the warning about an unknown configuration value. 137 oc = outputcapture.OutputCapture() 138 oc.capture_output() 139 self.assert_configuration('Unknown', 'Unknown') 140 oc.restore_output() 141 142 def test_default_configuration__standalone(self): 143 # FIXME: This test runs a standalone python script to test 144 # reading the default configuration to work around any possible 145 # caching / reset bugs. See https://bugs.webkit.org/show_bug?id=49360 146 # for the motivation. We can remove this test when we remove the 147 # global configuration cache in config.py. 148 e = executive.Executive() 149 fs = filesystem.FileSystem() 150 c = config.Config(e, fs) 151 script = c.path_from_webkit_base('Tools', 'Scripts', 152 'webkitpy', 'layout_tests', 'port', 'config_standalone.py') 153 154 # Note: don't use 'Release' here, since that's the normal default. 155 expected = 'Debug' 156 157 args = [sys.executable, script, '--mock', expected] 158 actual = e.run_command(args).rstrip() 159 self.assertEqual(actual, expected) 160 161 def test_default_configuration__no_perl(self): 162 # We need perl to run webkit-build-directory to find out where the 163 # default configuration file is. See what happens if perl isn't 164 # installed. (We should get the default value, 'Release'). 165 c = self.make_config(exception=OSError) 166 actual = c.default_configuration() 167 self.assertEqual(actual, 'Release') 168 169 def test_default_configuration__scripterror(self): 170 # We run webkit-build-directory to find out where the default 171 # configuration file is. See what happens if that script fails. 172 # (We should get the default value, 'Release'). 173 c = self.make_config(exception=executive.ScriptError()) 174 actual = c.default_configuration() 175 self.assertEqual(actual, 'Release') 176 177 def test_path_from_webkit_base(self): 178 # FIXME: We use a real filesystem here. Should this move to a 179 # mocked one? 180 c = config.Config(executive.Executive(), filesystem.FileSystem()) 181 self.assertTrue(c.path_from_webkit_base('foo')) 182 183 def test_webkit_base_dir(self): 184 # FIXME: We use a real filesystem here. Should this move to a 185 # mocked one? 186 c = config.Config(executive.Executive(), filesystem.FileSystem()) 187 base_dir = c.webkit_base_dir() 188 self.assertTrue(base_dir) 189 self.assertNotEqual(base_dir[-1], '/') 190 191 orig_cwd = os.getcwd() 192 if sys.platform == 'win32': 193 os.chdir(os.environ['USERPROFILE']) 194 else: 195 os.chdir(os.environ['HOME']) 196 c = config.Config(executive.Executive(), filesystem.FileSystem()) 197 try: 198 base_dir_2 = c.webkit_base_dir() 199 self.assertEqual(base_dir, base_dir_2) 200 finally: 201 os.chdir(orig_cwd) 202 203 204if __name__ == '__main__': 205 unittest.main() 206