Skip to content

Commit 91e83d7

Browse files
committed
test: verify feature unification behavior in workspaces
1 parent e3fa31e commit 91e83d7

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//! Tests for workspace feature unification.
2+
3+
use cargo_test_support::prelude::*;
4+
use cargo_test_support::{basic_manifest, cargo_process, project, str};
5+
6+
#[cargo_test]
7+
fn workspace_feature_unification() {
8+
let p = project()
9+
.file(
10+
".cargo/config.toml",
11+
r#"
12+
[resolver]
13+
feature-unification = "workspace"
14+
"#,
15+
)
16+
.file(
17+
"Cargo.toml",
18+
r#"
19+
[workspace]
20+
resolver = "2"
21+
members = ["common", "a", "b"]
22+
"#,
23+
)
24+
.file(
25+
"common/Cargo.toml",
26+
r#"
27+
[package]
28+
name = "common"
29+
version = "0.1.0"
30+
edition = "2021"
31+
32+
[features]
33+
a = []
34+
b = []
35+
"#,
36+
)
37+
.file(
38+
"common/src/lib.rs",
39+
r#"
40+
#[cfg(not(all(feature = "a", feature = "b")))]
41+
compile_error!("features were not unified");
42+
"#,
43+
)
44+
.file(
45+
"a/Cargo.toml",
46+
r#"
47+
[package]
48+
name = "a"
49+
version = "0.1.0"
50+
edition = "2021"
51+
52+
[dependencies]
53+
common = { path = "../common", features = ["a"] }
54+
"#,
55+
)
56+
.file("a/src/lib.rs", "")
57+
.file(
58+
"b/Cargo.toml",
59+
r#"
60+
[package]
61+
name = "b"
62+
version = "0.1.0"
63+
edition = "2021"
64+
65+
[dependencies]
66+
common = { path = "../common", features = ["b"] }
67+
"#,
68+
)
69+
.file("b/src/lib.rs", "")
70+
.build();
71+
72+
p.cargo("check -p common")
73+
.with_stderr_contains("[WARNING] unused config key `resolver.feature-unification` in `[ROOT]/foo/.cargo/config.toml`")
74+
.with_stderr_contains("[ERROR] features were not unified")
75+
.with_status(101)
76+
.run();
77+
p.cargo("check -p a")
78+
.with_stderr_contains("[WARNING] unused config key `resolver.feature-unification` in `[ROOT]/foo/.cargo/config.toml`")
79+
.with_stderr_contains("[ERROR] features were not unified")
80+
.with_status(101)
81+
.run();
82+
p.cargo("check -p b")
83+
.with_stderr_contains("[WARNING] unused config key `resolver.feature-unification` in `[ROOT]/foo/.cargo/config.toml`")
84+
.with_stderr_contains("[ERROR] features were not unified")
85+
.with_status(101)
86+
.run();
87+
p.cargo("check")
88+
.with_stderr_contains("[WARNING] unused config key `resolver.feature-unification` in `[ROOT]/foo/.cargo/config.toml`")
89+
.run();
90+
}
91+
92+
#[cargo_test]
93+
fn cargo_install_ignores_config() {
94+
let p = project()
95+
.file(
96+
"Cargo.toml",
97+
r#"
98+
[package]
99+
name = "a"
100+
version = "0.1.0"
101+
edition = "2021"
102+
103+
[dependencies]
104+
common = { path = "common", features = ["a"] }
105+
106+
[workspace]
107+
members = ["common", "b"]
108+
"#,
109+
)
110+
.file("src/main.rs", "fn main() {}")
111+
.file(
112+
"common/Cargo.toml",
113+
r#"
114+
[package]
115+
name = "common"
116+
version = "0.1.0"
117+
edition = "2021"
118+
119+
[features]
120+
a = []
121+
b = []
122+
"#,
123+
)
124+
.file(
125+
"common/src/lib.rs",
126+
r#"
127+
#[cfg(all(feature = "a", feature = "b"))]
128+
compile_error!("features should not be unified");
129+
"#,
130+
)
131+
.file(
132+
"b/Cargo.toml",
133+
r#"
134+
[package]
135+
name = "b"
136+
version = "0.1.0"
137+
edition = "2021"
138+
139+
[dependencies]
140+
common = { path = "../common", features = ["b"] }
141+
"#,
142+
)
143+
.file("b/src/lib.rs", "")
144+
.build();
145+
146+
cargo_process("install --path")
147+
.arg(p.root())
148+
.env("CARGO_RESOLVER_FEATURE_UNIFICATION", "workspace")
149+
.with_stderr_data(str![[r#"
150+
[INSTALLING] a v0.1.0 ([ROOT]/foo)
151+
[COMPILING] common v0.1.0 ([ROOT]/foo/common)
152+
[COMPILING] a v0.1.0 ([ROOT]/foo)
153+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
154+
[INSTALLING] [ROOT]/home/.cargo/bin/a
155+
[INSTALLED] package `a v0.1.0 ([ROOT]/foo)` (executable `a`)
156+
[WARNING] be sure to add `[ROOT]/home/.cargo/bin` to your PATH to be able to run the installed binaries
157+
158+
"#]])
159+
.run();
160+
}
161+
162+
#[cargo_test]
163+
fn unstable_config_on_stable() {
164+
let p = project()
165+
.file(
166+
"Cargo.toml",
167+
r#"
168+
[workspace]
169+
resolver = "2"
170+
members = ["bar"]
171+
"#,
172+
)
173+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
174+
.file("bar/src/lib.rs", "")
175+
.build();
176+
177+
p.cargo("check")
178+
.env("CARGO_RESOLVER_FEATURE_UNIFICATION", "workspace")
179+
.with_stderr_data(str![[r#"
180+
[CHECKING] bar v0.1.0 ([ROOT]/foo/bar)
181+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
182+
183+
"#]])
184+
.run();
185+
}

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ mod doc;
8585
mod docscrape;
8686
mod edition;
8787
mod error;
88+
mod feature_unification;
8889
mod features;
8990
mod features2;
9091
mod features_namespaced;

0 commit comments

Comments
 (0)