2017-05-01 18:04:05 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright (C) 2017 Mike Pennisi. All rights reserved.
|
|
|
|
# This code is governed by the BSD license found in the LICENSE file.
|
|
|
|
|
|
|
|
import argparse
|
2018-01-11 16:20:37 +01:00
|
|
|
import inflect
|
|
|
|
import os
|
2018-09-16 17:43:36 +02:00
|
|
|
try:
|
2019-08-08 20:11:34 +02:00
|
|
|
from pip._internal import main as pip
|
|
|
|
from pip._internal.req import parse_requirements, InstallRequirement
|
2018-09-16 17:43:36 +02:00
|
|
|
except ImportError:
|
2019-08-08 20:11:34 +02:00
|
|
|
from pip import main as pip
|
|
|
|
from pip.req import parse_requirements, InstallRequirement
|
2018-01-11 16:20:37 +01:00
|
|
|
import sys
|
2017-10-18 18:19:37 +02:00
|
|
|
|
2018-01-11 16:20:37 +01:00
|
|
|
ie = inflect.engine()
|
2017-10-18 18:19:37 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
__import__('yaml')
|
|
|
|
except ImportError:
|
2018-09-16 17:43:36 +02:00
|
|
|
for item in parse_requirements("./tools/lint/requirements.txt", session="test262"):
|
2019-08-08 20:11:34 +02:00
|
|
|
if isinstance(item, InstallRequirement):
|
2017-10-18 18:19:37 +02:00
|
|
|
requirement = item.name
|
|
|
|
|
|
|
|
if len(str(item.req.specifier)) > 0:
|
|
|
|
requirement = "{}{}".format(requirement, item.req.specifier)
|
|
|
|
|
|
|
|
# print(requirement)
|
2019-08-08 20:11:34 +02:00
|
|
|
pip(['install', requirement])
|
2017-10-18 18:19:37 +02:00
|
|
|
|
2017-05-01 18:04:05 +02:00
|
|
|
|
|
|
|
from lib.collect_files import collect_files
|
2017-12-28 23:01:17 +01:00
|
|
|
from lib.checks.esid import CheckEsid
|
2017-06-12 23:59:14 +02:00
|
|
|
from lib.checks.features import CheckFeatures
|
2017-05-01 18:04:05 +02:00
|
|
|
from lib.checks.frontmatter import CheckFrontmatter
|
2017-09-08 18:45:58 +02:00
|
|
|
from lib.checks.harnessfeatures import CheckHarnessFeatures
|
2018-06-08 04:38:36 +02:00
|
|
|
from lib.checks.harness import CheckHarness
|
2017-05-01 18:04:05 +02:00
|
|
|
from lib.checks.license import CheckLicense
|
2017-04-29 21:33:06 +02:00
|
|
|
from lib.checks.negative import CheckNegative
|
2018-05-02 22:14:31 +02:00
|
|
|
from lib.checks.filename import CheckFileName
|
2019-02-25 21:11:17 +01:00
|
|
|
from lib.checks.nopadding import CheckNoPadding
|
2019-03-06 18:24:44 +01:00
|
|
|
from lib.checks.flags import CheckFlags
|
2017-05-01 18:04:05 +02:00
|
|
|
from lib.eprint import eprint
|
|
|
|
import lib.frontmatter
|
2018-12-18 18:39:57 +01:00
|
|
|
import lib.exceptions
|
2017-05-01 18:04:05 +02:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Test262 linting tool')
|
2018-12-18 18:39:57 +01:00
|
|
|
parser.add_argument('--exceptions',
|
2017-05-01 18:04:05 +02:00
|
|
|
type=argparse.FileType('r'),
|
|
|
|
help='file containing expected linting errors')
|
|
|
|
parser.add_argument('path',
|
|
|
|
nargs='+',
|
|
|
|
help='file name or directory of files to lint')
|
|
|
|
|
2017-04-29 21:33:06 +02:00
|
|
|
checks = [
|
2018-01-11 16:20:37 +01:00
|
|
|
CheckEsid(),
|
2018-05-02 22:14:31 +02:00
|
|
|
CheckFileName(),
|
2018-01-11 16:20:37 +01:00
|
|
|
CheckFrontmatter(),
|
|
|
|
CheckFeatures('features.txt'),
|
|
|
|
CheckHarnessFeatures(),
|
2018-06-08 04:38:36 +02:00
|
|
|
CheckHarness(),
|
2018-01-11 16:20:37 +01:00
|
|
|
CheckLicense(),
|
2019-02-25 21:11:17 +01:00
|
|
|
CheckNegative(),
|
2019-03-06 18:24:44 +01:00
|
|
|
CheckNoPadding(),
|
|
|
|
CheckFlags(),
|
2018-01-11 16:20:37 +01:00
|
|
|
]
|
2017-05-01 18:04:05 +02:00
|
|
|
|
|
|
|
def lint(file_names):
|
|
|
|
errors = dict()
|
|
|
|
|
|
|
|
for file_name in file_names:
|
|
|
|
with open(file_name, 'r') as f:
|
|
|
|
content = f.read()
|
|
|
|
meta = lib.frontmatter.parse(content)
|
|
|
|
for check in checks:
|
|
|
|
error = check.run(file_name, meta, content)
|
|
|
|
|
|
|
|
if error is not None:
|
|
|
|
if file_name not in errors:
|
|
|
|
errors[file_name] = dict()
|
|
|
|
errors[file_name][check.ID] = error
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = parser.parse_args()
|
2018-12-18 18:39:57 +01:00
|
|
|
if args.exceptions:
|
|
|
|
exceptions = lib.exceptions.parse(args.exceptions)
|
2017-05-01 18:04:05 +02:00
|
|
|
else:
|
2018-12-18 18:39:57 +01:00
|
|
|
exceptions = dict()
|
2017-05-01 18:04:05 +02:00
|
|
|
|
|
|
|
files = [path for _path in args.path for path in collect_files(_path)]
|
|
|
|
file_count = len(files)
|
2018-01-11 16:20:37 +01:00
|
|
|
print 'Linting %s %s' % (file_count, ie.plural('file', file_count))
|
2017-05-01 18:04:05 +02:00
|
|
|
|
|
|
|
all_errors = lint(files)
|
|
|
|
unexpected_errors = dict(all_errors)
|
|
|
|
|
|
|
|
for file_name, failures in all_errors.iteritems():
|
2018-12-18 18:39:57 +01:00
|
|
|
if file_name not in exceptions:
|
2017-05-01 18:04:05 +02:00
|
|
|
continue
|
2018-12-18 18:39:57 +01:00
|
|
|
if set(failures.keys()) == exceptions[file_name]:
|
2017-05-01 18:04:05 +02:00
|
|
|
del unexpected_errors[file_name]
|
|
|
|
|
|
|
|
error_count = len(unexpected_errors)
|
2018-01-11 16:20:37 +01:00
|
|
|
print 'Linting complete. %s %s found.' % (error_count, ie.plural('error', error_count))
|
2017-05-01 18:04:05 +02:00
|
|
|
|
|
|
|
if error_count == 0:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2018-01-11 16:20:37 +01:00
|
|
|
for file_name, failures in iter(sorted(unexpected_errors.iteritems())):
|
2017-05-01 18:04:05 +02:00
|
|
|
for ID, message in failures.iteritems():
|
2018-01-11 16:20:37 +01:00
|
|
|
eprint('%s: %s - %s' % (os.path.abspath(file_name), ID, message))
|
2017-05-01 18:04:05 +02:00
|
|
|
|
|
|
|
sys.exit(1)
|