Skip to content

Commit ce87640

Browse files
authored
[bazel + rust + ci]: Enable rust linting (#12722)
1 parent 02359d3 commit ce87640

File tree

8 files changed

+81
-2
lines changed

8 files changed

+81
-2
lines changed

WORKSPACE

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ load("@apple_rules_lint//lint:setup.bzl", "lint_setup")
2020
# Add your linters here.
2121
lint_setup({
2222
"java-spotbugs": "//java:spotbugs-config",
23+
"rust-rustfmt": "//rust:enable-rustfmt",
2324
})
2425

2526
http_archive(

rust/BUILD.bazel

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
load("@crates//:defs.bzl", "all_crate_deps")
2-
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rust_test_suite")
2+
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rust_test_suite", "rustfmt_config")
3+
4+
rustfmt_config(
5+
name = "enable-rustfmt",
6+
)
37

48
# We want the release versions of Selenium to include the prebuilt
59
# binaries, but if we're doing day-to-day dev work, then we should

rust/defs.bzl

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load(
2+
"//rust/private:rustfmt_wrapper.bzl",
3+
_rust_binary = "rust_binary",
4+
_rust_library = "rust_library",
5+
_rust_test = "rust_test",
6+
_rust_test_suite = "rust_test_suite",
7+
)
8+
load("//rust/private:rustfmt_config.bzl", _rustfmt_config = "rustfmt_config")
9+
10+
rust_binary = _rust_binary
11+
rust_library = _rust_library
12+
rust_test = _rust_test
13+
rust_test_suite = _rust_test_suite
14+
rustfmt_config = _rustfmt_config

rust/private/BUILD.bazel

Whitespace-only changes.

rust/private/rustfmt_config.bzl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RustfmtConfig = provider()
2+
3+
def _rust_config_impl(ctx):
4+
return [
5+
RustfmtConfig(),
6+
]
7+
8+
rustfmt_config = rule(
9+
_rust_config_impl,
10+
)

rust/private/rustfmt_wrapper.bzl

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
load("@apple_rules_lint//lint:defs.bzl", "get_lint_config")
2+
load(
3+
"@rules_rust//rust:defs.bzl",
4+
"rustfmt_test",
5+
_rust_binary = "rust_binary",
6+
_rust_library = "rust_library",
7+
_rust_test = "rust_test",
8+
_rust_test_suite = "rust_test_suite",
9+
)
10+
11+
def _wrap_with_fmt_test(name, tags):
12+
config = get_lint_config("rust-rustfmt", tags)
13+
if config:
14+
rustfmt_test(
15+
name = "%s-fmt" % name,
16+
targets = [
17+
":%s" % name,
18+
],
19+
tags = [
20+
"lint",
21+
"rust-rustfmt",
22+
"rustfmt",
23+
],
24+
)
25+
26+
def rust_library(name, **kwargs):
27+
_rust_library(name = name, **kwargs)
28+
_wrap_with_fmt_test(name, kwargs.get("tags", []))
29+
30+
def rust_binary(name, **kwargs):
31+
_rust_binary(name = name, **kwargs)
32+
_wrap_with_fmt_test(name, kwargs.get("tags", []))
33+
34+
def rust_test(name, **kwargs):
35+
_rust_test(name = name, **kwargs)
36+
_wrap_with_fmt_test(name, kwargs.get("tags", []))
37+
38+
def rust_test_suite(name, srcs = [], **kwargs):
39+
_rust_test_suite(name = name, srcs = srcs, **kwargs)
40+
for src in srcs:
41+
if not src.endswith(".rs"):
42+
fail("srcs should have `.rs` extensions")
43+
44+
# Prefixed with `name` to allow parameterization with macros
45+
# The test name should not end with `.rs`
46+
test_name = name + "_" + src[:-3]
47+
_wrap_with_fmt_test(test_name, kwargs.get("tags", []))

rust/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ fn main() {
179179
}
180180
selenium_manager.set_logger(log);
181181

182-
183182
if cli.clear_cache || BooleanKey("clear-cache", false).get_value() {
184183
clear_cache(selenium_manager.get_logger(), &cache_path);
185184
}

scripts/format.sh

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ bazel run //:buildifier
1515
section "Java"
1616
echo " google-java-format" >&2
1717
find "$PWD/java" -type f -name '*.java' | xargs "$GOOGLE_JAVA_FORMAT" --replace
18+
19+
section "Rust"
20+
echo " rustfmt (fix with: bazel run @rules_rust//:rustfmt)" >&2
21+
bazel test //rust/... --test_tag_filters="rust-rustfmt" --build_tests_only

0 commit comments

Comments
 (0)