Skip to content

Commit

Permalink
[WPT] Unify mixed-content and referrer-policy generator scripts
Browse files Browse the repository at this point in the history
To prepare unifying generator mechanisms of mixed-content and
referrer-policy WPT tests, this CL unifies minor diffs
between their generator scripts.

A part of this CL applies
https://chromium-review.googlesource.com/c/chromium/src/+/1389926
to mixed-content test generators, which is no-op because
mixed-content tests don't use 'expansion=override' feature.

This CL doesn't affect the generated results and thus
this CL doesn't contain any changes to generated files.

Bug: 906850
Change-Id: I1976422799fd1344fbd471a606b174dc69d40eba
Reviewed-on: https://chromium-review.googlesource.com/c/1410085
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#624410}
  • Loading branch information
hiroshige-g authored and chromium-wpt-export-bot committed Jan 19, 2019
1 parent 1fc2d6a commit 4a04c1d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 61 deletions.
37 changes: 26 additions & 11 deletions mixed-content/generic/tools/generate.py
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

import copy
import os, sys, json
from common_paths import *
import spec_validator
Expand Down Expand Up @@ -67,10 +68,11 @@ def generate_selection(selection, spec, test_html_template_basename):
test_html_template_basename)
generated_disclaimer = disclaimer_template \
% {'generating_script_filename': os.path.relpath(__file__,
test_root_directory),
test_root_directory),
'html_template_filename': os.path.relpath(html_template_filename,
test_root_directory)}
test_root_directory)}

# Adjust the template for the test invoking JS. Indent it to look nice.
selection['generated_disclaimer'] = generated_disclaimer.rstrip()
test_description_template = \
test_description_template.rstrip().replace("\n", "\n" + " " * 33)
Expand Down Expand Up @@ -104,6 +106,7 @@ def generate_selection(selection, spec, test_html_template_basename):
# Write out the generated HTML file.
write_file(test_filename, test_html_template % selection)


def generate_test_source_files(spec_json, target):
test_expansion_schema = spec_json['test_expansion_schema']
specification = spec_json['specification']
Expand All @@ -116,33 +119,45 @@ def generate_test_source_files(spec_json, target):
html_template = "test.%s.html.template" % target

artifact_order = test_expansion_schema.keys() + ['name']
artifact_order.remove('expansion')

# Create list of excluded tests.
exclusion_dict = {}
for excluded_pattern in spec_json['excluded_tests']:
excluded_expansion = \
expand_pattern(excluded_pattern,
test_expansion_schema)
for excluded_selection in permute_expansion(excluded_expansion, artifact_order):
expand_pattern(excluded_pattern, test_expansion_schema)
for excluded_selection in permute_expansion(excluded_expansion,
artifact_order):
excluded_selection_path = selection_pattern % excluded_selection
exclusion_dict[excluded_selection_path] = True

for spec in specification:
# Used to make entries with expansion="override" override preceding
# entries with the same |selection_path|.
output_dict = {}

for expansion_pattern in spec['test_expansion']:
expansion = expand_pattern(expansion_pattern,
test_expansion_schema)
expansion = expand_pattern(expansion_pattern, test_expansion_schema)
for selection in permute_expansion(expansion, artifact_order):
selection_path = selection_pattern % selection
if not selection_path in exclusion_dict:
generate_selection(selection,
spec,
html_template)
if selection_path in output_dict:
if expansion_pattern['expansion'] != 'override':
print("Error: %s's expansion is default but overrides %s" % (selection['name'], output_dict[selection_path]['name']))
sys.exit(1)
output_dict[selection_path] = copy.deepcopy(selection)
else:
print('Excluding selection:', selection_path)

for selection_path in output_dict:
selection = output_dict[selection_path]
generate_selection(selection,
spec,
html_template)


def main(target, spec_filename):
spec_json = load_spec_json(spec_filename);
spec_json = load_spec_json(spec_filename)
spec_validator.assert_valid_spec_json(spec_json)
generate_test_source_files(spec_json, target)

Expand Down
25 changes: 16 additions & 9 deletions mixed-content/generic/tools/spec_validator.py
Expand Up @@ -30,17 +30,17 @@ def assert_contains(obj, field):
assert field in obj, 'Must contain field "%s"' % field


