]> git.proxmox.com Git - cargo.git/blame - tests/testsuite/profile_config.rs
bump version to 0.66.0+pve1-1~bpo11+pve1
[cargo.git] / tests / testsuite / profile_config.rs
CommitLineData
83571aee
EH
1//! Tests for profiles defined in config files.
2
77ee608d 3use cargo_test_support::paths::CargoPathExt;
51f32532 4use cargo_test_support::registry::Package;
61fb34b0 5use cargo_test_support::{basic_lib_manifest, paths, project};
2f7b5225 6
0e0d9688 7#[cargo_test]
2f7b5225 8fn profile_config_validate_warnings() {
7fe2fbc8 9 let p = project()
dda81d31 10 .file("Cargo.toml", &basic_lib_manifest("foo"))
fecb7246 11 .file("src/lib.rs", "")
2f7b5225
EH
12 .file(
13 ".cargo/config",
14 r#"
6f8c7d5a
EH
15 [profile.test]
16 opt-level = 3
2f7b5225 17
6f8c7d5a
EH
18 [profile.asdf]
19 opt-level = 3
2f7b5225 20
6f8c7d5a
EH
21 [profile.dev]
22 bad-key = true
2f7b5225 23
6f8c7d5a
EH
24 [profile.dev.build-override]
25 bad-key-bo = true
2f7b5225 26
6f8c7d5a
EH
27 [profile.dev.package.bar]
28 bad-key-bar = true
29 "#,
fecb7246
AC
30 )
31 .build();
2f7b5225 32
8e935d4d 33 p.cargo("build")
85984a87 34 .with_stderr_unordered(
84a80d8f 35 "\
91015d52
EH
36[WARNING] unused config key `profile.dev.bad-key` in `[..].cargo/config`
37[WARNING] unused config key `profile.dev.package.bar.bad-key-bar` in `[..].cargo/config`
38[WARNING] unused config key `profile.dev.build-override.bad-key-bo` in `[..].cargo/config`
84a80d8f
EH
39[COMPILING] foo [..]
40[FINISHED] [..]
2f7b5225 41",
fecb7246
AC
42 )
43 .run();
2f7b5225
EH
44}
45
0e0d9688 46#[cargo_test]
2f7b5225 47fn profile_config_error_paths() {
77ee608d 48 // Errors in config show where the error is located.
7fe2fbc8 49 let p = project()
2f7b5225
EH
50 .file("Cargo.toml", &basic_lib_manifest("foo"))
51 .file("src/lib.rs", "")
52 .file(
53 ".cargo/config",
54 r#"
6f8c7d5a
EH
55 [profile.dev]
56 opt-level = 3
57 "#,
fecb7246
AC
58 )
59 .file(
2f7b5225
EH
60 paths::home().join(".cargo/config"),
61 r#"
62 [profile.dev]
63 rpath = "foo"
64 "#,
fecb7246
AC
65 )
66 .build();
2f7b5225 67
8e935d4d 68 p.cargo("build")
85984a87
DW
69 .with_status(101)
70 .with_stderr(
2f7b5225 71 "\
dafacbb7
EH
72[ERROR] error in [..]/foo/.cargo/config: could not load config key `profile.dev`
73
74Caused by:
75 error in [..]/home/.cargo/config: `profile.dev.rpath` expected true/false, but found a string
2f7b5225 76",
fecb7246
AC
77 )
78 .run();
2f7b5225
EH
79}
80
0e0d9688 81#[cargo_test]
2f7b5225 82fn profile_config_validate_errors() {
7fe2fbc8 83 let p = project()
dda81d31 84 .file("Cargo.toml", &basic_lib_manifest("foo"))
fecb7246 85 .file("src/lib.rs", "")
2f7b5225
EH
86 .file(
87 ".cargo/config",
88 r#"
6f8c7d5a
EH
89 [profile.dev.package.foo]
90 panic = "abort"
91 "#,
fecb7246
AC
92 )
93 .build();
2f7b5225 94
8e935d4d 95 p.cargo("build")
85984a87
DW
96 .with_status(101)
97 .with_stderr(
2f7b5225 98 "\
77ee608d 99[ERROR] config profile `dev` is not valid (defined in `[..]/foo/.cargo/config`)
2f7b5225
EH
100
101Caused by:
fcfe0b89 102 `panic` may not be specified in a `package` profile
2f7b5225 103",
fecb7246
AC
104 )
105 .run();
2f7b5225
EH
106}
107
0e0d9688 108#[cargo_test]
2f7b5225 109fn profile_config_syntax_errors() {
7fe2fbc8 110 let p = project()
2f7b5225
EH
111 .file("Cargo.toml", &basic_lib_manifest("foo"))
112 .file("src/lib.rs", "")
113 .file(
114 ".cargo/config",
115 r#"
6f8c7d5a
EH
116 [profile.dev]
117 codegen-units = "foo"
118 "#,
fecb7246
AC
119 )
120 .build();
2f7b5225 121
8e935d4d 122 p.cargo("build")
85984a87
DW
123 .with_status(101)
124 .with_stderr(
2f7b5225 125 "\
dafacbb7
EH
126[ERROR] error in [..]/.cargo/config: could not load config key `profile.dev`
127
128Caused by:
129 error in [..]/foo/.cargo/config: `profile.dev.codegen-units` expected an integer, but found a string
2f7b5225 130",
fecb7246
AC
131 )
132 .run();
2f7b5225
EH
133}
134
0e0d9688 135#[cargo_test]
2f7b5225 136fn profile_config_override_spec_multiple() {
7fe2fbc8 137 let p = project()
2f7b5225
EH
138 .file(
139 "Cargo.toml",
140 r#"
2f7b5225
EH
141 [package]
142 name = "foo"
143 version = "0.0.1"
144
145 [dependencies]
146 bar = { path = "bar" }
147 "#,
fecb7246
AC
148 )
149 .file(
2f7b5225
EH
150 ".cargo/config",
151 r#"
6f8c7d5a
EH
152 [profile.dev.package.bar]
153 opt-level = 3
2f7b5225 154
6f8c7d5a
EH
155 [profile.dev.package."bar:0.5.0"]
156 opt-level = 3
157 "#,
fecb7246
AC
158 )
159 .file("src/lib.rs", "")
dda81d31 160 .file("bar/Cargo.toml", &basic_lib_manifest("bar"))
fecb7246 161 .file("bar/src/lib.rs", "")
2f7b5225
EH
162 .build();
163
164 // Unfortunately this doesn't tell you which file, hopefully it's not too
165 // much of a problem.
8e935d4d 166 p.cargo("build -v")
85984a87
DW
167 .with_status(101)
168 .with_stderr(
2f7b5225 169 "\
fcfe0b89 170[ERROR] multiple package overrides in profile `dev` match package `bar v0.5.0 ([..])`
cdec3838 171found package specs: bar, bar@0.5.0",
fecb7246
AC
172 )
173 .run();
2f7b5225
EH
174}
175
0e0d9688 176#[cargo_test]
2f7b5225
EH
177fn profile_config_all_options() {
178 // Ensure all profile options are supported.
7fe2fbc8 179 let p = project()
2b6fd6f0 180 .file("src/main.rs", "fn main() {}")
2f7b5225
EH
181 .file(
182 ".cargo/config",
183 r#"
6f8c7d5a
EH
184 [profile.release]
185 opt-level = 1
186 debug = true
187 debug-assertions = true
188 overflow-checks = false
189 rpath = true
190 lto = true
191 codegen-units = 2
192 panic = "abort"
193 incremental = true
194 "#,
fecb7246
AC
195 )
196 .build();
2f7b5225 197
8e935d4d 198 p.cargo("build --release -v")
2b6fd6f0 199 .env_remove("CARGO_INCREMENTAL")
85984a87 200 .with_stderr(
2f7b5225
EH
201 "\
202[COMPILING] foo [..]
203[RUNNING] `rustc --crate-name foo [..] \
204 -C opt-level=1 \
205 -C panic=abort \
ed4568e1 206 -C lto[..]\
2f7b5225
EH
207 -C codegen-units=2 \
208 -C debuginfo=2 \
209 -C debug-assertions=on \
210 -C overflow-checks=off [..]\
2b6fd6f0
EH
211 -C rpath [..]\
212 -C incremental=[..]
2f7b5225
EH
213[FINISHED] release [optimized + debuginfo] [..]
214",
fecb7246
AC
215 )
216 .run();
2f7b5225
EH
217}
218
0e0d9688 219#[cargo_test]
2f7b5225
EH
220fn profile_config_override_precedence() {
221 // Config values take precedence over manifest values.
7fe2fbc8 222 let p = project()
2f7b5225
EH
223 .file(
224 "Cargo.toml",
225 r#"
6f8c7d5a
EH
226 [package]
227 name = "foo"
228 version = "0.0.1"
2f7b5225 229
6f8c7d5a
EH
230 [dependencies]
231 bar = {path = "bar"}
2f7b5225 232
6f8c7d5a
EH
233 [profile.dev]
234 codegen-units = 2
2f7b5225 235
6f8c7d5a
EH
236 [profile.dev.package.bar]
237 opt-level = 3
238 "#,
fecb7246
AC
239 )
240 .file("src/lib.rs", "")
dda81d31 241 .file("bar/Cargo.toml", &basic_lib_manifest("bar"))
fecb7246 242 .file("bar/src/lib.rs", "")
2f7b5225
EH
243 .file(
244 ".cargo/config",
245 r#"
6f8c7d5a
EH
246 [profile.dev.package.bar]
247 opt-level = 2
248 "#,
fecb7246
AC
249 )
250 .build();
2f7b5225 251
8e935d4d 252 p.cargo("build -v")
85984a87 253 .with_stderr(
2f7b5225
EH
254 "\
255[COMPILING] bar [..]
bac300bd 256[RUNNING] `rustc --crate-name bar [..] -C opt-level=2[..]-C codegen-units=2 [..]
2f7b5225
EH
257[COMPILING] foo [..]
258[RUNNING] `rustc --crate-name foo [..]-C codegen-units=2 [..]
259[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
fecb7246
AC
260 )
261 .run();
2f7b5225
EH
262}
263
0e0d9688 264#[cargo_test]
2f7b5225 265fn profile_config_no_warn_unknown_override() {
7fe2fbc8 266 let p = project()
dda81d31 267 .file("Cargo.toml", &basic_lib_manifest("foo"))
fecb7246 268 .file("src/lib.rs", "")
2f7b5225
EH
269 .file(
270 ".cargo/config",
271 r#"
6f8c7d5a
EH
272 [profile.dev.package.bar]
273 codegen-units = 4
274 "#,
fecb7246
AC
275 )
276 .build();
2f7b5225 277
8e935d4d 278 p.cargo("build")
85984a87
DW
279 .with_stderr_does_not_contain("[..]warning[..]")
280 .run();
2f7b5225
EH
281}
282
0e0d9688 283#[cargo_test]
2f7b5225 284fn profile_config_mixed_types() {
7fe2fbc8 285 let p = project()
2f7b5225
EH
286 .file("Cargo.toml", &basic_lib_manifest("foo"))
287 .file("src/lib.rs", "")
288 .file(
289 ".cargo/config",
290 r#"
6f8c7d5a
EH
291 [profile.dev]
292 opt-level = 3
293 "#,
fecb7246
AC
294 )
295 .file(
2f7b5225
EH
296 paths::home().join(".cargo/config"),
297 r#"
298 [profile.dev]
299 opt-level = 's'
300 "#,
fecb7246
AC
301 )
302 .build();
2f7b5225 303
8e935d4d 304 p.cargo("build -v")
85984a87
DW
305 .with_stderr_contains("[..]-C opt-level=3 [..]")
306 .run();
2f7b5225 307}
77ee608d
EH
308
309#[cargo_test]
310fn named_config_profile() {
311 // Exercises config named profies.
312 // foo -> middle -> bar -> dev
313 // middle exists in Cargo.toml, the others in .cargo/config
314 use super::config::ConfigBuilder;
555209ce 315 use cargo::core::compiler::CompileKind;
77ee608d 316 use cargo::core::profiles::{Profiles, UnitFor};
ed4568e1 317 use cargo::core::{PackageId, Workspace};
7f73a6c7 318 use cargo::util::interning::InternedString;
77ee608d 319 use std::fs;
77ee608d
EH
320 paths::root().join(".cargo").mkdir_p();
321 fs::write(
322 paths::root().join(".cargo/config"),
323 r#"
ed4568e1
AC
324 [profile.foo]
325 inherits = "middle"
326 codegen-units = 2
327 [profile.foo.build-override]
328 codegen-units = 6
329 [profile.foo.package.dep]
330 codegen-units = 7
331
332 [profile.middle]
333 inherits = "bar"
334 codegen-units = 3
335
336 [profile.bar]
337 inherits = "dev"
338 codegen-units = 4
339 debug = 1
340 "#,
341 )
342 .unwrap();
343 fs::write(
344 paths::root().join("Cargo.toml"),
345 r#"
ed4568e1
AC
346 [workspace]
347
348 [profile.middle]
349 inherits = "bar"
350 codegen-units = 1
351 opt-level = 1
352 [profile.middle.package.dep]
353 overflow-checks = false
354
355 [profile.foo.build-override]
356 codegen-units = 5
357 debug-assertions = false
358 [profile.foo.package.dep]
359 codegen-units = 8
77ee608d
EH
360 "#,
361 )
362 .unwrap();
895f5271 363 let config = ConfigBuilder::new().build();
77ee608d 364 let profile_name = InternedString::new("foo");
ed4568e1
AC
365 let ws = Workspace::new(&paths::root().join("Cargo.toml"), &config).unwrap();
366 let profiles = Profiles::new(&ws, profile_name).unwrap();
77ee608d
EH
367
368 let crates_io = cargo::core::source::SourceId::crates_io(&config).unwrap();
369 let a_pkg = PackageId::new("a", "0.1.0", crates_io).unwrap();
370 let dep_pkg = PackageId::new("dep", "0.1.0", crates_io).unwrap();
371
372 // normal package
a4f0988e 373 let kind = CompileKind::Host;
555209ce 374 let p = profiles.get_profile(a_pkg, true, true, UnitFor::new_normal(kind), kind);
77ee608d
EH
375 assert_eq!(p.name, "foo");
376 assert_eq!(p.codegen_units, Some(2)); // "foo" from config
377 assert_eq!(p.opt_level, "1"); // "middle" from manifest
378 assert_eq!(p.debuginfo, Some(1)); // "bar" from config
379 assert_eq!(p.debug_assertions, true); // "dev" built-in (ignore build-override)
380 assert_eq!(p.overflow_checks, true); // "dev" built-in (ignore package override)
381
382 // build-override
555209ce 383 let bo = profiles.get_profile(a_pkg, true, true, UnitFor::new_host(false, kind), kind);
77ee608d
EH
384 assert_eq!(bo.name, "foo");
385 assert_eq!(bo.codegen_units, Some(6)); // "foo" build override from config
dc4b695f 386 assert_eq!(bo.opt_level, "0"); // default to zero
77ee608d
EH
387 assert_eq!(bo.debuginfo, Some(1)); // SAME as normal
388 assert_eq!(bo.debug_assertions, false); // "foo" build override from manifest
389 assert_eq!(bo.overflow_checks, true); // SAME as normal
390
391 // package overrides
555209ce 392 let po = profiles.get_profile(dep_pkg, false, true, UnitFor::new_normal(kind), kind);
77ee608d
EH
393 assert_eq!(po.name, "foo");
394 assert_eq!(po.codegen_units, Some(7)); // "foo" package override from config
395 assert_eq!(po.opt_level, "1"); // SAME as normal
396 assert_eq!(po.debuginfo, Some(1)); // SAME as normal
397 assert_eq!(po.debug_assertions, true); // SAME as normal
398 assert_eq!(po.overflow_checks, false); // "middle" package override from manifest
399}
400
401#[cargo_test]
402fn named_env_profile() {
403 // Environment variables used to define a named profile.
404 let p = project()
405 .file(
406 "Cargo.toml",
407 r#"
77ee608d
EH
408 [package]
409 name = "foo"
410 version = "0.1.0"
411 "#,
412 )
413 .file("src/lib.rs", "")
414 .build();
415
895f5271 416 p.cargo("build -v --profile=other")
77ee608d
EH
417 .env("CARGO_PROFILE_OTHER_CODEGEN_UNITS", "1")
418 .env("CARGO_PROFILE_OTHER_INHERITS", "dev")
419 .with_stderr_contains("[..]-C codegen-units=1 [..]")
420 .run();
421}
51f32532
EH
422
423#[cargo_test]
424fn test_with_dev_profile() {
895f5271
EH
425 // The `test` profile inherits from `dev` for both local crates and
426 // dependencies.
51f32532
EH
427 Package::new("somedep", "1.0.0").publish();
428 let p = project()
429 .file(
430 "Cargo.toml",
431 r#"
432 [package]
433 name = "foo"
434 version = "0.1.0"
435
436 [dependencies]
437 somedep = "1.0"
438 "#,
439 )
440 .file("src/lib.rs", "")
441 .build();
442 p.cargo("test --lib --no-run -v")
443 .env("CARGO_PROFILE_DEV_DEBUG", "0")
444 .with_stderr(
445 "\
446[UPDATING] [..]
447[DOWNLOADING] [..]
448[DOWNLOADED] [..]
449[COMPILING] somedep v1.0.0
450[RUNNING] `rustc --crate-name somedep [..]-C debuginfo=0[..]
451[COMPILING] foo v0.1.0 [..]
895f5271 452[RUNNING] `rustc --crate-name foo [..]-C debuginfo=0[..]
51f32532 453[FINISHED] [..]
bef4d79f 454[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
51f32532
EH
455",
456 )
457 .run();
458}