1 # Copyright (C) 2011 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.
31 from webkitpy.common.checkout.baselineoptimizer import BaselineOptimizer
32 from webkitpy.common.checkout.scm.scm_mock import MockSCM
33 from webkitpy.common.host_mock import MockHost
34 from webkitpy.common.webkit_finder import WebKitFinder
37 class ExcludingMockSCM(MockSCM):
38 def __init__(self, exclusion_list, filesystem=None, executive=None):
39 MockSCM.__init__(self, filesystem, executive)
40 self._exclusion_list = exclusion_list
42 def exists(self, path):
43 if path in self._exclusion_list:
45 return MockSCM.exists(self, path)
47 def delete(self, path):
48 return self.delete_list([path])
50 def delete_list(self, paths):
52 if path in self._exclusion_list:
53 raise Exception("File is not SCM managed: " + path)
54 return MockSCM.delete_list(self, paths)
56 def move(self, origin, destination):
57 if origin in self._exclusion_list:
58 raise Exception("File is not SCM managed: " + origin)
59 return MockSCM.move(self, origin, destination)
62 class BaselineOptimizerTest(unittest.TestCase):
63 def test_move_baselines(self):
64 host = MockHost(scm=ExcludingMockSCM(['/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt']))
65 host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/win/another/test-expected.txt', 'result A')
66 host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt', 'result A')
67 host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt', 'result B')
68 baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_names(), skip_scm_commands=False)
69 baseline_optimizer._move_baselines('another/test-expected.txt', {
70 '/mock-checkout/third_party/WebKit/LayoutTests/platform/win': 'aaa',
71 '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac': 'aaa',
72 '/mock-checkout/third_party/WebKit/LayoutTests': 'bbb',
74 '/mock-checkout/third_party/WebKit/LayoutTests': 'aaa',
76 self.assertEqual(host.filesystem.read_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt'), 'result A')
78 def test_move_baselines_skip_scm_commands(self):
79 host = MockHost(scm=ExcludingMockSCM(['/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt']))
80 host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/win/another/test-expected.txt', 'result A')
81 host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt', 'result A')
82 host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt', 'result B')
83 baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_names(), skip_scm_commands=True)
84 baseline_optimizer._move_baselines('another/test-expected.txt', {
85 '/mock-checkout/third_party/WebKit/LayoutTests/platform/win': 'aaa',
86 '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac': 'aaa',
87 '/mock-checkout/third_party/WebKit/LayoutTests': 'bbb',
89 '/mock-checkout/third_party/WebKit/LayoutTests/platform/linux': 'bbb',
90 '/mock-checkout/third_party/WebKit/LayoutTests': 'aaa',
92 self.assertEqual(host.filesystem.read_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt'), 'result A')
94 self.assertEqual(baseline_optimizer._files_to_delete, [
95 '/mock-checkout/third_party/WebKit/LayoutTests/platform/win/another/test-expected.txt',
98 self.assertEqual(baseline_optimizer._files_to_add, [
99 '/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt',
100 '/mock-checkout/third_party/WebKit/LayoutTests/platform/linux/another/test-expected.txt',
103 def _assertOptimization(self, results_by_directory, expected_new_results_by_directory, baseline_dirname='', expected_files_to_delete=None, host=None):
107 webkit_base = WebKitFinder(fs).webkit_base()
108 baseline_name = 'mock-baseline-expected.txt'
110 for dirname, contents in results_by_directory.items():
111 path = fs.join(webkit_base, 'LayoutTests', dirname, baseline_name)
112 fs.write_binary_file(path, contents)
114 baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_names(), skip_scm_commands=expected_files_to_delete is not None)
115 self.assertTrue(baseline_optimizer.optimize(fs.join(baseline_dirname, baseline_name)))
117 for dirname, contents in expected_new_results_by_directory.items():
118 path = fs.join(webkit_base, 'LayoutTests', dirname, baseline_name)
120 self.assertTrue(not fs.exists(path) or path in baseline_optimizer._files_to_delete)
122 self.assertEqual(fs.read_binary_file(path), contents)
124 # Check that the files that were in the original set have been deleted where necessary.
125 for dirname in results_by_directory:
126 path = fs.join(webkit_base, 'LayoutTests', dirname, baseline_name)
127 if not dirname in expected_new_results_by_directory:
128 self.assertTrue(not fs.exists(path) or path in baseline_optimizer._files_to_delete)
130 if expected_files_to_delete:
131 self.assertEqual(baseline_optimizer._files_to_delete, expected_files_to_delete)
133 def test_linux_redundant_with_win(self):
134 self._assertOptimization({
136 'platform/linux': '1',
141 def test_covers_mac_win_linux(self):
142 self._assertOptimization({
145 'platform/linux': '1',
151 def test_overwrites_root(self):
152 self._assertOptimization({
155 'platform/linux': '1',
161 def test_no_new_common_directory(self):
162 self._assertOptimization({
164 'platform/linux': '1',
168 'platform/linux': '1',
173 def test_local_optimization(self):
174 self._assertOptimization({
176 'platform/linux': '1',
177 'platform/linux-x86': '1',
180 'platform/linux': '1',
183 def test_local_optimization_skipping_a_port_in_the_middle(self):
184 self._assertOptimization({
185 'platform/mac-snowleopard': '1',
187 'platform/linux-x86': '1',
189 'platform/mac-snowleopard': '1',
193 def test_baseline_redundant_with_root(self):
194 self._assertOptimization({
203 def test_root_baseline_unused(self):
204 self._assertOptimization({
213 def test_root_baseline_unused_and_non_existant(self):
214 self._assertOptimization({
222 def test_virtual_root_redundant_with_actual_root(self):
223 self._assertOptimization({
224 'virtual/softwarecompositing': '2',
227 'virtual/softwarecompositing': None,
229 }, baseline_dirname='virtual/softwarecompositing')
231 def test_virtual_root_redundant_with_ancestors(self):
232 self._assertOptimization({
233 'virtual/softwarecompositing': '2',
234 'platform/mac/compositing': '2',
235 'platform/win/compositing': '2',
237 'virtual/softwarecompositing': None,
239 }, baseline_dirname='virtual/softwarecompositing')
241 def test_virtual_root_redundant_with_ancestors_skip_scm_commands(self):
242 self._assertOptimization({
243 'virtual/softwarecompositing': '2',
244 'platform/mac/compositing': '2',
245 'platform/win/compositing': '2',
247 'virtual/softwarecompositing': None,
250 baseline_dirname='virtual/softwarecompositing',
251 expected_files_to_delete=[
252 '/mock-checkout/third_party/WebKit/LayoutTests/virtual/softwarecompositing/mock-baseline-expected.txt',
253 '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/compositing/mock-baseline-expected.txt',
254 '/mock-checkout/third_party/WebKit/LayoutTests/platform/win/compositing/mock-baseline-expected.txt',
257 def test_virtual_root_redundant_with_ancestors_skip_scm_commands_with_file_not_in_scm(self):
258 self._assertOptimization({
259 'virtual/softwarecompositing': '2',
260 'platform/mac/compositing': '2',
261 'platform/win/compositing': '2',
263 'virtual/softwarecompositing': None,
266 baseline_dirname='virtual/softwarecompositing',
267 expected_files_to_delete=[
268 '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/compositing/mock-baseline-expected.txt',
269 '/mock-checkout/third_party/WebKit/LayoutTests/platform/win/compositing/mock-baseline-expected.txt',
271 host=MockHost(scm=ExcludingMockSCM(['/mock-checkout/third_party/WebKit/LayoutTests/virtual/softwarecompositing/mock-baseline-expected.txt'])))
273 def test_virtual_root_not_redundant_with_ancestors(self):
274 self._assertOptimization({
275 'virtual/softwarecompositing': '2',
276 'platform/mac/compositing': '1',
278 'virtual/softwarecompositing': '2',
279 'platform/mac/compositing': '1',
280 }, baseline_dirname='virtual/softwarecompositing')