D7579: tests: add test for Rust formatting

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Sat Dec 7 16:12:18 EST 2019

indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

  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.

  rHG Mercurial





diff --git a/tests/test-check-rust-format.t b/tests/test-check-rust-format.t
new file mode 100644
--- /dev/null
+++ b/tests/test-check-rust-format.t
@@ -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
diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -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'
diff --git a/tests/hghave.py b/tests/hghave.py
--- a/tests/hghave.py
+++ b/tests/hghave.py
@@ -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')
+ at 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'
+    )

To: indygreg, #hg-reviewers
Cc: mercurial-devel

More information about the Mercurial-devel mailing list