Skip to content

Commit 3e67bb7

Browse files
committed
test: Add test for weak optional feature in edition2024
1 parent b3425d8 commit 3e67bb7

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

tests/testsuite/lints/unused_optional_dependencies.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,105 @@ warning: unused optional dependency
199199
)
200200
.run();
201201
}
202+
203+
#[cargo_test(nightly, reason = "edition2024 is not stable")]
204+
fn inactive_weak_optional_dep() {
205+
Package::new("dep_name", "0.1.0")
206+
.feature("dep_feature", &[])
207+
.publish();
208+
209+
// `dep_name`` is included as a weak optional dependency throught speficying the `dep_name?/dep_feature` in feature table.
210+
// In edition2024, `dep_name` need to be add `dep:dep_name` to feature table to speficying activate it.
211+
212+
// This test explain the conclusion mentioned above
213+
let p = project()
214+
.file(
215+
"Cargo.toml",
216+
r#"
217+
cargo-features = ["edition2024"]
218+
[package]
219+
name = "foo"
220+
version = "0.1.0"
221+
edition = "2024"
222+
223+
[dependencies]
224+
dep_name = { version = "0.1.0", optional = true }
225+
226+
[features]
227+
foo_feature = ["dep:dep_name", "dep_name?/dep_feature"]
228+
"#,
229+
)
230+
.file("src/lib.rs", "")
231+
.build();
232+
p.cargo("check -Zcargo-lints")
233+
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
234+
.run();
235+
236+
// This test proves no regression when dep_name isn't included
237+
let p = project()
238+
.file(
239+
"Cargo.toml",
240+
r#"
241+
cargo-features = ["edition2024"]
242+
[package]
243+
name = "foo"
244+
version = "0.1.0"
245+
edition = "2024"
246+
247+
[dependencies]
248+
249+
[features]
250+
foo_feature = ["dep_name?/dep_feature"]
251+
"#,
252+
)
253+
.file("src/lib.rs", "")
254+
.build();
255+
256+
p.cargo("check -Zcargo-lints")
257+
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
258+
.with_status(101)
259+
.with_stderr(
260+
"\
261+
error: failed to parse manifest at `[ROOT]/foo/Cargo.toml`
262+
263+
Caused by:
264+
feature `foo_feature` includes `dep_name?/dep_feature`, but `dep_name` is not a dependency
265+
",
266+
)
267+
.run();
268+
269+
// This test is that we need to improve in edition2024, we need to tell that a weak optioanl dependency needs specify
270+
// the `dep:` syntax, like `dep:dep_name`.
271+
let p = project()
272+
.file(
273+
"Cargo.toml",
274+
r#"
275+
cargo-features = ["edition2024"]
276+
[package]
277+
name = "foo"
278+
version = "0.1.0"
279+
edition = "2024"
280+
281+
[dependencies]
282+
dep_name = { version = "0.1.0", optional = true }
283+
284+
[features]
285+
foo_feature = ["dep_name?/dep_feature"]
286+
"#,
287+
)
288+
.file("src/lib.rs", "")
289+
.build();
290+
291+
p.cargo("check -Zcargo-lints")
292+
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
293+
.with_status(101)
294+
.with_stderr(
295+
"\
296+
error: failed to parse manifest at `[ROOT]/foo/Cargo.toml`
297+
298+
Caused by:
299+
feature `foo_feature` includes `dep_name?/dep_feature`, but `dep_name` is not a dependency
300+
",
301+
)
302+
.run();
303+
}

0 commit comments

Comments
 (0)