From 66bd632bae7e8c1ae55d4f1239d08143224e4a17 Mon Sep 17 00:00:00 2001 From: jugglinmike Date: Mon, 12 Jun 2017 17:59:14 -0400 Subject: Lint test `features` tag (#1060) A recent commit introduced a document that enumerated acceptable values for the test "features" metadata tag. However, this list was incomplete, and maintaining it placed extra burden on the project owners. Restructure the document into a machine-readable format. Add entries for all previously-omitted values. Add in-line documentation with recommendations for maintenance of the file. Extend the project's linting tool to validate tests according to the document's contents. --- tools/lint/lib/checks/features.py | 38 +++++++++++++++++++++++ tools/lint/lint.py | 3 +- tools/lint/test/fixtures/features_empty.js | 11 +++++++ tools/lint/test/fixtures/features_unrecognized.js | 11 +++++++ tools/lint/test/fixtures/features_valid.js | 10 ++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tools/lint/lib/checks/features.py create mode 100644 tools/lint/test/fixtures/features_empty.js create mode 100644 tools/lint/test/fixtures/features_unrecognized.js create mode 100644 tools/lint/test/fixtures/features_valid.js (limited to 'tools') diff --git a/tools/lint/lib/checks/features.py b/tools/lint/lib/checks/features.py new file mode 100644 index 000000000..f7cae4894 --- /dev/null +++ b/tools/lint/lib/checks/features.py @@ -0,0 +1,38 @@ +from ..check import Check + +_REQUIRED_FIELDS = set(['description']) +_OPTIONAL_FIELDS = set([ + 'author', 'es5id', 'es6id', 'esid', 'features', 'flags', 'includes', + 'info', 'negative', 'timeout' +]) +_VALID_FIELDS = _REQUIRED_FIELDS | _OPTIONAL_FIELDS + +class CheckFeatures(Check): + '''Ensure tests specify only `features` from a list of valid values.''' + ID = 'FEATURES' + + def __init__(self, filename): + with open(filename, 'r') as f: + self.valid_features = self._parse(f.read()) + + @staticmethod + def _parse(content): + features = [] + for line in content.split(): + if not line or line.startswith('#'): + continue + features.append(line) + return features + + def run(self, name, meta, source): + if not meta or 'features' not in meta: + return + + features = meta['features'] + + if len(features) == 0: + return 'If present, the `features` tag must have at least one member' + + for feature in features: + if feature not in self.valid_features: + return 'Unrecognized feature: "%s"' % feature diff --git a/tools/lint/lint.py b/tools/lint/lint.py index b26569516..9cdbca474 100755 --- a/tools/lint/lint.py +++ b/tools/lint/lint.py @@ -6,6 +6,7 @@ import argparse import sys from lib.collect_files import collect_files +from lib.checks.features import CheckFeatures from lib.checks.frontmatter import CheckFrontmatter from lib.checks.license import CheckLicense from lib.eprint import eprint @@ -20,7 +21,7 @@ parser.add_argument('path', nargs='+', help='file name or directory of files to lint') -checks = [CheckFrontmatter(), CheckLicense()] +checks = [CheckFrontmatter(), CheckFeatures('features.txt'), CheckLicense()] def lint(file_names): errors = dict() diff --git a/tools/lint/test/fixtures/features_empty.js b/tools/lint/test/fixtures/features_empty.js new file mode 100644 index 000000000..47322aa59 --- /dev/null +++ b/tools/lint/test/fixtures/features_empty.js @@ -0,0 +1,11 @@ +FEATURES +^ expected errors | v input +// Copyright (C) 2017 Mike Pennisi. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-assignment-operators-static-semantics-early-errors +description: Minimal test +features: [] +---*/ + +// empty diff --git a/tools/lint/test/fixtures/features_unrecognized.js b/tools/lint/test/fixtures/features_unrecognized.js new file mode 100644 index 000000000..aec50a950 --- /dev/null +++ b/tools/lint/test/fixtures/features_unrecognized.js @@ -0,0 +1,11 @@ +FEATURES +^ expected errors | v input +// Copyright (C) 2017 Mike Pennisi. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-assignment-operators-static-semantics-early-errors +description: Minimal test +features: [not-a-valid-feature] +---*/ + +// empty diff --git a/tools/lint/test/fixtures/features_valid.js b/tools/lint/test/fixtures/features_valid.js new file mode 100644 index 000000000..1508f570a --- /dev/null +++ b/tools/lint/test/fixtures/features_valid.js @@ -0,0 +1,10 @@ +^ expected errors | v input +// Copyright (C) 2017 Mike Pennisi. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-assignment-operators-static-semantics-early-errors +description: Minimal test +features: [async-functions, object-spread] +---*/ + +async function f({ ...a }) {} -- cgit v1.2.1