Skip to content

Commit

Permalink
Add test that rustfmt binary works
Browse files Browse the repository at this point in the history
  • Loading branch information
colatkinson committed Feb 10, 2020
1 parent 1cb6ef1 commit 0c4a27f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/rustfmt/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@io_bazel_rules_rust//test/rustfmt:rustfmt_generator.bzl", "rustfmt_generator")

rustfmt_generator(
name = "formatted",
src = ":unformatted.rs",
)

sh_test(
name = "rustfmt_test",
size = "small",
data = [
":formatted.rs",
":unformatted.rs",
],
srcs = [":rustfmt_test.sh"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
29 changes: 29 additions & 0 deletions test/rustfmt/rustfmt_generator.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@io_bazel_rules_rust//rust:private/utils.bzl", "find_toolchain")

def _rustfmt_generator_impl(ctx):
toolchain = find_toolchain(ctx)
rustfmt_bin = toolchain.rustfmt
output = ctx.outputs.out

ctx.actions.run_shell(
inputs = depset([rustfmt_bin, ctx.file.src]),
outputs = [output],
command = "{} --emit stdout --quiet {} > {}".format(rustfmt_bin.path, ctx.file.src.path, output.path),
tools = [rustfmt_bin],
)


rustfmt_generator = rule(
_rustfmt_generator_impl,
doc = "Given an unformatted Rust source file, output the file after being run through rustfmt.",
attrs = {
"src": attr.label(
doc = "The file to be formatted.",
allow_single_file = True,
)
},
outputs = {"out": "%{name}.rs"},
toolchains = [
"@io_bazel_rules_rust//rust:toolchain",
],
)
8 changes: 8 additions & 0 deletions test/rustfmt/rustfmt_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -euxo pipefail

formatted="$(rlocation io_bazel_rules_rust/test/rustfmt/formatted.rs)"
unformatted="$(rlocation io_bazel_rules_rust/test/rustfmt/unformatted.rs)"

# Ensure that the file was formatted
! diff "$unformatted" "$formatted"
1 change: 1 addition & 0 deletions test/rustfmt/unformatted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn example(){println!("test");}

0 comments on commit 0c4a27f

Please sign in to comment.