Patchwork D7579: tests: add test for Rust formatting

login
register
mail settings
Submitter phabricator
Date Dec. 7, 2019, 9:12 p.m.
Message ID <differential-rev-PHID-DREV-tvwuljh4j4untprntsqp-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/43638/
State Superseded
Headers show

Comments

phabricator - Dec. 7, 2019, 9:12 p.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7579

AFFECTED FILES
  tests/hghave.py
  tests/run-tests.py
  tests/test-check-rust-format.t

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel
phabricator - Dec. 9, 2019, 1:15 p.m.
Alphare added a comment.


  While I am not a huge fan of the use of a nightly toolchain, I like the fact that we get the latest features and behavior as `rustfmt` is still pre-1.0 and subject to change. So far, it has been nice to us and has not broken anything I can recall, I vote for keeping it that way until it reaches 1.0.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7579/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7579

To: indygreg, #hg-reviewers
Cc: Alphare, mercurial-devel
phabricator - Dec. 10, 2019, 3:58 p.m.
durin42 added a comment.


  What are we depending on in the rustfmt settings that's nightly behavior?

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7579/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7579

To: indygreg, #hg-reviewers, Alphare, pulkit
Cc: durin42, Alphare, mercurial-devel
phabricator - Dec. 10, 2019, 4:55 p.m.
Alphare added a comment.


  In D7579#111704 <https://phab.mercurial-scm.org/D7579#111704>, @durin42 wrote:
  
  > What are we depending on in the rustfmt settings that's nightly behavior?
  
  
  
    wrap_comments = true
    error_on_line_overflow = true
  
  Both depend on nightly. I can live without `error_on_line_overflow`, `wrap_comments` is pretty neat.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7579/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7579

To: indygreg, #hg-reviewers, Alphare, pulkit
Cc: durin42, Alphare, mercurial-devel

Patch

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')
+
+
+@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'
+    )