]> git.lizzy.rs Git - rust.git/commitdiff
dev-fmt: better error handling
authorMichael Wright <mikerite@lavabit.com>
Sun, 28 Jul 2019 04:41:13 +0000 (06:41 +0200)
committerMichael Wright <mikerite@lavabit.com>
Sun, 28 Jul 2019 04:41:13 +0000 (06:41 +0200)
Check if rustfmt is installed at the start and exit if it isn't.

clippy_dev/src/fmt.rs

index 5ccdbec14288c78bd79a6853d02bfb89512e6ae5..3b5d6d2dbb4ca42ad6027aef9f7d607f7335a23a 100644 (file)
@@ -10,6 +10,7 @@ pub enum CliError {
     CommandFailed(String),
     IoError(io::Error),
     ProjectRootNotFound,
+    RustfmtNotInstalled,
     WalkDirError(walkdir::Error),
 }
 
@@ -36,6 +37,8 @@ fn try_run(context: &FmtContext) -> Result<bool, CliError> {
 
         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 @@ fn output_err(err: CliError) {
             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 {