From 7bbb3cebec62d193f24cfd75bde9d023c3a9c368 Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 4 Oct 2024 14:52:49 +0200 Subject: [PATCH] Add tests in preparation of boolean literal support in cfgs --- tests/testsuite/cfg.rs | 241 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) diff --git a/tests/testsuite/cfg.rs b/tests/testsuite/cfg.rs index 0b71e861b583..54bbf8b7d329 100644 --- a/tests/testsuite/cfg.rs +++ b/tests/testsuite/cfg.rs @@ -521,3 +521,244 @@ error[E0463]: can't find crate for `bar` "#]]) .run(); } + +#[cargo_test] +fn cfg_booleans_gate() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + authors = [] + + [target.'cfg(true)'.dependencies] + b = { path = 'b' } + + [target.'cfg(false)'.dependencies] + c = { path = 'c' } + "#, + ) + .file("src/lib.rs", "") + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "") + .file("c/Cargo.toml", &basic_manifest("c", "0.0.1")) + .file("c/src/lib.rs", "") + .build(); + + p.cargo("check") + // FIXME: true/false should error out as they are gated + .with_stderr_data(str![[r#" +[LOCKING] 2 packages to latest compatible versions +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); +} + +#[cargo_test] +fn cfg_booleans_gate_config() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + "#, + ) + .file("src/lib.rs", "") + .file( + ".cargo/config.toml", + r#" + [target.'cfg(true)'] + rustflags = [] + "#, + ) + .build(); + + p.cargo("check") + // FIXME: true/false should error out as they are gated + .with_stderr_data(str![[r#" +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); +} + +#[cargo_test] +fn cfg_booleans() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + authors = [] + + [target.'cfg(true)'.dependencies] + b = { path = 'b' } + + [target.'cfg(false)'.dependencies] + c = { path = 'c' } + "#, + ) + .file("src/lib.rs", "") + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "") + .file("c/Cargo.toml", &basic_manifest("c", "0.0.1")) + .file("c/src/lib.rs", "") + .build(); + + p.cargo("check") + // FIXME: `b` should be compiled + .with_stderr_data(str![[r#" +[LOCKING] 2 packages to latest compatible versions +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); +} + +#[cargo_test] +fn cfg_booleans_config() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + "#, + ) + .file("src/lib.rs", "") + .file( + ".cargo/config.toml", + r#" + [target.'cfg(true)'] + rustflags = [] + "#, + ) + .build(); + + p.cargo("check") + .with_stderr_data(str![[r#" +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); +} + +#[cargo_test] +fn cfg_booleans_not() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + authors = [] + + [target.'cfg(not(false))'.dependencies] + b = { path = 'b' } + "#, + ) + .file("src/lib.rs", "") + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "") + .build(); + + p.cargo("check") + .with_stderr_data(str![[r#" +[LOCKING] 1 package to latest compatible version +[CHECKING] b v0.0.1 ([ROOT]/foo/b) +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); +} + +#[cargo_test] +fn cfg_booleans_combinators() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + authors = [] + + [target.'cfg(all(any(true), not(false), true))'.dependencies] + b = { path = 'b' } + "#, + ) + .file("src/lib.rs", "") + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "") + .build(); + + p.cargo("check") + // FIXME: `b` should be compiled + .with_stderr_data(str![[r#" +[LOCKING] 1 package to latest compatible version +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); +} + +#[cargo_test] +fn cfg_booleans_rustflags_no_effect() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + authors = [] + + [target.'cfg(true)'.dependencies] + b = { path = 'b' } + + [target.'cfg(false)'.dependencies] + c = { path = 'c' } + "#, + ) + .file("src/lib.rs", "") + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "") + .file("c/Cargo.toml", &basic_manifest("c", "0.0.1")) + .file("c/src/lib.rs", "") + .build(); + + p.cargo("check") + // FIXME: only `b` should be compiled, the rustflags don't take effect + .with_stderr_data(str![[r#" +[LOCKING] 2 packages to latest compatible versions +[CHECKING] b v0.0.1 ([ROOT]/foo/b) +[CHECKING] c v0.0.1 ([ROOT]/foo/c) +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .env("RUSTFLAGS", "--cfg true --cfg false") + .run(); +}