def assert_string_from(obj, field, items):
def assert_value_from(obj, field, items):
assert obj[field] in items, \
'Field "%s" must be from: %s' % (field, str(items))


def assert_string_or_list_items_from(obj, field, items):
if isinstance(obj[field], basestring):
assert_string_from(obj, field, items)
def assert_atom_or_list_items_from(obj, field, items):
if isinstance(obj[field], basestring) or isinstance(obj[field], int):
assert_value_from(obj, field, items)
return

assert isinstance(obj[field], list), "%s must be a list!" % field
assert isinstance(obj[field], list), '%s must be a list' % field
for allowed_value in obj[field]:
assert allowed_value != '*', "Wildcard is not supported for lists!"
assert allowed_value in items, \
Expand All @@ -63,8 +63,8 @@ def assert_value_unique_in(value, used_values):

def assert_valid_artifact(exp_pattern, artifact_key, schema):
if isinstance(schema, list):
assert_string_or_list_items_from(exp_pattern, artifact_key,
["*"] + schema)
assert_atom_or_list_items_from(exp_pattern, artifact_key,
["*"] + schema)
return

for sub_artifact_key, sub_schema in schema.iteritems():
Expand Down Expand Up @@ -110,7 +110,7 @@ def validate(spec_json, details):
for spec_exp in spec['test_expansion']:
details['object'] = spec_exp
assert_non_empty_string(spec_exp, 'name')
# The name is unique in same expansion group.
# The name is unique in same expansion group.
assert_value_unique_in((spec_exp['expansion'], spec_exp['name']),
used_spec_names)
assert_contains_only_fields(spec_exp, valid_test_expansion_fields)
Expand All @@ -136,7 +136,14 @@ def validate(spec_json, details):
for excluded_test_expansion in excluded_tests:
assert_contains_only_fields(excluded_test_expansion,
valid_test_expansion_fields)

details['object'] = excluded_test_expansion
for artifact in test_expansion_schema:
details['test_expansion_field'] = artifact
assert_valid_artifact(
excluded_test_expansion,
artifact,
test_expansion_schema[artifact])
del details['test_expansion_field']

del details['object']

Expand Down
17 changes: 11 additions & 6 deletions referrer-policy/generic/tools/common_paths.py
Expand Up @@ -30,26 +30,31 @@ def get_template(basename):
return f.read()


def write_file(filename, contents):
with open(filename, "w") as f:
f.write(contents)


def read_nth_line(fp, line_number):
fp.seek(0)
for i, line in enumerate(fp):
if (i + 1) == line_number:
return line


def load_spec_json():
def load_spec_json(path_to_spec = None):
if path_to_spec is None:
path_to_spec = spec_filename

re_error_location = re.compile('line ([0-9]+) column ([0-9]+)')
with open(spec_filename, "r") as f:
with open(path_to_spec, "r") as f:
try:
spec_json = json.load(f)
return json.load(f)
except ValueError as ex:
print(ex.message)
match = re_error_location.search(ex.message)
if match:
line_number, column = int(match.group(1)), int(match.group(2))
print(read_nth_line(f, line_number).rstrip())
print(" " * (column - 1) + "^")

sys.exit(1)

return spec_json
67 changes: 38 additions & 29 deletions referrer-policy/generic/tools/generate.py
Expand Up @@ -9,24 +9,29 @@
import argparse


def expand_test_expansion_pattern(spec_test_expansion, test_expansion_schema):
def expand_pattern(expansion_pattern, test_expansion_schema):
expansion = {}
for artifact in spec_test_expansion:
artifact_value = spec_test_expansion[artifact]
for artifact_key in expansion_pattern:
artifact_value = expansion_pattern[artifact_key]
if artifact_value == '*':
expansion[artifact] = test_expansion_schema[artifact]
expansion[artifact_key] = test_expansion_schema[artifact_key]
elif isinstance(artifact_value, list):
expansion[artifact] = artifact_value
expansion[artifact_key] = artifact_value
elif isinstance(artifact_value, dict):
# Flattened expansion.
expansion[artifact_key] = []
values_dict = expand_pattern(artifact_value,
test_expansion_schema[artifact_key])
for sub_key in values_dict.keys():
expansion[artifact_key] += values_dict[sub_key]
else:
expansion[artifact] = [artifact_value]
expansion[artifact_key] = [artifact_value]

