Skip to content

Commit

Permalink
Auto merge of #13880 - Muscraft:refactor-cargo-lint-tests, r=weihanglo
Browse files Browse the repository at this point in the history
Refactor cargo lint tests

In #13621, it was brought up that [the lints tests are nested more deeply than other UI tests](#13621 (comment)). This got me wondering if there was a better way to structure all the lint tests.
What I came up with was:
- Lints should not have UI tests, only parts of the diagnostic system, i.e., how warnings, errors, notes, etc., look
  - This is because UI tests should focus on parts of the system that make up each lint's output
  - We can always add UI tests for each lint if desired
- All tests related to the linting system should live in `tests/testsuite/lints/`
- Tests related to `[lints.cargo]` should stay in `lints_table.rs` as it is for the whole lints table
- Each lint will get a file in `lints/` for all of its tests
  - This makes `lints/mod.rs` smaller and targeted only at tests for the linting system itself
  - It makes it much easier to know where to place a test
  • Loading branch information
bors committed May 9, 2024
2 parents 7297f0f + b79fd59 commit baca68e
Show file tree
Hide file tree
Showing 25 changed files with 793 additions and 914 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::registry::Package;
use cargo_test_support::{file, str};
use cargo_test_support::str;
use cargo_test_support::{file, project};

#[cargo_test]
fn case() {
Package::new("bar", "0.1.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["test-dummy-unstable"]
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
bar = { version = "0.1.0", optional = true }
version = "0.0.1"
edition = "2015"
authors = []
im-a-teapot = true
[lints.cargo]
implicit_features = "allow"
"#,
im_a_teapot = "deny"
"#,
)
.file("src/lib.rs", "")
.build();

snapbox::cmd::Command::cargo_ui()
.masquerade_as_nightly_cargo(&["cargo-lints"])
.masquerade_as_nightly_cargo(&["cargo-lints", "test-dummy-unstable"])
.current_dir(p.root())
.arg("check")
.arg("-Zcargo-lints")
.assert()
.success()
.code(101)
.stdout_matches(str![""])
.stderr_matches(file!["stderr.term.svg"]);
}
41 changes: 41 additions & 0 deletions tests/testsuite/lints/error/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 137 additions & 0 deletions tests/testsuite/lints/implicit_features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use cargo_test_support::project;
use cargo_test_support::registry::Package;

#[cargo_test]
fn default() {
Package::new("bar", "0.1.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
bar = { version = "0.1.0", optional = true }
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints")
.masquerade_as_nightly_cargo(&["cargo-lints"])
.with_stderr(
"\
[UPDATING] [..]
[LOCKING] 2 packages to latest compatible versions
[CHECKING] foo v0.1.0 ([CWD])
[FINISHED] [..]
",
)
.run();
}

#[cargo_test]
fn warn() {
Package::new("bar", "0.1.0").publish();
Package::new("baz", "0.1.0").publish();
Package::new("target-dep", "0.1.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
bar = { version = "0.1.0", optional = true }
[build-dependencies]
baz = { version = "0.1.0", optional = true }
[target.'cfg(target_os = "linux")'.dependencies]
target-dep = { version = "0.1.0", optional = true }
[lints.cargo]
implicit_features = "warn"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints")
.masquerade_as_nightly_cargo(&["cargo-lints"])
.with_stderr(
"\
warning: implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition
--> Cargo.toml:8:1
|
8 | bar = { version = \"0.1.0\", optional = true }
| ---
|
= note: `cargo::implicit_features` is set to `warn` in `[lints]`
warning: implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition
--> Cargo.toml:11:1
|
11 | baz = { version = \"0.1.0\", optional = true }
| ---
|
warning: implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition
--> Cargo.toml:14:1
|
14 | target-dep = { version = \"0.1.0\", optional = true }
| ----------
|
[UPDATING] [..]
[LOCKING] 4 packages to latest compatible versions
[CHECKING] foo v0.1.0 ([CWD])
[FINISHED] [..]
",
)
.run();
}

#[cargo_test(nightly, reason = "edition2024 is not stable")]
fn implicit_features_edition_2024() {
Package::new("bar", "0.1.0").publish();
Package::new("baz", "0.1.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["edition2024"]
[package]
name = "foo"
version = "0.1.0"
edition = "2024"
[dependencies]
bar = { version = "0.1.0", optional = true }
baz = { version = "0.1.0", optional = true }
[features]
baz = ["dep:baz"]
[lints.cargo]
unused_optional_dependency = "allow"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints")
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
.with_stderr(
"\
[UPDATING] [..]
[LOCKING] 2 packages to latest Rust [..] compatible versions
[CHECKING] foo v0.1.0 ([CWD])
[FINISHED] [..]
",
)
.run();
}
32 changes: 0 additions & 32 deletions tests/testsuite/lints/implicit_features/edition_2021/mod.rs

This file was deleted.

This file was deleted.

45 changes: 0 additions & 45 deletions tests/testsuite/lints/implicit_features/edition_2021_warn/mod.rs

This file was deleted.

Loading

0 comments on commit baca68e

Please sign in to comment.