Skip to content

Commit

Permalink
refactor: Only one snapshot test per lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed May 8, 2024
1 parent c310fcb commit a3015ec
Show file tree
Hide file tree
Showing 20 changed files with 377 additions and 575 deletions.
243 changes: 243 additions & 0 deletions tests/testsuite/cargo_lints.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
//! Tests for `[lint.cargo]`, Cargo's linting system, and Cargo's lints
//!
//! The first section is general tests of `[lint.cargo]` and the linting system
//! itself.
//!
//! The second section is tests for specific lints.
//! Search for `LINT_SPECIFIC_TESTS` to find this section, or search for the
//! lint name to find tests for a specific lint.

use cargo_test_support::project;
use cargo_test_support::registry::Package;

Expand Down Expand Up @@ -432,3 +441,237 @@ error: encountered 2 errors(s) while verifying lints
)
.run();
}

// LINT_SPECIFIC_TESTS
//
// Below this is where tests for specific lints should be added. Tests should
// be organized by lint, kept in alphabetical order, by lint name. A comment
// should be added above each lint's tests with the uppercased name of the lint

// IMPLICIT_FEATURES

#[cargo_test]
fn implicit_features_default_allow() {
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(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();
}

// UNKNOWN_LINTS

#[cargo_test]
fn unknown_lints_inherited() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["foo"]
[workspace.lints.cargo]
this-lint-does-not-exist = "warn"
"#,
)
.file(
"foo/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []
[lints]
workspace = true
"#,
)
.file("foo/src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints")
.masquerade_as_nightly_cargo(&["cargo-lints"])
.with_stderr(
"\
warning: unknown lint: `this-lint-does-not-exist`
--> Cargo.toml:6:1
|
6 | this-lint-does-not-exist = \"warn\"
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `cargo::this-lint-does-not-exist` was inherited
--> foo/Cargo.toml:9:1
|
9 | workspace = true
| ----------------
|
= note: `cargo::unknown_lints` is set to `warn` by default
[CHECKING] foo v0.0.1 ([CWD]/foo)
[FINISHED] [..]
",
)
.run();
}

// UNUSED_OPTIONAL_DEPENDENCIES

#[cargo_test]
fn unused_optional_dependencies_edition_2021() {
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 }
[lints.cargo]
implicit_features = "allow"
"#,
)
.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(nightly, reason = "edition2024 is not stable")]
fn unused_optional_dependencies_renamed_deps() {
Package::new("bar", "0.1.0").publish();
Package::new("bar", "0.2.0").publish();
Package::new("target-dep", "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 }
[build-dependencies]
baz = { version = "0.2.0", package = "bar", optional = true }
[target.'cfg(target_os = "linux")'.dependencies]
target-dep = { version = "0.1.0", optional = true }
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints")
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
.with_stderr(
"\
warning: unused optional dependency
--> Cargo.toml:9:1
|
9 | bar = { version = \"0.1.0\", optional = true }
| ---
|
= note: `cargo::unused_optional_dependency` is set to `warn` by default
= help: remove the dependency or activate it in a feature with `dep:bar`
warning: unused optional dependency
--> Cargo.toml:12:1
|
12 | baz = { version = \"0.2.0\", package = \"bar\", optional = true }
| ---
|
= help: remove the dependency or activate it in a feature with `dep:baz`
warning: unused optional dependency
--> Cargo.toml:15:1
|
15 | target-dep = { version = \"0.1.0\", optional = true }
| ----------
|
= help: remove the dependency or activate it in a feature with `dep:target-dep`
[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 a3015ec

Please sign in to comment.