Skip to content

Commit

Permalink
Validate existence of test reference files (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and gsnedders committed Feb 28, 2017
1 parent f2a532c commit 2e6b3ea
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from manifest.sourcefile import SourceFile, js_meta_re, python_meta_re
from six import binary_type, iteritems, itervalues
from six.moves import range
from six.moves.urllib.parse import urlsplit

here = os.path.abspath(os.path.split(__file__)[0])

Expand Down Expand Up @@ -221,6 +222,31 @@ def check_parsed(repo_root, path, f, css_mode):
if source_file.type == "visual" and not source_file.name_is_visual:
return [("CONTENT-VISUAL", "Visual test whose filename doesn't end in '-visual'", path, None)]

for reftest_node in source_file.reftest_nodes:
href = reftest_node.attrib.get("href", "")
parts = urlsplit(href)
if len(parts.netloc) is not 0 or len(parts.scheme) is not 0:
errors.append(("ABSOLUTE-URL-REF",
"Reference test with a reference file specified via an absolute URL: '%s'" % href, path, None))
continue

ref_path = parts.path

if ref_path[0] == "/":
# Remove the leading "forward-slash" character in root-relative
# URLs so that the `os.path.join` method does not interpret the
# value as an absolute path.
ref_path = ref_path[1:]
else:
ref_path = source_file.dir_path + "/" + ref_path

reference_file = os.path.join(repo_root, ref_path)
reference_rel = reftest_node.attrib.get("rel", "")

if not os.path.isfile(reference_file):
errors.append(("NON-EXISTENT-REF",
"Reference test with a non-existent '%s' relationship reference: '%s'" % (reference_rel, href), path, None))

if len(source_file.timeout_nodes) > 1:
errors.append(("MULTIPLE-TIMEOUT", "More than one meta name='timeout'", path, None))

Expand Down
1 change: 1 addition & 0 deletions lint/tests/dummy/dependency.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is used to demonstrate acceptance of root-relative reftest references.
1 change: 1 addition & 0 deletions lint/tests/dummy/ref/absolute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="match" href="http://example.com/reference.html">
1 change: 1 addition & 0 deletions lint/tests/dummy/ref/existent_relative.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="match" href="existent_relative.html">
1 change: 1 addition & 0 deletions lint/tests/dummy/ref/existent_root_relative.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="match" href="/dependency.html">
1 change: 1 addition & 0 deletions lint/tests/dummy/ref/non_existent_relative.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="match" href="non_existent_file.html">
1 change: 1 addition & 0 deletions lint/tests/dummy/ref/non_existent_root_relative.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="match" href="/non_existent_file.html">
66 changes: 66 additions & 0 deletions lint/tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,72 @@ def test_lint_failing(capsys):
assert err == ""


def test_ref_existent_relative(capsys):
with _mock_lint("check_path") as mocked_check_path:
with _mock_lint("check_file_contents") as mocked_check_file_contents:
rv = lint(_dummy_repo, ["ref/existent_relative.html"], False, False)
assert rv == 0
assert mocked_check_path.call_count == 1
assert mocked_check_file_contents.call_count == 1
out, err = capsys.readouterr()
assert out == ""
assert err == ""


def test_ref_existent_root_relative(capsys):
with _mock_lint("check_path") as mocked_check_path:
with _mock_lint("check_file_contents") as mocked_check_file_contents:
rv = lint(_dummy_repo, ["ref/existent_root_relative.html"], False, False)
assert rv == 0
assert mocked_check_path.call_count == 1
assert mocked_check_file_contents.call_count == 1
out, err = capsys.readouterr()
assert out == ""
assert err == ""


def test_ref_non_existent_relative(capsys):
with _mock_lint("check_path") as mocked_check_path:
with _mock_lint("check_file_contents") as mocked_check_file_contents:
rv = lint(_dummy_repo, ["ref/non_existent_relative.html"], False, False)
assert rv == 1
assert mocked_check_path.call_count == 1
assert mocked_check_file_contents.call_count == 1
out, err = capsys.readouterr()
assert "NON-EXISTENT-REF" in out
assert "ref/non_existent_relative.html" in out
assert "non_existent_file.html" in out
assert err == ""


def test_ref_non_existent_root_relative(capsys):
with _mock_lint("check_path") as mocked_check_path:
with _mock_lint("check_file_contents") as mocked_check_file_contents:
rv = lint(_dummy_repo, ["ref/non_existent_root_relative.html"], False, False)
assert rv == 1
assert mocked_check_path.call_count == 1
assert mocked_check_file_contents.call_count == 1
out, err = capsys.readouterr()
assert "NON-EXISTENT-REF" in out
assert "ref/non_existent_root_relative.html" in out
assert "/non_existent_file.html" in out
assert err == ""


def test_ref_absolute_url(capsys):
with _mock_lint("check_path") as mocked_check_path:
with _mock_lint("check_file_contents") as mocked_check_file_contents:
rv = lint(_dummy_repo, ["ref/absolute.html"], False, False)
assert rv == 1
assert mocked_check_path.call_count == 1
assert mocked_check_file_contents.call_count == 1
out, err = capsys.readouterr()
assert "ABSOLUTE-URL-REF" in out
assert "http://example.com/reference.html" in out
assert "ref/absolute.html" in out
assert err == ""


def test_lint_passing_and_failing(capsys):
with _mock_lint("check_path") as mocked_check_path:
with _mock_lint("check_file_contents") as mocked_check_file_contents:
Expand Down

0 comments on commit 2e6b3ea

Please sign in to comment.