Commit 0837e8e4 authored by Luke Zielinski's avatar Luke Zielinski Committed by Commit Bot

Refactor wpt_common.py to use blinkpy FileSystem, for easier testing

Change-Id: I966fc253ab338522bdcf5c09c8523a53748ce406
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418477
Auto-Submit: Luke Z <lpz@chromium.org>
Commit-Queue: Stephen McGruer <smcgruer@chromium.org>
Reviewed-by: default avatarStephen McGruer <smcgruer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808454}
parent d236b21e
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
import base64 import base64
import json import json
import os import os
import shutil
import sys import sys
import common import common
...@@ -18,6 +17,7 @@ if BLINK_TOOLS_DIR not in sys.path: ...@@ -18,6 +17,7 @@ if BLINK_TOOLS_DIR not in sys.path:
sys.path.append(BLINK_TOOLS_DIR) sys.path.append(BLINK_TOOLS_DIR)
from blinkpy.common.host import Host from blinkpy.common.host import Host
from blinkpy.common.system.filesystem import FileSystem
from blinkpy.web_tests.models import test_failures from blinkpy.web_tests.models import test_failures
class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter): class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter):
...@@ -28,6 +28,7 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter): ...@@ -28,6 +28,7 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter):
def __init__(self): def __init__(self):
super(BaseWptScriptAdapter, self).__init__() super(BaseWptScriptAdapter, self).__init__()
self.fs = FileSystem()
host = Host() host = Host()
self.port = host.port_factory.get() self.port = host.port_factory.get()
self.wpt_manifest = self.port.wpt_manifest("external/wpt") self.wpt_manifest = self.port.wpt_manifest("external/wpt")
...@@ -50,24 +51,23 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter): ...@@ -50,24 +51,23 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter):
results_dir = os.path.dirname(self.options.isolated_script_test_output) results_dir = os.path.dirname(self.options.isolated_script_test_output)
layout_test_results = os.path.join(results_dir, 'layout-test-results') layout_test_results = os.path.join(results_dir, 'layout-test-results')
if os.path.exists(layout_test_results): if os.path.exists(layout_test_results):
shutil.rmtree(layout_test_results) self.fs.rmtree(layout_test_results)
os.mkdir(layout_test_results) self.fs.maybe_make_directory(layout_test_results)
# Perform post-processing of wptrunner output # Perform post-processing of wptrunner output
self.process_wptrunner_output() self.process_wptrunner_output()
shutil.copyfile(self.options.isolated_script_test_output, self.fs.copyfile(self.options.isolated_script_test_output,
os.path.join(layout_test_results, 'full_results.json')) os.path.join(layout_test_results, 'full_results.json'))
# create full_results_jsonp.js file which is used to # create full_results_jsonp.js file which is used to
# load results into the results viewer # load results into the results viewer
with open(self.options.isolated_script_test_output, 'r') \ self.fs.write_text_file(
as full_results, \ os.path.join(layout_test_results, 'full_results_jsonp.js'),
open(os.path.join( 'ADD_FULL_RESULTS(%s);' % self.fs.read_text_file(
layout_test_results, 'full_results_jsonp.js'), 'w') \ self.options.isolated_script_test_output))
as json_js:
json_js.write('ADD_FULL_RESULTS(%s);' % full_results.read())
# copy layout test results viewer to layout-test-results directory # copy layout test results viewer to layout-test-results directory
shutil.copyfile( self.fs.copyfile(
os.path.join(WEB_TESTS_DIR, 'fast', 'harness', 'results.html'), os.path.join(WEB_TESTS_DIR, 'fast', 'harness', 'results.html'),
os.path.join(layout_test_results, 'results.html')) os.path.join(layout_test_results, 'results.html'))
...@@ -77,15 +77,15 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter): ...@@ -77,15 +77,15 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter):
This output contains a single large json file containing the raw content This output contains a single large json file containing the raw content
or artifacts which need to be extracted into their own files and removed or artifacts which need to be extracted into their own files and removed
from the json file (to avoid duplication).""" from the json file (to avoid duplication)."""
output_json = json.load( output_json = json.loads(
open(self.options.isolated_script_test_output, "r")) self.fs.read_text_file(self.options.isolated_script_test_output))
test_json = output_json["tests"] test_json = output_json["tests"]
results_dir = os.path.dirname(self.options.isolated_script_test_output) results_dir = os.path.dirname(self.options.isolated_script_test_output)
self._process_test_leaves(results_dir, output_json["path_delimiter"], self._process_test_leaves(results_dir, output_json["path_delimiter"],
test_json, "") test_json, "")
# Write output_json back to the same file after modifying it in memory # Write output_json back to the same file after modifying it in memory
with open(self.options.isolated_script_test_output, "w") as output_file: self.fs.write_text_file(self.options.isolated_script_test_output,
json.dump(output_json, output_file) json.dumps(output_json))
def _process_test_leaves(self, results_dir, delim, root_node, path_so_far): def _process_test_leaves(self, results_dir, delim, root_node, path_so_far):
"""Finds and processes each test leaf below the specified root. """Finds and processes each test leaf below the specified root.
...@@ -178,9 +178,7 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter): ...@@ -178,9 +178,7 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter):
# Note: Here we read-in the checked-in ini file and pass its contents to # Note: Here we read-in the checked-in ini file and pass its contents to
# |_write_log_artifact| to reuse code. This is probably less efficient # |_write_log_artifact| to reuse code. This is probably less efficient
# than just copying, but makes for cleaner code. # than just copying, but makes for cleaner code.
contents = "" contents = self.fs.read_text_file(expected_ini_path)
with open(expected_ini_path, "r") as ini_file:
contents = ini_file.read()
return self._write_log_artifact(test_failures.FILENAME_SUFFIX_EXPECTED, return self._write_log_artifact(test_failures.FILENAME_SUFFIX_EXPECTED,
results_dir, test_name, [contents]) results_dir, test_name, [contents])
...@@ -209,9 +207,10 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter): ...@@ -209,9 +207,10 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter):
log_artifact_full_path = os.path.join(results_dir, log_artifact_full_path = os.path.join(results_dir,
log_artifact_sub_path) log_artifact_sub_path)
if not os.path.exists(os.path.dirname(log_artifact_full_path)): if not os.path.exists(os.path.dirname(log_artifact_full_path)):
os.makedirs(os.path.dirname(log_artifact_full_path)) self.fs.maybe_make_directory(
with open(log_artifact_full_path, "w") as artifact_file: os.path.dirname(log_artifact_full_path))
artifact_file.write("\n".join(log_artifact).encode("utf-8")) self.fs.write_text_file(log_artifact_full_path,
"\n".join(log_artifact))
return log_artifact_sub_path return log_artifact_sub_path
...@@ -259,8 +258,8 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter): ...@@ -259,8 +258,8 @@ class BaseWptScriptAdapter(common.BaseIsolatedScriptArgsAdapter):
screenshot_full_path = os.path.join(results_dir,screenshot_sub_path) screenshot_full_path = os.path.join(results_dir,screenshot_sub_path)
if not os.path.exists(os.path.dirname(screenshot_full_path)): if not os.path.exists(os.path.dirname(screenshot_full_path)):
os.makedirs(os.path.dirname(screenshot_full_path)) self.fs.maybe_make_directory(
os.path.dirname(screenshot_full_path))
# Note: we are writing raw bytes to this file # Note: we are writing raw bytes to this file
with open(screenshot_full_path, "wb") as artifact_file: self.fs.write_binary_file(screenshot_full_path, image_bytes)
artifact_file.write(image_bytes)
return result return result
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