forked from facebook/buck2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·513 lines (432 loc) · 14.9 KB
/
test.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
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
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
"""
Fake script that acts as a test
"""
import argparse
import importlib.machinery
import json
import os
import signal
import subprocess
import sys
import tempfile
import time
from contextlib import contextmanager
from enum import Enum
from pathlib import Path
from typing import Dict, Generator, Iterable, List, Optional
# To prevent the next line from creating a pycache dir
sys.dont_write_bytecode = True
lint_levels = importlib.machinery.SourceFileLoader(
"lint_levels", str(Path(__file__).parent / "lint_levels.bzl")
).load_module()
def is_opensource() -> bool:
# @oss-disable: return False
return True # @oss-enable
def is_macos() -> bool:
return sys.platform == "darwin"
def is_windows() -> bool:
return sys.platform == "win32"
class Colors(Enum):
# Copied from https://stackoverflow.com/questions/287871/how-to-print-colored-text-to-the-terminal
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
def print_running(msg: str) -> None:
print(Colors.OKGREEN.value + "Running " + msg + Colors.ENDC.value)
def print_error(msg: str) -> None:
print(
Colors.FAIL.value + Colors.BOLD.value + "ERROR: " + msg + Colors.ENDC.value,
file=sys.stderr,
)
def print_warn(msg: str) -> None:
print(
Colors.WARNING.value
+ Colors.BOLD.value
+ "WARNING: "
+ msg
+ Colors.ENDC.value,
file=sys.stderr,
)
@contextmanager
def timing() -> Generator:
start = time.time()
yield
duration = time.time() - start
print(f"Finished in {duration:.2f} seconds.")
def run(
args: Iterable[str],
capture_output: bool = False,
env: Optional[Dict[str, str]] = None,
timeout: Optional[int] = None,
) -> subprocess.CompletedProcess:
"""
Runs a command (args) in a new process.
If the command fails, raise CalledProcessError.
If the command passes, return CompletedProcess.
If capture_output is False, print to the console, otherwise record it as CompletedProcess.stdout/stderr.
If error is specified, print error on stderr when there is a CalledProcessError.
"""
# On Ci stderr gets out of order with stdout. To avoid this, we need to flush stdout/stderr first.
sys.stdout.flush()
sys.stderr.flush()
try:
result = subprocess.run(
tuple(args),
# We'd like to use the capture_output argument,
# but that isn't available in Python 3.6 which we use on Windows
stdout=subprocess.PIPE if capture_output else sys.stdout,
stderr=sys.stderr,
check=True,
encoding="utf-8",
env=env or os.environ.copy(),
timeout=timeout,
)
return result
except subprocess.CalledProcessError as e:
# Print the console info if we were capturing it
if capture_output:
print(e.stdout, file=sys.stdout)
sys.exit(1)
def check_no_changes(git: bool):
status_cmd = []
diff_cmd = []
if git:
status_cmd = ["git", "status", "--porcelain"]
diff_cmd = ["git", "diff"]
else:
status_cmd = ["hg", "status", "-mard"]
diff_cmd = ["hg", "diff", "--pager=none"]
status = run(status_cmd, capture_output=True)
if status.stdout.strip():
run(status_cmd)
run(diff_cmd)
print_error(
"File changes! Caused either by formatting or by tests creating stray files."
)
sys.exit(1)
def list_starlark_files(git: bool):
cmd = None
includes = [
"**.bxl",
"**.bzl",
"**/TARGETS",
"**/TARGETS.v2",
]
excludes = [
"starlark-rust/starlark/testcases/",
"tests/core/**/test_*_data/**",
"tests/e2e/**/test_*_data/**",
"**.rs",
"**.fixture",
"**.buckconfig",
"**.bcfg",
"**/targets/**", # TODO(lmvasquezg) Exclude only non-starlark files here
"**/BUCK", # TODO(lmvasquezg) fix starlark linter to accept these
"**/BUCK.v2",
]
if git:
excludes = [f":!:{s}" for s in excludes]
cmd = ["git", "ls-files", "--"] + includes + excludes
else:
includes = [f"--include={s}" for s in includes]
excludes = [f"--exclude={s}" for s in excludes]
cmd = (
[
"hg",
"files",
".",
]
+ includes
+ excludes
)
starlark_files = (
run(
cmd,
capture_output=True,
)
.stdout.strip()
.splitlines()
)
return starlark_files
def rustfmt(buck2_dir: Path, ci: bool, git: bool) -> None:
"""
Make the formatting consistent, using the custom rustfmt,
which is a pre-release of rustfmt 2.0.
We do that by putting rustfmt on the PATH, but that PATH
also has a copy of rustup tools, so use our rustup captured before.
Mixing and matching cargo-fmt and rust-fmt doesn't work on Windows,
so skip formatting for now.
"""
# @oss-disable: internal = True
internal = False # @oss-enable
if not internal:
return
print_running("rustfmt")
cargo_fmt = run(
["rustup", "which", "cargo-fmt"], capture_output=True
).stdout.strip()
env = os.environ.copy()
env["RUSTFMT"] = str(
buck2_dir.parent.parent / "tools" / "third-party" / "rustfmt" / "rustfmt"
)
if run([cargo_fmt, "--"], env=env).returncode != 0:
sys.exit(1)
# On CI, fail if any committed files have changed,
# mainly because of cargo fmt changing a source file
if ci:
check_no_changes(git)
RUSTC_ALLOW = {
# These are not in the shared-with-buck2 lists because they only appear in third-party deps.
# Normally cargo would suppress those, but we do vendored builds and so it doesn't.
"unfulfilled-lint-expectations",
"unknown-lints",
# This is not *actually* a warning but rather a warning level.
"warnings",
}
def _get_default_rustc_warnings() -> List[str]:
"""
We want to error on all Rustc default warnings. The very natural way to do
this would be to simply enable -Dwarnings, which would enable the
`warnings` lint group from rustc like `clippy::all` does.
Unfortunately, that's not at all what -Dwarnings does! `warnings` is not a
lint group, it's a special magical keyword that turns all `warnings` into
errors and is completely incompatible with tweaking lint levels (so e.g. if
you `--allow` a warning, but have `-Dwarnings` , that will error out).
So, we have to ask rustc to list all the default warnings for us, and error
out on them here.
"""
rustc = run(["rustup", "which", "rustc"], capture_output=True).stdout.strip()
out = run([rustc, "-Whelp"], capture_output=True).stdout.strip()
# This is some parsing that wants to be a little robust to changes in the
# output we're reading we're parsing help here.
lints = []
for line in out.split("\n"):
maybe_a_lint = line.split(None, 2)
if len(maybe_a_lint) == 3 and maybe_a_lint[1] == "warn":
lint = maybe_a_lint[0]
if lint not in RUSTC_ALLOW:
lints.append(lint)
return lints
def clippy(package_args: List[str], fix: bool) -> None:
"""
Run cargo clippy.
Also fails on any rustc warnings or build errors.
We'd really like a quiet option (at least for CI), but it doesn't exist
"""
print_running("clippy")
rustc_default_warnings = _get_default_rustc_warnings()
clippy_fix_args = ["--fix"] if fix else []
clippy_deny_lints = [*lint_levels.CLIPPY_DENY, *rustc_default_warnings]
clippy_allow_lints = lint_levels.CLIPPY_ALLOW
if fix:
clippy_deny_lints.extend(lint_levels.CLIPPY_AUTOFIX)
else:
clippy_allow_lints.extend(lint_levels.CLIPPY_AUTOFIX)
clippy_deny_args = [f"--deny={c}" for c in clippy_deny_lints]
clippy_allow_args = [f"--allow={c}" for c in clippy_allow_lints]
run(
[
"cargo",
"clippy",
*package_args,
*clippy_fix_args,
"-Z=unstable-options",
"--profile=test",
"--tests",
"--benches",
"--",
*clippy_deny_args,
*clippy_allow_args,
]
)
def starlark_linter(buck2: str, git: bool) -> None:
if git:
print_warn("Skipping starlark linter on git")
return
print_running("starlark linter")
starlark_files = list_starlark_files(git)
with tempfile.NamedTemporaryFile(mode="w+t") as fp:
fp.writelines([x + "\n" for x in starlark_files])
fp.flush()
run(
[
buck2,
"--isolation-dir=starlark-linter",
"starlark",
"lint",
"--no-buckd",
"@" + fp.name,
]
)
def _lookup(d, *keys):
"""Nested lookup in a dict"""
for k in keys:
if d is None:
return None
d = d.get(k)
return d
def rustdoc(package_args: List[str]) -> None:
print_running("cargo doc")
# We have to chose between showing the output, or capturing it.
# We have to capture it to figure out if there were warnings.
# We would strongly like to show it, because it might take a while.
# Cheat and do it twice, as we know Rust caches it, so the second time is quick.
run(["cargo", "doc", "--no-deps", *package_args])
output = run(
["cargo", "doc", "--message-format=json", "--no-deps", *package_args],
capture_output=True,
)
has_warnings = False
# We'd really like to turn on warnings-as-errors, but we can't
# We'd really like to get this information from the exit code, but we can't
# Therefore, look for output that suggests there was a warning produced.
# Alas, that's the substring 'warning', since given console output, even 'warning:'
# might get an escape code within it.
for line in output.stdout.split("\n"):
line = line.strip()
if not line:
continue
line = json.loads(line)
# If it's not a compiler message then ignore it.
if line.get("reason") != "compiler-message":
continue
# If it's not from buck2 itself (e.g. a dep), ignore.
target = line.get("target", {}).get("src_path", "")
if "/buck2/" not in target:
continue
# If it's not a doc warning, ignore it. The `message` field will
# contain a `code` field that itself has a `code` field that is machine
# readable for we look for this.
code = _lookup(line, "message", "code", "code")
if code is None or "rustdoc::" not in code:
continue
has_warnings = True
print_error("Documentation warning:")
print(line.get("message", {}).get("rendered", ""))
if has_warnings:
sys.exit(1)
def test(package_args: List[str]) -> None:
print_running("cargo test --lib")
extra_args = []
# Limit number of parallel jobs to prevent OOMs
if is_windows():
extra_args = ["--jobs", str(os.cpu_count() // 2)]
# Hour should be enough for all tests to run
timeout_sec = 60 * 60
run(["cargo", "test", "--lib", *extra_args, *package_args], timeout=timeout_sec)
print_running("cargo test --doc")
run(["cargo", "test", "--doc", *extra_args, *package_args], timeout=timeout_sec)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--ci",
action="store_true",
default=False,
help="Whether to run CI workflow",
)
parser.add_argument(
"--git",
action="store_true",
default=False,
help="Use `git` to check repo state, the script defaults to `hg`",
)
parser.add_argument(
"--buck2",
action="store",
default="buck2",
help="Path to a buck2 binary",
)
parser.add_argument(
"--lint-only",
action="store_true",
default=False,
help="Perform formatting and lints only. Do not run tests.",
)
parser.add_argument(
"--lint-rust-only",
action="store_true",
default=False,
help="Perform rust formatting and lints only. Do not run tests.",
)
parser.add_argument(
"--lint-starlark-only",
action="store_true",
default=False,
help="Perform starlark formatting and lints only. Do not run tests.",
)
parser.add_argument(
"--rustfmt-only",
action="store_true",
default=False,
help="Perform formatting only. Do not run lints or tests.",
)
parser.add_argument(
"--exclude",
action="append",
help="Packages excluded from linting.",
)
parser.add_argument(
"--clippy-fix",
action="store_true",
default=False,
help="Apply Clippy suggestions",
)
parser.add_argument(
"packages",
nargs="*",
type=str,
help="The packages to run lint on. If not specified, all packages",
)
args = parser.parse_args()
# Change to buck2 directory
buck2_dir = Path(__file__).parent.absolute()
os.chdir(str(buck2_dir))
package_args = [f"--package={p.rstrip('/')}" for p in args.packages]
if args.exclude:
package_args.append("--workspace")
package_args.extend([f"--exclude={p.rstrip('/')}" for p in args.exclude])
if package_args == [] and not (args.lint_rust_only or args.rustfmt_only):
with timing():
starlark_linter(args.buck2, args.git)
if not (args.rustfmt_only or args.lint_starlark_only):
if args.ci and is_opensource() and is_macos():
# TODO(nga): re-enable with next rust version bump (current is nightly-2024-02-01)
print_error("Clippy crashes on macOS; skipping")
else:
with timing():
clippy(package_args, args.clippy_fix)
if not args.lint_starlark_only:
with timing():
rustfmt(buck2_dir, args.ci, args.git)
if not (
args.lint_only
or args.lint_rust_only
or args.lint_starlark_only
or args.rustfmt_only
):
with timing():
rustdoc(package_args)
with timing():
test(package_args)
# On CI, check to make sure our test doesn't overwrite existing files
if args.ci:
check_no_changes(args.git)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
# no stack trace on interrupt
sys.exit(signal.SIGINT)