return expansion


def permute_expansion(expansion, selection = {}, artifact_index = 0):
artifact_order = ['delivery_method', 'redirection', 'origin',
'source_protocol', 'target_protocol', 'subresource',
'referrer_url', 'name']
def permute_expansion(expansion, artifact_order, selection = {}, artifact_index = 0):
assert isinstance(artifact_order, list), "artifact_order should be a list"

if artifact_index >= len(artifact_order):
yield selection
Expand All @@ -37,6 +42,7 @@ def permute_expansion(expansion, selection = {}, artifact_index = 0):
for artifact_value in expansion[artifact_key]:
selection[artifact_key] = artifact_value
for next_selection in permute_expansion(expansion,
artifact_order,
selection,
artifact_index + 1):
yield next_selection
Expand Down Expand Up @@ -116,29 +122,31 @@ def generate_selection(selection, spec, subresource_path,
selection['meta_delivery_method'] = "\n " + \
selection['meta_delivery_method']

with open(test_filename, 'w') as f:
f.write(test_html_template % selection)
# Write out the generated HTML file.
write_file(test_filename, test_html_template % selection)


def generate_test_source_files(spec_json, target):
test_expansion_schema = spec_json['test_expansion_schema']
specification = spec_json['specification']

spec_json_js_template = get_template('spec_json.js.template')
with open(generated_spec_json_filename, 'w') as f:
f.write(spec_json_js_template
% {'spec_json': json.dumps(spec_json)})
write_file(generated_spec_json_filename,
spec_json_js_template % {'spec_json': json.dumps(spec_json)})

# Choose a debug/release template depending on the target.
html_template = "test.%s.html.template" % target

artifact_order = test_expansion_schema.keys() + ['name']
artifact_order.remove('expansion')

# Create list of excluded tests.
exclusion_dict = {}
for excluded_pattern in spec_json['excluded_tests']:
excluded_expansion = \
expand_test_expansion_pattern(excluded_pattern,
test_expansion_schema)
for excluded_selection in permute_expansion(excluded_expansion):
expand_pattern(excluded_pattern, test_expansion_schema)
for excluded_selection in permute_expansion(excluded_expansion,
artifact_order):
excluded_selection_path = selection_pattern % excluded_selection
exclusion_dict[excluded_selection_path] = True

Expand All @@ -147,14 +155,13 @@ def generate_test_source_files(spec_json, target):
# entries with the same |selection_path|.
output_dict = {}

for spec_test_expansion in spec['test_expansion']:
expansion = expand_test_expansion_pattern(spec_test_expansion,
test_expansion_schema)
for selection in permute_expansion(expansion):
for expansion_pattern in spec['test_expansion']:
expansion = expand_pattern(expansion_pattern, test_expansion_schema)
for selection in permute_expansion(expansion, artifact_order):
selection_path = selection_pattern % selection
if not selection_path in exclusion_dict:
if selection_path in output_dict:
if spec_test_expansion['expansion'] != 'override':
if expansion_pattern['expansion'] != 'override':
print("Error: %s's expansion is default but overrides %s" % (selection['name'], output_dict[selection_path]['name']))
sys.exit(1)
output_dict[selection_path] = copy.deepcopy(selection)
Expand All @@ -166,13 +173,13 @@ def generate_test_source_files(spec_json, target):
subresource_path = \
spec_json["subresource_path"][selection["subresource"]]
generate_selection(selection,
spec,
subresource_path,
html_template)
spec,
subresource_path,
html_template)


def main(target):
spec_json = load_spec_json();
def main(target, spec_filename):
spec_json = load_spec_json(spec_filename)
spec_validator.assert_valid_spec_json(spec_json)
generate_test_source_files(spec_json, target)

Expand All @@ -182,6 +189,8 @@ def main(target):
parser.add_argument('-t', '--target', type = str,
choices = ("release", "debug"), default = "release",
help = 'Sets the appropriate template for generating tests')
parser.add_argument('-s', '--spec', type = str, default = None,
help = 'Specify a file used for describing and generating the tests')
# TODO(kristijanburnik): Add option for the spec_json file.
args = parser.parse_args()
main(args.target)
main(args.target, args.spec)

0 comments on commit 4a04c1d

Please sign in to comment.