Skip to content

Commit

Permalink
Merge pull request #70 from w3c/lint-repo-root
Browse files Browse the repository at this point in the history
Add a simple test for lint.
  • Loading branch information
jgraham committed May 31, 2016
2 parents 7eaf1d3 + 14f2fbb commit 6661d00
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ install:
- pip install ./html5lib
- pip install pytest-travis-fold
script:
- py.test manifest
- py.test lint manifest
- flake8
2 changes: 1 addition & 1 deletion lint/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import lint
from . import lint
72 changes: 33 additions & 39 deletions lint/lint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function, unicode_literals

import argparse
import fnmatch
import os
Expand All @@ -7,11 +9,12 @@

from collections import defaultdict

from .. import localpaths
from six import iteritems
from six.moves import range

from manifest.sourcefile import SourceFile

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

ERROR_MSG = """You must fix all errors; for details on how to fix them, see
https://github.com/w3c/web-platform-tests/blob/master/docs/lint-tool.md
Expand All @@ -26,25 +29,14 @@
%s:%s"""

def git(command, *args):
args = list(args)

proc_kwargs = {"cwd": repo_root}

command_line = ["git", command] + args

try:
return subprocess.check_output(command_line, **proc_kwargs)
except subprocess.CalledProcessError:
raise


def all_git_paths():
for item in git("ls-tree", "-r", "--name-only", "HEAD").split("\n"):
def all_git_paths(repo_root):
command_line = ["git", "ls-tree", "-r", "--name-only", "HEAD"]
output = subprocess.check_output(command_line, cwd=repo_root)
for item in output.split("\n"):
yield item


def check_path_length(path):
def check_path_length(repo_root, path):
if len(path) + 1 > 150:
return [("PATH LENGTH", "/%s longer than maximum path length (%d > 150)" % (path, len(path) + 1), None)]
return []
Expand Down Expand Up @@ -81,9 +73,9 @@ def filter_whitelist_errors(data, path, errors):
Filter out those errors that are whitelisted in `data`.
"""

whitelisted = [False for item in xrange(len(errors))]
whitelisted = [False for item in range(len(errors))]

for file_match, whitelist_errors in data.iteritems():
for file_match, whitelist_errors in iteritems(data):
if fnmatch.fnmatch(path, file_match):
for i, (error_type, msg, line) in enumerate(errors):
if "*" in whitelist_errors:
Expand Down Expand Up @@ -112,32 +104,32 @@ def search(self, line):
return self._re.search(line)

class TrailingWhitespaceRegexp(Regexp):
pattern = "[ \t\f\v]$"
pattern = b"[ \t\f\v]$"
error = "TRAILING WHITESPACE"

class TabsRegexp(Regexp):
pattern = "^\t"
pattern = b"^\t"
error = "INDENT TABS"

class CRRegexp(Regexp):
pattern = "\r$"
pattern = b"\r$"
error = "CR AT EOL"

class W3CTestOrgRegexp(Regexp):
pattern = "w3c\-test\.org"
pattern = b"w3c\-test\.org"
error = "W3C-TEST.ORG"

class Webidl2Regexp(Regexp):
pattern = "webidl2\.js"
pattern = b"webidl2\.js"
error = "WEBIDL2.JS"

class ConsoleRegexp(Regexp):
pattern = "console\.[a-zA-Z]+\s*\("
pattern = b"console\.[a-zA-Z]+\s*\("
error = "CONSOLE"
file_extensions = [".html", ".htm", ".js", ".xht", ".html", ".svg"]

class PrintRegexp(Regexp):
pattern = "print(?:\s|\s*\()"
pattern = b"print(?:\s|\s*\()"
error = "PRINT STATEMENT"
file_extensions = [".py"]

Expand All @@ -150,7 +142,7 @@ class PrintRegexp(Regexp):
ConsoleRegexp,
PrintRegexp]]

def check_regexp_line(path, f):
def check_regexp_line(repo_root, path, f):
errors = []

applicable_regexps = [regexp for regexp in regexps if regexp.applies(path)]
Expand All @@ -162,7 +154,7 @@ def check_regexp_line(path, f):

return errors

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

errors = []
Expand Down Expand Up @@ -213,7 +205,7 @@ def check_parsed(path, f):
"testharnessreport": False}
required_elements = [key for key, value in {"testharness": True,
"testharnessreport": len(testharnessreport_nodes) > 0,
"timeout": len(source_file.timeout_nodes) > 0}.iteritems()
"timeout": len(source_file.timeout_nodes) > 0}.items()
if value]

for elem in source_file.root.iter():
Expand All @@ -239,18 +231,18 @@ def check_parsed(path, f):

def output_errors(errors):
for error_type, error, line_number in errors:
print "%s: %s" % (error_type, error)
print("%s: %s" % (error_type, error))

def output_error_count(error_count):
if not error_count:
return

by_type = " ".join("%s: %d" % item for item in error_count.iteritems())
by_type = " ".join("%s: %d" % item for item in error_count.items())
count = sum(error_count.values())
if count == 1:
print "There was 1 error (%s)" % (by_type,)
print("There was 1 error (%s)" % (by_type,))
else:
print "There were %d errors (%s)" % (count, by_type)
print("There were %d errors (%s)" % (count, by_type))

def parse_args():
parser = argparse.ArgumentParser()
Expand All @@ -259,18 +251,20 @@ def parse_args():
return parser.parse_args()

def main():
from .. import localpaths
repo_root = localpaths.repo_root
args = parse_args()
paths = args.paths if args.paths else all_git_paths()
return lint(paths)
paths = args.paths if args.paths else all_git_paths(repo_root)
return lint(repo_root, paths)

def lint(paths):
def lint(repo_root, paths):
error_count = defaultdict(int)
last = None

whitelist = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist"))

def run_lint(path, fn, last, *args):
errors = filter_whitelist_errors(whitelist, path, fn(path, *args))
errors = filter_whitelist_errors(whitelist, path, fn(repo_root, path, *args))
if errors:
last = (errors[-1][0], path)

Expand All @@ -294,7 +288,7 @@ def run_lint(path, fn, last, *args):

output_error_count(error_count)
if error_count:
print ERROR_MSG % (last[0], last[1], last[0], last[1])
print(ERROR_MSG % (last[0], last[1], last[0], last[1]))
return sum(error_count.itervalues())

path_lints = [check_path_length]
Expand Down
Empty file added lint/tests/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions lint/tests/test_lint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from lint.lint import filter_whitelist_errors

def test_lint():
filtered = filter_whitelist_errors({}, '', [])
assert filtered == []

0 comments on commit 6661d00

Please sign in to comment.