1 # Copyright (C) 2013 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.
29 """Moves a directory of LayoutTests.
31 Given a path to a directory of LayoutTests, moves that directory, including all recursive children,
32 to the specified destination path. Updates all references in tests and resources to reflect the new
33 location. Also moves any corresponding platform-specific expected results and updates the test
34 expectations to reflect the move.
36 If the destination directory does not exist, it and any missing parent directories are created. If
37 the destination directory already exists, the child members of the origin directory are added to the
38 destination directory. If any of the child members clash with existing members of the destination
39 directory, the move fails.
41 Note that when new entries are added to the test expectations, no attempt is made to group or merge
42 them with existing entries. This should be be done manually and with lint-test-expectations.
52 from webkitpy.common.checkout.scm.detection import SCMDetector
53 from webkitpy.common.host import Host
54 from webkitpy.common.system.executive import Executive
55 from webkitpy.common.system.filesystem import FileSystem
56 from webkitpy.layout_tests.port.base import Port
57 from webkitpy.layout_tests.models.test_expectations import TestExpectations
61 _log = logging.getLogger(__name__)
62 _log.setLevel(logging.INFO)
64 PLATFORM_DIRECTORY = 'platform'
66 class LayoutTestsMover(object):
68 def __init__(self, port=None):
72 # Given that we use include_overrides=False and model_all_expectations=True when
73 # constructing the TestExpectations object, it doesn't matter which Port object we use.
74 self._port = host.port_factory.get()
75 self._port.host.initialize_scm()
76 self._filesystem = self._port.host.filesystem
77 self._scm = self._port.host.scm()
78 self._layout_tests_root = self._port.layout_tests_dir()
80 def _scm_path(self, *paths):
81 return self._filesystem.join('LayoutTests', *paths)
83 def _is_child_path(self, parent, possible_child):
84 normalized_parent = self._filesystem.normpath(parent)
85 normalized_child = self._filesystem.normpath(possible_child)
86 # We need to add a trailing separator to parent to avoid returning true for cases like
87 # parent='/foo/b', and possible_child='/foo/bar/baz'.
88 return normalized_parent == normalized_child or normalized_child.startswith(normalized_parent + self._filesystem.sep)
90 def _move_path(self, path, origin, destination):
91 if not self._is_child_path(origin, path):
93 return self._filesystem.normpath(self._filesystem.join(destination, self._filesystem.relpath(path, origin)))
95 def _validate_input(self):
96 if not self._filesystem.isdir(self._absolute_origin):
97 raise Exception('Source path %s is not a directory' % self._origin)
98 if not self._is_child_path(self._layout_tests_root, self._absolute_origin):
99 raise Exception('Source path %s is not in LayoutTests directory' % self._origin)
100 if self._filesystem.isfile(self._absolute_destination):
101 raise Exception('Destination path %s is a file' % self._destination)
102 if not self._is_child_path(self._layout_tests_root, self._absolute_destination):
103 raise Exception('Destination path %s is not in LayoutTests directory' % self._destination)
105 # If destination is an existing directory, we move the children of origin into destination.
106 # However, if any of the children of origin would clash with existing children of
107 # destination, we fail.
108 # FIXME: Consider adding support for recursively moving into an existing directory.
109 if self._filesystem.isdir(self._absolute_destination):
110 for file_path in self._filesystem.listdir(self._absolute_origin):
111 if self._filesystem.exists(self._filesystem.join(self._absolute_destination, file_path)):
112 raise Exception('Origin path %s clashes with existing destination path %s' %
113 (self._filesystem.join(self._origin, file_path), self._filesystem.join(self._destination, file_path)))
115 def _get_expectations_for_test(self, model, test_path):
116 """Given a TestExpectationsModel object, finds all expectations that match the specified
117 test, specified as a relative path. Handles the fact that expectations may be keyed by
121 if model.has_test(test_path):
122 expectations.add(model.get_expectation_line(test_path))
123 test_path = self._filesystem.dirname(test_path)
124 while not test_path == '':
125 # The model requires a trailing slash for directories.
126 test_path_for_model = test_path + '/'
127 if model.has_test(test_path_for_model):
128 expectations.add(model.get_expectation_line(test_path_for_model))
129 test_path = self._filesystem.dirname(test_path)
132 def _get_expectations(self, model, path):
133 """Given a TestExpectationsModel object, finds all expectations for all tests under the
134 specified relative path.
137 for test in self._filesystem.files_under(self._filesystem.join(self._layout_tests_root, path), dirs_to_skip=['script-tests', 'resources'],
138 file_filter=Port.is_test_file):
139 expectations = expectations.union(self._get_expectations_for_test(model, self._filesystem.relpath(test, self._layout_tests_root)))
143 def _clone_expectation_line_for_path(expectation_line, path):
144 """Clones a TestExpectationLine object and updates the clone to apply to the specified
147 clone = copy.copy(expectation_line)
148 clone.original_string = re.compile(expectation_line.name).sub(path, expectation_line.original_string)
151 # FIXME: Should we search existing expectations for matches, like in
152 # TestExpectationsParser._collect_matching_tests()?
153 clone.matching_tests = [path]
156 def _update_expectations(self):
157 """Updates all test expectations that are affected by the move.
159 _log.info('Updating expectations')
160 test_expectations = TestExpectations(self._port, include_overrides=False, model_all_expectations=True)
162 for expectation in self._get_expectations(test_expectations.model(), self._origin):
163 path = expectation.path
164 if self._is_child_path(self._origin, path):
165 # If the existing expectation is a child of the moved path, we simply replace it
166 # with an expectation for the updated path.
167 new_path = self._move_path(path, self._origin, self._destination)
168 _log.debug('Updating expectation for %s to %s' % (path, new_path))
169 test_expectations.remove_expectation_line(path)
170 test_expectations.add_expectation_line(LayoutTestsMover._clone_expectation_line_for_path(expectation, new_path))
172 # If the existing expectation is not a child of the moved path, we have to leave it
173 # in place. But we also add a new expectation for the destination path.
174 new_path = self._destination
175 _log.warning('Copying expectation for %s to %s. You should check that these expectations are still correct.' %
177 test_expectations.add_expectation_line(LayoutTestsMover._clone_expectation_line_for_path(expectation, new_path))
179 expectations_file = self._port.path_to_generic_test_expectations_file()
180 self._filesystem.write_text_file(expectations_file,
181 TestExpectations.list_to_string(test_expectations._expectations, reconstitute_only_these=[]))
182 self._scm.add(self._filesystem.relpath(expectations_file, self._scm.checkout_root))
184 def _find_references(self, input_files):
185 """Attempts to find all references to other files in the supplied list of files. Returns a
186 dictionary that maps from an absolute file path to an array of reference strings.
188 reference_regex = re.compile(r'(?:(?:src=|href=|importScripts\(|url\()(?:"([^"]+)"|\'([^\']+)\')|url\(([^\)\'"]+)\))')
190 for input_file in input_files:
191 matches = reference_regex.findall(self._filesystem.read_binary_file(input_file))
193 references[input_file] = [filter(None, match)[0] for match in matches]
196 def _get_updated_reference(self, root, reference):
197 """For a reference <reference> in a directory <root>, determines the updated reference.
198 Returns the the updated reference, or None if no update is required.
200 # If the reference is an absolute path or url, it's safe.
201 if reference.startswith('/') or urlparse.urlparse(reference).scheme:
204 # Both the root path and the target of the reference my be subject to the move, so there are
205 # four cases to consider. In the case where both or neither are subject to the move, the
206 # reference doesn't need updating.
208 # This is true even if the reference includes superfluous dot segments which mention a moved
209 # directory, as dot segments are collapsed during URL normalization. For example, if
210 # foo.html contains a reference 'bar/../script.js', this remains valid (though ugly) even if
211 # bar/ is moved to baz/, because the reference is always normalized to 'script.js'.
212 absolute_reference = self._filesystem.normpath(self._filesystem.join(root, reference))
213 if self._is_child_path(self._absolute_origin, root) == self._is_child_path(self._absolute_origin, absolute_reference):
216 new_root = self._move_path(root, self._absolute_origin, self._absolute_destination)
217 new_absolute_reference = self._move_path(absolute_reference, self._absolute_origin, self._absolute_destination)
218 return self._filesystem.relpath(new_absolute_reference, new_root)
220 def _get_all_updated_references(self, references):
221 """Determines the updated references due to the move. Returns a dictionary that maps from an
222 absolute file path to a dictionary that maps from a reference string to the corresponding
226 for file_path in references.keys():
227 root = self._filesystem.dirname(file_path)
228 # sript-tests/TEMPLATE.html files contain references which are written as if the file
229 # were in the parent directory. This special-casing is ugly, but there are plans to
230 # remove script-tests.
231 if root.endswith('script-tests') and file_path.endswith('TEMPLATE.html'):
232 root = self._filesystem.dirname(root)
234 for reference in references[file_path]:
235 update = self._get_updated_reference(root, reference)
237 local_updates[reference] = update
239 updates[file_path] = local_updates
242 def _update_file(self, path, updates):
243 contents = self._filesystem.read_binary_file(path)
244 # Note that this regex isn't quite as strict as that used to find the references, but this
245 # avoids the need for alternative match groups, which simplifies things.
246 for target in updates.keys():
247 regex = re.compile(r'((?:src=|href=|importScripts\(|url\()["\']?)%s(["\']?)' % target)
248 contents = regex.sub(r'\1%s\2' % updates[target], contents)
249 self._filesystem.write_binary_file(path, contents)
252 def _update_test_source_files(self):
253 def is_test_source_file(filesystem, dirname, basename):
254 pass_regex = re.compile(r'\.(css|js)$')
255 fail_regex = re.compile(r'-expected\.')
256 return (Port.is_test_file(filesystem, dirname, basename) or pass_regex.search(basename)) and not fail_regex.search(basename)
258 test_source_files = self._filesystem.files_under(self._layout_tests_root, file_filter=is_test_source_file)
259 _log.info('Considering %s test source files for references' % len(test_source_files))
260 references = self._find_references(test_source_files)
261 _log.info('Considering references in %s files' % len(references))
262 updates = self._get_all_updated_references(references)
263 _log.info('Updating references in %s files' % len(updates))
265 for file_path in updates.keys():
266 self._update_file(file_path, updates[file_path])
268 if count % 1000 == 0 or count == len(updates):
269 _log.debug('Updated references in %s files' % count)
271 def _move_directory(self, origin, destination):
272 """Moves the directory <origin> to <destination>. If <destination> is a directory, moves the
273 children of <origin> into <destination>. Uses relative paths.
275 absolute_origin = self._filesystem.join(self._layout_tests_root, origin)
276 if not self._filesystem.isdir(absolute_origin):
278 _log.info('Moving directory %s to %s' % (origin, destination))
279 # Note that FileSystem.move() may silently overwrite existing files, but we
280 # check for this in _validate_input().
281 absolute_destination = self._filesystem.join(self._layout_tests_root, destination)
282 self._filesystem.maybe_make_directory(absolute_destination)
283 for directory in self._filesystem.listdir(absolute_origin):
284 self._scm.move(self._scm_path(origin, directory), self._scm_path(destination, directory))
285 self._filesystem.rmtree(absolute_origin)
287 def _move_files(self):
288 """Moves the all files that correspond to the move, including platform-specific expected
291 self._move_directory(self._origin, self._destination)
292 for directory in self._filesystem.listdir(self._filesystem.join(self._layout_tests_root, PLATFORM_DIRECTORY)):
293 self._move_directory(self._filesystem.join(PLATFORM_DIRECTORY, directory, self._origin),
294 self._filesystem.join(PLATFORM_DIRECTORY, directory, self._destination))
296 def _commit_changes(self):
297 if not self._scm.supports_local_commits():
299 title = 'Move LayoutTests directory %s to %s' % (self._origin, self._destination)
300 _log.info('Committing change \'%s\'' % title)
301 self._scm.commit_locally_with_message('%s\n\nThis commit was automatically generated by move-layout-tests.' % title,
302 commit_all_working_directory_changes=False)
304 def move(self, origin, destination):
305 self._origin = origin
306 self._destination = destination
307 self._absolute_origin = self._filesystem.join(self._layout_tests_root, self._origin)
308 self._absolute_destination = self._filesystem.join(self._layout_tests_root, self._destination)
309 self._validate_input()
310 self._update_expectations()
311 self._update_test_source_files()
313 # FIXME: Handle virtual test suites.
314 self._commit_changes()
317 parser = optparse.OptionParser(description=__doc__)
318 parser.add_option('--origin',
319 help=('The directory of tests to move, as a relative path from the LayoutTests directory.'))
320 parser.add_option('--destination',
321 help=('The new path for the directory of tests, as a relative path from the LayoutTests directory.'))
322 options, _ = parser.parse_args()
323 LayoutTestsMover().move(options.origin, options.destination)