Skip to content

Commit

Permalink
Fix #1014
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Jan 19, 2024
1 parent 2b2ed03 commit a34dd16
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/experimental/derive_opportunity/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/general/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/restriction/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/supplementary/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/testing/clippy/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/testing/marker/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/testing/straggler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/trailofbits/dylint"
anyhow = "1.0"

ansi_term = { version = "0.12", optional = true }
bitflags = { version = "2.4", optional = true }
cargo-util = { version = "0.2", optional = true }
cargo_metadata = { version = "0.18", optional = true }
ctor = { version = "0.2", optional = true }
Expand All @@ -35,6 +36,7 @@ toml_edit = "0.21"
[features]
cargo = [
"ansi_term",
"bitflags",
"cargo_metadata",
"command",
"home",
Expand Down
31 changes: 25 additions & 6 deletions internal/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::CommandExt;
use ansi_term::Style;
use anyhow::{anyhow, ensure, Result};
use bitflags::bitflags;
use cargo_metadata::{Metadata, MetadataCommand, Package, PackageId};
use is_terminal::IsTerminal;
use once_cell::sync::Lazy;
Expand All @@ -22,6 +23,23 @@ static STABLE_CARGO: Lazy<PathBuf> = Lazy::new(|| {
PathBuf::from(stdout.trim_end())
});

bitflags! {
pub struct Quiet: u8 {
const MESSAGE = 1 << 0;
const STDERR = 1 << 1;
}
}

impl From<bool> for Quiet {
fn from(value: bool) -> Self {
if value {
Quiet::all()
} else {
Quiet::empty()
}
}
}

/// A `cargo` command builder
///
/// Note that [`std::process::Command`]is itself a builder. So technically that makes this a
Expand All @@ -30,8 +48,8 @@ pub struct Builder {
subcommand: String,
verb: String,
description: String,
quiet: Quiet,
stable: bool,
quiet: bool,
}

#[must_use]
Expand Down Expand Up @@ -80,15 +98,16 @@ impl Builder {
subcommand: subcommand.to_owned(),
verb: verb.to_owned(),
description: description.to_owned(),
quiet: false,
quiet: Quiet::empty(),
stable: false,
}
}

/// Whether to allow the command to write to standard error.
pub fn quiet(&mut self, value: bool) -> &mut Self {
pub fn quiet(&mut self, value: impl Into<Quiet>) -> &mut Self {
let value = value.into();
// smoelius: `cargo check` and `cargo fix` are never silenced.
if value {
if !value.is_empty() {
assert!(!matches!(self.subcommand.as_str(), "check" | "fix"));
}
self.quiet = value;
Expand All @@ -104,7 +123,7 @@ impl Builder {

/// Consumes the builder and returns a [`std::process::Command`].
pub fn build(&mut self) -> Command {
if !self.quiet {
if !self.quiet.contains(Quiet::MESSAGE) {
// smoelius: Writing directly to `stderr` prevents capture by `libtest`.
let message = format!("{} {}", self.verb, self.description);
std::io::stderr()
Expand Down Expand Up @@ -137,7 +156,7 @@ impl Builder {
command.envs(vec![(crate::env::PATH, new_path)]);
}
command.args([&self.subcommand]);
if self.quiet {
if !self.quiet.contains(Quiet::STDERR) {
command.stderr(Stdio::null());
}
command
Expand Down

0 comments on commit a34dd16

Please sign in to comment.