about summary refs log tree commit diff
path: root/clippy_dev
diff options
context:
space:
mode:
authorMichael Wright <mikerite@lavabit.com>2019-07-28 06:41:13 +0200
committerMichael Wright <mikerite@lavabit.com>2019-07-28 06:41:13 +0200
commitcc779c8050a863328ddf5c0197ba509473a54be7 (patch)
tree531f1175897203b6d6de3aa3bc3fd1130a93872d /clippy_dev
parentdc69a5c0b610df452217291db1a0ebc4f76401e3 (diff)
downloadrust-cc779c8050a863328ddf5c0197ba509473a54be7.tar.gz
rust-cc779c8050a863328ddf5c0197ba509473a54be7.zip
dev-fmt: better error handling
Check if rustfmt is installed at the start and exit if it isn't.
Diffstat (limited to 'clippy_dev')
-rw-r--r--clippy_dev/src/fmt.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs
index 5ccdbec1428..3b5d6d2dbb4 100644
--- a/clippy_dev/src/fmt.rs
+++ b/clippy_dev/src/fmt.rs
@@ -10,6 +10,7 @@ pub enum CliError {
     CommandFailed(String),
     IoError(io::Error),
     ProjectRootNotFound,
+    RustfmtNotInstalled,
     WalkDirError(walkdir::Error),
 }
 
@@ -36,6 +37,8 @@ pub fn run(check: bool, verbose: bool) {
 
         let project_root = project_root()?;
 
+        rustfmt_test(context)?;
+
         success &= cargo_fmt(context, project_root.as_path())?;
         success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
         success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
@@ -69,6 +72,9 @@ pub fn run(check: bool, verbose: bool) {
             CliError::ProjectRootNotFound => {
                 eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
             },
+            CliError::RustfmtNotInstalled => {
+                eprintln!("error: rustfmt nightly is not installed.");
+            },
             CliError::WalkDirError(err) => {
                 eprintln!("error: {}", err);
             },
@@ -139,6 +145,29 @@ fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
     Ok(success)
 }
 
+fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
+    let program = "rustfmt";
+    let dir = std::env::current_dir()?;
+    let args = &["+nightly", "--version"];
+
+    if context.verbose {
+        println!("{}", format_command(&program, &dir, args));
+    }
+
+    let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
+
+    if output.status.success() {
+        Ok(())
+    } else if std::str::from_utf8(&output.stderr)
+        .unwrap_or("")
+        .starts_with("error: 'rustfmt' is not installed")
+    {
+        Err(CliError::RustfmtNotInstalled)
+    } else {
+        Err(CliError::CommandFailed(format_command(&program, &dir, args)))
+    }
+}
+
 fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
     let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
     if context.check {