1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
|
import abc
import argparse
import ast
import json
import logging
import multiprocessing
import os
import re
import subprocess
import sys
import tempfile
from collections import defaultdict
from urllib.parse import urlsplit, urljoin
from . import fnmatch
from . import rules
from .. import localpaths
from ..ci.tc.github_checks_output import get_gh_checks_outputter, GitHubChecksOutputter
from ..gitignore.gitignore import PathFilter
from ..wpt import testfiles
from ..manifest.vcs import walk
from ..manifest.sourcefile import SourceFile, js_meta_re, python_meta_re, space_chars, get_any_variants
MYPY = False
if MYPY:
# MYPY is set to True when run under Mypy.
from typing import Any
from typing import Callable
from typing import Dict
from typing import IO
from typing import Iterable
from typing import List
from typing import Optional
from typing import Sequence
from typing import Set
from typing import Text
from typing import Tuple
from typing import Type
from typing import TypeVar
# The Ignorelist is a two level dictionary. The top level is indexed by
# error names (e.g. 'TRAILING WHITESPACE'). Each of those then has a map of
# file patterns (e.g. 'foo/*') to a set of specific line numbers for the
# exception. The line numbers are optional; if missing the entire file
# ignores the error.
Ignorelist = Dict[str, Dict[str, Set[Optional[int]]]]
# Define an arbitrary typevar
T = TypeVar("T")
try:
from xml.etree import cElementTree as ElementTree
except ImportError:
from xml.etree import ElementTree as ElementTree # type: ignore
logger = None # type: Optional[logging.Logger]
def setup_logging(prefix=False):
# type: (bool) -> None
global logger
if logger is None:
logger = logging.getLogger(os.path.basename(os.path.splitext(__file__)[0]))
handler = logging.StreamHandler(sys.stdout) # type: logging.Handler
# Only add a handler if the parent logger is missing a handler
parent = logger.parent
assert isinstance(parent, logging.Logger)
if parent and len(parent.handlers) == 0:
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)
if prefix:
format = logging.BASIC_FORMAT
else:
format = "%(message)s"
formatter = logging.Formatter(format)
for handler in logger.handlers:
handler.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
setup_logging()
ERROR_MSG = """You must fix all errors; for details on how to fix them, see
https://web-platform-tests.org/writing-tests/lint-tool.html
However, instead of fixing a particular error, it's sometimes
OK to add a line to the lint.ignore file in the root of the
web-platform-tests directory to make the lint tool ignore it.
For example, to make the lint tool ignore all '%s'
errors in the %s file,
you could add the following line to the lint.ignore file.
%s: %s"""
def all_filesystem_paths(repo_root, subdir=None):
# type: (Text, Optional[Text]) -> Iterable[Text]
path_filter = PathFilter(repo_root.encode("utf8"),
extras=[b".git/"])
if subdir:
expanded_path = subdir.encode("utf8")
subdir_str = expanded_path
else:
expanded_path = repo_root.encode("utf8")
for dirpath, dirnames, filenames in path_filter(walk(expanded_path)):
for filename, _ in filenames:
path = os.path.join(dirpath, filename)
if subdir:
path = os.path.join(subdir_str, path)
assert not os.path.isabs(path), path
yield path.decode("utf8")
def _all_files_equal(paths):
# type: (Iterable[Text]) -> bool
"""
Checks all the paths are files that are byte-for-byte identical
:param paths: the list of paths to compare
:returns: True if they are all identical
"""
paths = list(paths)
if len(paths) < 2:
return True
first = paths.pop()
size = os.path.getsize(first)
if any(os.path.getsize(path) != size for path in paths):
return False
# Chunk this to avoid eating up memory and file descriptors
bufsize = 4096*4 # 16KB, a "reasonable" number of disk sectors
groupsize = 8 # Hypothesised to be large enough in the common case that everything fits in one group
with open(first, "rb") as first_f:
for start in range(0, len(paths), groupsize):
path_group = paths[start:start+groupsize]
first_f.seek(0)
try:
files = [open(x, "rb") for x in path_group]
for _ in range(0, size, bufsize):
a = first_f.read(bufsize)
for f in files:
b = f.read(bufsize)
if a != b:
return False
finally:
for f in files:
f.close()
return True
def check_path_length(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
if len(path) + 1 > 150:
return [rules.PathLength.error(path, (path, len(path) + 1))]
return []
def check_file_type(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
if os.path.islink(path):
return [rules.FileType.error(path, (path, "symlink"))]
return []
def check_worker_collision(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
endings = [(".any.html", ".any.js"),
(".any.worker.html", ".any.js"),
(".worker.html", ".worker.js")]
for path_ending, generated in endings:
if path.endswith(path_ending):
return [rules.WorkerCollision.error(path, (path_ending, generated))]
return []
def check_gitignore_file(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
if not path.endswith(".gitignore"):
return []
path_parts = path.split(os.path.sep)
if len(path_parts) == 1:
return []
if path_parts[-1] != ".gitignore":
return []
if (path_parts[0] in ["tools", "docs"] or
path_parts[:2] == ["resources", "webidl2"] or
path_parts[:3] == ["css", "tools", "apiclient"]):
return []
return [rules.GitIgnoreFile.error(path)]
def check_mojom_js(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
if path.endswith(".mojom.js"):
return [rules.MojomJSFile.error(path)]
return []
def check_ahem_copy(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
lpath = path.lower()
if "ahem" in lpath and lpath.endswith(".ttf"):
return [rules.AhemCopy.error(path)]
return []
def check_tentative_directories(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
path_parts = path.split(os.path.sep)
for directory in path_parts[:-1]:
if "tentative" in directory and directory != "tentative":
return [rules.TentativeDirectoryName.error(path)]
return []
def check_git_ignore(repo_root, paths):
# type: (Text, List[Text]) -> List[rules.Error]
errors = []
with tempfile.TemporaryFile('w+', newline='') as f:
for path in paths:
f.write('%s\n' % os.path.join(repo_root, path))
f.seek(0)
try:
matches = subprocess.check_output(
["git", "check-ignore", "--verbose", "--no-index", "--stdin"], stdin=f)
for match in matches.strip().split(b'\n'):
match_filter, path_bytes = match.split()
_, _, filter_string = match_filter.split(b':')
# If the matching filter reported by check-ignore is a special-case exception,
# that's fine. Otherwise, it requires a new special-case exception.
if filter_string[0:1] != b'!':
path = path_bytes.decode("utf8")
errors.append(rules.IgnoredPath.error(path, (path,)))
except subprocess.CalledProcessError:
# Nonzero return code means that no match exists.
pass
return errors
drafts_csswg_re = re.compile(r"https?\:\/\/drafts\.csswg\.org\/([^/?#]+)")
w3c_tr_re = re.compile(r"https?\:\/\/www\.w3c?\.org\/TR\/([^/?#]+)")
w3c_dev_re = re.compile(r"https?\:\/\/dev\.w3c?\.org\/[^/?#]+\/([^/?#]+)")
def check_css_globally_unique(repo_root, paths):
# type: (Text, List[Text]) -> List[rules.Error]
"""
Checks that CSS filenames are sufficiently unique
This groups files by path classifying them as "test", "reference", or
"support".
"test" files must have a unique name across files that share links to the
same spec.
"reference" and "support" files, on the other hand, must have globally
unique names.
:param repo_root: the repository root
:param paths: list of all paths
:returns: a list of errors found in ``paths``
"""
test_files = defaultdict(set) # type: Dict[Text, Set[Text]]
ref_files = defaultdict(set) # type: Dict[Text, Set[Text]]
support_files = defaultdict(set) # type: Dict[Text, Set[Text]]
for path in paths:
if os.name == "nt":
path = path.replace("\\", "/")
if not path.startswith("css/"):
continue
source_file = SourceFile(repo_root, path, "/")
if source_file.name_is_non_test:
# If we're name_is_non_test for a reason apart from support, ignore it.
# We care about support because of the requirement all support files in css/ to be in
# a support directory; see the start of check_parsed.
offset = path.find("/support/")
if offset == -1:
continue
parts = source_file.dir_path.split(os.path.sep)
if (parts[0] in source_file.root_dir_non_test or
any(item in source_file.dir_non_test - {"support"} for item in parts) or
any(parts[:len(non_test_path)] == list(non_test_path) for non_test_path in source_file.dir_path_non_test)):
continue
support_name = path[offset+1:]
support_files[support_name].add(path)
elif source_file.name_is_reference:
ref_files[source_file.name].add(path)
else:
test_name = source_file.name # type: Text
test_name = test_name.replace('-manual', '')
test_files[test_name].add(path)
errors = []
for name, colliding in test_files.items():
if len(colliding) > 1:
if not _all_files_equal([os.path.join(repo_root, x) for x in colliding]):
# Only compute by_spec if there are prima-facie collisions because of cost
by_spec = defaultdict(set) # type: Dict[Text, Set[Text]]
for path in colliding:
source_file = SourceFile(repo_root, path, "/")
for link in source_file.spec_links:
for r in (drafts_csswg_re, w3c_tr_re, w3c_dev_re):
m = r.match(link)
if m:
spec = m.group(1)
break
else:
continue
by_spec[spec].add(path)
for spec, spec_paths in by_spec.items():
if not _all_files_equal([os.path.join(repo_root, x) for x in spec_paths]):
for x in spec_paths:
context1 = (name, spec, ", ".join(sorted(spec_paths)))
errors.append(rules.CSSCollidingTestName.error(x,
context1))
for rule_class, d in [(rules.CSSCollidingRefName, ref_files),
(rules.CSSCollidingSupportName, support_files)]:
for name, colliding in d.items():
if len(colliding) > 1:
if not _all_files_equal([os.path.join(repo_root, x) for x in colliding]):
context2 = (name, ", ".join(sorted(colliding)))
for x in colliding:
errors.append(rule_class.error(x, context2))
return errors
def check_unique_testharness_basenames(repo_root, paths):
# type: (Text, List[Text]) -> List[rules.Error]
"""
Checks that all testharness files have unique basename paths.
The 'basename path' refers to the entire path excluding the extension. For
example, 'foo/bar/baz.html' and 'foo/bar/baz.xhtml' have the same basename
path, but 'foo/bar/baz.html' and 'foo/qux/baz.html' do not.
Testharness files with identical basenames have caused issues in downstream
infrastructure (see https://github.com/web-platform-tests/wpt/issues/7570),
and may cause confusion in general.
:param repo_root: the repository root
:param paths: list of all paths
:returns: a list of errors found in ``paths``
"""
errors = []
file_dict = defaultdict(list)
for path in paths:
source_file = SourceFile(repo_root, path, "/")
if "testharness" not in source_file.possible_types:
continue
file_name, file_extension = os.path.splitext(path)
file_dict[file_name].append(file_extension)
for k, v in file_dict.items():
if len(v) == 1:
continue
context = (', '.join(v),)
for extension in v:
errors.append(rules.DuplicateBasenamePath.error(k + extension, context))
return errors
def check_unique_case_insensitive_paths(repo_root, paths):
# type: (Text, List[Text]) -> List[rules.Error]
seen = {} # type: Dict[Text, Text]
errors = []
for path in paths:
lower_path = path.lower()
if lower_path in seen:
context = (seen[lower_path],)
errors.append(rules.DuplicatePathCaseInsensitive.error(path, context))
else:
seen[lower_path] = path
return errors
def parse_ignorelist(f):
# type: (IO[Text]) -> Tuple[Ignorelist, Set[Text]]
"""
Parse the ignorelist file given by `f`, and return the parsed structure.
:returns: a tuple of an Ignorelist and a set of files that are completely
skipped by the linter (i.e. have a '*' entry).
"""
data = defaultdict(lambda:defaultdict(set)) # type: Ignorelist
skipped_files = set() # type: Set[Text]
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
parts = [item.strip() for item in line.split(":")]
if len(parts) == 2:
error_types_s, file_match = parts
line_number = None # type: Optional[int]
else:
error_types_s, file_match, line_number_s = parts
line_number = int(line_number_s)
error_types = {item.strip() for item in error_types_s.split(",")}
file_match = os.path.normcase(file_match)
if "*" in error_types:
skipped_files.add(file_match)
else:
for error_type in error_types:
data[error_type][file_match].add(line_number)
return data, skipped_files
def filter_ignorelist_errors(data, errors):
# type: (Ignorelist, Sequence[rules.Error]) -> List[rules.Error]
"""
Filter out those errors that are ignored in `data`.
"""
if not errors:
return []
skipped = [False for item in range(len(errors))]
for i, (error_type, msg, path, line) in enumerate(errors):
normpath = os.path.normcase(path)
# Allow skipping all lint errors except the IGNORED PATH lint,
# which explains how to fix it correctly and shouldn't be skipped.
if error_type in data and error_type != "IGNORED PATH":
wl_files = data[error_type]
for file_match, allowed_lines in wl_files.items():
if None in allowed_lines or line in allowed_lines:
if fnmatch.fnmatchcase(normpath, file_match):
skipped[i] = True
return [item for i, item in enumerate(errors) if not skipped[i]]
regexps = [item() for item in # type: ignore
[rules.TrailingWhitespaceRegexp,
rules.TabsRegexp,
rules.CRRegexp,
rules.SetTimeoutRegexp,
rules.W3CTestOrgRegexp,
rules.WebPlatformTestRegexp,
rules.Webidl2Regexp,
rules.ConsoleRegexp,
rules.GenerateTestsRegexp,
rules.PrintRegexp,
rules.LayoutTestsRegexp,
rules.MissingDepsRegexp,
rules.SpecialPowersRegexp,
rules.AssertThrowsRegexp,
rules.PromiseRejectsRegexp,
rules.AssertPreconditionRegexp]]
def check_regexp_line(repo_root, path, f):
# type: (Text, Text, IO[bytes]) -> List[rules.Error]
errors = [] # type: List[rules.Error]
applicable_regexps = [regexp for regexp in regexps if regexp.applies(path)]
for i, line in enumerate(f):
for regexp in applicable_regexps:
if regexp.search(line):
errors.append((regexp.name, regexp.description, path, i+1))
return errors
def check_parsed(repo_root, path, f):
# type: (Text, Text, IO[bytes]) -> List[rules.Error]
source_file = SourceFile(repo_root, path, "/", contents=f.read())
errors = [] # type: List[rules.Error]
if path.startswith("css/"):
if (source_file.type == "support" and
not source_file.name_is_non_test and
not source_file.name_is_reference):
return [rules.SupportWrongDir.error(path)]
if (source_file.type != "support" and
not source_file.name_is_reference and
not source_file.name_is_tentative and
not source_file.name_is_crashtest and
not source_file.spec_links):
return [rules.MissingLink.error(path)]
if source_file.name_is_non_test:
return []
if source_file.markup_type is None:
return []
if source_file.root is None:
return [rules.ParseFailed.error(path)]
if source_file.type == "manual" and not source_file.name_is_manual:
errors.append(rules.ContentManual.error(path))
if source_file.type == "visual" and not source_file.name_is_visual:
errors.append(rules.ContentVisual.error(path))
about_blank_parts = urlsplit("about:blank")
for reftest_node in source_file.reftest_nodes:
href = reftest_node.attrib.get("href", "").strip(space_chars)
parts = urlsplit(href)
if parts == about_blank_parts:
continue
if (parts.scheme or parts.netloc):
errors.append(rules.AbsoluteUrlRef.error(path, (href,)))
continue
ref_url = urljoin(source_file.url, href)
ref_parts = urlsplit(ref_url)
if source_file.url == ref_url:
errors.append(rules.SameFileRef.error(path))
continue
assert ref_parts.path != ""
reference_file = os.path.join(repo_root, ref_parts.path[1:])
reference_rel = reftest_node.attrib.get("rel", "")
if not os.path.isfile(reference_file):
errors.append(rules.NonexistentRef.error(path,
(reference_rel, href)))
if len(source_file.timeout_nodes) > 1:
errors.append(rules.MultipleTimeout.error(path))
for timeout_node in source_file.timeout_nodes:
timeout_value = timeout_node.attrib.get("content", "").lower()
if timeout_value != "long":
errors.append(rules.InvalidTimeout.error(path, (timeout_value,)))
required_elements = [] # type: List[Text]
testharnessreport_nodes = [] # type: List[ElementTree.Element]
if source_file.testharness_nodes:
test_type = source_file.manifest_items()[0]
if test_type not in ("testharness", "manual"):
errors.append(rules.TestharnessInOtherType.error(path, (test_type,)))
if len(source_file.testharness_nodes) > 1:
errors.append(rules.MultipleTestharness.error(path))
testharnessreport_nodes = source_file.root.findall(".//{http://www.w3.org/1999/xhtml}script[@src='/resources/testharnessreport.js']")
if not testharnessreport_nodes:
errors.append(rules.MissingTestharnessReport.error(path))
else:
if len(testharnessreport_nodes) > 1:
errors.append(rules.MultipleTestharnessReport.error(path))
for element in source_file.variant_nodes:
if "content" not in element.attrib:
errors.append(rules.VariantMissing.error(path))
else:
variant = element.attrib["content"]
if variant != "":
if (variant[0] not in ("?", "#") or
len(variant) == 1 or
(variant[0] == "?" and variant[1] == "#")):
errors.append(rules.MalformedVariant.error(path, (path,)))
required_elements.extend(key for key, value in {"testharness": True,
"testharnessreport": len(testharnessreport_nodes) > 0,
"timeout": len(source_file.timeout_nodes) > 0}.items()
if value)
testdriver_vendor_nodes = [] # type: List[ElementTree.Element]
if source_file.testdriver_nodes:
if len(source_file.testdriver_nodes) > 1:
errors.append(rules.MultipleTestdriver.error(path))
testdriver_vendor_nodes = source_file.root.findall(".//{http://www.w3.org/1999/xhtml}script[@src='/resources/testdriver-vendor.js']")
if not testdriver_vendor_nodes:
errors.append(rules.MissingTestdriverVendor.error(path))
else:
if len(testdriver_vendor_nodes) > 1:
errors.append(rules.MultipleTestdriverVendor.error(path))
required_elements.append("testdriver")
if len(testdriver_vendor_nodes) > 0:
required_elements.append("testdriver-vendor")
if required_elements:
seen_elements = defaultdict(bool)
for elem in source_file.root.iter():
if source_file.timeout_nodes and elem == source_file.timeout_nodes[0]:
seen_elements["timeout"] = True
if seen_elements["testharness"]:
errors.append(rules.LateTimeout.error(path))
elif source_file.testharness_nodes and elem == source_file.testharness_nodes[0]:
seen_elements["testharness"] = True
elif testharnessreport_nodes and elem == testharnessreport_nodes[0]:
seen_elements["testharnessreport"] = True
if not seen_elements["testharness"]:
errors.append(rules.EarlyTestharnessReport.error(path))
elif source_file.testdriver_nodes and elem == source_file.testdriver_nodes[0]:
seen_elements["testdriver"] = True
elif testdriver_vendor_nodes and elem == testdriver_vendor_nodes[0]:
seen_elements["testdriver-vendor"] = True
if not seen_elements["testdriver"]:
errors.append(rules.EarlyTestdriverVendor.error(path))
if all(seen_elements[name] for name in required_elements):
break
for element in source_file.root.findall(".//{http://www.w3.org/1999/xhtml}script[@src]"):
src = element.attrib["src"]
def incorrect_path(script, src):
# type: (Text, Text) -> bool
return (script == src or
("/%s" % script in src and src != "/resources/%s" % script))
if incorrect_path("testharness.js", src):
errors.append(rules.TestharnessPath.error(path))
if incorrect_path("testharnessreport.js", src):
errors.append(rules.TestharnessReportPath.error(path))
if incorrect_path("testdriver.js", src):
errors.append(rules.TestdriverPath.error(path))
if incorrect_path("testdriver-vendor.js", src):
errors.append(rules.TestdriverVendorPath.error(path))
script_path = None
try:
script_path = urlsplit(urljoin(source_file.url, src)).path
except ValueError:
# This happens if the contents of src isn't something that looks like a URL to Python
pass
if (script_path == "/common/reftest-wait.js" and
"reftest-wait" not in source_file.root.attrib.get("class", "").split()):
errors.append(rules.MissingReftestWait.error(path))
return errors
class ASTCheck(metaclass=abc.ABCMeta):
@abc.abstractproperty
def rule(self):
# type: () -> Type[rules.Rule]
pass
@abc.abstractmethod
def check(self, root):
# type: (ast.AST) -> List[int]
pass
class OpenModeCheck(ASTCheck):
rule = rules.OpenNoMode
def check(self, root):
# type: (ast.AST) -> List[int]
errors = []
for node in ast.walk(root):
if isinstance(node, ast.Call):
if hasattr(node.func, "id") and node.func.id in ("open", "file"): # type: ignore
if (len(node.args) < 2 and
all(item.arg != "mode" for item in node.keywords)):
errors.append(node.lineno)
return errors
ast_checkers = [item() for item in [OpenModeCheck]]
def check_python_ast(repo_root, path, f):
# type: (Text, Text, IO[bytes]) -> List[rules.Error]
if not path.endswith(".py"):
return []
try:
root = ast.parse(f.read())
except SyntaxError as e:
return [rules.ParseFailed.error(path, line_no=e.lineno)]
errors = []
for checker in ast_checkers:
for lineno in checker.check(root):
errors.append(checker.rule.error(path, line_no=lineno))
return errors
broken_js_metadata = re.compile(br"//\s*META:")
broken_python_metadata = re.compile(br"#\s*META:")
def check_global_metadata(value):
# type: (bytes) -> Iterable[Tuple[Type[rules.Rule], Tuple[Any, ...]]]
global_values = {item.strip().decode("utf8") for item in value.split(b",") if item.strip()}
# TODO: this could check for duplicates and such
for global_value in global_values:
if not get_any_variants(global_value):
yield (rules.UnknownGlobalMetadata, ())
def check_script_metadata(repo_root, path, f):
# type: (Text, Text, IO[bytes]) -> List[rules.Error]
if path.endswith((".worker.js", ".any.js")):
meta_re = js_meta_re
broken_metadata = broken_js_metadata
elif path.endswith(".py"):
meta_re = python_meta_re
broken_metadata = broken_python_metadata
else:
return []
done = False
errors = []
for idx, line in enumerate(f):
assert isinstance(line, bytes), line
m = meta_re.match(line)
if m:
key, value = m.groups()
if key == b"global":
for rule_class, context in check_global_metadata(value):
errors.append(rule_class.error(path, context, idx + 1))
elif key == b"timeout":
if value != b"long":
errors.append(rules.UnknownTimeoutMetadata.error(path,
line_no=idx + 1))
elif key not in (b"title", b"script", b"variant", b"quic"):
errors.append(rules.UnknownMetadata.error(path,
line_no=idx + 1))
else:
done = True
if done:
if meta_re.match(line):
errors.append(rules.StrayMetadata.error(path, line_no=idx + 1))
elif meta_re.search(line):
errors.append(rules.IndentedMetadata.error(path,
line_no=idx + 1))
elif broken_metadata.search(line):
errors.append(rules.BrokenMetadata.error(path, line_no=idx + 1))
return errors
ahem_font_re = re.compile(br"font.*:.*ahem", flags=re.IGNORECASE)
# Ahem can appear either in the global location or in the support
# directory for legacy Mozilla imports
ahem_stylesheet_re = re.compile(br"\/fonts\/ahem\.css|support\/ahem.css",
flags=re.IGNORECASE)
def check_ahem_system_font(repo_root, path, f):
# type: (Text, Text, IO[bytes]) -> List[rules.Error]
if not path.endswith((".html", ".htm", ".xht", ".xhtml")):
return []
contents = f.read()
errors = []
if ahem_font_re.search(contents) and not ahem_stylesheet_re.search(contents):
errors.append(rules.AhemSystemFont.error(path))
return errors
def check_path(repo_root, path):
# type: (Text, Text) -> List[rules.Error]
"""
Runs lints that check the file path.
:param repo_root: the repository root
:param path: the path of the file within the repository
:returns: a list of errors found in ``path``
"""
errors = []
for path_fn in path_lints:
errors.extend(path_fn(repo_root, path))
return errors
def check_all_paths(repo_root, paths):
# type: (Text, List[Text]) -> List[rules.Error]
"""
Runs lints that check all paths globally.
:param repo_root: the repository root
:param paths: a list of all the paths within the repository
:returns: a list of errors found in ``f``
"""
errors = []
for paths_fn in all_paths_lints:
errors.extend(paths_fn(repo_root, paths))
return errors
def check_file_contents(repo_root, path, f=None):
# type: (Text, Text, Optional[IO[bytes]]) -> List[rules.Error]
"""
Runs lints that check the file contents.
:param repo_root: the repository root
:param path: the path of the file within the repository
:param f: a file-like object with the file contents
:returns: a list of errors found in ``f``
"""
if f is None:
f = open(os.path.join(repo_root, path), 'rb')
with f:
errors = []
for file_fn in file_lints:
errors.extend(file_fn(repo_root, path, f))
f.seek(0)
return errors
def check_file_contents_apply(args):
# type: (Tuple[Text, Text]) -> List[rules.Error]
return check_file_contents(*args)
def output_errors_text(log, errors):
# type: (Callable[[Any], None], List[rules.Error]) -> None
for error_type, description, path, line_number in errors:
pos_string = path
if line_number:
pos_string += ":%s" % line_number
log(f"{pos_string}: {description} ({error_type})")
def output_errors_markdown(log, errors):
# type: (Callable[[Any], None], List[rules.Error]) -> None
if not errors:
return
heading = """Got lint errors:
| Error Type | Position | Message |
|------------|----------|---------|"""
for line in heading.split("\n"):
log(line)
for error_type, description, path, line_number in errors:
pos_string = path
if line_number:
pos_string += ":%s" % line_number
log(f"{error_type} | {pos_string} | {description} |")
def output_errors_json(log, errors):
# type: (Callable[[Any], None], List[rules.Error]) -> None
for error_type, error, path, line_number in errors:
# We use 'print' rather than the log function to ensure that the output
# is valid JSON (e.g. with no logger preamble).
print(json.dumps({"path": path, "lineno": line_number,
"rule": error_type, "message": error}))
def output_errors_github_checks(outputter, errors, first_reported):
# type: (GitHubChecksOutputter, List[rules.Error], bool) -> None
"""Output errors to the GitHub Checks output markdown format.
:param outputter: the GitHub Checks outputter
:param errors: a list of error tuples (error type, message, path, line number)
:param first_reported: True if these are the first reported errors
"""
if first_reported:
outputter.output(
"\nChanges in this PR contain lint errors, listed below. These "
"errors must either be fixed or added to the list of ignored "
"errors; see [the documentation]("
"https://web-platform-tests.org/writing-tests/lint-tool.html). "
"For help, please tag `@web-platform-tests/wpt-core-team` in a "
"comment.\n")
outputter.output("```")
output_errors_text(outputter.output, errors)
def output_error_count(error_count):
# type: (Dict[Text, int]) -> None
if not error_count:
return
assert logger is not None
by_type = " ".join("%s: %d" % item for item in error_count.items())
count = sum(error_count.values())
logger.info("")
if count == 1:
logger.info(f"There was 1 error ({by_type})")
else:
logger.info("There were %d errors (%s)" % (count, by_type))
def changed_files(wpt_root):
# type: (Text) -> List[Text]
revish = testfiles.get_revish(revish=None)
changed, _ = testfiles.files_changed(revish, None, include_uncommitted=True, include_new=True)
return [os.path.relpath(item, wpt_root) for item in changed]
def lint_paths(kwargs, wpt_root):
# type: (Dict[Text, Any], Text) -> List[Text]
if kwargs.get("paths"):
paths = []
for path in kwargs.get("paths", []):
if os.path.isdir(path):
path_dir = list(all_filesystem_paths(wpt_root, path))
paths.extend(path_dir)
elif os.path.isfile(path):
paths.append(os.path.relpath(os.path.abspath(path), wpt_root))
elif kwargs["all"]:
paths = list(all_filesystem_paths(wpt_root))
elif kwargs["paths_file"]:
paths = []
with open(kwargs["paths_file"], 'r', newline='') as f:
for line in f.readlines():
path = line.strip()
if os.path.isdir(path):
path_dir = list(all_filesystem_paths(wpt_root, path))
paths.extend(path_dir)
elif os.path.isfile(path):
paths.append(os.path.relpath(os.path.abspath(path), wpt_root))
else:
changed_paths = changed_files(wpt_root)
force_all = False
for path in changed_paths:
path = path.replace(os.path.sep, "/")
if path == "lint.ignore" or path.startswith("tools/lint/"):
force_all = True
break
paths = (list(changed_paths) if not force_all
else list(all_filesystem_paths(wpt_root)))
return paths
def create_parser():
# type: () -> argparse.ArgumentParser
parser = argparse.ArgumentParser()
parser.add_argument("paths", nargs="*",
help="List of paths to lint")
parser.add_argument("--json", action="store_true",
help="Output machine-readable JSON format")
parser.add_argument("--markdown", action="store_true",
help="Output markdown")
parser.add_argument("--repo-root", type=str,
help="The WPT directory. Use this "
"option if the lint script exists outside the repository")
parser.add_argument("--ignore-glob", type=str, action="append",
help="Additional file glob to ignore (repeat to add more). "
"Globs are matched against paths relative to REPO_ROOT "
"using fnmatch, except that path separators are normalized.")
parser.add_argument("--all", action="store_true", help="If no paths are passed, try to lint the whole "
"working directory, not just files that changed")
parser.add_argument("--github-checks-text-file", type=str,
help="Path to GitHub checks output file for Taskcluster runs")
parser.add_argument("-j", "--jobs", type=int, default=0,
help="Level to parallelism to use (defaults to 0, which detects the number of CPUs)")
parser.add_argument("--paths-file", help="File containing a list of files to lint, one per line")
return parser
def main(**kwargs):
# type: (**Any) -> int
assert logger is not None
if kwargs.get("json") and kwargs.get("markdown"):
logger.critical("Cannot specify --json and --markdown")
sys.exit(2)
repo_root = kwargs.get('repo_root') or localpaths.repo_root
output_format = {(True, False): "json",
(False, True): "markdown",
(False, False): "normal"}[(kwargs.get("json", False),
kwargs.get("markdown", False))]
if output_format == "markdown":
setup_logging(True)
paths = lint_paths(kwargs, repo_root)
ignore_glob = kwargs.get("ignore_glob", [])
github_checks_outputter = get_gh_checks_outputter(kwargs["github_checks_text_file"])
jobs = kwargs.get("jobs", 0)
return lint(repo_root, paths, output_format, ignore_glob, github_checks_outputter, jobs)
# best experimental guess at a decent cut-off for using the parallel path
MIN_FILES_FOR_PARALLEL = 80
def lint(repo_root, paths, output_format, ignore_glob=None, github_checks_outputter=None, jobs=0):
# type: (Text, List[Text], Text, Optional[List[Text]], Optional[GitHubChecksOutputter], int) -> int
error_count = defaultdict(int) # type: Dict[Text, int]
last = None
if jobs == 0:
jobs = multiprocessing.cpu_count()
if sys.platform == 'win32':
# Using too many child processes in Python 3 hits either hangs or a
# ValueError exception, and, has diminishing returns. Clamp to 56 to
# give margin for error.
jobs = min(jobs, 56)
with open(os.path.join(repo_root, "lint.ignore")) as f:
ignorelist, skipped_files = parse_ignorelist(f)
if ignore_glob:
skipped_files |= set(ignore_glob)
output_errors = {"json": output_errors_json,
"markdown": output_errors_markdown,
"normal": output_errors_text}[output_format]
def process_errors(errors):
# type: (List[rules.Error]) -> Optional[Tuple[Text, Text]]
"""
Filters and prints the errors, and updates the ``error_count`` object.
:param errors: a list of error tuples (error type, message, path, line number)
:returns: ``None`` if there were no errors, or
a tuple of the error type and the path otherwise
"""
errors = filter_ignorelist_errors(ignorelist, errors)
if not errors:
return None
assert logger is not None
output_errors(logger.error, errors)
if github_checks_outputter:
first_output = len(error_count) == 0
output_errors_github_checks(github_checks_outputter, errors, first_output)
for error_type, error, path, line in errors:
error_count[error_type] += 1
return (errors[-1][0], path)
to_check_content = []
skip = set()
for path in paths:
abs_path = os.path.join(repo_root, path)
if not os.path.exists(abs_path):
skip.add(path)
continue
if any(fnmatch.fnmatch(path, file_match) for file_match in skipped_files):
skip.add(path)
continue
errors = check_path(repo_root, path)
last = process_errors(errors) or last
if not os.path.isdir(abs_path):
to_check_content.append((repo_root, path))
paths = [p for p in paths if p not in skip]
if jobs > 1 and len(to_check_content) >= MIN_FILES_FOR_PARALLEL:
pool = multiprocessing.Pool(jobs)
# submit this job first, as it's the longest running
all_paths_result = pool.apply_async(check_all_paths, (repo_root, paths))
# each item tends to be quick, so pass things in large chunks to avoid too much IPC overhead
errors_it = pool.imap_unordered(check_file_contents_apply, to_check_content, chunksize=40)
pool.close()
for errors in errors_it:
last = process_errors(errors) or last
errors = all_paths_result.get()
pool.join()
last = process_errors(errors) or last
else:
for item in to_check_content:
errors = check_file_contents(*item)
last = process_errors(errors) or last
errors = check_all_paths(repo_root, paths)
last = process_errors(errors) or last
if output_format in ("normal", "markdown"):
output_error_count(error_count)
if error_count:
assert last is not None
assert logger is not None
for line in (ERROR_MSG % (last[0], last[1], last[0], last[1])).split("\n"):
logger.info(line)
if error_count and github_checks_outputter:
github_checks_outputter.output("```")
return sum(error_count.values())
path_lints = [check_file_type, check_path_length, check_worker_collision, check_ahem_copy,
check_mojom_js, check_tentative_directories, check_gitignore_file]
all_paths_lints = [check_css_globally_unique, check_unique_testharness_basenames,
check_unique_case_insensitive_paths]
file_lints = [check_regexp_line, check_parsed, check_python_ast, check_script_metadata,
check_ahem_system_font]
# Don't break users of the lint that don't have git installed.
try:
subprocess.check_output(["git", "--version"])
all_paths_lints += [check_git_ignore]
except subprocess.CalledProcessError:
print('No git present; skipping .gitignore lint.')
if __name__ == "__main__":
args = create_parser().parse_args()
error_count = main(**vars(args))
if error_count > 0:
sys.exit(1)
|