|
1 | 1 | //! Tests for `[features]` table.
|
2 | 2 |
|
| 3 | +use std::fs::File; |
| 4 | + |
3 | 5 | use cargo_test_support::prelude::*;
|
| 6 | +use cargo_test_support::publish::validate_crate_contents; |
4 | 7 | use cargo_test_support::registry::{Dependency, Package};
|
5 | 8 | use cargo_test_support::str;
|
6 | 9 | use cargo_test_support::{basic_manifest, project};
|
@@ -2282,3 +2285,173 @@ fn invalid_feature_name_slash_error() {
|
2282 | 2285 | "#]])
|
2283 | 2286 | .run();
|
2284 | 2287 | }
|
| 2288 | + |
| 2289 | +#[cargo_test] |
| 2290 | +fn feature_metadata() { |
| 2291 | + let p = project() |
| 2292 | + .file( |
| 2293 | + "Cargo.toml", |
| 2294 | + r#" |
| 2295 | + [package] |
| 2296 | + name = "foo" |
| 2297 | + edition = "2015" |
| 2298 | +
|
| 2299 | + [features] |
| 2300 | + a = [] |
| 2301 | + b = [] |
| 2302 | + c = { enables = ["a", "b"] } |
| 2303 | + "#, |
| 2304 | + ) |
| 2305 | + .file( |
| 2306 | + "src/main.rs", |
| 2307 | + r#" |
| 2308 | + #[cfg(feature = "a")] |
| 2309 | + fn a() {} |
| 2310 | + #[cfg(feature = "b")] |
| 2311 | + fn b() {} |
| 2312 | + fn main() { |
| 2313 | + a(); |
| 2314 | + b(); |
| 2315 | + } |
| 2316 | + "#, |
| 2317 | + ) |
| 2318 | + .build(); |
| 2319 | + |
| 2320 | + p.cargo("check --features c").run(); |
| 2321 | +} |
| 2322 | + |
| 2323 | +#[cargo_test] |
| 2324 | +fn feature_metadata_missing_enables() { |
| 2325 | + let p = project() |
| 2326 | + .file( |
| 2327 | + "Cargo.toml", |
| 2328 | + r#" |
| 2329 | + [package] |
| 2330 | + name = "foo" |
| 2331 | + edition = "2015" |
| 2332 | +
|
| 2333 | + [features] |
| 2334 | + foo = {} |
| 2335 | + "#, |
| 2336 | + ) |
| 2337 | + .file("src/lib.rs", "") |
| 2338 | + .build(); |
| 2339 | + |
| 2340 | + p.cargo("check") |
| 2341 | + .with_status(101) |
| 2342 | + .with_stderr_data(str![[r#" |
| 2343 | +[ERROR] missing field `enables` |
| 2344 | + --> Cargo.toml:7:23 |
| 2345 | + | |
| 2346 | +7 | foo = {} |
| 2347 | + | ^^ |
| 2348 | + | |
| 2349 | +
|
| 2350 | +"#]]) |
| 2351 | + .run(); |
| 2352 | +} |
| 2353 | + |
| 2354 | +#[cargo_test] |
| 2355 | +fn unused_keys_in_feature_metadata() { |
| 2356 | + let p = project() |
| 2357 | + .file( |
| 2358 | + "Cargo.toml", |
| 2359 | + r#" |
| 2360 | + [package] |
| 2361 | + name = "foo" |
| 2362 | + edition = "2015" |
| 2363 | +
|
| 2364 | + [features] |
| 2365 | + foo = { enables = ["bar"], foobar = false } |
| 2366 | + bar = [] |
| 2367 | + "#, |
| 2368 | + ) |
| 2369 | + .file("src/lib.rs", "") |
| 2370 | + .build(); |
| 2371 | + |
| 2372 | + p.cargo("check") |
| 2373 | + .with_stderr_data(str![[r#" |
| 2374 | +[WARNING] unused manifest key: `features.foo.foobar` |
| 2375 | +[CHECKING] foo v0.0.0 ([ROOT]/foo) |
| 2376 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 2377 | +
|
| 2378 | +"#]]) |
| 2379 | + .run(); |
| 2380 | +} |
| 2381 | + |
| 2382 | +#[cargo_test] |
| 2383 | +fn normalize_feature_metadata() { |
| 2384 | + let p = project() |
| 2385 | + .file( |
| 2386 | + "Cargo.toml", |
| 2387 | + r#" |
| 2388 | + [package] |
| 2389 | + name = "foo" |
| 2390 | + edition = "2015" |
| 2391 | +
|
| 2392 | + [features] |
| 2393 | + a = [] |
| 2394 | + b = [] |
| 2395 | + c = { enables = ["a", "b"] } |
| 2396 | + "#, |
| 2397 | + ) |
| 2398 | + .file( |
| 2399 | + "src/main.rs", |
| 2400 | + r#" |
| 2401 | + #[cfg(feature = "a")] |
| 2402 | + fn a() {} |
| 2403 | + #[cfg(feature = "b")] |
| 2404 | + fn b() {} |
| 2405 | + fn main() { |
| 2406 | + a(); |
| 2407 | + b(); |
| 2408 | + } |
| 2409 | + "#, |
| 2410 | + ) |
| 2411 | + .build(); |
| 2412 | + |
| 2413 | + p.cargo("package --no-verify -v").run(); |
| 2414 | + let f = File::open(&p.root().join("target/package/foo-0.0.0.crate")).unwrap(); |
| 2415 | + let normalized_manifest = str![[r#" |
| 2416 | +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO |
| 2417 | +# |
| 2418 | +# When uploading crates to the registry Cargo will automatically |
| 2419 | +# "normalize" Cargo.toml files for maximal compatibility |
| 2420 | +# with all versions of Cargo and also rewrite `path` dependencies |
| 2421 | +# to registry (e.g., crates.io) dependencies. |
| 2422 | +# |
| 2423 | +# If you are reading this file be aware that the original Cargo.toml |
| 2424 | +# will likely look very different (and much more reasonable). |
| 2425 | +# See Cargo.toml.orig for the original contents. |
| 2426 | +
|
| 2427 | +[package] |
| 2428 | +edition = "2015" |
| 2429 | +name = "foo" |
| 2430 | +build = false |
| 2431 | +autolib = false |
| 2432 | +autobins = false |
| 2433 | +autoexamples = false |
| 2434 | +autotests = false |
| 2435 | +autobenches = false |
| 2436 | +readme = false |
| 2437 | +
|
| 2438 | +[features] |
| 2439 | +a = [] |
| 2440 | +b = [] |
| 2441 | +c = [ |
| 2442 | + "a", |
| 2443 | + "b", |
| 2444 | +] |
| 2445 | +
|
| 2446 | +[[bin]] |
| 2447 | +name = "foo" |
| 2448 | +path = "src/main.rs" |
| 2449 | +
|
| 2450 | +"#]]; |
| 2451 | + validate_crate_contents( |
| 2452 | + f, |
| 2453 | + "foo-0.0.0.crate", |
| 2454 | + &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"], |
| 2455 | + [("Cargo.toml", normalized_manifest)], |
| 2456 | + ); |
| 2457 | +} |
0 commit comments