Skip to content

Commit

Permalink
Support rustfmt on crates with generated sources
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Nov 13, 2023
1 parent 54bef51 commit d653b4e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# rustfmt options: https://rust-lang.github.io/rustfmt/

# Skipping children allows rustfmt to run on crates with
# generated source files. Without this formatting will
# fail with: `failed to resolve mod ${MOD}`.
skip_children = true
12 changes: 12 additions & 0 deletions test/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load(":rustfmt_integration_test_suite.bzl", "rustfmt_integration_test_suite")

exports_files([
"test_rustfmt.toml",
])

write_file(
name = "srcs/generated/generated",
out = "srcs/generated/generated.rs",
content = """\
pub fn greeting() {
println!("Guten tag!");
}
""".splitlines(),
newline = "unix",
)

rustfmt_integration_test_suite(
name = "rustfmt_integration_test_suite",
)
2 changes: 2 additions & 0 deletions test/rustfmt/rustfmt_failure_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ EOF
check_build_result $TEST_FAILED ${variant}_unformatted_2018_test
check_build_result $TEST_OK ${variant}_formatted_2015_test
check_build_result $TEST_OK ${variant}_formatted_2018_test
check_build_result $TEST_OK ${variant}_generated_test
done

# Format a specific target
Expand All @@ -81,6 +82,7 @@ EOF
check_build_result $TEST_OK ${variant}_unformatted_2018_test
check_build_result $TEST_OK ${variant}_formatted_2015_test
check_build_result $TEST_OK ${variant}_formatted_2018_test
check_build_result $TEST_OK ${variant}_generated_test
done

# Format all targets
Expand Down
26 changes: 26 additions & 0 deletions test/rustfmt/rustfmt_integration_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def rustfmt_integration_test_suite(name, **kwargs):

tests = []
for variant, rust_rule in _VARIANTS.items():
#
# Test edition 2018
#
rust_rule(
name = "{}_formatted_2018".format(variant),
srcs = ["srcs/2018/formatted.rs"],
Expand All @@ -53,6 +56,9 @@ def rustfmt_integration_test_suite(name, **kwargs):
targets = [":{}_unformatted_2018".format(variant)],
)

#
# Test edition 2015
#
rust_rule(
name = "{}_formatted_2015".format(variant),
srcs = ["srcs/2015/formatted.rs"],
Expand All @@ -77,11 +83,30 @@ def rustfmt_integration_test_suite(name, **kwargs):
targets = [":{}_unformatted_2015".format(variant)],
)

#
# Test targets with generated sources
#
rust_rule(
name = "{}_generated".format(variant),
srcs = [
"srcs/generated/lib.rs",
"srcs/generated/generated.rs",
],
crate_root = "srcs/generated/lib.rs",
edition = "2021",
)

rustfmt_test(
name = "{}_generated_test".format(variant),
targets = [":{}_generated".format(variant)],
)

tests.extend([
"{}_formatted_2015_test".format(variant),
"{}_formatted_2018_test".format(variant),
"{}_unformatted_2015_test".format(variant),
"{}_unformatted_2018_test".format(variant),
"{}_generated_test".format(variant),
])

native.test_suite(
Expand All @@ -93,4 +118,5 @@ def rustfmt_integration_test_suite(name, **kwargs):
native.sh_binary(
name = "{}.test_runner".format(name),
srcs = ["rustfmt_failure_test.sh"],
testonly = True,
)
5 changes: 5 additions & 0 deletions test/rustfmt/srcs/generated/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod generated;

pub fn main() {
generated::greeting();
}
5 changes: 5 additions & 0 deletions tools/rustfmt/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# rustfmt options: https://rust-lang.github.io/rustfmt/

# Skipping children allows rustfmt to run on crates with
# generated source files. Without this formatting will
# fail with: `failed to resolve mod ${MOD}`.
skip_children = true

0 comments on commit d653b4e

Please sign in to comment.