tests: add test for Rust formatting
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 07 Dec 2019 13:07:25 -0800
changeset 43837 e8a3bbffdc7d
parent 43836 ce088b38f92b
child 43838 072b745936f1
tests: add test for Rust formatting We enforce formatting for Python and C. It makes sense to do it for Rust as well. Since our rustfmt.toml relies on unstable rustfmt features, we need to use a Nightly rustfmt with --unstable-features in order for it to work. This is a bit hacky and I would prefer we remove this requirement. But for now, this commit assumes this is the way things must be and we go out of our way to detect and use the rustfmt from the "nightly" toolchain, as installed via rustup. We had to add some environment variables to the tests to make the Rust binaries happy. Otherwise when running rustfmt we get an error about no default toolchain being installed. Differential Revision: https://phab.mercurial-scm.org/D7579
tests/hghave.py
tests/run-tests.py
tests/test-check-rust-format.t
--- a/tests/hghave.py	Sat Dec 07 13:06:25 2019 -0800
+++ b/tests/hghave.py	Sat Dec 07 13:07:25 2019 -0800
@@ -1015,3 +1015,11 @@
     version = matchoutput(pytypecmd, b'[0-9a-b.]+')
     sv = distutils.version.StrictVersion
     return version and sv(_strpath(version.group(0))) >= sv('2019.10.17')
+
+
+@check("rustfmt", "rustfmt tool")
+def has_rustfmt():
+    # We use Nightly's rustfmt due to current unstable config options.
+    return matchoutput(
+        '`rustup which --toolchain nightly rustfmt` --version', b'rustfmt'
+    )
--- a/tests/run-tests.py	Sat Dec 07 13:06:25 2019 -0800
+++ b/tests/run-tests.py	Sat Dec 07 13:07:25 2019 -0800
@@ -1363,6 +1363,20 @@
         if PYTHON3 and os.name == 'nt':
             env['PYTHONLEGACYWINDOWSSTDIO'] = '1'
 
+        # Modified HOME in test environment can confuse Rust tools. So set
+        # CARGO_HOME and RUSTUP_HOME automatically if a Rust toolchain is
+        # present and these variables aren't already defined.
+        cargo_home_path = os.path.expanduser('~/.cargo')
+        rustup_home_path = os.path.expanduser('~/.rustup')
+
+        if os.path.exists(cargo_home_path) and b'CARGO_HOME' not in osenvironb:
+            env['CARGO_HOME'] = cargo_home_path
+        if (
+            os.path.exists(rustup_home_path)
+            and b'RUSTUP_HOME' not in osenvironb
+        ):
+            env['RUSTUP_HOME'] = rustup_home_path
+
         # Reset some environment variables to well-known values so that
         # the tests produce repeatable output.
         env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-check-rust-format.t	Sat Dec 07 13:07:25 2019 -0800
@@ -0,0 +1,9 @@
+#require rustfmt test-repo
+
+  $ . "$TESTDIR/helpers-testrepo.sh"
+
+  $ cd "$TESTDIR"/..
+  $ RUSTFMT=$(rustup which --toolchain nightly rustfmt)
+  $ for f in `testrepohg files 'glob:**/*.rs'` ; do
+  >   $RUSTFMT --check --unstable-features --color=never $f
+  > done