|
| 1 | +//! Tests for overriding warning behavior using `build.warnings` config option. |
| 2 | +
|
| 3 | +use std::sync::LazyLock; |
| 4 | + |
| 5 | +use cargo_test_support::{cargo_test, project, str, Project}; |
| 6 | +use snapbox::data::Inline; |
| 7 | + |
| 8 | +const ALLOW_CLEAN: LazyLock<Inline> = LazyLock::new(|| { |
| 9 | + str![[r#" |
| 10 | +[CHECKING] foo v0.0.1 ([ROOT]/foo) |
| 11 | +[WARNING] unused variable: `x` |
| 12 | +... |
| 13 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 14 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 15 | +
|
| 16 | +"#]] |
| 17 | +}); |
| 18 | + |
| 19 | +const ALLOW_CACHED: LazyLock<Inline> = LazyLock::new(|| { |
| 20 | + str![[r#" |
| 21 | +[WARNING] unused variable: `x` |
| 22 | +... |
| 23 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 24 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 25 | +
|
| 26 | +"#]] |
| 27 | +}); |
| 28 | + |
| 29 | +static WARN: LazyLock<Inline> = LazyLock::new(|| { |
| 30 | + str![[r#" |
| 31 | +... |
| 32 | +[WARNING] unused variable: `x` |
| 33 | +... |
| 34 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 35 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 36 | +
|
| 37 | +"#]] |
| 38 | +}); |
| 39 | + |
| 40 | +const DENY: LazyLock<Inline> = LazyLock::new(|| { |
| 41 | + str![[r#" |
| 42 | +... |
| 43 | +[WARNING] unused variable: `x` |
| 44 | +... |
| 45 | +[WARNING] `foo` (bin "foo") generated 1 warning |
| 46 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 47 | +
|
| 48 | +"#]] |
| 49 | +}); |
| 50 | + |
| 51 | +fn make_project(main_src: &str) -> Project { |
| 52 | + project() |
| 53 | + .file( |
| 54 | + "Cargo.toml", |
| 55 | + &format!( |
| 56 | + r#" |
| 57 | + [package] |
| 58 | + name = "foo" |
| 59 | + version = "0.0.1" |
| 60 | + edition = "2021" |
| 61 | + "# |
| 62 | + ), |
| 63 | + ) |
| 64 | + .file("src/main.rs", &format!("fn main() {{ {} }}", main_src)) |
| 65 | + .build() |
| 66 | +} |
| 67 | + |
| 68 | +#[cargo_test] |
| 69 | +fn rustc_caching_allow_first() { |
| 70 | + let p = make_project("let x = 3;"); |
| 71 | + p.cargo("check").with_stderr_data(ALLOW_CLEAN.clone()).run(); |
| 72 | + |
| 73 | + p.cargo("check").with_stderr_data(DENY.clone()).run(); |
| 74 | +} |
| 75 | + |
| 76 | +#[cargo_test] |
| 77 | +fn rustc_caching_deny_first() { |
| 78 | + let p = make_project("let x = 3;"); |
| 79 | + p.cargo("check").with_stderr_data(DENY.clone()).run(); |
| 80 | + |
| 81 | + p.cargo("check") |
| 82 | + .with_stderr_data(ALLOW_CACHED.clone()) |
| 83 | + .run(); |
| 84 | +} |
| 85 | + |
| 86 | +#[cargo_test] |
| 87 | +fn config() { |
| 88 | + let p = make_project("let x = 3;"); |
| 89 | + p.cargo("check").with_stderr_data(DENY.clone()).run(); |
| 90 | + |
| 91 | + // CLI has precedence over env. |
| 92 | + p.cargo("check").with_stderr_data(WARN.clone()).run(); |
| 93 | +} |
| 94 | + |
| 95 | +#[cargo_test] |
| 96 | +fn requires_nightly() { |
| 97 | + // build.warnings has no effect without -Zwarnings. |
| 98 | + let p = make_project("let x = 3;"); |
| 99 | + p.cargo("check").with_stderr_data(WARN.clone()).run(); |
| 100 | +} |
0 commit comments