-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathqljs_external_test_suite.py
192 lines (168 loc) · 5.65 KB
/
qljs_external_test_suite.py
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
#!/usr/bin/env python3
# Copyright (C) 2020 Matthew "strager" Glazar
# See end of file for extended copyright information.
import fnmatch
import pathlib
import pipes
import signal
import subprocess
import sys
import typing
import unittest
def run_quick_lint_js(
quick_lint_js_executable: pathlib.Path, js_file: pathlib.Path
) -> "LintResult":
try:
result = subprocess.run(
[quick_lint_js_executable, "--", js_file],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=10,
)
except subprocess.TimeoutExpired as e:
result = subprocess.CompletedProcess(
args=e.cmd, returncode=-signal.SIGALRM, stdout=e.stdout, stderr=e.stderr
)
# Fall through.
return LintResult(js_file=js_file, result=result)
class LintResult(typing.NamedTuple):
js_file: pathlib.Path
result: subprocess.CompletedProcess
@property
def crashed(self) -> bool:
return self.result.returncode not in (0, 1)
@property
def user_runnable_command(self) -> str:
return " ".join(pipes.quote(str(arg)) for arg in self.result.args)
def dump(self) -> None:
sys.stderr.write(f"error: command crashed: {self.user_runnable_command}\n")
sys.stderr.buffer.write(self.result.stdout)
sys.stderr.write(f"\nContents of {self.js_file}:\n")
sys.stderr.buffer.write(self.js_file.read_bytes())
class TestMatchPath(unittest.TestCase):
def test_match_just_file_name(self) -> None:
self.assertTrue(
match_path(
pattern="file.js",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
self.assertFalse(
match_path(
pattern="notfile.js",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
self.assertFalse(
match_path(
pattern="e.js",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
def test_match_with_file_name_pattern_ignores_directory_names(self) -> None:
self.assertFalse(
match_path(
pattern="path",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
self.assertFalse(
match_path(
pattern="to",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
self.assertFalse(
match_path(
pattern="/",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
def test_match_file_name_and_parent(self) -> None:
self.assertTrue(
match_path(
pattern="to/file.js",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
self.assertFalse(
match_path(
pattern="other/file.js",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
self.assertFalse(
match_path(
pattern="o/file.js",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
self.assertFalse(
match_path(
pattern="to/file",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
def test_match_file_glob_with_parent_directory(self) -> None:
self.assertTrue(
match_path(
pattern="to/*",
path=pathlib.PosixPath("/path/to/file.js"),
)
)
def test_realistic(self) -> None:
self.assertTrue(
match_path(
pattern="comment/migrated_0036.js",
path=pathlib.PosixPath(
"/home/strager/tmp/Projects/esprima/test/fixtures/comment/migrated_0036.js"
),
)
)
self.assertFalse(
match_path(
pattern="comment/migrated_0036.js",
path=pathlib.PosixPath(
"/home/strager/tmp/Projects/esprima/test/fixtures/expression/primary/object/migrated_0036.js"
),
)
)
self.assertTrue(
match_path(
pattern="expression/primary/object/migrated_0036.js",
path=pathlib.PosixPath(
"/home/strager/tmp/Projects/esprima/test/fixtures/expression/primary/object/migrated_0036.js"
),
)
)
def match_path(pattern: str, path) -> bool:
Path = path.__class__
try:
pattern_parts = _pattern_cache[(Path, pattern)]
except KeyError:
pattern_parts = Path(pattern).parts
_pattern_cache[(Path, pattern)] = pattern_parts
return all(
fnmatch.fnmatchcase(part, pattern)
for (pattern, part) in zip(pattern_parts, path.parts[-len(pattern_parts) :])
)
_pattern_cache = {}
if __name__ == "__main__":
unittest.main()
# quick-lint-js finds bugs in JavaScript programs.
# Copyright (C) 2020 Matthew "strager" Glazar
#
# This file is part of quick-lint-js.
#
# quick-lint-js is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# quick-lint-js is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.