Skip to content

Commit

Permalink
Merge pull request #102 from w3c/lint-parse
Browse files Browse the repository at this point in the history
Add tests for parser-based lints; r=gsnedders
  • Loading branch information
gsnedders authored Aug 15, 2016
2 parents ed860f7 + 518f95c commit 80b3765
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def check_regexp_line(repo_root, path, f):
return errors

def check_parsed(repo_root, path, f):
source_file = SourceFile(repo_root, path, "/")
source_file = SourceFile(repo_root, path, "/", contents=f.read())

errors = []

Expand Down
150 changes: 134 additions & 16 deletions lint/tests/test_file_lints.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from __future__ import unicode_literals

from ..lint import check_file_contents
import os
import pytest
import six

INTERESTING_FILE_NAMES = {
"python": [
"test.py",
],
"web": [
"js": [
"test.js",
],
"web-lax": [
"test.htm",
"test.html",
"test.js",
],
"web-strict": [
"test.svg",
"test.xht",
"test.xhtml",
Expand All @@ -21,40 +26,56 @@
def check_with_files(input_bytes):
return {
filename: (check_file_contents("", filename, six.BytesIO(input_bytes)), kind)
for (kind, filenames) in INTERESTING_FILE_NAMES.items()
for filename in filenames
for (filename, kind) in
(
(os.path.join("html", filename), kind)
for (kind, filenames) in INTERESTING_FILE_NAMES.items()
for filename in filenames
)
}


def test_trailing_whitespace():
error_map = check_with_files(b"test; ")

for (filename, (errors, _)) in error_map.items():
assert errors == [("TRAILING WHITESPACE", "Whitespace at EOL", filename, 1)]
for (filename, (errors, kind)) in error_map.items():
expected = [("TRAILING WHITESPACE", "Whitespace at EOL", filename, 1)]
if kind == "web-strict":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, None))
assert errors == expected


def test_indent_tabs():
error_map = check_with_files(b"def foo():\n\x09pass")

for (filename, (errors, _)) in error_map.items():
assert errors == [("INDENT TABS", "Tabs used for indentation", filename, 2)]
for (filename, (errors, kind)) in error_map.items():
expected = [("INDENT TABS", "Tabs used for indentation", filename, 2)]
if kind == "web-strict":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, None))
assert errors == expected


def test_cr_not_at_eol():
error_map = check_with_files(b"line1\rline2\r")

for (filename, (errors, _)) in error_map.items():
assert errors == [("CR AT EOL", "CR character in line separator", filename, 1)]
for (filename, (errors, kind)) in error_map.items():
expected = [("CR AT EOL", "CR character in line separator", filename, 1)]
if kind == "web-strict":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, None))
assert errors == expected


def test_cr_at_eol():
error_map = check_with_files(b"line1\r\nline2\r\n")

for (filename, (errors, _)) in error_map.items():
assert errors == [
for (filename, (errors, kind)) in error_map.items():
expected = [
("CR AT EOL", "CR character in line separator", filename, 1),
("CR AT EOL", "CR character in line separator", filename, 2),
]
if kind == "web-strict":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, None))
assert errors == expected


def test_w3c_test_org():
Expand All @@ -64,6 +85,8 @@ def test_w3c_test_org():
expected = [("W3C-TEST.ORG", "External w3c-test.org domain used", filename, 1)]
if kind == "python":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, 1))
elif kind == "web-strict":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, None))
assert errors == expected


Expand All @@ -74,20 +97,107 @@ def test_webidl2_js():
expected = [("WEBIDL2.JS", "Legacy webidl2.js script used", filename, 1)]
if kind == "python":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, 1))
elif kind == "web-strict":
expected.append(("PARSE-FAILED", "Unable to parse file", filename, None))
assert errors == expected


def test_console():
error_map = check_with_files(b"console.log('error');\nconsole.error ('log')\n")
error_map = check_with_files(b"<script>\nconsole.log('error');\nconsole.error ('log')\n</script>")

for (filename, (errors, kind)) in error_map.items():
if kind == "web":
if kind in ["web-lax", "web-strict", "js"]:
assert errors == [
("CONSOLE", "Console logging API used", filename, 1),
("CONSOLE", "Console logging API used", filename, 2),
("CONSOLE", "Console logging API used", filename, 3),
]
else:
assert errors == []
assert errors == [("PARSE-FAILED", "Unable to parse file", filename, 1)]


def test_meta_timeout():
code = b"""
<html xmlns="http://www.w3.org/1999/xhtml">
<meta name="timeout" />
<meta name="timeout" content="short" />
<meta name="timeout" content="long" />
</html>
"""
error_map = check_with_files(code)

for (filename, (errors, kind)) in error_map.items():
if kind in ["web-lax", "web-strict"]:
assert errors == [
("MULTIPLE-TIMEOUT", "More than one meta name='timeout'", filename, None),
("INVALID-TIMEOUT", "Invalid timeout value ", filename, None),
("INVALID-TIMEOUT", "Invalid timeout value short", filename, None),
]
elif kind == "python":
assert errors == [
("PARSE-FAILED", "Unable to parse file", filename, 2),
]


def test_early_testharnessreport():
code = b"""
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testharness.js"></script>
</html>
"""
error_map = check_with_files(code)

for (filename, (errors, kind)) in error_map.items():
if kind in ["web-lax", "web-strict"]:
assert errors == [
("EARLY-TESTHARNESSREPORT", "testharnessreport.js script seen before testharness.js script", filename, None),
]
elif kind == "python":
assert errors == [
("PARSE-FAILED", "Unable to parse file", filename, 2),
]


def test_multiple_testharness():
code = b"""
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharness.js"></script>
</html>
"""
error_map = check_with_files(code)

for (filename, (errors, kind)) in error_map.items():
if kind in ["web-lax", "web-strict"]:
assert errors == [
("MULTIPLE-TESTHARNESS", "More than one <script src='/resources/testharness.js'>", filename, None),
("MISSING-TESTHARNESSREPORT", "Missing <script src='/resources/testharnessreport.js'>", filename, None),
]
elif kind == "python":
assert errors == [
("PARSE-FAILED", "Unable to parse file", filename, 2),
]


def test_multiple_testharnessreport():
code = b"""
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testharnessreport.js"></script>
</html>
"""
error_map = check_with_files(code)

for (filename, (errors, kind)) in error_map.items():
if kind in ["web-lax", "web-strict"]:
assert errors == [
("MULTIPLE-TESTHARNESSREPORT", "More than one <script src='/resources/testharnessreport.js'>", filename, None),
]
elif kind == "python":
assert errors == [
("PARSE-FAILED", "Unable to parse file", filename, 2),
]


@pytest.mark.skipif(six.PY3, reason="Cannot parse print statements from python 3")
Expand All @@ -100,6 +210,10 @@ def test_print_statement():
("PRINT STATEMENT", "Print function used", filename, 2),
("PRINT STATEMENT", "Print function used", filename, 3),
]
elif kind == "web-strict":
assert errors == [
("PARSE-FAILED", "Unable to parse file", filename, None),
]
else:
assert errors == []

Expand All @@ -112,6 +226,10 @@ def test_print_function():
assert errors == [
("PRINT STATEMENT", "Print function used", filename, 2),
]
elif kind == "web-strict":
assert errors == [
("PARSE-FAILED", "Unable to parse file", filename, None),
]
else:
assert errors == []

Expand Down

0 comments on commit 80b3765

Please sign in to comment.