Skip to content

Commit ed9ec38

Browse files
committed
Support term.quiet configuration
This matches how users can specify `--verbose` on the command line, or `term.verbose` in the configuration.
1 parent 451a043 commit ed9ec38

File tree

4 files changed

+85
-21
lines changed

4 files changed

+85
-21
lines changed

src/cargo/util/config/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -909,20 +909,19 @@ impl Config {
909909

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

912-
let verbosity = match (verbose, term.verbose, quiet) {
913-
(true, _, false) | (_, Some(true), false) => Verbosity::Verbose,
914-
915-
// Command line takes precedence over configuration, so ignore the
916-
// configuration..
917-
(false, _, true) => Verbosity::Quiet,
918-
919-
// Can't pass both at the same time on the command line regardless
920-
// of configuration.
921-
(true, _, true) => {
922-
bail!("cannot set both --verbose and --quiet");
923-
}
924-
925-
(false, _, false) => Verbosity::Normal,
912+
// The command line takes precedence over configuration.
913+
let verbosity = match (verbose, quiet) {
914+
(true, true) => bail!("cannot set both --verbose and --quiet"),
915+
(true, false) => Verbosity::Verbose,
916+
(false, true) => Verbosity::Quiet,
917+
(false, false) => match (term.verbose, term.quiet) {
918+
(Some(true), Some(true)) => {
919+
bail!("cannot set both `term.verbose` and `term.quiet`")
920+
}
921+
(Some(true), Some(false)) => Verbosity::Verbose,
922+
(Some(false), Some(true)) => Verbosity::Quiet,
923+
_ => Verbosity::Normal,
924+
},
926925
};
927926

928927
let cli_target_dir = target_dir.as_ref().map(|dir| Filesystem::new(dir.clone()));
@@ -2127,6 +2126,7 @@ pub struct CargoBuildConfig {
21272126
#[derive(Deserialize, Default)]
21282127
struct TermConfig {
21292128
verbose: Option<bool>,
2129+
quiet: Option<bool>,
21302130
color: Option<String>,
21312131
#[serde(default)]
21322132
#[serde(deserialize_with = "progress_or_string")]

src/doc/src/reference/config.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,16 @@ metadata_key2 = "value"
992992

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

995+
##### `term.quiet`
996+
* Type: boolean
997+
* Default: false
998+
* Environment: `CARGO_TERM_QUIET`
999+
1000+
Controls whether or not log messages are displayed by Cargo.
1001+
1002+
Specifying the `--quiet` flag will override and force quiet output.
1003+
Specifying the `--verbose` flag will override and disable quiet output.
1004+
9951005
##### `term.verbose`
9961006
* Type: boolean
9971007
* Default: false

tests/testsuite/config_cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ fn cli_priority() {
3939
jobs = 3
4040
rustc = 'file'
4141
[term]
42+
quiet = false
4243
verbose = false
4344
",
4445
);
4546
let config = ConfigBuilder::new().build();
4647
assert_eq!(config.get::<i32>("build.jobs").unwrap(), 3);
4748
assert_eq!(config.get::<String>("build.rustc").unwrap(), "file");
49+
assert_eq!(config.get::<bool>("term.quiet").unwrap(), false);
4850
assert_eq!(config.get::<bool>("term.verbose").unwrap(), false);
4951

5052
let config = ConfigBuilder::new()
@@ -58,6 +60,14 @@ fn cli_priority() {
5860
assert_eq!(config.get::<i32>("build.jobs").unwrap(), 1);
5961
assert_eq!(config.get::<String>("build.rustc").unwrap(), "cli");
6062
assert_eq!(config.get::<bool>("term.verbose").unwrap(), true);
63+
64+
// Setting both term.verbose and term.quiet is invalid and is tested
65+
// in the run test suite.
66+
let config = ConfigBuilder::new()
67+
.env("CARGO_TERM_QUIET", "false")
68+
.config_arg("term.quiet=true")
69+
.build();
70+
assert_eq!(config.get::<bool>("term.quiet").unwrap(), true);
6171
}
6272

6373
#[cargo_test]

tests/testsuite/run.rs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,34 @@ fn simple() {
2222
}
2323

2424
#[cargo_test]
25-
fn simple_quiet() {
25+
fn quiet_arg() {
2626
let p = project()
2727
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
2828
.build();
2929

30-
p.cargo("run -q").with_stdout("hello").run();
30+
p.cargo("build -q")
31+
.with_stderr_does_not_contain("[FINISHED] [..]")
32+
.run();
3133

32-
p.cargo("run --quiet").with_stdout("hello").run();
34+
p.cargo("build --quiet")
35+
.with_stderr_does_not_contain("[FINISHED] [..]")
36+
.run();
3337
}
3438

3539
#[cargo_test]
36-
fn simple_quiet_and_verbose() {
40+
fn quiet_arg_and_verbose_arg() {
3741
let p = project()
3842
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
3943
.build();
4044

41-
p.cargo("run -q -v")
45+
p.cargo("build -q -v")
4246
.with_status(101)
4347
.with_stderr("[ERROR] cannot set both --verbose and --quiet")
4448
.run();
4549
}
4650

4751
#[cargo_test]
48-
fn quiet_and_verbose_config() {
52+
fn quiet_arg_and_verbose_config() {
4953
let p = project()
5054
.file(
5155
".cargo/config",
@@ -57,7 +61,47 @@ fn quiet_and_verbose_config() {
5761
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
5862
.build();
5963

60-
p.cargo("run -q").run();
64+
p.cargo("build -q")
65+
.with_stderr_does_not_contain("[FINISHED] [..]")
66+
.run();
67+
}
68+
69+
#[cargo_test]
70+
fn verbose_arg_and_quiet_config() {
71+
let p = project()
72+
.file(
73+
".cargo/config",
74+
r#"
75+
[term]
76+
quiet = true
77+
"#,
78+
)
79+
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
80+
.build();
81+
82+
p.cargo("build -v")
83+
.with_stderr_contains("[RUNNING] `rustc [..]")
84+
.run();
85+
}
86+
87+
#[cargo_test]
88+
fn quiet_config_and_verbose_config() {
89+
let p = project()
90+
.file(
91+
".cargo/config",
92+
r#"
93+
[term]
94+
verbose = true
95+
quiet = true
96+
"#,
97+
)
98+
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
99+
.build();
100+
101+
p.cargo("build")
102+
.with_status(101)
103+
.with_stderr("[ERROR] cannot set both `term.verbose` and `term.quiet`")
104+
.run();
61105
}
62106

63107
#[cargo_test]

0 commit comments

Comments
 (0)