Skip to content

Commit ada0636

Browse files
anbaMs2ger
authored andcommitted
Add linter for parseTestRecord
1 parent 98a7f03 commit ada0636

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import os
2+
import sys
3+
from ..check import Check
4+
5+
6+
def load_module_2(dirname, module_name):
7+
import imp
8+
9+
f = None
10+
try:
11+
(f, pathname, description) = imp.find_module(module_name, [dirname])
12+
module = imp.load_module(module_name, f, pathname, description)
13+
return module
14+
except:
15+
raise ImportError("Can't load " + module_name)
16+
finally:
17+
if f:
18+
f.close()
19+
20+
21+
def load_module_3(dirname, module_name):
22+
import importlib.machinery
23+
import importlib.util
24+
25+
# Create a FileFinder to load Python source files.
26+
loader_details = (
27+
importlib.machinery.SourceFileLoader,
28+
importlib.machinery.SOURCE_SUFFIXES,
29+
)
30+
finder = importlib.machinery.FileFinder(dirname, loader_details)
31+
32+
# Find the module spec.
33+
spec = finder.find_spec(module_name)
34+
if spec is None:
35+
raise RuntimeError("Can't load " + module_name)
36+
37+
# Create and execute the module.
38+
module = importlib.util.module_from_spec(spec)
39+
spec.loader.exec_module(module)
40+
41+
# Return the executed module
42+
return module
43+
44+
45+
def load_parse_test_record():
46+
checks_dir = os.path.dirname(os.path.realpath(__file__))
47+
packing_dir = os.path.join(checks_dir, "../../../packaging")
48+
module_name = "parseTestRecord"
49+
50+
# The "imp" module is deprecated in Python 3.4 and will be removed in
51+
# Python 3.12. Use it only if the current Python version is too old to use
52+
# the "importlib" module.
53+
if sys.version_info < (3, 4):
54+
return load_module_2(packing_dir, module_name)
55+
56+
return load_module_3(packing_dir, module_name)
57+
58+
59+
parse_test_record = load_parse_test_record().parseTestRecord
60+
61+
62+
class CheckParseTestRecord(Check):
63+
'''Ensure tests can be parsed using parseTestRecord.py'''
64+
ID = 'ParseTestRecord'
65+
66+
def run(self, name, meta, source):
67+
# Skip if not a test file.
68+
if not meta:
69+
return None
70+
71+
errors = []
72+
test_rec = parse_test_record(source, name, lambda e: errors.append(e))
73+
74+
# Return if parse_test_record encountered errors.
75+
if errors:
76+
return "\n".join(errors)
77+
78+
# Ensure all flags in `test_rec` are consistent with `meta`.
79+
if "flags" in meta:
80+
if "flags" not in test_rec:
81+
return "Flags not present in parseTestRecord"
82+
83+
if meta["flags"] != test_rec["flags"]:
84+
return "Flags don't match parseTestRecord"
85+
86+
for flag in meta["flags"]:
87+
if flag not in test_rec:
88+
return "Flag not present in parseTestRecord: " + flag
89+
elif "flags" in test_rec:
90+
return "Unexpected flags present in parseTestRecord"
91+
92+
return None

tools/lint/lib/frontmatter.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def parse(src):
2828
return None
2929

3030
try:
31-
return Result(yaml.load(match.group(1), MyLoader), MyLoader.events)
31+
# NB: Call strip() to match parseTestRecord.
32+
return Result(yaml.load(match.group(1).strip(), MyLoader), MyLoader.events)
3233
except (yaml.scanner.ScannerError, yaml.parser.ParserError):
3334
return None

tools/lint/lint.py

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from lib.checks.nopadding import CheckNoPadding
4242
from lib.checks.flags import CheckFlags
4343
from lib.checks.posix import CheckPosix
44+
from lib.checks.parsetestrecord import CheckParseTestRecord
4445
from lib.eprint import eprint
4546
import lib.frontmatter
4647
import lib.exceptions
@@ -70,6 +71,7 @@ def checks(features):
7071
CheckNoPadding(),
7172
CheckFlags(),
7273
CheckPosix(),
74+
CheckParseTestRecord(),
7375
]
7476

7577
def lint(file_names, features):

0 commit comments

Comments
 (0)