From b0dc18d158883044be1fca34dd7ad7daeeacfbb8 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 4 Feb 2025 08:25:31 +0000 Subject: [PATCH] Add new tests --- tests/testsuite/package_features.rs | 148 ++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/tests/testsuite/package_features.rs b/tests/testsuite/package_features.rs index 023d0df94ea..b012873a9a1 100644 --- a/tests/testsuite/package_features.rs +++ b/tests/testsuite/package_features.rs @@ -426,6 +426,154 @@ feature set .run(); } +#[cargo_test] +fn command_line_optional_dep() { + // Enabling a dependency used as a `dep:` errors + Package::new("bar", "1.0.0").publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.1.0" + edition = "2015" + + [features] + foo = ["dep:bar"] + + [dependencies] + bar = { version = "1.0.0", optional = true } + "#, + ) + .file("src/lib.rs", r#""#) + .build(); + + p.cargo("check --features bar") + .with_status(101) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[LOCKING] 1 package to latest compatible version +[ERROR] Package `a v0.1.0 ([ROOT]/foo)` does not have feature `bar`. It has an optional dependency with that name, but that dependency uses the "dep:" syntax in the features table, so it does not have an implicit feature with that name. + +"#]]) + .run(); +} + +#[cargo_test] +fn command_line_optional_dep_three_options() { + // Trying to enable an optional dependency used as a `dep:` errors, when there are three features which would enable the dependency + Package::new("bar", "1.0.0").publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.1.0" + edition = "2015" + + [features] + f1 = ["dep:bar"] + f2 = ["dep:bar"] + f3 = ["dep:bar"] + + [dependencies] + bar = { version = "1.0.0", optional = true } + "#, + ) + .file("src/lib.rs", r#""#) + .build(); + + p.cargo("check --features bar") + .with_status(101) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[LOCKING] 1 package to latest compatible version +[ERROR] Package `a v0.1.0 ([ROOT]/foo)` does not have feature `bar`. It has an optional dependency with that name, but that dependency uses the "dep:" syntax in the features table, so it does not have an implicit feature with that name. + +"#]]) + .run(); +} + +#[cargo_test] +fn command_line_optional_dep_many_options() { + // Trying to enable an optional dependency used as a `dep:` errors, when there are many features which would enable the dependency + Package::new("bar", "1.0.0").publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.1.0" + edition = "2015" + + [features] + f1 = ["dep:bar"] + f2 = ["dep:bar"] + f3 = ["dep:bar"] + f4 = ["dep:bar"] + + [dependencies] + bar = { version = "1.0.0", optional = true } + "#, + ) + .file("src/lib.rs", r#""#) + .build(); + + p.cargo("check --features bar") + .with_status(101) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[LOCKING] 1 package to latest compatible version +[ERROR] Package `a v0.1.0 ([ROOT]/foo)` does not have feature `bar`. It has an optional dependency with that name, but that dependency uses the "dep:" syntax in the features table, so it does not have an implicit feature with that name. + +"#]]) + .run(); +} + +#[cargo_test] +fn command_line_optional_dep_many_paths() { + // Trying to enable an optional dependency used as a `dep:` errors, when a features would enable the dependency in multiple ways + Package::new("bar", "1.0.0") + .feature("a", &[]) + .feature("b", &[]) + .feature("c", &[]) + .feature("d", &[]) + .publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.1.0" + edition = "2015" + + [features] + f1 = ["dep:bar", "bar/a", "bar/b"] # Remove the implicit feature + f2 = ["bar/b", "bar/c"] # Overlaps with previous + f3 = ["bar/d"] # No overlap with previous + + [dependencies] + bar = { version = "1.0.0", optional = true } + "#, + ) + .file("src/lib.rs", r#""#) + .build(); + + p.cargo("check --features bar") + .with_status(101) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[LOCKING] 1 package to latest compatible version +[ERROR] Package `a v0.1.0 ([ROOT]/foo)` does not have feature `bar`. It has an optional dependency with that name, but that dependency uses the "dep:" syntax in the features table, so it does not have an implicit feature with that name. + +"#]]) + .run(); +} + #[cargo_test] fn virtual_member_slash() { // member slash feature syntax