Commit d8df3771 authored by qyearsley's avatar qyearsley Committed by Commit bot

In update_w3c_test_expectations use ExpectationLine.tokenize_line and refactor.

In this CL:
 - Make test methods in update_w3c_test_expectations have more realistic examples.
 - Change some lines in write_to_test_expectations to make that method shorter.

This is a refactoring CL (no behavior changes).

BUG=647395

Review-Url: https://codereview.chromium.org/2349543002
Cr-Commit-Position: refs/heads/master@{#419215}
parent eb357150
...@@ -18,6 +18,7 @@ from webkitpy.common.net.git_cl import GitCL ...@@ -18,6 +18,7 @@ from webkitpy.common.net.git_cl import GitCL
from webkitpy.common.net.rietveld import Rietveld from webkitpy.common.net.rietveld import Rietveld
from webkitpy.common.webkit_finder import WebKitFinder from webkitpy.common.webkit_finder import WebKitFinder
from webkitpy.w3c.test_parser import TestParser from webkitpy.w3c.test_parser import TestParser
from webkitpy.layout_tests.models.test_expectations import TestExpectationLine
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
...@@ -256,38 +257,31 @@ class W3CExpectationsLineAdder(object): ...@@ -256,38 +257,31 @@ class W3CExpectationsLineAdder(object):
def write_to_test_expectations(self, line_list): def write_to_test_expectations(self, line_list):
"""Writes to TestExpectations. """Writes to TestExpectations.
Writes the test expectations lines in |line_list| to the test
expectations file.
The place in the file where the new lines are inserted is after a The place in the file where the new lines are inserted is after a
marker comment line. If this marker comment line is not found, it will marker comment line. If this marker comment line is not found, it will
be added to the end of the file. be added to the end of the file.
Args: Args:
line_list: A list of w3c test expectations lines. line_list: A list of lines to add to the TestExpectations file.
""" """
_log.debug('Lines to write to TestExpectations: %r', line_list) _log.debug('Lines to write to TestExpectations: %r', line_list)
port = self.host.port_factory.get() port = self.host.port_factory.get()
expectations_file = port.path_to_generic_test_expectations_file() expectations_file_path = port.path_to_generic_test_expectations_file()
comment_line = '# Tests added from W3C auto import bot' marker_comment = '# Tests added from W3C auto import bot'
file_contents = self.host.filesystem.read_text_file(expectations_file) file_contents = self.host.filesystem.read_text_file(expectations_file_path)
w3c_comment_line_index = file_contents.find(comment_line) marker_comment_index = file_contents.find(marker_comment)
all_lines = '' line_list = [line for line in line_list if self._test_name_from_expectation_string(line) not in file_contents]
for line in line_list: if marker_comment_index == -1:
end_bracket_index = line.split().index(']') file_contents += '\n%s\n' % marker_comment
test_name = line.split()[end_bracket_index + 1] file_contents += '\n'.join(line_list)
if test_name in file_contents:
continue
all_lines += str(line) + '\n'
all_lines = all_lines[:-1]
if w3c_comment_line_index == -1:
file_contents += '\n%s\n' % comment_line
file_contents += all_lines
else: else:
end_of_comment_line = (file_contents[w3c_comment_line_index:].find('\n')) + w3c_comment_line_index end_of_marker_line = (file_contents[marker_comment_index:].find('\n')) + marker_comment_index
new_data = file_contents[: end_of_comment_line + 1] + all_lines + file_contents[end_of_comment_line:] file_contents = file_contents[:end_of_marker_line + 1] + '\n'.join(line_list) + file_contents[end_of_marker_line:]
file_contents = new_data self.host.filesystem.write_text_file(expectations_file_path, file_contents)
self.host.filesystem.write_text_file(expectations_file, file_contents)
@staticmethod
def _test_name_from_expectation_string(expectation_string):
return TestExpectationLine.tokenize_line(filename='', expectation_string=expectation_string, line_number=0).name
def get_expected_txt_files(self, tests_results): def get_expected_txt_files(self, tests_results):
"""Fetches new baseline files for tests that should be rebaselined. """Fetches new baseline files for tests that should be rebaselined.
......
...@@ -172,34 +172,62 @@ class UpdateW3CTestExpectationsTest(logtesting.LoggingTestCase): ...@@ -172,34 +172,62 @@ class UpdateW3CTestExpectationsTest(logtesting.LoggingTestCase):
} }
}) })
def test_write_to_test_expectations_under_comment(self): def test_write_to_test_expectations_with_marker_comment(self):
expectations_path = '/mock-checkout/third_party/WebKit/LayoutTests/TestExpectations' expectations_path = '/mock-checkout/third_party/WebKit/LayoutTests/TestExpectations'
self.host.filesystem.files[expectations_path] = '# Tests added from W3C auto import bot\n' self.host.filesystem.files[expectations_path] = '# Tests added from W3C auto import bot\n'
line_adder = W3CExpectationsLineAdder(self.host) line_adder = W3CExpectationsLineAdder(self.host)
line_list = ['fake crbug [ foo ] fake/file/path.html [Pass]'] line_list = ['crbug.com/123 [ FakePlatform ] fake/file/path.html [ Pass ]']
line_adder.write_to_test_expectations(line_list) line_adder.write_to_test_expectations(line_list)
value = line_adder.host.filesystem.read_text_file(expectations_path) value = line_adder.host.filesystem.read_text_file(expectations_path)
self.assertEqual(value, '# Tests added from W3C auto import bot\nfake crbug [ foo ] fake/file/path.html [Pass]\n') self.assertMultiLineEqual(
value,
('# Tests added from W3C auto import bot\n'
'crbug.com/123 [ FakePlatform ] fake/file/path.html [ Pass ]\n'))
def test_write_to_test_expectations_to_eof(self): def test_write_to_test_expectations_with_no_marker_comment(self):
expectations_path = '/mock-checkout/third_party/WebKit/LayoutTests/TestExpectations' expectations_path = '/mock-checkout/third_party/WebKit/LayoutTests/TestExpectations'
self.host.filesystem.files[expectations_path] = 'not empty\n' self.host.filesystem.files[expectations_path] = 'crbug.com/111 [ FakePlatform ]\n'
line_adder = W3CExpectationsLineAdder(self.host) line_adder = W3CExpectationsLineAdder(self.host)
line_list = ['fake crbug [ foo ] fake/file/path.html [Pass]'] line_list = ['crbug.com/123 [ FakePlatform ] fake/file/path.html [ Pass ]']
line_adder.write_to_test_expectations(line_list) line_adder.write_to_test_expectations(line_list)
value = self.host.filesystem.read_text_file(expectations_path) value = self.host.filesystem.read_text_file(expectations_path)
self.assertEqual( self.assertMultiLineEqual(
value, value,
'not empty\n\n# Tests added from W3C auto import bot\nfake crbug [ foo ] fake/file/path.html [Pass]') ('crbug.com/111 [ FakePlatform ]\n'
'\n'
'# Tests added from W3C auto import bot\n'
'crbug.com/123 [ FakePlatform ] fake/file/path.html [ Pass ]'))
def test_write_to_test_expectations_skip_lines(self): def test_write_to_test_expectations_skips_existing_lines(self):
expectations_path = '/mock-checkout/third_party/WebKit/LayoutTests/TestExpectations' expectations_path = '/mock-checkout/third_party/WebKit/LayoutTests/TestExpectations'
self.host.filesystem.files[expectations_path] = 'dont copy me\n' self.host.filesystem.files[expectations_path] = 'crbug.com/111 dont/copy/me.html [ Failure ]\n'
line_adder = W3CExpectationsLineAdder(self.host) line_adder = W3CExpectationsLineAdder(self.host)
line_list = ['[ ] dont copy me', '[ ] but copy me'] line_list = [
'crbug.com/111 dont/copy/me.html [ Failure ]',
'crbug.com/222 do/copy/me.html [ Failure ]'
]
line_adder.write_to_test_expectations(line_list) line_adder.write_to_test_expectations(line_list)
value = self.host.filesystem.read_text_file(expectations_path) value = self.host.filesystem.read_text_file(expectations_path)
self.assertEqual(value, 'dont copy me\n\n# Tests added from W3C auto import bot\n[ ] but copy me') self.assertEqual(
value,
('crbug.com/111 dont/copy/me.html [ Failure ]\n'
'\n'
'# Tests added from W3C auto import bot\n'
'crbug.com/222 do/copy/me.html [ Failure ]'))
def test_write_to_test_expectations_with_marker_and_no_lines(self):
expectations_path = '/mock-checkout/third_party/WebKit/LayoutTests/TestExpectations'
self.host.filesystem.files[expectations_path] = (
'# Tests added from W3C auto import bot\n'
'crbug.com/123 [ FakePlatform ] fake/file/path.html [ Pass ]\n')
line_adder = W3CExpectationsLineAdder(self.host)
line_adder.write_to_test_expectations([])
value = line_adder.host.filesystem.read_text_file(expectations_path)
self.assertMultiLineEqual(
value,
('# Tests added from W3C auto import bot\n'
'\n'
'crbug.com/123 [ FakePlatform ] fake/file/path.html [ Pass ]\n'))
def test_is_js_test_true(self): def test_is_js_test_true(self):
self.host.filesystem.files['/mock-checkout/third_party/WebKit/LayoutTests/foo/bar.html'] = ( self.host.filesystem.files['/mock-checkout/third_party/WebKit/LayoutTests/foo/bar.html'] = (
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment