| OLD | NEW |
| 1 # Copyright (C) 2011 Google Inc. All rights reserved. | 1 # Copyright (C) 2011 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| 11 # in the documentation and/or other materials provided with the | 11 # in the documentation and/or other materials provided with the |
| 12 # distribution. | 12 # distribution. |
| 13 # * Neither the name of Google Inc. nor the names of its | 13 # * Neither the name of Google Inc. nor the names of its |
| 14 # contributors may be used to endorse or promote products derived from | 14 # contributors may be used to endorse or promote products derived from |
| 15 # this software without specific prior written permission. | 15 # this software without specific prior written permission. |
| 16 # | 16 # |
| 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | 28 |
| 29 import hashlib |
| 29 import sys | 30 import sys |
| 30 import unittest2 as unittest | 31 import unittest2 as unittest |
| 31 | 32 |
| 32 from webkitpy.common.checkout.baselineoptimizer import BaselineOptimizer | 33 from webkitpy.common.checkout.baselineoptimizer import BaselineOptimizer |
| 33 from webkitpy.common.system.filesystem_mock import MockFileSystem | 34 from webkitpy.common.system.filesystem_mock import MockFileSystem |
| 34 from webkitpy.common.host_mock import MockHost | 35 from webkitpy.common.host_mock import MockHost |
| 35 | 36 |
| 36 | 37 |
| 37 class TestBaselineOptimizer(BaselineOptimizer): | 38 class TestBaselineOptimizer(BaselineOptimizer): |
| 38 def __init__(self, mock_results_by_directory): | 39 def __init__(self, mock_results_by_directory, create_mock_files, baseline_na
me): |
| 39 host = MockHost() | 40 host = MockHost() |
| 40 BaselineOptimizer.__init__(self, host, host.port_factory.all_port_names(
)) | 41 BaselineOptimizer.__init__(self, host, host.port_factory.all_port_names(
)) |
| 41 self._mock_results_by_directory = mock_results_by_directory | 42 self._mock_results_by_directory = mock_results_by_directory |
| 43 self._filesystem = host.filesystem |
| 44 self._port_factory = host.port_factory |
| 45 self._created_mock_files = create_mock_files |
| 46 self._baseline_name = baseline_name |
| 47 |
| 48 self._create_mock_files(mock_results_by_directory) |
| 42 | 49 |
| 43 # We override this method for testing so we don't have to construct an | 50 # We override this method for testing so we don't have to construct an |
| 44 # elaborate mock file system. | 51 # elaborate mock file system. |
| 45 def read_results_by_directory(self, baseline_name): | 52 def read_results_by_directory(self, baseline_name): |
| 53 if self._created_mock_files: |
| 54 return super(TestBaselineOptimizer, self).read_results_by_directory(
baseline_name) |
| 46 return self._mock_results_by_directory | 55 return self._mock_results_by_directory |
| 47 | 56 |
| 48 def _move_baselines(self, baseline_name, results_by_directory, new_results_b
y_directory): | 57 def _move_baselines(self, baseline_name, results_by_directory, new_results_b
y_directory): |
| 49 self.new_results_by_directory = new_results_by_directory | 58 self.new_results_by_directory.append(new_results_by_directory) |
| 59 |
| 60 if self._created_mock_files: |
| 61 super(TestBaselineOptimizer, self)._move_baselines(baseline_name, re
sults_by_directory, new_results_by_directory) |
| 62 return |
| 63 self._mock_results_by_directory = new_results_by_directory |
| 64 |
| 65 def _create_mock_files(self, results_by_directory): |
| 66 root = self._port_factory.get().webkit_base() |
| 67 for directory in results_by_directory: |
| 68 if 'virtual' in directory: |
| 69 virtual_suite = self._port_factory.get().lookup_virtual_suite(se
lf._baseline_name) |
| 70 if virtual_suite: |
| 71 baseline_name = self._baseline_name[len(virtual_suite.name)
+ 1:] |
| 72 else: |
| 73 baseline_name = self._baseline_name |
| 74 else: |
| 75 baseline_name = self._port_factory.get().lookup_virtual_test_bas
e(self._baseline_name) |
| 76 path = self._filesystem.join(root, directory, baseline_name) |
| 77 self._filesystem.write_text_file(path, results_by_directory[director
y]) |
| 50 | 78 |
| 51 | 79 |
| 52 class BaselineOptimizerTest(unittest.TestCase): | 80 class BaselineOptimizerTest(unittest.TestCase): |
| 81 VIRTUAL_DIRECTORY = 'virtual/softwarecompositing' |
| 82 |
| 83 def _appendVirtualSuffix(self, results_by_directory): |
| 84 new_results_by_directory = {} |
| 85 for directory in results_by_directory: |
| 86 new_results_by_directory[directory + '/' + self.VIRTUAL_DIRECTORY] =
results_by_directory[directory] |
| 87 return new_results_by_directory |
| 88 |
| 89 def _assertOneLevelOptimization(self, results_by_directory, expected_new_res
ults_by_directory, baseline_name, create_mock_files=False): |
| 90 baseline_optimizer = TestBaselineOptimizer(results_by_directory, create_
mock_files, baseline_name) |
| 91 self.assertTrue(baseline_optimizer.optimize(baseline_name)) |
| 92 if type(expected_new_results_by_directory) != list: |
| 93 expected_new_results_by_directory = [expected_new_results_by_directo
ry] |
| 94 self.assertEqual(baseline_optimizer.new_results_by_directory, expected_n
ew_results_by_directory) |
| 95 |
| 53 def _assertOptimization(self, results_by_directory, expected_new_results_by_
directory): | 96 def _assertOptimization(self, results_by_directory, expected_new_results_by_
directory): |
| 54 baseline_optimizer = TestBaselineOptimizer(results_by_directory) | 97 baseline_name = 'mock-baseline.png' |
| 55 self.assertTrue(baseline_optimizer.optimize('mock-baseline.png')) | 98 self._assertOneLevelOptimization(results_by_directory, expected_new_resu
lts_by_directory, baseline_name) |
| 56 self.assertEqual(baseline_optimizer.new_results_by_directory, expected_n
ew_results_by_directory) | 99 |
| 100 results_by_directory = self._appendVirtualSuffix(results_by_directory) |
| 101 expected_new_results_by_directory = self._appendVirtualSuffix(expected_n
ew_results_by_directory) |
| 102 baseline_name = self.VIRTUAL_DIRECTORY + '/' + baseline_name |
| 103 self._assertOneLevelOptimization(results_by_directory, [expected_new_res
ults_by_directory, expected_new_results_by_directory], baseline_name) |
| 57 | 104 |
| 58 def test_move_baselines(self): | 105 def test_move_baselines(self): |
| 59 host = MockHost() | 106 host = MockHost() |
| 60 host.filesystem.write_binary_file('/mock-checkout/LayoutTests/platform/w
in/another/test-expected.txt', 'result A') | 107 host.filesystem.write_binary_file('/mock-checkout/LayoutTests/platform/w
in/another/test-expected.txt', 'result A') |
| 61 host.filesystem.write_binary_file('/mock-checkout/LayoutTests/platform/m
ac/another/test-expected.txt', 'result A') | 108 host.filesystem.write_binary_file('/mock-checkout/LayoutTests/platform/m
ac/another/test-expected.txt', 'result A') |
| 62 host.filesystem.write_binary_file('/mock-checkout/LayoutTests/another/te
st-expected.txt', 'result B') | 109 host.filesystem.write_binary_file('/mock-checkout/LayoutTests/another/te
st-expected.txt', 'result B') |
| 63 baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_
names()) | 110 baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_
names()) |
| 64 baseline_optimizer._move_baselines('another/test-expected.txt', { | 111 baseline_optimizer._move_baselines('another/test-expected.txt', { |
| 65 'LayoutTests/platform/win': 'aaa', | 112 'LayoutTests/platform/win': 'aaa', |
| 66 'LayoutTests/platform/mac': 'aaa', | 113 'LayoutTests/platform/mac': 'aaa', |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 }) | 206 }) |
| 160 | 207 |
| 161 def test_root_baseline_unused_and_non_existant(self): | 208 def test_root_baseline_unused_and_non_existant(self): |
| 162 self._assertOptimization({ | 209 self._assertOptimization({ |
| 163 'LayoutTests/platform/mac': '1', | 210 'LayoutTests/platform/mac': '1', |
| 164 'LayoutTests/platform/win': '2', | 211 'LayoutTests/platform/win': '2', |
| 165 }, { | 212 }, { |
| 166 'LayoutTests/platform/mac': '1', | 213 'LayoutTests/platform/mac': '1', |
| 167 'LayoutTests/platform/win': '2', | 214 'LayoutTests/platform/win': '2', |
| 168 }) | 215 }) |
| 216 |
| 217 def test_virtual_root_redundant_with_actual_root(self): |
| 218 baseline_name = self.VIRTUAL_DIRECTORY + '/mock-baseline.png' |
| 219 hash_of_two = hashlib.sha1('2').hexdigest() |
| 220 expected_result = [{'LayoutTests/virtual/softwarecompositing': hash_of_t
wo}, {'LayoutTests': hash_of_two}] |
| 221 self._assertOneLevelOptimization({ |
| 222 'LayoutTests/' + self.VIRTUAL_DIRECTORY: '2', |
| 223 'LayoutTests': '2', |
| 224 }, expected_result, baseline_name, create_mock_files=True) |
| 225 |
| 226 def test_virtual_root_redundant_with_ancestors(self): |
| 227 baseline_name = self.VIRTUAL_DIRECTORY + '/mock-baseline.png' |
| 228 hash_of_two = hashlib.sha1('2').hexdigest() |
| 229 expected_result = [{'LayoutTests/virtual/softwarecompositing': hash_of_t
wo}, {'LayoutTests': hash_of_two}] |
| 230 self._assertOneLevelOptimization({ |
| 231 'LayoutTests/' + self.VIRTUAL_DIRECTORY: '2', |
| 232 'LayoutTests/platform/mac': '2', |
| 233 'LayoutTests/platform/win': '2', |
| 234 }, expected_result, baseline_name, create_mock_files=True) |
| 235 |
| 236 def test_virtual_root_not_redundant_with_ancestors(self): |
| 237 baseline_name = self.VIRTUAL_DIRECTORY + '/mock-baseline.png' |
| 238 hash_of_two = hashlib.sha1('2').hexdigest() |
| 239 expected_result = [{'LayoutTests/virtual/softwarecompositing': hash_of_t
wo}, {'LayoutTests/platform/mac': hash_of_two}] |
| 240 self._assertOneLevelOptimization({ |
| 241 'LayoutTests/' + self.VIRTUAL_DIRECTORY: '2', |
| 242 'LayoutTests/platform/mac': '2', |
| 243 }, expected_result, baseline_name, create_mock_files=True) |
| OLD | NEW |