From 0acc21f16f098b2784b1382b82816889d694f91a Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 3 Dec 2019 13:53:29 -0500 Subject: [PATCH] Makefile.am (check): Check whether formatting is required Given that developers should be doing TDD and therefore running this target frequently, this has the effect of providing immediate feedback when formatting is needed and outputting a diff. Developers will then quickly understand what changes need to be made to avoid future issues (and can run `cargo fmt` to fix it), at which point they'll rarely ever encounter formatting errors. The original purpose was to ensure pipelines fail when the formatter has not been run. --- tamer/Makefile.am | 10 ++++++++-- tamer/README.md | 31 +++++++++++++++++++++++++++++++ tamer/configure.ac | 9 +++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/tamer/Makefile.am b/tamer/Makefile.am index 2cc502c3..3248bc66 100644 --- a/tamer/Makefile.am +++ b/tamer/Makefile.am @@ -18,7 +18,7 @@ .DELETE_ON_ERROR: -.PHONY: all +.PHONY: all fix fmt CARGO_BUILD_FLAGS=@CARGO_BUILD_FLAGS@ @@ -31,6 +31,12 @@ html-am: # note that 'cargo check' is something else; see 'cargo --help' test: check -check-am: +check-am: check-fmt @CARGO@ test +check-fmt: + @CARGO@ fmt -- --check + +fix: fmt +fmt: + @CARGO@ fmt diff --git a/tamer/README.md b/tamer/README.md index 6d44f02c..2d14ef4a 100644 --- a/tamer/README.md +++ b/tamer/README.md @@ -47,3 +47,34 @@ $ ./configure CARGO_BUILD_FLAGS=--release && make $ ./configure && make $ ./configure CARGO_BUILD_FLAGS=--release && make CARGO_BUILD_FLAGS= ``` + + +## Hacking +This section contains advice for those developing TAMER. + + +### Running Tests +Developers should be using test-driven development (TDD). `make check` will +run all necessary tests. + + +### Code Format +Rust provides `rustfmt` that can automatically format code for you. This +project mandates its use and therefore eliminates personal preference in +code style (for better or worse). + +Formatting checks are run during `make check` and, on failure, will output +the diff that would be applied if you ran `make fmt` (or `make fix`); this +will run `cargo fmt` for you (and will use the binaries configured via +`configure`). + +Since developers should be doing test-driven development (TDD) and therefore +should be running `make check` frequently, the hope is that frequent +feedback on formatting issues will allow developers to quickly adjust their +habits to avoid triggering formatting errors at all. + +If you want to automatically fix formatting errors and then run tests: + +```sh +$ make fmt check +``` diff --git a/tamer/configure.ac b/tamer/configure.ac index 9ed35266..350b77b3 100644 --- a/tamer/configure.ac +++ b/tamer/configure.ac @@ -55,6 +55,15 @@ AX_COMPARE_VERSION([$rustc_version], [ge], [$rustc_ver_req], AC_ARG_VAR([CARGO_BUILD_FLAGS], [Flags to be passed to `cargo build' when invoked via Make]) +# Cargo commands may be available but not necessarily installed for the +# active toolchain. Let's check that. +AC_MSG_CHECKING([whether cargo-fmt is available for active toolchain]) +AS_IF([cargo fmt --help &>/dev/null], + [AC_MSG_RESULT(yes)], + [AC_MSG_RESULT(no) + cargo fmt --help # run again so user can see output + AC_MSG_ERROR([missing cargo-fmt for active toolchain])]) + AC_CONFIG_FILES([Makefile]) AC_OUTPUT