1 # Copyright (C) 2010 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.
33 from webkitpy.common.system.executive import Executive, ScriptError
34 from webkitpy.common.system.executive_mock import MockExecutive, MockExecutive2
35 from webkitpy.common.system.filesystem import FileSystem
36 from webkitpy.common.system.filesystem_mock import MockFileSystem
37 from webkitpy.common.system.outputcapture import OutputCapture
42 class ConfigTest(unittest.TestCase):
44 config.clear_cached_configuration()
46 def make_config(self, output='', files=None, exit_code=0, exception=None, run_command_fn=None):
47 e = MockExecutive2(output=output, exit_code=exit_code, exception=exception, run_command_fn=run_command_fn)
48 fs = MockFileSystem(files)
49 return config.Config(e, fs)
51 def assert_configuration(self, contents, expected):
52 # This tests that a configuration file containing
53 # _contents_ ends up being interpreted as _expected_.
54 c = self.make_config('foo', {'foo/Configuration': contents})
55 self.assertEqual(c.default_configuration(), expected)
57 def test_build_directory(self):
59 def mock_webkit_build_directory(arg_list):
60 if arg_list == ['--top-level']:
62 elif arg_list == ['--configuration', '--debug']:
63 return '/WebKitBuild/Debug'
64 elif arg_list == ['--configuration', '--release']:
65 return '/WebKitBuild/Release'
68 def mock_run_command(arg_list):
69 if 'webkit-build-directory' in arg_list[1]:
70 return mock_webkit_build_directory(arg_list[2:])
73 c = self.make_config(run_command_fn=mock_run_command)
74 self.assertTrue(c.build_directory(None).endswith('WebKitBuild'))
76 # Test again to check caching
77 self.assertTrue(c.build_directory(None).endswith('WebKitBuild'))
80 self.assertTrue(c.build_directory('Release').endswith('/Release'))
81 self.assertTrue(c.build_directory('Debug').endswith('/Debug'))
82 self.assertRaises(KeyError, c.build_directory, 'Unknown')
84 def test_default_configuration__release(self):
85 self.assert_configuration('Release', 'Release')
87 def test_default_configuration__debug(self):
88 self.assert_configuration('Debug', 'Debug')
90 def test_default_configuration__deployment(self):
91 self.assert_configuration('Deployment', 'Release')
93 def test_default_configuration__development(self):
94 self.assert_configuration('Development', 'Debug')
96 def test_default_configuration__notfound(self):
97 # This tests what happens if the default configuration file doesn't exist.
98 c = self.make_config(output='foo', files={'foo/Configuration': None})
99 self.assertEqual(c.default_configuration(), "Release")
101 def test_default_configuration__unknown(self):
102 # Ignore the warning about an unknown configuration value.
105 self.assert_configuration('Unknown', 'Unknown')
108 def test_default_configuration__standalone(self):
109 # FIXME: This test runs a standalone python script to test
110 # reading the default configuration to work around any possible
111 # caching / reset bugs. See https://bugs.webkit.org/show_bug.cgi?id=49360
112 # for the motivation. We can remove this test when we remove the
113 # global configuration cache in config.py.
116 c = config.Config(e, fs)
117 script = c.path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'layout_tests', 'port', 'config_standalone.py')
119 # Note: don't use 'Release' here, since that's the normal default.
122 # FIXME: Why are we running a python subprocess here??
123 args = [sys.executable, script, '--mock', expected]
124 actual = e.run_command(args).rstrip()
125 self.assertEqual(actual, expected)
127 def test_default_configuration__no_perl(self):
128 # We need perl to run webkit-build-directory to find out where the
129 # default configuration file is. See what happens if perl isn't
130 # installed. (We should get the default value, 'Release').
131 c = self.make_config(exception=OSError)
132 actual = c.default_configuration()
133 self.assertEqual(actual, 'Release')
135 def test_default_configuration__scripterror(self):
136 # We run webkit-build-directory to find out where the default
137 # configuration file is. See what happens if that script fails.
138 # (We should get the default value, 'Release').
139 c = self.make_config(exception=ScriptError())
140 actual = c.default_configuration()
141 self.assertEqual(actual, 'Release')
143 def test_path_from_webkit_base(self):
144 c = config.Config(MockExecutive(), MockFileSystem())
145 self.assertTrue(c.path_from_webkit_base('foo'))
147 def test_webkit_base_dir(self):
148 # FIXME: We use a real filesystem here. Should this move to a mocked one?
149 executive = Executive()
150 filesystem = FileSystem()
151 c = config.Config(executive, filesystem)
152 base_dir = c.webkit_base_dir()
153 self.assertTrue(base_dir)
154 self.assertNotEqual(base_dir[-1], '/')
156 # FIXME: Once we use a MockFileSystem for this test we don't need to save the orig_cwd.
157 orig_cwd = filesystem.getcwd()
158 if sys.platform == 'win32':
159 filesystem.chdir(os.environ['USERPROFILE'])
161 filesystem.chdir(os.environ['HOME'])
162 c = config.Config(executive, filesystem)
164 base_dir_2 = c.webkit_base_dir()
165 self.assertEqual(base_dir, base_dir_2)
167 filesystem.chdir(orig_cwd)
170 if __name__ == '__main__':