From c8e871fb4dd355a664c6b0e642a0375705b6c41d Mon Sep 17 00:00:00 2001 From: Johann Hofmann Date: Wed, 24 Aug 2016 21:25:31 +0200 Subject: [PATCH] Add instructions on how to run rustfmt on Travis This adds very simplistic instructions on how to run rustfmt on CI. I also wrote a blog post for more details: http://johannh.me/blog/rustfmt-ci.html --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 90383d27e5c..588eca92f2c 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ diff, replace, overwrite, display, coverage, and checkstyle. * `overwrite` Overwrites the original files _without_ creating backups. * `display` Will print the formatted files to stdout. * `diff` Will print a diff between the original files and formatted files to stdout. + Will also exit with an error code if there are any differences. * `checkstyle` Will output the lines that need to be corrected as a checkstyle XML file, that can be used by tools like Jenkins. @@ -111,6 +112,28 @@ You can run `rustfmt --help` for more information. * [Atom](atom.md) * Visual Studio Code using [RustyCode](https://github.com/saviorisdead/RustyCode) or [vsc-rustfmt](https://github.com/Connorcpu/vsc-rustfmt) +## Checking style on a CI server + +To keep your code base consistently formatted, it can be helpful to fail the CI build +when a pull request contains unformatted code. Using `--write-mode=diff` instructs +rustfmt to exit with an error code if the input is not formatted correctly. +It will also print any found differences. + +A minimal Travis setup could look like this: + +```yaml +language: rust +cache: cargo +before_script: (cargo install rustfmt || true) +script: +- | + cargo fmt -- --write-mode=diff && + cargo build && + cargo test +``` + +Note that using `cache: cargo` is optional but highly recommended to speed up the installation. + ## How to build and test `cargo build` to build.