From df47457e9b330c5e7bba20fbcfa19204e2b200be Mon Sep 17 00:00:00 2001 From: Colin Atkinson Date: Sun, 9 Feb 2020 20:00:56 -0500 Subject: [PATCH] Add test that rustfmt binary works --- test/rustfmt/BUILD | 17 +++++++++++++++++ test/rustfmt/rustfmt_generator.bzl | 29 +++++++++++++++++++++++++++++ test/rustfmt/rustfmt_test.sh | 8 ++++++++ test/rustfmt/unformatted.rs | 1 + 4 files changed, 55 insertions(+) create mode 100644 test/rustfmt/BUILD create mode 100644 test/rustfmt/rustfmt_generator.bzl create mode 100755 test/rustfmt/rustfmt_test.sh create mode 100644 test/rustfmt/unformatted.rs diff --git a/test/rustfmt/BUILD b/test/rustfmt/BUILD new file mode 100644 index 0000000000..922938a4cb --- /dev/null +++ b/test/rustfmt/BUILD @@ -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"], +) diff --git a/test/rustfmt/rustfmt_generator.bzl b/test/rustfmt/rustfmt_generator.bzl new file mode 100644 index 0000000000..cf8da48897 --- /dev/null +++ b/test/rustfmt/rustfmt_generator.bzl @@ -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", + ], +) diff --git a/test/rustfmt/rustfmt_test.sh b/test/rustfmt/rustfmt_test.sh new file mode 100755 index 0000000000..99678d836b --- /dev/null +++ b/test/rustfmt/rustfmt_test.sh @@ -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" diff --git a/test/rustfmt/unformatted.rs b/test/rustfmt/unformatted.rs new file mode 100644 index 0000000000..f36f291bf6 --- /dev/null +++ b/test/rustfmt/unformatted.rs @@ -0,0 +1 @@ +fn example(){println!("test");}