Add helper function for creating cargo commands.

This commit is contained in:
Jason Newcomb 2025-05-23 14:32:10 -04:00
parent ed50c8a0e9
commit 46ff0a8565
5 changed files with 21 additions and 18 deletions

View file

@ -1,6 +1,5 @@
use crate::utils::run_exit_on_err;
use crate::utils::{cargo_cmd, run_exit_on_err};
use itertools::Itertools;
use std::process::Command;
/// # Panics
///
@ -9,7 +8,7 @@ use std::process::Command;
pub fn dogfood(fix: bool, allow_dirty: bool, allow_staged: bool, allow_no_vcs: bool) {
run_exit_on_err(
"cargo test",
Command::new("cargo")
cargo_cmd()
.args(["test", "--test", "dogfood"])
.args(["--features", "internal"])
.args(["--", "dogfood_clippy", "--nocapture"])

View file

@ -1,6 +1,6 @@
use crate::utils::{cargo_clippy_path, run_exit_on_err};
use crate::utils::{cargo_clippy_path, cargo_cmd, run_exit_on_err};
use std::fs;
use std::process::{self, Command};
use std::{env, fs};
pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>) {
let is_file = match fs::metadata(path) {
@ -14,7 +14,7 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
if is_file {
run_exit_on_err(
"cargo run",
Command::new(env::var("CARGO").unwrap_or("cargo".into()))
cargo_cmd()
.args(["run", "--bin", "clippy-driver", "--"])
.args(["-L", "./target/debug"])
.args(["-Z", "no-codegen"])
@ -25,10 +25,7 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
.env("RUSTC_ICE", "0"),
);
} else {
run_exit_on_err(
"cargo build",
Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into())).arg("build"),
);
run_exit_on_err("cargo build", cargo_cmd().arg("build"));
run_exit_on_err(
"cargo clippy",
Command::new(cargo_clippy_path())

View file

@ -1,10 +1,10 @@
use crate::utils::{ErrAction, expect_action};
use crate::utils::{ErrAction, cargo_cmd, expect_action};
use core::fmt::Display;
use core::mem;
use std::path::Path;
use std::process::Command;
use std::time::{Duration, SystemTime};
use std::{env, fs, thread};
use std::{fs, thread};
use walkdir::WalkDir;
#[cfg(windows)]
@ -27,9 +27,7 @@ pub fn run(port: u16, lint: Option<String>) -> ! {
if is_metadata_outdated(mem::replace(&mut last_update, SystemTime::now())) {
// Ignore the command result; we'll fall back to displaying the old metadata.
let _ = expect_action(
Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into()))
.arg("collect-metadata")
.status(),
cargo_cmd().arg("collect-metadata").status(),
ErrAction::Run,
"cargo collect-metadata",
);

View file

@ -1,10 +1,9 @@
use crate::utils::run_exit_on_err;
use crate::utils::{cargo_cmd, run_exit_on_err};
use std::env::consts::EXE_SUFFIX;
use std::env::current_dir;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use walkdir::WalkDir;
pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
@ -46,7 +45,7 @@ pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
run_exit_on_err(
"cargo build",
Command::new("cargo").arg("build").args(release.then_some("--release")),
cargo_cmd().arg("build").args(release.then_some("--release")),
);
install_bin("cargo-clippy", &dest, standalone, release);

View file

@ -130,6 +130,16 @@ pub fn cargo_clippy_path() -> PathBuf {
path
}
/// Creates a `Command` for running cargo.
#[must_use]
pub fn cargo_cmd() -> Command {
if let Some(path) = env::var_os("CARGO") {
Command::new(path)
} else {
Command::new("cargo")
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
pub major: u16,