1# Copyright (C) 2013 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 logging 30import unittest 31 32from webkitpy.common.host_mock import MockHost 33from webkitpy.common.checkout.scm.scm_mock import MockSCM 34from webkitpy.common.system.filesystem_mock import MockFileSystem 35from webkitpy.layout_tests.layout_tests_mover import LayoutTestsMover 36from webkitpy.layout_tests.port import base 37 38 39class MockPort(base.Port): 40 41 def __init__(self, **kwargs): 42 # This sets up a mock FileSystem and SCM using that FileSystem. 43 host = MockHost() 44 super(MockPort, self).__init__(host, host.port_factory.all_port_names()[0], **kwargs) 45 46 host.filesystem.maybe_make_directory(self._absolute_path('platform')) 47 host.filesystem.maybe_make_directory(self._absolute_path('existing_directory')) 48 host.filesystem.write_text_file(self._absolute_path('existing_file.txt'), '') 49 host.filesystem.write_text_file(self._absolute_path('VirtualTestSuites'), '[]') 50 host.filesystem.write_text_file(self._absolute_path('TestExpectations'), """ 51crbug.com/42 [ Debug ] origin/path/test.html [ Pass Timeout Failure ] 52crbug.com/42 [ Win ] origin/path [ Slow ] 53crbug.com/42 [ Release ] origin [ Crash ] 54""") 55 host.filesystem.write_text_file(self._absolute_path('existing_directory_with_contents', 'test.html'), '') 56 host.filesystem.write_text_file(self._absolute_path('origin', 'path', 'test.html'), """ 57<script src="local_script.js"> 58<script src="../../unmoved/remote_script.js"> 59<script src='../../unmoved/remote_script_single_quotes.js'> 60<script href="../../unmoved/remote_script.js"> 61<script href='../../unmoved/remote_script_single_quotes.js'> 62<script href=""> 63""") 64 host.filesystem.write_text_file(self._absolute_path('origin', 'path', 'test.css'), """ 65url('../../unmoved/url_function.js') 66url("../../unmoved/url_function_double_quotes.js") 67url(../../unmoved/url_function_no_quotes.js) 68url('') 69url() 70""") 71 host.filesystem.write_text_file(self._absolute_path('origin', 'path', 'test.js'), """ 72importScripts('../../unmoved/import_scripts_function.js') 73importScripts("../../unmoved/import_scripts_function_double_quotes.js") 74importScripts('') 75""") 76 host.filesystem.write_text_file(self._absolute_path('unmoved', 'test.html'), """ 77<script src="local_script.js"> 78<script src="../origin/path/remote_script.js"> 79""") 80 81 def _absolute_path(self, *paths): 82 return self.host.scm().absolute_path('LayoutTests', *paths) 83 84 def layout_tests_dir(self): 85 return self._absolute_path() 86 87 88class LayoutTestsMoverTest(unittest.TestCase): 89 90 def setUp(self): 91 port = MockPort() 92 self._port = port 93 self._filesystem = self._port.host.filesystem 94 self._mover = LayoutTestsMover(port=self._port) 95 96 def test_non_existent_origin_raises(self): 97 self.assertRaises(Exception, self._mover.move, 'non_existent', 'destination') 98 99 def test_origin_outside_layout_tests_directory_raises(self): 100 self.assertRaises(Exception, self._mover.move, '../outside', 'destination') 101 102 def test_file_destination_raises(self): 103 self.assertRaises(Exception, self._mover.move, 'origin/path', 'existing_file.txt') 104 105 def test_destination_outside_layout_tests_directory_raises(self): 106 self.assertRaises(Exception, self._mover.move, 'origin/path', '../outside') 107 108 def test_basic_operation(self): 109 self._mover.move('origin/path', 'destination') 110 self.assertFalse(self._filesystem.exists(self._port._absolute_path('origin/path'))) 111 self.assertTrue(self._filesystem.isfile(self._port._absolute_path('destination/test.html'))) 112 113 def test_move_to_existing_directory(self): 114 self._mover.move('origin/path', 'existing_directory') 115 self.assertFalse(self._filesystem.exists(self._port._absolute_path('origin', 'path'))) 116 self.assertTrue(self._filesystem.isfile(self._port._absolute_path('existing_directory', 'test.html'))) 117 118 def test_collision_in_existing_directory_raises(self): 119 self.assertRaises(Exception, self._mover.move, 'origin/path', 'existing_directory_with_contents') 120 121 def test_move_to_layout_tests_root(self): 122 self._mover.move('origin/path', '') 123 self.assertFalse(self._filesystem.exists(self._port._absolute_path('origin', 'path'))) 124 self.assertTrue(self._filesystem.isfile(self._port._absolute_path('test.html'))) 125 126 def test_moved_reference_in_moved_file_not_updated(self): 127 self._mover.move('origin/path', 'destination') 128 self.assertTrue('src="local_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html'))) 129 130 def test_unmoved_reference_in_unmoved_file_not_updated(self): 131 self._mover.move('origin/path', 'destination') 132 self.assertTrue('src="local_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('unmoved', 'test.html'))) 133 134 def test_moved_reference_in_unmoved_file_is_updated(self): 135 self._mover.move('origin/path', 'destination') 136 self.assertTrue('src="../destination/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('unmoved', 'test.html'))) 137 138 def test_unmoved_reference_in_moved_file_is_updated(self): 139 self._mover.move('origin/path', 'destination') 140 self.assertTrue('src="../unmoved/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html'))) 141 142 def test_references_in_html_file_are_updated(self): 143 self._mover.move('origin/path', 'destination') 144 self.assertTrue('src="../unmoved/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html'))) 145 self.assertTrue('src=\'../unmoved/remote_script_single_quotes.js\'' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html'))) 146 self.assertTrue('href="../unmoved/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html'))) 147 self.assertTrue('href=\'../unmoved/remote_script_single_quotes.js\'' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html'))) 148 self.assertTrue('href=""' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html'))) 149 150 def test_references_in_css_file_are_updated(self): 151 self._mover.move('origin/path', 'destination') 152 self.assertTrue('url(\'../unmoved/url_function.js\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css'))) 153 self.assertTrue('url("../unmoved/url_function_double_quotes.js")' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css'))) 154 self.assertTrue('url(../unmoved/url_function_no_quotes.js)' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css'))) 155 self.assertTrue('url(\'\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css'))) 156 self.assertTrue('url()' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css'))) 157 158 def test_references_in_javascript_file_are_updated(self): 159 self._mover.move('origin/path', 'destination') 160 self.assertTrue('importScripts(\'../unmoved/import_scripts_function.js\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.js'))) 161 self.assertTrue('importScripts("../unmoved/import_scripts_function_double_quotes.js")' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.js'))) 162 self.assertTrue('importScripts(\'\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.js'))) 163 164 def test_expectation_is_updated(self): 165 self._mover.move('origin/path', 'destination') 166 self.assertFalse('origin/path/test.html' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations'))) 167 self.assertTrue('crbug.com/42 [ Debug ] destination/test.html [ Pass Timeout Failure ]' 168 in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations'))) 169 170 def test_directory_expectation_is_updated(self): 171 self._mover.move('origin/path', 'destination') 172 self.assertFalse('origin/path' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations'))) 173 self.assertTrue('crbug.com/42 [ Win ] destination [ Slow ]' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations'))) 174 175 def test_expectation_is_added_when_subdirectory_moved(self): 176 self._mover.move('origin/path', 'destination') 177 self.assertTrue('crbug.com/42 [ Release ] origin [ Crash ]' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations'))) 178 self.assertTrue('crbug.com/42 [ Release ] destination [ Crash ]' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations'))) 179