Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support term.quiet configuration #10152

Merged
merged 4 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,20 +909,19 @@ impl Config {

let color = color.or_else(|| term.color.as_deref());

let verbosity = match (verbose, term.verbose, quiet) {
(true, _, false) | (_, Some(true), false) => Verbosity::Verbose,

// Command line takes precedence over configuration, so ignore the
// configuration..
(false, _, true) => Verbosity::Quiet,

// Can't pass both at the same time on the command line regardless
// of configuration.
(true, _, true) => {
bail!("cannot set both --verbose and --quiet");
}

(false, _, false) => Verbosity::Normal,
// The command line takes precedence over configuration.
let verbosity = match (verbose, quiet) {
(true, true) => bail!("cannot set both --verbose and --quiet"),
(true, false) => Verbosity::Verbose,
(false, true) => Verbosity::Quiet,
(false, false) => match (term.verbose, term.quiet) {
(Some(true), Some(true)) => {
bail!("cannot set both `term.verbose` and `term.quiet`")
}
(Some(true), Some(false)) => Verbosity::Verbose,
(Some(false), Some(true)) => Verbosity::Quiet,
_ => Verbosity::Normal,
},
};

let cli_target_dir = target_dir.as_ref().map(|dir| Filesystem::new(dir.clone()));
Expand Down Expand Up @@ -2127,6 +2126,7 @@ pub struct CargoBuildConfig {
#[derive(Deserialize, Default)]
struct TermConfig {
verbose: Option<bool>,
quiet: Option<bool>,
color: Option<String>,
#[serde(default)]
#[serde(deserialize_with = "progress_or_string")]
Expand Down
10 changes: 10 additions & 0 deletions src/doc/src/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,16 @@ metadata_key2 = "value"

The `[term]` table controls terminal output and interaction.

##### `term.quiet`
* Type: boolean
* Default: false
* Environment: `CARGO_TERM_QUIET`

Controls whether or not log messages are displayed by Cargo.

Specifying the `--quiet` flag will override and force quiet output.
Specifying the `--verbose` flag will override and disable quiet output.

##### `term.verbose`
* Type: boolean
* Default: false
Expand Down
10 changes: 10 additions & 0 deletions tests/testsuite/config_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ fn cli_priority() {
jobs = 3
rustc = 'file'
[term]
quiet = false
verbose = false
",
);
let config = ConfigBuilder::new().build();
assert_eq!(config.get::<i32>("build.jobs").unwrap(), 3);
assert_eq!(config.get::<String>("build.rustc").unwrap(), "file");
assert_eq!(config.get::<bool>("term.quiet").unwrap(), false);
assert_eq!(config.get::<bool>("term.verbose").unwrap(), false);

let config = ConfigBuilder::new()
Expand All @@ -58,6 +60,14 @@ fn cli_priority() {
assert_eq!(config.get::<i32>("build.jobs").unwrap(), 1);
assert_eq!(config.get::<String>("build.rustc").unwrap(), "cli");
assert_eq!(config.get::<bool>("term.verbose").unwrap(), true);

// Setting both term.verbose and term.quiet is invalid and is tested
// in the run test suite.
let config = ConfigBuilder::new()
.env("CARGO_TERM_QUIET", "false")
.config_arg("term.quiet=true")
.build();
assert_eq!(config.get::<bool>("term.quiet").unwrap(), true);
}

#[cargo_test]
Expand Down
58 changes: 51 additions & 7 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,34 @@ fn simple() {
}

#[cargo_test]
fn simple_quiet() {
fn quiet_arg() {
let p = project()
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.build();

p.cargo("run -q").with_stdout("hello").run();
p.cargo("build -q")
.with_stderr_does_not_contain("[FINISHED] [..]")
.run();

p.cargo("run --quiet").with_stdout("hello").run();
p.cargo("build --quiet")
.with_stderr_does_not_contain("[FINISHED] [..]")
.run();
}

#[cargo_test]
fn simple_quiet_and_verbose() {
fn quiet_arg_and_verbose_arg() {
let p = project()
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.build();

p.cargo("run -q -v")
p.cargo("build -q -v")
.with_status(101)
.with_stderr("[ERROR] cannot set both --verbose and --quiet")
.run();
}

#[cargo_test]
fn quiet_and_verbose_config() {
fn quiet_arg_and_verbose_config() {
let p = project()
.file(
".cargo/config",
Expand All @@ -57,7 +61,47 @@ fn quiet_and_verbose_config() {
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.build();

p.cargo("run -q").run();
p.cargo("build -q")
.with_stderr_does_not_contain("[FINISHED] [..]")
.run();
}

#[cargo_test]
fn verbose_arg_and_quiet_config() {
let p = project()
.file(
".cargo/config",
r#"
[term]
quiet = true
"#,
)
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.build();

p.cargo("build -v")
.with_stderr_contains("[RUNNING] `rustc [..]")
.run();
}

#[cargo_test]
fn quiet_config_and_verbose_config() {
let p = project()
.file(
".cargo/config",
r#"
[term]
verbose = true
quiet = true
"#,
)
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.build();

p.cargo("build")
.with_status(101)
.with_stderr("[ERROR] cannot set both `term.verbose` and `term.quiet`")
.run();
}

#[cargo_test]
Expand Down