diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index c7ba255803a7..8a0cf5f08f0c 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -10,8 +10,7 @@ use std::collections::BTreeMap; use std::fmt; use std::io::{BufRead, BufReader, ErrorKind}; use std::path::{Path, PathBuf}; -use std::process::Command; -use std::str::{from_utf8, FromStr}; +use std::str::FromStr; use toml_edit::easy as toml; #[derive(Clone, Copy, Debug, PartialEq)] @@ -830,14 +829,12 @@ mod tests { paths::write(&path_of_source_file, default_file_content)?; // Format the newly created source file - match Command::new("rustfmt").arg(&path_of_source_file).output() { - Err(e) => log::warn!("failed to call rustfmt: {}", e), - Ok(output) => { - if !output.status.success() { - log::warn!("rustfmt failed: {:?}", from_utf8(&output.stdout)); - } - } - }; + if let Err(e) = cargo_util::ProcessBuilder::new("rustfmt") + .arg(&path_of_source_file) + .exec_with_output() + { + log::warn!("failed to call rustfmt: {:#}", e); + } } } diff --git a/tests/testsuite/init/formats_source/mod.rs b/tests/testsuite/init/formats_source/mod.rs index 7d3616200483..b8454beea9e5 100644 --- a/tests/testsuite/init/formats_source/mod.rs +++ b/tests/testsuite/init/formats_source/mod.rs @@ -1,11 +1,19 @@ use cargo_test_support::compare::assert_ui; use cargo_test_support::prelude::*; -use cargo_test_support::Project; +use cargo_test_support::{process, Project}; use cargo_test_support::curr_dir; -#[cargo_test(requires_rustfmt)] +#[cargo_test] fn formats_source() { + // This cannot use `requires_rustfmt` because rustfmt is not available in + // the rust-lang/rust environment. Additionally, if running cargo without + // rustup (but with rustup installed), this test also fails due to HOME + // preventing the proxy from choosing a toolchain. + if let Err(e) = process("rustfmt").arg("-V").exec_with_output() { + eprintln!("skipping test, rustfmt not available:\n{e:?}"); + return; + } let project = Project::from_template(curr_dir!().join("in")); let project_root = &project.root();