]> git.proxmox.com Git - cargo.git/blame - tests/testsuite/build.rs
HTTPS all the things
[cargo.git] / tests / testsuite / build.rs
CommitLineData
ee5e24ff 1use std::env;
22e7ede6 2use std::fs::{self, File};
a6dad622 3use std::io::prelude::*;
bc1dea82 4
04ddd4d0
DW
5use crate::support::paths::{root, CargoPathExt};
6use crate::support::registry::Package;
7use crate::support::ProjectBuilder;
8use crate::support::{
85984a87
DW
9 basic_bin_manifest, basic_lib_manifest, basic_manifest, is_nightly, rustc_host, sleep_ms,
10};
04ddd4d0 11use crate::support::{main_file, project, Execs};
fecb7246 12use cargo::util::paths::dylib_path_envvar;
d1ec90b3 13
6950bbb0
AC
14#[test]
15fn cargo_compile_simple() {
7fe2fbc8 16 let p = project()
964e72ff 17 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
18 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
19 .build();
d1ec90b3 20
85984a87 21 p.cargo("build").run();
570fe892 22 assert!(p.bin("foo").is_file());
7983a5fd 23
21d9c4ae 24 p.process(&p.bin("foo")).with_stdout("i am foo\n").run();
6950bbb0 25}
d1ec90b3 26
f377ae2c
MS
27#[test]
28fn cargo_fail_with_no_stderr() {
7fe2fbc8 29 let p = project()
f377ae2c 30 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
31 .file("src/foo.rs", &String::from("refusal"))
32 .build();
85984a87
DW
33 p.cargo("build --message-format=json")
34 .with_status(101)
35 .with_stderr_does_not_contain("--- stderr")
36 .run();
f377ae2c
MS
37}
38
a5bf3b88
NM
39/// Check that the `CARGO_INCREMENTAL` environment variable results in
40/// `rustc` getting `-Zincremental` passed to it.
41#[test]
42fn cargo_compile_incremental() {
7fe2fbc8 43 let p = project()
a5bf3b88 44 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
45 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
46 .build();
a5bf3b88 47
85984a87
DW
48 p.cargo("build -v")
49 .env("CARGO_INCREMENTAL", "1")
50 .with_stderr_contains(
16aeb0cd 51 "[RUNNING] `rustc [..] -C incremental=[..]/target/debug/incremental[..]`\n",
fecb7246
AC
52 )
53 .run();
2456da68 54
85984a87
DW
55 p.cargo("test -v")
56 .env("CARGO_INCREMENTAL", "1")
57 .with_stderr_contains(
16aeb0cd 58 "[RUNNING] `rustc [..] -C incremental=[..]/target/debug/incremental[..]`\n",
fecb7246
AC
59 )
60 .run();
a5bf3b88
NM
61}
62
45cc30bc
AC
63#[test]
64fn incremental_profile() {
7fe2fbc8 65 let p = project()
1e682848
AC
66 .file(
67 "Cargo.toml",
68 r#"
45cc30bc
AC
69 [package]
70 name = "foo"
71 version = "0.1.0"
72 authors = []
73
74 [profile.dev]
75 incremental = false
76
77 [profile.release]
78 incremental = true
1e682848 79 "#,
fecb7246
AC
80 )
81 .file("src/main.rs", "fn main() {}")
45cc30bc
AC
82 .build();
83
85984a87
DW
84 p.cargo("build -v")
85 .env_remove("CARGO_INCREMENTAL")
86 .with_stderr_does_not_contain("[..]C incremental=[..]")
87 .run();
45cc30bc 88
85984a87
DW
89 p.cargo("build -v")
90 .env("CARGO_INCREMENTAL", "1")
91 .with_stderr_contains("[..]C incremental=[..]")
92 .run();
45cc30bc 93
85984a87
DW
94 p.cargo("build --release -v")
95 .env_remove("CARGO_INCREMENTAL")
96 .with_stderr_contains("[..]C incremental=[..]")
97 .run();
45cc30bc 98
85984a87
DW
99 p.cargo("build --release -v")
100 .env("CARGO_INCREMENTAL", "0")
101 .with_stderr_does_not_contain("[..]C incremental=[..]")
102 .run();
45cc30bc
AC
103}
104
105#[test]
106fn incremental_config() {
7fe2fbc8 107 let p = project()
45cc30bc 108 .file("src/main.rs", "fn main() {}")
1e682848
AC
109 .file(
110 ".cargo/config",
111 r#"
45cc30bc
AC
112 [build]
113 incremental = false
1e682848 114 "#,
fecb7246
AC
115 )
116 .build();
45cc30bc 117
85984a87
DW
118 p.cargo("build -v")
119 .env_remove("CARGO_INCREMENTAL")
120 .with_stderr_does_not_contain("[..]C incremental=[..]")
121 .run();
45cc30bc 122
85984a87
DW
123 p.cargo("build -v")
124 .env("CARGO_INCREMENTAL", "1")
125 .with_stderr_contains("[..]C incremental=[..]")
126 .run();
45cc30bc
AC
127}
128
68a681ff
TB
129#[test]
130fn cargo_compile_with_workspace_excluded() {
85984a87 131 let p = project().file("src/main.rs", "fn main() {}").build();
68a681ff 132
85984a87
DW
133 p.cargo("build --all --exclude foo")
134 .with_stderr_does_not_contain("[..]virtual[..]")
135 .with_stderr_contains("[..]no packages to compile")
136 .with_status(101)
137 .run();
68a681ff
TB
138}
139
6950bbb0
AC
140#[test]
141fn cargo_compile_manifest_path() {
7fe2fbc8 142 let p = project()
964e72ff 143 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
144 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
145 .build();
2a944836 146
85984a87
DW
147 p.cargo("build --manifest-path foo/Cargo.toml")
148 .cwd(p.root().parent().unwrap())
149 .run();
570fe892 150 assert!(p.bin("foo").is_file());
6950bbb0 151}
2a944836 152
6950bbb0
AC
153#[test]
154fn cargo_compile_with_invalid_manifest() {
7fe2fbc8 155 let p = project().file("Cargo.toml", "").build();
7983a5fd 156
85984a87
DW
157 p.cargo("build")
158 .with_status(101)
159 .with_stderr(
1e682848 160 "\
29cdad4f 161[ERROR] failed to parse manifest at `[..]`
ed58e0be 162
d14abb6e 163Caused by:
3e060339 164 virtual manifests must be configured with [workspace]
1e682848 165",
fecb7246
AC
166 )
167 .run();
6950bbb0 168}
d4265ef2 169
6950bbb0
AC
170#[test]
171fn cargo_compile_with_invalid_manifest2() {
7fe2fbc8 172 let p = project()
1e682848
AC
173 .file(
174 "Cargo.toml",
175 r"
d4265ef2
AC
176 [project]
177 foo = bar
1e682848 178 ",
fecb7246
AC
179 )
180 .build();
d4265ef2 181
85984a87
DW
182 p.cargo("build")
183 .with_status(101)
184 .with_stderr(
1e682848 185 "\
29cdad4f 186[ERROR] failed to parse manifest at `[..]`
d14abb6e
AC
187
188Caused by:
189 could not parse input as TOML
ed58e0be 190
a5a298f1
AC
191Caused by:
192 invalid number at line 3
1e682848 193",
fecb7246
AC
194 )
195 .run();
6950bbb0 196}
7983a5fd 197
6950bbb0
AC
198#[test]
199fn cargo_compile_with_invalid_manifest3() {
85984a87 200 let p = project().file("src/Cargo.toml", "a = bar").build();
127f659c 201
85984a87
DW
202 p.cargo("build --manifest-path src/Cargo.toml")
203 .with_status(101)
204 .with_stderr(
1e682848 205 "\
29cdad4f 206[ERROR] failed to parse manifest at `[..]`
d14abb6e
AC
207
208Caused by:
a5a298f1
AC
209 could not parse input as TOML
210
211Caused by:
212 invalid number at line 1
1e682848 213",
fecb7246
AC
214 )
215 .run();
6950bbb0 216}
127f659c 217
f1759e97
PF
218#[test]
219fn cargo_compile_duplicate_build_targets() {
7fe2fbc8 220 let p = project()
1e682848
AC
221 .file(
222 "Cargo.toml",
223 r#"
f1759e97
PF
224 [package]
225 name = "foo"
226 version = "0.0.1"
227 authors = []
228
229 [lib]
230 name = "main"
bfec42f0 231 path = "src/main.rs"
f1759e97
PF
232 crate-type = ["dylib"]
233
234 [dependencies]
1e682848 235 "#,
fecb7246
AC
236 )
237 .file("src/main.rs", "#![allow(warnings)] fn main() {}")
d43ee1dd 238 .build();
f1759e97 239
85984a87
DW
240 p.cargo("build")
241 .with_stderr(
1e682848 242 "\
c2619648
AC
243warning: file found to be present in multiple build targets: [..]main.rs
244[COMPILING] foo v0.0.1 ([..])
245[FINISHED] [..]
1e682848 246",
fecb7246
AC
247 )
248 .run();
f1759e97
PF
249}
250
6950bbb0
AC
251#[test]
252fn cargo_compile_with_invalid_version() {
7fe2fbc8 253 let p = project()
ab19c483 254 .file("Cargo.toml", &basic_manifest("foo", "1.0"))
d43ee1dd 255 .build();
9514dafd 256
85984a87
DW
257 p.cargo("build")
258 .with_status(101)
259 .with_stderr(
1e682848 260 "\
29cdad4f 261[ERROR] failed to parse manifest at `[..]`
ed58e0be 262
d14abb6e 263Caused by:
ab19c483 264 Expected dot for key `package.version`
1e682848 265",
fecb7246
AC
266 )
267 .run();
6950bbb0 268}
9514dafd 269
6950bbb0 270#[test]
dc2d0c0f 271fn cargo_compile_with_empty_package_name() {
7fe2fbc8 272 let p = project()
ab19c483 273 .file("Cargo.toml", &basic_manifest("", "0.0.0"))
d43ee1dd 274 .build();
10ac930f 275
85984a87
DW
276 p.cargo("build")
277 .with_status(101)
278 .with_stderr(
1e682848 279 "\
29cdad4f 280[ERROR] failed to parse manifest at `[..]`
10ac930f
LJ
281
282Caused by:
37cffbe0 283 package name cannot be an empty string
1e682848 284",
fecb7246
AC
285 )
286 .run();
6950bbb0 287}
10ac930f 288
dc2d0c0f
ZL
289#[test]
290fn cargo_compile_with_invalid_package_name() {
291 let p = project()
292 .file("Cargo.toml", &basic_manifest("foo::bar", "0.0.0"))
293 .build();
294
295 p.cargo("build")
296 .with_status(101)
297 .with_stderr(
298 "\
299[ERROR] failed to parse manifest at `[..]`
300
301Caused by:
302 Invalid character `:` in package name: `foo::bar`
303",
fecb7246
AC
304 )
305 .run();
dc2d0c0f
ZL
306}
307
6950bbb0
AC
308#[test]
309fn cargo_compile_with_invalid_bin_target_name() {
7fe2fbc8 310 let p = project()
1e682848
AC
311 .file(
312 "Cargo.toml",
313 r#"
10ac930f
LJ
314 [package]
315 name = "foo"
316 authors = []
317 version = "0.0.0"
318
319 [[bin]]
320 name = ""
1e682848 321 "#,
fecb7246
AC
322 )
323 .build();
10ac930f 324
85984a87
DW
325 p.cargo("build")
326 .with_status(101)
327 .with_stderr(
1e682848 328 "\
29cdad4f 329[ERROR] failed to parse manifest at `[..]`
10ac930f
LJ
330
331Caused by:
31d64e13 332 binary target names cannot be empty
1e682848 333",
fecb7246
AC
334 )
335 .run();
6950bbb0 336}
10ac930f 337
6950bbb0
AC
338#[test]
339fn cargo_compile_with_forbidden_bin_target_name() {
7fe2fbc8 340 let p = project()
1e682848
AC
341 .file(
342 "Cargo.toml",
343 r#"
82aeaf74
YKL
344 [package]
345 name = "foo"
346 authors = []
347 version = "0.0.0"
348
349 [[bin]]
350 name = "build"
1e682848 351 "#,
fecb7246
AC
352 )
353 .build();
82aeaf74 354
85984a87
DW
355 p.cargo("build")
356 .with_status(101)
357 .with_stderr(
1e682848 358 "\
29cdad4f 359[ERROR] failed to parse manifest at `[..]`
82aeaf74
YKL
360
361Caused by:
362 the binary target name `build` is forbidden
1e682848 363",
fecb7246
AC
364 )
365 .run();
6950bbb0 366}
82aeaf74 367
257c2332
LL
368#[test]
369fn cargo_compile_with_bin_and_crate_type() {
7fe2fbc8 370 let p = project()
257c2332
LL
371 .file(
372 "Cargo.toml",
373 r#"
374 [package]
375 name = "foo"
376 authors = []
377 version = "0.0.0"
378
379 [[bin]]
380 name = "the_foo_bin"
381 path = "src/foo.rs"
382 crate-type = ["cdylib", "rlib"]
383 "#,
fecb7246
AC
384 )
385 .file("src/foo.rs", "fn main() {}")
257c2332
LL
386 .build();
387
85984a87
DW
388 p.cargo("build")
389 .with_status(101)
390 .with_stderr(
257c2332
LL
391 "\
392[ERROR] failed to parse manifest at `[..]`
393
394Caused by:
395 the target `the_foo_bin` is a binary and can't have any crate-types set \
396(currently \"cdylib, rlib\")",
fecb7246
AC
397 )
398 .run();
257c2332
LL
399}
400
401#[test]
402fn cargo_compile_with_bin_and_proc() {
7fe2fbc8 403 let p = project()
257c2332
LL
404 .file(
405 "Cargo.toml",
406 r#"
407 [package]
408 name = "foo"
409 authors = []
410 version = "0.0.0"
411
412 [[bin]]
413 name = "the_foo_bin"
414 path = "src/foo.rs"
415 proc-macro = true
416 "#,
fecb7246
AC
417 )
418 .file("src/foo.rs", "fn main() {}")
257c2332
LL
419 .build();
420
85984a87
DW
421 p.cargo("build")
422 .with_status(101)
423 .with_stderr(
257c2332
LL
424 "\
425[ERROR] failed to parse manifest at `[..]`
426
427Caused by:
428 the target `the_foo_bin` is a binary and can't have `proc-macro` set `true`",
fecb7246
AC
429 )
430 .run();
257c2332
LL
431}
432
6950bbb0
AC
433#[test]
434fn cargo_compile_with_invalid_lib_target_name() {
7fe2fbc8 435 let p = project()
1e682848
AC
436 .file(
437 "Cargo.toml",
438 r#"
10ac930f
LJ
439 [package]
440 name = "foo"
441 authors = []
442 version = "0.0.0"
443
444 [lib]
445 name = ""
1e682848 446 "#,
fecb7246
AC
447 )
448 .build();
10ac930f 449
85984a87
DW
450 p.cargo("build")
451 .with_status(101)
452 .with_stderr(
1e682848 453 "\
29cdad4f 454[ERROR] failed to parse manifest at `[..]`
10ac930f
LJ
455
456Caused by:
31d64e13 457 library target names cannot be empty
1e682848 458",
fecb7246
AC
459 )
460 .run();
6950bbb0 461}
10ac930f 462
8735e8b5
DW
463#[test]
464fn cargo_compile_with_invalid_non_numeric_dep_version() {
465 let p = project()
466 .file(
467 "Cargo.toml",
468 r#"
469 [package]
470 name = "foo"
471 version = "0.0.1"
472
473 [dependencies]
474 crossbeam = "y"
475 "#,
fecb7246
AC
476 )
477 .build();
8735e8b5
DW
478
479 p.cargo("build")
480 .with_status(101)
481 .with_stderr(
482 "\
483[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
484
485Caused by:
486 failed to parse the version requirement `y` for dependency `crossbeam`
487
488Caused by:
489 the given version requirement is invalid
490",
fecb7246
AC
491 )
492 .run();
8735e8b5
DW
493}
494
6950bbb0
AC
495#[test]
496fn cargo_compile_without_manifest() {
7fc0dffe 497 let p = project().no_manifest().build();
7983a5fd 498
85984a87
DW
499 p.cargo("build")
500 .with_status(101)
501 .with_stderr("[ERROR] could not find `Cargo.toml` in `[..]` or any parent directory")
502 .run();
6950bbb0 503}
21322f07 504
6950bbb0
AC
505#[test]
506fn cargo_compile_with_invalid_code() {
7fe2fbc8 507 let p = project()
964e72ff 508 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
509 .file("src/foo.rs", "invalid rust code!")
510 .build();
21322f07 511
85984a87
DW
512 p.cargo("build")
513 .with_status(101)
514 .with_stderr_contains(
1e682848 515 "\
29cdad4f 516[ERROR] Could not compile `foo`.
4e6c3bd1 517
1e682848 518To learn more, run the command again with --verbose.\n",
fecb7246
AC
519 )
520 .run();
570fe892 521 assert!(p.root().join("Cargo.lock").is_file());
6950bbb0 522}
21322f07 523
6950bbb0
AC
524#[test]
525fn cargo_compile_with_invalid_code_in_deps() {
7fe2fbc8 526 let p = project()
1e682848
AC
527 .file(
528 "Cargo.toml",
529 r#"
f6d22b64
AC
530 [package]
531 name = "foo"
532 version = "0.0.1"
533 authors = []
534
535 [dependencies.bar]
536 path = "../bar"
537 [dependencies.baz]
538 path = "../baz"
1e682848 539 "#,
fecb7246
AC
540 )
541 .file("src/main.rs", "invalid rust code!")
d43ee1dd 542 .build();
85984a87
DW
543 let _bar = project()
544 .at("bar")
f58d107e 545 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
d43ee1dd
NK
546 .file("src/lib.rs", "invalid rust code!")
547 .build();
85984a87
DW
548 let _baz = project()
549 .at("baz")
f58d107e 550 .file("Cargo.toml", &basic_manifest("baz", "0.1.0"))
d43ee1dd
NK
551 .file("src/lib.rs", "invalid rust code!")
552 .build();
f58d107e
EH
553 p.cargo("build")
554 .with_status(101)
555 .with_stderr_contains("[..]invalid rust code[..]")
556 .with_stderr_contains("[ERROR] Could not compile [..]")
557 .run();
6950bbb0 558}
f6d22b64 559
6950bbb0
AC
560#[test]
561fn cargo_compile_with_warnings_in_the_root_package() {
7fe2fbc8 562 let p = project()
964e72ff 563 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
564 .file("src/foo.rs", "fn main() {} fn dead() {}")
565 .build();
21322f07 566
85984a87
DW
567 p.cargo("build")
568 .with_stderr_contains("[..]function is never used: `dead`[..]")
569 .run();
6950bbb0 570}
21322f07 571
6950bbb0
AC
572#[test]
573fn cargo_compile_with_warnings_in_a_dep_package() {
7fe2fbc8 574 let p = project()
1e682848
AC
575 .file(
576 "Cargo.toml",
577 r#"
21322f07
YK
578 [project]
579
580 name = "foo"
581 version = "0.5.0"
582 authors = ["wycats@example.com"]
583
18e884d8
AC
584 [dependencies.bar]
585 path = "bar"
21322f07
YK
586
587 [[bin]]
588
589 name = "foo"
1e682848 590 "#,
fecb7246
AC
591 )
592 .file("src/foo.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
ab19c483 593 .file("bar/Cargo.toml", &basic_lib_manifest("bar"))
1e682848
AC
594 .file(
595 "bar/src/bar.rs",
596 r#"
55321111
AC
597 pub fn gimme() -> &'static str {
598 "test passed"
21322f07
YK
599 }
600
601 fn dead() {}
1e682848 602 "#,
fecb7246
AC
603 )
604 .build();
21322f07 605
85984a87
DW
606 p.cargo("build")
607 .with_stderr_contains("[..]function is never used: `dead`[..]")
608 .run();
21322f07 609
570fe892 610 assert!(p.bin("foo").is_file());
21322f07 611
21d9c4ae 612 p.process(&p.bin("foo")).with_stdout("test passed\n").run();
6950bbb0 613}
7983a5fd 614
6950bbb0
AC
615#[test]
616fn cargo_compile_with_nested_deps_inferred() {
7fe2fbc8 617 let p = project()
1e682848
AC
618 .file(
619 "Cargo.toml",
620 r#"
ee1a81a8
YKCL
621 [project]
622
623 name = "foo"
624 version = "0.5.0"
625 authors = ["wycats@example.com"]
626
18e884d8
AC
627 [dependencies.bar]
628 path = 'bar'
ee1a81a8
YKCL
629
630 [[bin]]
ee1a81a8 631 name = "foo"
1e682848 632 "#,
fecb7246
AC
633 )
634 .file("src/foo.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
1e682848
AC
635 .file(
636 "bar/Cargo.toml",
637 r#"
ee1a81a8
YKCL
638 [project]
639
640 name = "bar"
641 version = "0.5.0"
642 authors = ["wycats@example.com"]
643
18e884d8
AC
644 [dependencies.baz]
645 path = "../baz"
1e682848 646 "#,
fecb7246
AC
647 )
648 .file(
1e682848
AC
649 "bar/src/lib.rs",
650 r#"
ee1a81a8
YKCL
651 extern crate baz;
652
653 pub fn gimme() -> String {
654 baz::gimme()
655 }
1e682848 656 "#,
fecb7246
AC
657 )
658 .file("baz/Cargo.toml", &basic_manifest("baz", "0.5.0"))
1e682848
AC
659 .file(
660 "baz/src/lib.rs",
661 r#"
ee1a81a8 662 pub fn gimme() -> String {
139e81ea 663 "test passed".to_string()
ee1a81a8 664 }
1e682848 665 "#,
fecb7246
AC
666 )
667 .build();
ee1a81a8 668
66262110 669 p.cargo("build").run();
ee1a81a8 670
570fe892
DW
671 assert!(p.bin("foo").is_file());
672 assert!(!p.bin("libbar.rlib").is_file());
673 assert!(!p.bin("libbaz.rlib").is_file());
ee1a81a8 674
21d9c4ae 675 p.process(&p.bin("foo")).with_stdout("test passed\n").run();
6950bbb0 676}
ee1a81a8 677
6950bbb0
AC
678#[test]
679fn cargo_compile_with_nested_deps_correct_bin() {
7fe2fbc8 680 let p = project()
1e682848
AC
681 .file(
682 "Cargo.toml",
683 r#"
ee1a81a8
YKCL
684 [project]
685
686 name = "foo"
687 version = "0.5.0"
688 authors = ["wycats@example.com"]
689
18e884d8
AC
690 [dependencies.bar]
691 path = "bar"
ee1a81a8
YKCL
692
693 [[bin]]
ee1a81a8 694 name = "foo"
1e682848 695 "#,
fecb7246
AC
696 )
697 .file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
1e682848
AC
698 .file(
699 "bar/Cargo.toml",
700 r#"
ee1a81a8
YKCL
701 [project]
702
703 name = "bar"
704 version = "0.5.0"
705 authors = ["wycats@example.com"]
706
18e884d8
AC
707 [dependencies.baz]
708 path = "../baz"
1e682848 709 "#,
fecb7246
AC
710 )
711 .file(
1e682848
AC
712 "bar/src/lib.rs",
713 r#"
ee1a81a8
YKCL
714 extern crate baz;
715
716 pub fn gimme() -> String {
717 baz::gimme()
718 }
1e682848 719 "#,
fecb7246
AC
720 )
721 .file("baz/Cargo.toml", &basic_manifest("baz", "0.5.0"))
1e682848
AC
722 .file(
723 "baz/src/lib.rs",
724 r#"
ee1a81a8 725 pub fn gimme() -> String {
139e81ea 726 "test passed".to_string()
ee1a81a8 727 }
1e682848 728 "#,
fecb7246
AC
729 )
730 .build();
ee1a81a8 731
66262110 732 p.cargo("build").run();
ee1a81a8 733
570fe892
DW
734 assert!(p.bin("foo").is_file());
735 assert!(!p.bin("libbar.rlib").is_file());
736 assert!(!p.bin("libbaz.rlib").is_file());
ee1a81a8 737
21d9c4ae 738 p.process(&p.bin("foo")).with_stdout("test passed\n").run();
6950bbb0 739}
ee1a81a8 740
6950bbb0
AC
741#[test]
742fn cargo_compile_with_nested_deps_shorthand() {
7fe2fbc8 743 let p = project()
1e682848
AC
744 .file(
745 "Cargo.toml",
746 r#"
48dc0814
YK
747 [project]
748
749 name = "foo"
750 version = "0.5.0"
751 authors = ["wycats@example.com"]
752
18e884d8
AC
753 [dependencies.bar]
754 path = "bar"
1e682848 755 "#,
fecb7246
AC
756 )
757 .file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
1e682848
AC
758 .file(
759 "bar/Cargo.toml",
760 r#"
48dc0814
YK
761 [project]
762
763 name = "bar"
764 version = "0.5.0"
765 authors = ["wycats@example.com"]
766
18e884d8
AC
767 [dependencies.baz]
768 path = "../baz"
48dc0814 769
22dfc520 770 [lib]
48dc0814
YK
771
772 name = "bar"
1e682848 773 "#,
fecb7246
AC
774 )
775 .file(
1e682848
AC
776 "bar/src/bar.rs",
777 r#"
48dc0814
YK
778 extern crate baz;
779
7e6433fe 780 pub fn gimme() -> String {
48dc0814
YK
781 baz::gimme()
782 }
1e682848 783 "#,
fecb7246
AC
784 )
785 .file("baz/Cargo.toml", &basic_lib_manifest("baz"))
1e682848
AC
786 .file(
787 "baz/src/baz.rs",
788 r#"
7e6433fe 789 pub fn gimme() -> String {
139e81ea 790 "test passed".to_string()
48dc0814 791 }
1e682848 792 "#,
fecb7246
AC
793 )
794 .build();
48dc0814 795
66262110 796 p.cargo("build").run();
48dc0814 797
570fe892
DW
798 assert!(p.bin("foo").is_file());
799 assert!(!p.bin("libbar.rlib").is_file());
800 assert!(!p.bin("libbaz.rlib").is_file());
48dc0814 801
21d9c4ae 802 p.process(&p.bin("foo")).with_stdout("test passed\n").run();
6950bbb0 803}
48dc0814 804
6950bbb0
AC
805#[test]
806fn cargo_compile_with_nested_deps_longhand() {
7fe2fbc8 807 let p = project()
1e682848
AC
808 .file(
809 "Cargo.toml",
810 r#"
92602f6d
YKCL
811 [project]
812
813 name = "foo"
814 version = "0.5.0"
815 authors = ["wycats@example.com"]
816
18e884d8
AC
817 [dependencies.bar]
818 path = "bar"
819 version = "0.5.0"
92602f6d
YKCL
820
821 [[bin]]
822
823 name = "foo"
1e682848 824 "#,
fecb7246
AC
825 )
826 .file("src/foo.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
1e682848
AC
827 .file(
828 "bar/Cargo.toml",
829 r#"
92602f6d
YKCL
830 [project]
831
832 name = "bar"
833 version = "0.5.0"
834 authors = ["wycats@example.com"]
835
836 [dependencies.baz]
18e884d8 837 path = "../baz"
92602f6d
YKCL
838 version = "0.5.0"
839
22dfc520 840 [lib]
92602f6d
YKCL
841
842 name = "bar"
1e682848 843 "#,
fecb7246
AC
844 )
845 .file(
1e682848
AC
846 "bar/src/bar.rs",
847 r#"
92602f6d
YKCL
848 extern crate baz;
849
850 pub fn gimme() -> String {
851 baz::gimme()
852 }
1e682848 853 "#,
fecb7246
AC
854 )
855 .file("baz/Cargo.toml", &basic_lib_manifest("baz"))
1e682848
AC
856 .file(
857 "baz/src/baz.rs",
858 r#"
92602f6d 859 pub fn gimme() -> String {
139e81ea 860 "test passed".to_string()
92602f6d 861 }
1e682848 862 "#,
fecb7246
AC
863 )
864 .build();
92602f6d 865
85984a87 866 p.cargo("build").run();
92602f6d 867
570fe892
DW
868 assert!(p.bin("foo").is_file());
869 assert!(!p.bin("libbar.rlib").is_file());
870 assert!(!p.bin("libbaz.rlib").is_file());
92602f6d 871
21d9c4ae 872 p.process(&p.bin("foo")).with_stdout("test passed\n").run();
6950bbb0 873}
92602f6d 874
f65aeebf
MS
875// Check that Cargo gives a sensible error if a dependency can't be found
876// because of a name mismatch.
6950bbb0
AC
877#[test]
878fn cargo_compile_with_dep_name_mismatch() {
7fe2fbc8 879 let p = project()
1e682848
AC
880 .file(
881 "Cargo.toml",
882 r#"
f65aeebf
MS
883 [package]
884
885 name = "foo"
886 version = "0.0.1"
887 authors = ["wycats@example.com"]
888
889 [[bin]]
890
891 name = "foo"
892
893 [dependencies.notquitebar]
894
895 path = "bar"
1e682848 896 "#,
fecb7246
AC
897 )
898 .file("src/bin/foo.rs", &main_file(r#""i am foo""#, &["bar"]))
964e72ff 899 .file("bar/Cargo.toml", &basic_bin_manifest("bar"))
d43ee1dd
NK
900 .file("bar/src/bar.rs", &main_file(r#""i am bar""#, &[]))
901 .build();
f65aeebf 902
85984a87
DW
903 p.cargo("build")
904 .with_status(101)
d5fc8dc3 905 .with_stderr(
1e682848 906 r#"error: no matching package named `notquitebar` found
89f43938
ZL
907location searched: [CWD]/bar
908required by package `foo v0.0.1 ([CWD])`
1e682848 909"#,
fecb7246
AC
910 )
911 .run();
6950bbb0 912}
f65aeebf 913
6950bbb0
AC
914#[test]
915fn cargo_compile_with_filename() {
7fe2fbc8 916 let p = project()
089dafc2 917 .file("src/lib.rs", "")
1e682848
AC
918 .file(
919 "src/bin/a.rs",
920 r#"
089dafc2
RQ
921 extern crate foo;
922 fn main() { println!("hello a.rs"); }
1e682848 923 "#,
fecb7246
AC
924 )
925 .file("examples/a.rs", r#"fn main() { println!("example"); }"#)
d43ee1dd 926 .build();
089dafc2 927
85984a87
DW
928 p.cargo("build --bin bin.rs")
929 .with_status(101)
930 .with_stderr("[ERROR] no bin target named `bin.rs`")
931 .run();
089dafc2 932
85984a87
DW
933 p.cargo("build --bin a.rs")
934 .with_status(101)
935 .with_stderr(
1e682848 936 "\
29cdad4f 937[ERROR] no bin target named `a.rs`
089dafc2 938
1e682848 939Did you mean `a`?",
fecb7246
AC
940 )
941 .run();
089dafc2 942
85984a87
DW
943 p.cargo("build --example example.rs")
944 .with_status(101)
945 .with_stderr("[ERROR] no example target named `example.rs`")
946 .run();
089dafc2 947
85984a87
DW
948 p.cargo("build --example a.rs")
949 .with_status(101)
950 .with_stderr(
1e682848 951 "\
29cdad4f 952[ERROR] no example target named `a.rs`
089dafc2 953
1e682848 954Did you mean `a`?",
fecb7246
AC
955 )
956 .run();
6950bbb0 957}
089dafc2 958
ec5f78f9
PA
959#[test]
960fn cargo_compile_path_with_offline() {
7fe2fbc8 961 let p = project()
1e682848
AC
962 .file(
963 "Cargo.toml",
964 r#"
ec5f78f9
PA
965 [package]
966 name = "foo"
967 version = "0.0.1"
968 authors = []
969
970 [dependencies.bar]
971 path = "bar"
1e682848 972 "#,
fecb7246
AC
973 )
974 .file("src/lib.rs", "")
ab19c483 975 .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
ec5f78f9
PA
976 .file("bar/src/lib.rs", "")
977 .build();
978
85984a87
DW
979 p.cargo("build -Zoffline")
980 .masquerade_as_nightly_cargo()
981 .run();
ec5f78f9
PA
982}
983
984#[test]
985fn cargo_compile_with_downloaded_dependency_with_offline() {
986 Package::new("present_dep", "1.2.3")
ab19c483 987 .file("Cargo.toml", &basic_manifest("present_dep", "1.2.3"))
ec5f78f9
PA
988 .file("src/lib.rs", "")
989 .publish();
990
991 {
992 // make package downloaded
7fe2fbc8 993 let p = project()
1e682848
AC
994 .file(
995 "Cargo.toml",
996 r#"
ec5f78f9
PA
997 [project]
998 name = "foo"
999 version = "0.1.0"
1000
1001 [dependencies]
1002 present_dep = "1.2.3"
1e682848 1003 "#,
fecb7246
AC
1004 )
1005 .file("src/lib.rs", "")
ec5f78f9 1006 .build();
85984a87 1007 p.cargo("build").run();
ec5f78f9
PA
1008 }
1009
85984a87
DW
1010 let p2 = project()
1011 .at("bar")
1e682848
AC
1012 .file(
1013 "Cargo.toml",
1014 r#"
ec5f78f9
PA
1015 [project]
1016 name = "bar"
1017 version = "0.1.0"
1018
1019 [dependencies]
1020 present_dep = "1.2.3"
1e682848 1021 "#,
fecb7246
AC
1022 )
1023 .file("src/lib.rs", "")
ec5f78f9
PA
1024 .build();
1025
85984a87
DW
1026 p2.cargo("build -Zoffline")
1027 .masquerade_as_nightly_cargo()
1028 .with_stderr(
1e682848 1029 "\
ec5f78f9 1030[COMPILING] present_dep v1.2.3
3ed34979 1031[COMPILING] bar v0.1.0 ([..])
85984a87 1032[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
fecb7246
AC
1033 )
1034 .run();
ec5f78f9
PA
1035}
1036
1037#[test]
1038fn cargo_compile_offline_not_try_update() {
85984a87
DW
1039 let p = project()
1040 .at("bar")
1e682848
AC
1041 .file(
1042 "Cargo.toml",
1043 r#"
ec5f78f9
PA
1044 [project]
1045 name = "bar"
1046 version = "0.1.0"
1047
1048 [dependencies]
1049 not_cached_dep = "1.2.5"
1e682848 1050 "#,
fecb7246
AC
1051 )
1052 .file("src/lib.rs", "")
ec5f78f9
PA
1053 .build();
1054
85984a87
DW
1055 p.cargo("build -Zoffline")
1056 .masquerade_as_nightly_cargo()
1057 .with_status(101)
1058 .with_stderr(
1e682848 1059 "\
0117eb1f 1060error: no matching package named `not_cached_dep` found
ec5f78f9 1061location searched: registry `[..]`
0117eb1f 1062required by package `bar v0.1.0 ([..])`
3ed34979
PA
1063As a reminder, you're using offline mode (-Z offline) \
1064which can sometimes cause surprising resolution failures, \
bfea4d57 1065if this error is too confusing you may wish to retry \
1e682848 1066without the offline flag.",
fecb7246
AC
1067 )
1068 .run();
ec5f78f9
PA
1069}
1070
1071#[test]
1e682848 1072fn compile_offline_without_maxvers_cached() {
ec5f78f9
PA
1073 Package::new("present_dep", "1.2.1").publish();
1074 Package::new("present_dep", "1.2.2").publish();
1075
1076 Package::new("present_dep", "1.2.3")
ab19c483 1077 .file("Cargo.toml", &basic_manifest("present_dep", "1.2.3"))
85984a87
DW
1078 .file(
1079 "src/lib.rs",
1080 r#"pub fn get_version()->&'static str {"1.2.3"}"#,
fecb7246
AC
1081 )
1082 .publish();
ec5f78f9
PA
1083
1084 Package::new("present_dep", "1.2.5")
ab19c483 1085 .file("Cargo.toml", &basic_manifest("present_dep", "1.2.5"))
ec5f78f9
PA
1086 .file("src/lib.rs", r#"pub fn get_version(){"1.2.5"}"#)
1087 .publish();
1088
1089 {
1090 // make package cached
7fe2fbc8 1091 let p = project()
1e682848
AC
1092 .file(
1093 "Cargo.toml",
1094 r#"
ec5f78f9
PA
1095 [project]
1096 name = "foo"
1097 version = "0.1.0"
1098
1099 [dependencies]
1100 present_dep = "=1.2.3"
1e682848 1101 "#,
fecb7246
AC
1102 )
1103 .file("src/lib.rs", "")
ec5f78f9 1104 .build();
85984a87 1105 p.cargo("build").run();
ec5f78f9
PA
1106 }
1107
7fe2fbc8 1108 let p2 = project()
1e682848
AC
1109 .file(
1110 "Cargo.toml",
1111 r#"
ec5f78f9
PA
1112 [project]
1113 name = "foo"
1114 version = "0.1.0"
1115
1116 [dependencies]
1117 present_dep = "1.2"
1e682848 1118 "#,
fecb7246
AC
1119 )
1120 .file(
1e682848
AC
1121 "src/main.rs",
1122 "\
ec5f78f9
PA
1123extern crate present_dep;
1124fn main(){
1125 println!(\"{}\", present_dep::get_version());
1e682848 1126}",
fecb7246
AC
1127 )
1128 .build();
ec5f78f9 1129
85984a87
DW
1130 p2.cargo("run -Zoffline")
1131 .masquerade_as_nightly_cargo()
2cd9cce6 1132 .with_stderr(
85984a87 1133 "\
ec5f78f9 1134[COMPILING] present_dep v1.2.3
89f43938 1135[COMPILING] foo v0.1.0 ([CWD])
3ed34979 1136[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1e682848 1137 Running `[..]`",
fecb7246
AC
1138 )
1139 .with_stdout("1.2.3")
85984a87 1140 .run();
ec5f78f9
PA
1141}
1142
22ff9fcb
E
1143#[test]
1144fn incompatible_dependencies() {
1145 Package::new("bad", "0.1.0").publish();
1146 Package::new("bad", "1.0.0").publish();
1147 Package::new("bad", "1.0.1").publish();
1148 Package::new("bad", "1.0.2").publish();
f8c9928c
DW
1149 Package::new("bar", "0.1.0").dep("bad", "0.1.0").publish();
1150 Package::new("baz", "0.1.1").dep("bad", "=1.0.0").publish();
1151 Package::new("baz", "0.1.0").dep("bad", "=1.0.0").publish();
1152 Package::new("qux", "0.1.2").dep("bad", ">=1.0.1").publish();
1153 Package::new("qux", "0.1.1").dep("bad", ">=1.0.1").publish();
1154 Package::new("qux", "0.1.0").dep("bad", ">=1.0.1").publish();
22ff9fcb 1155
f8c9928c 1156 let p = project()
1e682848
AC
1157 .file(
1158 "Cargo.toml",
1159 r#"
22ff9fcb 1160 [project]
f8c9928c 1161 name = "foo"
22ff9fcb
E
1162 version = "0.0.1"
1163
1164 [dependencies]
22ff9fcb
E
1165 bar = "0.1.0"
1166 baz = "0.1.0"
f8c9928c 1167 qux = "0.1.0"
1e682848 1168 "#,
fecb7246
AC
1169 )
1170 .file("src/main.rs", "fn main(){}")
22ff9fcb
E
1171 .build();
1172
85984a87
DW
1173 p.cargo("build")
1174 .with_status(101)
1175 .with_stderr_contains(
1e682848 1176 "\
a0282214 1177error: failed to select a version for `bad`.
f8c9928c
DW
1178 ... required by package `qux v0.1.0`
1179 ... which is depended on by `foo v0.0.1 ([..])`
70dada8a
E
1180versions that meet the requirements `>= 1.0.1` are: 1.0.2, 1.0.1
1181
1182all possible versions conflict with previously selected packages.
1183
0117eb1f 1184 previously selected package `bad v1.0.0`
f8c9928c
DW
1185 ... which is depended on by `baz v0.1.0`
1186 ... which is depended on by `foo v0.0.1 ([..])`
70dada8a 1187
1e682848 1188failed to select a version for `bad` which could resolve this conflict",
fecb7246
AC
1189 )
1190 .run();
22ff9fcb
E
1191}
1192
a0282214
E
1193#[test]
1194fn incompatible_dependencies_with_multi_semver() {
1195 Package::new("bad", "1.0.0").publish();
1196 Package::new("bad", "1.0.1").publish();
1197 Package::new("bad", "2.0.0").publish();
1198 Package::new("bad", "2.0.1").publish();
1199 Package::new("bar", "0.1.0").dep("bad", "=1.0.0").publish();
1200 Package::new("baz", "0.1.0").dep("bad", ">=2.0.1").publish();
1201
f8c9928c 1202 let p = project()
1e682848
AC
1203 .file(
1204 "Cargo.toml",
1205 r#"
a0282214 1206 [project]
f8c9928c 1207 name = "foo"
a0282214
E
1208 version = "0.0.1"
1209
1210 [dependencies]
1211 bar = "0.1.0"
1212 baz = "0.1.0"
1213 bad = ">=1.0.1, <=2.0.0"
1e682848 1214 "#,
fecb7246
AC
1215 )
1216 .file("src/main.rs", "fn main(){}")
a0282214
E
1217 .build();
1218
85984a87
DW
1219 p.cargo("build")
1220 .with_status(101)
1221 .with_stderr_contains(
1e682848 1222 "\
a0282214 1223error: failed to select a version for `bad`.
f8c9928c 1224 ... required by package `foo v0.0.1 ([..])`
70dada8a
E
1225versions that meet the requirements `>= 1.0.1, <= 2.0.0` are: 2.0.0, 1.0.1
1226
a0282214 1227all possible versions conflict with previously selected packages.
70dada8a 1228
a0282214
E
1229 previously selected package `bad v2.0.1`
1230 ... which is depended on by `baz v0.1.0`
f8c9928c 1231 ... which is depended on by `foo v0.0.1 ([..])`
70dada8a 1232
a0282214
E
1233 previously selected package `bad v1.0.0`
1234 ... which is depended on by `bar v0.1.0`
f8c9928c 1235 ... which is depended on by `foo v0.0.1 ([..])`
70dada8a 1236
1e682848 1237failed to select a version for `bad` which could resolve this conflict",
fecb7246
AC
1238 )
1239 .run();
a0282214
E
1240}
1241
ec5f78f9
PA
1242#[test]
1243fn compile_offline_while_transitive_dep_not_cached() {
f8c9928c
DW
1244 let baz = Package::new("baz", "1.0.0");
1245 let baz_path = baz.archive_dst();
1246 baz.publish();
ec5f78f9
PA
1247
1248 let mut content = Vec::new();
1249
f8c9928c 1250 let mut file = File::open(baz_path.clone()).ok().unwrap();
ec5f78f9
PA
1251 let _ok = file.read_to_end(&mut content).ok().unwrap();
1252 drop(file);
f8c9928c 1253 drop(File::create(baz_path.clone()).ok().unwrap());
ec5f78f9 1254
f8c9928c 1255 Package::new("bar", "0.1.0").dep("baz", "1.0.0").publish();
ec5f78f9 1256
f8c9928c 1257 let p = project()
1e682848
AC
1258 .file(
1259 "Cargo.toml",
1260 r#"
ec5f78f9 1261 [project]
f8c9928c 1262 name = "foo"
ec5f78f9
PA
1263 version = "0.0.1"
1264
1265 [dependencies]
f8c9928c 1266 bar = "0.1.0"
1e682848 1267 "#,
fecb7246
AC
1268 )
1269 .file("src/main.rs", "fn main(){}")
ec5f78f9
PA
1270 .build();
1271
f8c9928c 1272 // simulate download bar, but fail to download baz
f58d107e
EH
1273 p.cargo("build")
1274 .with_status(101)
1275 .with_stderr_contains("[..]failed to verify the checksum of `baz[..]")
1276 .run();
ec5f78f9 1277
f8c9928c 1278 drop(File::create(baz_path).ok().unwrap().write_all(&content));
ec5f78f9 1279
85984a87
DW
1280 p.cargo("build -Zoffline")
1281 .masquerade_as_nightly_cargo()
1282 .with_status(101)
1283 .with_stderr(
1e682848 1284 "\
f8c9928c 1285error: no matching package named `baz` found
ec5f78f9 1286location searched: registry `[..]`
f8c9928c 1287required by package `bar v0.1.0`
89f43938 1288 ... which is depended on by `foo v0.0.1 ([CWD])`
3ed34979
PA
1289As a reminder, you're using offline mode (-Z offline) \
1290which can sometimes cause surprising resolution failures, \
bfea4d57 1291if this error is too confusing you may wish to retry \
1e682848 1292without the offline flag.",
fecb7246
AC
1293 )
1294 .run();
ec5f78f9
PA
1295}
1296
6950bbb0
AC
1297#[test]
1298fn compile_path_dep_then_change_version() {
7fe2fbc8 1299 let p = project()
1e682848
AC
1300 .file(
1301 "Cargo.toml",
1302 r#"
96589b35
AC
1303 [package]
1304 name = "foo"
1305 version = "0.0.1"
1306 authors = []
1307
1308 [dependencies.bar]
1309 path = "bar"
1e682848 1310 "#,
fecb7246
AC
1311 )
1312 .file("src/lib.rs", "")
ab19c483 1313 .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
d43ee1dd
NK
1314 .file("bar/src/lib.rs", "")
1315 .build();
96589b35 1316
85984a87 1317 p.cargo("build").run();
96589b35 1318
1e682848
AC
1319 File::create(&p.root().join("bar/Cargo.toml"))
1320 .unwrap()
ab19c483 1321 .write_all(basic_manifest("bar", "0.0.2").as_bytes())
1e682848 1322 .unwrap();
96589b35 1323
85984a87 1324 p.cargo("build").run();
6950bbb0 1325}
96589b35 1326
6950bbb0
AC
1327#[test]
1328fn ignores_carriage_return_in_lockfile() {
7fe2fbc8 1329 let p = project()
ca7d9ee2 1330 .file("src/main.rs", r"mod a; fn main() {}")
d43ee1dd
NK
1331 .file("src/a.rs", "")
1332 .build();
05dc1802 1333
85984a87 1334 p.cargo("build").run();
05dc1802
CNG
1335
1336 let lockfile = p.root().join("Cargo.lock");
1337 let mut lock = String::new();
1e682848
AC
1338 File::open(&lockfile)
1339 .unwrap()
1340 .read_to_string(&mut lock)
1341 .unwrap();
05dc1802 1342 let lock = lock.replace("\n", "\r\n");
1e682848
AC
1343 File::create(&lockfile)
1344 .unwrap()
1345 .write_all(lock.as_bytes())
1346 .unwrap();
85984a87 1347 p.cargo("build").run();
6950bbb0 1348}
05dc1802 1349
4650194d
NK
1350#[test]
1351fn cargo_default_env_metadata_env_var() {
1352 // Ensure that path dep + dylib + env_var get metadata
1353 // (even though path_dep + dylib should not)
7fe2fbc8 1354 let p = project()
1e682848
AC
1355 .file(
1356 "Cargo.toml",
1357 r#"
4650194d
NK
1358 [package]
1359 name = "foo"
1360 version = "0.0.1"
1361 authors = []
1362
1363 [dependencies.bar]
1364 path = "bar"
1e682848 1365 "#,
fecb7246
AC
1366 )
1367 .file("src/lib.rs", "// hi")
1e682848
AC
1368 .file(
1369 "bar/Cargo.toml",
1370 r#"
4650194d
NK
1371 [package]
1372 name = "bar"
1373 version = "0.0.1"
1374 authors = []
1375
1376 [lib]
1377 name = "bar"
1378 crate_type = ["dylib"]
1e682848 1379 "#,
fecb7246
AC
1380 )
1381 .file("bar/src/lib.rs", "// hello")
d43ee1dd 1382 .build();
4650194d
NK
1383
1384 // No metadata on libbar since it's a dylib path dependency
85984a87
DW
1385 p.cargo("build -v")
1386 .with_stderr(&format!(
1e682848 1387 "\
89f43938 1388[COMPILING] bar v0.0.1 ([CWD]/bar)
4779dbfe 1389[RUNNING] `rustc --crate-name bar bar/src/lib.rs --color never --crate-type dylib \
844d7ae7 1390 --emit=dep-info,link \
6d864a81 1391 -C prefer-dynamic -C debuginfo=2 \
4650194d
NK
1392 -C metadata=[..] \
1393 --out-dir [..] \
89f43938
ZL
1394 -L dependency=[CWD]/target/debug/deps`
1395[COMPILING] foo v0.0.1 ([CWD])
4779dbfe 1396[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
6d864a81 1397 --emit=dep-info,link -C debuginfo=2 \
4650194d
NK
1398 -C metadata=[..] \
1399 -C extra-filename=[..] \
1400 --out-dir [..] \
89f43938
ZL
1401 -L dependency=[CWD]/target/debug/deps \
1402 --extern bar=[CWD]/target/debug/deps/{prefix}bar{suffix}`
34628b65 1403[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
1e682848
AC
1404 prefix = env::consts::DLL_PREFIX,
1405 suffix = env::consts::DLL_SUFFIX,
fecb7246
AC
1406 ))
1407 .run();
4650194d 1408
85984a87 1409 p.cargo("clean").run();
4650194d
NK
1410
1411 // If you set the env-var, then we expect metadata on libbar
85984a87
DW
1412 p.cargo("build -v")
1413 .env("__CARGO_DEFAULT_LIB_METADATA", "stable")
1414 .with_stderr(&format!(
1e682848 1415 "\
89f43938 1416[COMPILING] bar v0.0.1 ([CWD]/bar)
4779dbfe 1417[RUNNING] `rustc --crate-name bar bar/src/lib.rs --color never --crate-type dylib \
844d7ae7 1418 --emit=dep-info,link \
6d864a81 1419 -C prefer-dynamic -C debuginfo=2 \
4650194d
NK
1420 -C metadata=[..] \
1421 --out-dir [..] \
89f43938
ZL
1422 -L dependency=[CWD]/target/debug/deps`
1423[COMPILING] foo v0.0.1 ([CWD])
4779dbfe 1424[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
6d864a81 1425 --emit=dep-info,link -C debuginfo=2 \
4650194d
NK
1426 -C metadata=[..] \
1427 -C extra-filename=[..] \
1428 --out-dir [..] \
89f43938
ZL
1429 -L dependency=[CWD]/target/debug/deps \
1430 --extern bar=[CWD]/target/debug/deps/{prefix}bar-[..]{suffix}`
34628b65 1431[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
4650194d 1432",
1e682848
AC
1433 prefix = env::consts::DLL_PREFIX,
1434 suffix = env::consts::DLL_SUFFIX,
fecb7246
AC
1435 ))
1436 .run();
4650194d
NK
1437}
1438
6950bbb0
AC
1439#[test]
1440fn crate_env_vars() {
7fe2fbc8 1441 let p = project()
1e682848
AC
1442 .file(
1443 "Cargo.toml",
1444 r#"
a5a298f1
AC
1445 [project]
1446 name = "foo"
1447 version = "0.5.1-alpha.1"
1448 description = "This is foo"
0c3851c0
JC
1449 homepage = "https://example.com"
1450 repository = "https://example.com/repo.git"
a5a298f1 1451 authors = ["wycats@example.com"]
1e682848 1452 "#,
fecb7246
AC
1453 )
1454 .file(
1e682848
AC
1455 "src/main.rs",
1456 r#"
a986938b 1457 extern crate foo;
a12beb61 1458
3a654e4e 1459
a5a298f1
AC
1460 static VERSION_MAJOR: &'static str = env!("CARGO_PKG_VERSION_MAJOR");
1461 static VERSION_MINOR: &'static str = env!("CARGO_PKG_VERSION_MINOR");
1462 static VERSION_PATCH: &'static str = env!("CARGO_PKG_VERSION_PATCH");
1463 static VERSION_PRE: &'static str = env!("CARGO_PKG_VERSION_PRE");
1464 static VERSION: &'static str = env!("CARGO_PKG_VERSION");
1465 static CARGO_MANIFEST_DIR: &'static str = env!("CARGO_MANIFEST_DIR");
1466 static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
1467 static HOMEPAGE: &'static str = env!("CARGO_PKG_HOMEPAGE");
07f872a6 1468 static REPOSITORY: &'static str = env!("CARGO_PKG_REPOSITORY");
a5a298f1 1469 static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
a12beb61
CL
1470
1471 fn main() {
97321331
AC
1472 let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR,
1473 VERSION_MINOR, VERSION_PATCH, VERSION_PRE,
1474 CARGO_MANIFEST_DIR);
a5a298f1
AC
1475 assert_eq!(s, foo::version());
1476 println!("{}", s);
1477 assert_eq!("foo", PKG_NAME);
0c3851c0
JC
1478 assert_eq!("https://example.com", HOMEPAGE);
1479 assert_eq!("https://example.com/repo.git", REPOSITORY);
a5a298f1 1480 assert_eq!("This is foo", DESCRIPTION);
446486b7
SD
1481 let s = format!("{}.{}.{}-{}", VERSION_MAJOR,
1482 VERSION_MINOR, VERSION_PATCH, VERSION_PRE);
a5a298f1 1483 assert_eq!(s, VERSION);
a986938b 1484 }
1e682848 1485 "#,
fecb7246
AC
1486 )
1487 .file(
1e682848
AC
1488 "src/lib.rs",
1489 r#"
a986938b 1490 pub fn version() -> String {
97321331 1491 format!("{}-{}-{} @ {} in {}",
a986938b
AC
1492 env!("CARGO_PKG_VERSION_MAJOR"),
1493 env!("CARGO_PKG_VERSION_MINOR"),
1494 env!("CARGO_PKG_VERSION_PATCH"),
97321331
AC
1495 env!("CARGO_PKG_VERSION_PRE"),
1496 env!("CARGO_MANIFEST_DIR"))
a12beb61 1497 }
1e682848 1498 "#,
fecb7246
AC
1499 )
1500 .build();
a12beb61 1501
9e779198 1502 println!("build");
85984a87 1503 p.cargo("build -v").run();
a12beb61 1504
9e779198 1505 println!("bin");
fecb7246
AC
1506 p.process(&p.bin("foo"))
1507 .with_stdout("0-5-1 @ alpha.1 in [CWD]")
1508 .run();
a986938b 1509
9e779198 1510 println!("test");
85984a87 1511 p.cargo("test -v").run();
6950bbb0 1512}
a12beb61 1513
6950bbb0
AC
1514#[test]
1515fn crate_authors_env_vars() {
7fe2fbc8 1516 let p = project()
1e682848
AC
1517 .file(
1518 "Cargo.toml",
1519 r#"
dd26ce3b
MM
1520 [project]
1521 name = "foo"
1522 version = "0.5.1-alpha.1"
1523 authors = ["wycats@example.com", "neikos@example.com"]
1e682848 1524 "#,
fecb7246
AC
1525 )
1526 .file(
1e682848
AC
1527 "src/main.rs",
1528 r#"
dd26ce3b
MM
1529 extern crate foo;
1530
1531 static AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");
1532
1533 fn main() {
1534 let s = "wycats@example.com:neikos@example.com";
1535 assert_eq!(AUTHORS, foo::authors());
1536 println!("{}", AUTHORS);
1537 assert_eq!(s, AUTHORS);
1538 }
1e682848 1539 "#,
fecb7246
AC
1540 )
1541 .file(
1e682848
AC
1542 "src/lib.rs",
1543 r#"
dd26ce3b
MM
1544 pub fn authors() -> String {
1545 format!("{}", env!("CARGO_PKG_AUTHORS"))
1546 }
1e682848 1547 "#,
fecb7246
AC
1548 )
1549 .build();
dd26ce3b
MM
1550
1551 println!("build");
85984a87 1552 p.cargo("build -v").run();
dd26ce3b
MM
1553
1554 println!("bin");
21d9c4ae
DW
1555 p.process(&p.bin("foo"))
1556 .with_stdout("wycats@example.com:neikos@example.com")
1557 .run();
dd26ce3b
MM
1558
1559 println!("test");
85984a87 1560 p.cargo("test -v").run();
6950bbb0 1561}
dd26ce3b 1562
81390379
MW
1563#[test]
1564fn vv_prints_rustc_env_vars() {
1565 let p = project()
1566 .file(
1567 "Cargo.toml",
1568 r#"
1569 [project]
1570 name = "foo"
1571 version = "0.0.1"
1572 authors = ["escape='\"@example.com"]
1573 "#,
1574 )
1575 .file("src/main.rs", "fn main() {}")
1576 .build();
1577
1578 let mut b = p.cargo("build -vv");
1579
1580 if cfg!(windows) {
1581 b.with_stderr_contains(
e557f660 1582 "[RUNNING] `[..]set CARGO_PKG_NAME=foo&& [..]rustc [..]`"
81390379 1583 ).with_stderr_contains(
e557f660 1584 r#"[RUNNING] `[..]set CARGO_PKG_AUTHORS="escape='\"@example.com"&& [..]rustc [..]`"#
81390379
MW
1585 )
1586 } else {
db09895f 1587 b.with_stderr_contains("[RUNNING] `[..]CARGO_PKG_NAME=foo [..]rustc [..]`")
1588 .with_stderr_contains(
1589 r#"[RUNNING] `[..]CARGO_PKG_AUTHORS='escape='\''"@example.com' [..]rustc [..]`"#,
1590 )
81390379
MW
1591 };
1592
1593 b.run();
1594}
1595
2683d0e9 1596// The tester may already have LD_LIBRARY_PATH=::/foo/bar which leads to a false positive error
b5ee3635 1597fn setenv_for_removing_empty_component(mut execs: Execs) -> Execs {
2683d0e9
NK
1598 let v = dylib_path_envvar();
1599 if let Ok(search_path) = env::var(v) {
85984a87
DW
1600 let new_search_path =
1601 env::join_paths(env::split_paths(&search_path).filter(|e| !e.as_os_str().is_empty()))
1602 .expect("join_paths");
b5ee3635 1603 execs.env(v, new_search_path); // build_command() will override LD_LIBRARY_PATH accordingly
2683d0e9 1604 }
b5ee3635 1605 execs
2683d0e9
NK
1606}
1607
05884243
MB
1608// Regression test for #4277
1609#[test]
1610fn crate_library_path_env_var() {
7fe2fbc8 1611 let p = project()
1e682848
AC
1612 .file(
1613 "src/main.rs",
1614 &format!(
1615 r##"
05884243
MB
1616 fn main() {{
1617 let search_path = env!("{}");
1618 let paths = std::env::split_paths(&search_path).collect::<Vec<_>>();
1619 assert!(!paths.contains(&"".into()));
1620 }}
1e682848
AC
1621 "##,
1622 dylib_path_envvar()
1623 ),
fecb7246
AC
1624 )
1625 .build();
05884243 1626
85984a87 1627 setenv_for_removing_empty_component(p.cargo("run")).run();
05884243
MB
1628}
1629
1630// Regression test for #4277
1631#[test]
1632fn build_with_fake_libc_not_loading() {
7fe2fbc8 1633 let p = project()
ca7d9ee2 1634 .file("src/main.rs", "fn main() {}")
05884243 1635 .file("src/lib.rs", r#" "#)
d43ee1dd
NK
1636 .file("libc.so.6", r#""#)
1637 .build();
05884243 1638
85984a87 1639 setenv_for_removing_empty_component(p.cargo("build")).run();
05884243
MB
1640}
1641
ee1a81a8 1642// this is testing that src/<pkg-name>.rs still works (for now)
6950bbb0
AC
1643#[test]
1644fn many_crate_types_old_style_lib_location() {
7fe2fbc8 1645 let p = project()
1e682848
AC
1646 .file(
1647 "Cargo.toml",
1648 r#"
bc1dea82
AC
1649 [project]
1650
1651 name = "foo"
1652 version = "0.5.0"
1653 authors = ["wycats@example.com"]
1654
22dfc520 1655 [lib]
bc1dea82
AC
1656
1657 name = "foo"
1658 crate_type = ["rlib", "dylib"]
1e682848 1659 "#,
fecb7246
AC
1660 )
1661 .file("src/foo.rs", "pub fn foo() {}")
d43ee1dd 1662 .build();
85984a87
DW
1663 p.cargo("build")
1664 .with_stderr_contains(
1e682848 1665 "\
05400b80 1666[WARNING] path `[..]src/foo.rs` was erroneously implicitly accepted for library `foo`,
1e682848 1667please rename the file to `src/lib.rs` or set lib.path in Cargo.toml",
fecb7246
AC
1668 )
1669 .run();
bc1dea82 1670
570fe892 1671 assert!(p.root().join("target/debug/libfoo.rlib").is_file());
1e682848 1672 let fname = format!("{}foo{}", env::consts::DLL_PREFIX, env::consts::DLL_SUFFIX);
570fe892 1673 assert!(p.root().join("target/debug").join(&fname).is_file());
6950bbb0 1674}
ee1a81a8 1675
6950bbb0
AC
1676#[test]
1677fn many_crate_types_correct() {
7fe2fbc8 1678 let p = project()
1e682848
AC
1679 .file(
1680 "Cargo.toml",
1681 r#"
ee1a81a8
YKCL
1682 [project]
1683
1684 name = "foo"
1685 version = "0.5.0"
1686 authors = ["wycats@example.com"]
1687
22dfc520 1688 [lib]
ee1a81a8
YKCL
1689
1690 name = "foo"
1691 crate_type = ["rlib", "dylib"]
1e682848 1692 "#,
fecb7246
AC
1693 )
1694 .file("src/lib.rs", "pub fn foo() {}")
d43ee1dd 1695 .build();
85984a87 1696 p.cargo("build").run();
bc1dea82 1697
570fe892 1698 assert!(p.root().join("target/debug/libfoo.rlib").is_file());
1e682848 1699 let fname = format!("{}foo{}", env::consts::DLL_PREFIX, env::consts::DLL_SUFFIX);
570fe892 1700 assert!(p.root().join("target/debug").join(&fname).is_file());
6950bbb0 1701}
a2aa2bf9 1702
6950bbb0
AC
1703#[test]
1704fn self_dependency() {
7fe2fbc8 1705 let p = project()
1e682848
AC
1706 .file(
1707 "Cargo.toml",
1708 r#"
71e4252a
AC
1709 [package]
1710
1711 name = "test"
1712 version = "0.0.0"
1713 authors = []
1714
1715 [dependencies.test]
1716
1717 path = "."
1718
22dfc520 1719 [lib]
71e4252a 1720 name = "test"
bfec42f0 1721 path = "src/test.rs"
1e682848 1722 "#,
fecb7246
AC
1723 )
1724 .file("src/test.rs", "fn main() {}")
d43ee1dd 1725 .build();
85984a87
DW
1726 p.cargo("build")
1727 .with_status(101)
1728 .with_stderr(
1e682848 1729 "\
89f43938
ZL
1730[ERROR] cyclic package dependency: package `test v0.0.0 ([CWD])` depends on itself. Cycle:
1731package `test v0.0.0 ([CWD])`",
fecb7246
AC
1732 )
1733 .run();
6950bbb0 1734}
f4881742 1735
6950bbb0
AC
1736#[test]
1737fn ignore_broken_symlinks() {
48420965 1738 // windows and symlinks don't currently agree that well
1e682848
AC
1739 if cfg!(windows) {
1740 return;
1741 }
48420965 1742
7fe2fbc8 1743 let p = project()
964e72ff
AC
1744 .file("Cargo.toml", &basic_bin_manifest("foo"))
1745 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
d43ee1dd
NK
1746 .symlink("Notafile", "bar")
1747 .build();
f4881742 1748
85984a87 1749 p.cargo("build").run();
570fe892 1750 assert!(p.bin("foo").is_file());
f4881742 1751
21d9c4ae 1752 p.process(&p.bin("foo")).with_stdout("i am foo\n").run();
6950bbb0 1753}
03ccdd81 1754
6950bbb0
AC
1755#[test]
1756fn missing_lib_and_bin() {
081e7930 1757 let p = project().build();
85984a87
DW
1758 p.cargo("build")
1759 .with_status(101)
1760 .with_stderr(
1e682848 1761 "\
29cdad4f 1762[ERROR] failed to parse manifest at `[..]Cargo.toml`
d14abb6e
AC
1763
1764Caused by:
d726c576 1765 no targets specified in the manifest
1e682848 1766 either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present\n",
fecb7246
AC
1767 )
1768 .run();
6950bbb0 1769}
558e7534 1770
6950bbb0
AC
1771#[test]
1772fn lto_build() {
07cacbf8
AC
1773 // FIXME: currently this hits a linker bug on 32-bit MSVC
1774 if cfg!(all(target_env = "msvc", target_pointer_width = "32")) {
1e682848 1775 return;
07cacbf8
AC
1776 }
1777
7fe2fbc8 1778 let p = project()
1e682848
AC
1779 .file(
1780 "Cargo.toml",
1781 r#"
74bd3c3b
AG
1782 [package]
1783
1784 name = "test"
1785 version = "0.0.0"
1786 authors = []
442de299
AC
1787
1788 [profile.release]
74bd3c3b 1789 lto = true
1e682848 1790 "#,
fecb7246
AC
1791 )
1792 .file("src/main.rs", "fn main() {}")
d43ee1dd 1793 .build();
85984a87 1794 p.cargo("build -v --release")
2cd9cce6 1795 .with_stderr(
1e682848 1796 "\
89f43938 1797[COMPILING] test v0.0.0 ([CWD])
4779dbfe 1798[RUNNING] `rustc --crate-name test src/main.rs --color never --crate-type bin \
844d7ae7 1799 --emit=dep-info,link \
48420965 1800 -C opt-level=3 \
74bd3c3b 1801 -C lto \
083ff01e 1802 -C metadata=[..] \
89f43938
ZL
1803 --out-dir [CWD]/target/release/deps \
1804 -L dependency=[CWD]/target/release/deps`
cd955f12 1805[FINISHED] release [optimized] target(s) in [..]
74bd3c3b 1806",
fecb7246
AC
1807 )
1808 .run();
6950bbb0 1809}
74bd3c3b 1810
6950bbb0
AC
1811#[test]
1812fn verbose_build() {
85984a87
DW
1813 let p = project().file("src/lib.rs", "").build();
1814 p.cargo("build -v")
2cd9cce6 1815 .with_stderr(
1e682848 1816 "\
89f43938 1817[COMPILING] foo v0.0.1 ([CWD])
4779dbfe 1818[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
6d864a81 1819 --emit=dep-info,link -C debuginfo=2 \
083ff01e 1820 -C metadata=[..] \
0863469c 1821 --out-dir [..] \
89f43938 1822 -L dependency=[CWD]/target/debug/deps`
34628b65 1823[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
7a542c51 1824",
fecb7246
AC
1825 )
1826 .run();
6950bbb0 1827}
558e7534 1828
6950bbb0
AC
1829#[test]
1830fn verbose_release_build() {
85984a87
DW
1831 let p = project().file("src/lib.rs", "").build();
1832 p.cargo("build -v --release")
2cd9cce6 1833 .with_stderr(
1e682848 1834 "\
89f43938 1835[COMPILING] foo v0.0.1 ([CWD])
4779dbfe 1836[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
844d7ae7 1837 --emit=dep-info,link \
48420965 1838 -C opt-level=3 \
083ff01e 1839 -C metadata=[..] \
0863469c 1840 --out-dir [..] \
89f43938 1841 -L dependency=[CWD]/target/release/deps`
cd955f12 1842[FINISHED] release [optimized] target(s) in [..]
7a542c51 1843",
fecb7246
AC
1844 )
1845 .run();
6950bbb0 1846}
558e7534 1847
6950bbb0
AC
1848#[test]
1849fn verbose_release_build_deps() {
7fe2fbc8 1850 let p = project()
1e682848
AC
1851 .file(
1852 "Cargo.toml",
1853 r#"
558e7534
AC
1854 [package]
1855
1856 name = "test"
1857 version = "0.0.0"
1858 authors = []
1859
1860 [dependencies.foo]
1861 path = "foo"
1e682848 1862 "#,
fecb7246
AC
1863 )
1864 .file("src/lib.rs", "")
1e682848
AC
1865 .file(
1866 "foo/Cargo.toml",
1867 r#"
558e7534
AC
1868 [package]
1869
1870 name = "foo"
1871 version = "0.0.0"
1872 authors = []
8defed61 1873
22dfc520 1874 [lib]
8defed61
AC
1875 name = "foo"
1876 crate_type = ["dylib", "rlib"]
1e682848 1877 "#,
fecb7246
AC
1878 )
1879 .file("foo/src/lib.rs", "")
d43ee1dd 1880 .build();
85984a87
DW
1881 p.cargo("build -v --release")
1882 .with_stderr(&format!(
1e682848 1883 "\
89f43938 1884[COMPILING] foo v0.0.0 ([CWD]/foo)
4779dbfe 1885[RUNNING] `rustc --crate-name foo foo/src/lib.rs --color never \
844d7ae7
NC
1886 --crate-type dylib --crate-type rlib \
1887 --emit=dep-info,link \
1888 -C prefer-dynamic \
48420965 1889 -C opt-level=3 \
083ff01e 1890 -C metadata=[..] \
0863469c 1891 --out-dir [..] \
89f43938
ZL
1892 -L dependency=[CWD]/target/release/deps`
1893[COMPILING] test v0.0.0 ([CWD])
4779dbfe 1894[RUNNING] `rustc --crate-name test src/lib.rs --color never --crate-type lib \
844d7ae7 1895 --emit=dep-info,link \
48420965 1896 -C opt-level=3 \
083ff01e 1897 -C metadata=[..] \
0863469c 1898 --out-dir [..] \
89f43938
ZL
1899 -L dependency=[CWD]/target/release/deps \
1900 --extern foo=[CWD]/target/release/deps/{prefix}foo{suffix} \
1901 --extern foo=[CWD]/target/release/deps/libfoo.rlib`
cd955f12 1902[FINISHED] release [optimized] target(s) in [..]
7a542c51 1903",
1e682848
AC
1904 prefix = env::consts::DLL_PREFIX,
1905 suffix = env::consts::DLL_SUFFIX
fecb7246
AC
1906 ))
1907 .run();
6950bbb0 1908}
246c9e2a 1909
6950bbb0
AC
1910#[test]
1911fn explicit_examples() {
f8c9928c 1912 let p = project()
1e682848
AC
1913 .file(
1914 "Cargo.toml",
1915 r#"
246c9e2a 1916 [package]
f8c9928c 1917 name = "foo"
246c9e2a
BL
1918 version = "1.0.0"
1919 authors = []
1920
22dfc520 1921 [lib]
f8c9928c 1922 name = "foo"
246c9e2a
BL
1923 path = "src/lib.rs"
1924
1925 [[example]]
1926 name = "hello"
1927 path = "examples/ex-hello.rs"
1928
1929 [[example]]
1930 name = "goodbye"
1931 path = "examples/ex-goodbye.rs"
1e682848 1932 "#,
fecb7246
AC
1933 )
1934 .file(
1e682848
AC
1935 "src/lib.rs",
1936 r#"
246c9e2a
BL
1937 pub fn get_hello() -> &'static str { "Hello" }
1938 pub fn get_goodbye() -> &'static str { "Goodbye" }
1939 pub fn get_world() -> &'static str { "World" }
1e682848 1940 "#,
fecb7246
AC
1941 )
1942 .file(
1e682848
AC
1943 "examples/ex-hello.rs",
1944 r#"
f8c9928c
DW
1945 extern crate foo;
1946 fn main() { println!("{}, {}!", foo::get_hello(), foo::get_world()); }
1e682848 1947 "#,
fecb7246
AC
1948 )
1949 .file(
1e682848
AC
1950 "examples/ex-goodbye.rs",
1951 r#"
f8c9928c
DW
1952 extern crate foo;
1953 fn main() { println!("{}, {}!", foo::get_goodbye(), foo::get_world()); }
1e682848 1954 "#,
fecb7246
AC
1955 )
1956 .build();
246c9e2a 1957
85984a87 1958 p.cargo("test -v").run();
21d9c4ae
DW
1959 p.process(&p.bin("examples/hello"))
1960 .with_stdout("Hello, World!\n")
1961 .run();
1962 p.process(&p.bin("examples/goodbye"))
1963 .with_stdout("Goodbye, World!\n")
1964 .run();
6950bbb0 1965}
246c9e2a 1966
5c088f69
AK
1967#[test]
1968fn non_existing_example() {
f8c9928c 1969 let p = project()
1e682848
AC
1970 .file(
1971 "Cargo.toml",
1972 r#"
5c088f69 1973 [package]
f8c9928c 1974 name = "foo"
5c088f69
AK
1975 version = "1.0.0"
1976 authors = []
1977
1978 [lib]
f8c9928c 1979 name = "foo"
5c088f69
AK
1980 path = "src/lib.rs"
1981
1982 [[example]]
1983 name = "hello"
1e682848 1984 "#,
fecb7246
AC
1985 )
1986 .file("src/lib.rs", "")
d43ee1dd 1987 .build();
5c088f69 1988
85984a87
DW
1989 p.cargo("test -v")
1990 .with_status(101)
1991 .with_stderr(
1e682848 1992 "\
5c088f69
AK
1993[ERROR] failed to parse manifest at `[..]`
1994
1995Caused by:
1e682848 1996 can't find `hello` example, specify example.path",
fecb7246
AC
1997 )
1998 .run();
5c088f69
AK
1999}
2000
0ae2b97f
AK
2001#[test]
2002fn non_existing_binary() {
f8c9928c 2003 let p = project()
081e7930 2004 .file("Cargo.toml", &basic_bin_manifest("foo"))
0ae2b97f 2005 .file("src/lib.rs", "")
d43ee1dd
NK
2006 .file("src/bin/ehlo.rs", "")
2007 .build();
0ae2b97f 2008
85984a87
DW
2009 p.cargo("build -v")
2010 .with_status(101)
2011 .with_stderr(
1e682848 2012 "\
0ae2b97f
AK
2013[ERROR] failed to parse manifest at `[..]`
2014
2015Caused by:
081e7930 2016 can't find `foo` bin, specify bin.path",
fecb7246
AC
2017 )
2018 .run();
0ae2b97f
AK
2019}
2020
2021#[test]
081e7930 2022fn legacy_binary_paths_warnings() {
f8c9928c 2023 let p = project()
1e682848
AC
2024 .file(
2025 "Cargo.toml",
2026 r#"
0ae2b97f
AK
2027 [package]
2028 name = "foo"
2029 version = "1.0.0"
2030 authors = []
2031
2032 [[bin]]
2033 name = "bar"
1e682848 2034 "#,
fecb7246
AC
2035 )
2036 .file("src/lib.rs", "")
d43ee1dd
NK
2037 .file("src/main.rs", "fn main() {}")
2038 .build();
0ae2b97f 2039
85984a87
DW
2040 p.cargo("build -v")
2041 .with_stderr_contains(
1e682848 2042 "\
05400b80 2043[WARNING] path `[..]src/main.rs` was erroneously implicitly accepted for binary `bar`,
1e682848 2044please set bin.path in Cargo.toml",
fecb7246
AC
2045 )
2046 .run();
0ae2b97f 2047
f8c9928c 2048 let p = project()
1e682848
AC
2049 .file(
2050 "Cargo.toml",
2051 r#"
0ae2b97f
AK
2052 [package]
2053 name = "foo"
2054 version = "1.0.0"
2055 authors = []
2056
2057 [[bin]]
2058 name = "bar"
1e682848 2059 "#,
fecb7246
AC
2060 )
2061 .file("src/lib.rs", "")
d43ee1dd
NK
2062 .file("src/bin/main.rs", "fn main() {}")
2063 .build();
0ae2b97f 2064
85984a87
DW
2065 p.cargo("build -v")
2066 .with_stderr_contains(
1e682848 2067 "\
05400b80 2068[WARNING] path `[..]src/bin/main.rs` was erroneously implicitly accepted for binary `bar`,
1e682848 2069please set bin.path in Cargo.toml",
fecb7246
AC
2070 )
2071 .run();
0ae2b97f 2072
f8c9928c 2073 let p = project()
1e682848
AC
2074 .file(
2075 "Cargo.toml",
2076 r#"
0ae2b97f
AK
2077 [package]
2078 name = "foo"
2079 version = "1.0.0"
2080 authors = []
2081
2082 [[bin]]
2083 name = "bar"
1e682848 2084 "#,
fecb7246
AC
2085 )
2086 .file("src/bar.rs", "fn main() {}")
d43ee1dd 2087 .build();
0ae2b97f 2088
85984a87
DW
2089 p.cargo("build -v")
2090 .with_stderr_contains(
1e682848 2091 "\
05400b80 2092[WARNING] path `[..]src/bar.rs` was erroneously implicitly accepted for binary `bar`,
1e682848 2093please set bin.path in Cargo.toml",
fecb7246
AC
2094 )
2095 .run();
0ae2b97f
AK
2096}
2097
6950bbb0
AC
2098#[test]
2099fn implicit_examples() {
f8c9928c 2100 let p = project()
1e682848
AC
2101 .file(
2102 "src/lib.rs",
2103 r#"
246c9e2a
BL
2104 pub fn get_hello() -> &'static str { "Hello" }
2105 pub fn get_goodbye() -> &'static str { "Goodbye" }
2106 pub fn get_world() -> &'static str { "World" }
1e682848 2107 "#,
fecb7246
AC
2108 )
2109 .file(
1e682848
AC
2110 "examples/hello.rs",
2111 r#"
f8c9928c 2112 extern crate foo;
a6dad622 2113 fn main() {
f8c9928c 2114 println!("{}, {}!", foo::get_hello(), foo::get_world());
a6dad622 2115 }
1e682848 2116 "#,
fecb7246
AC
2117 )
2118 .file(
1e682848
AC
2119 "examples/goodbye.rs",
2120 r#"
f8c9928c 2121 extern crate foo;
a6dad622 2122 fn main() {
f8c9928c 2123 println!("{}, {}!", foo::get_goodbye(), foo::get_world());
a6dad622 2124 }
1e682848 2125 "#,
fecb7246
AC
2126 )
2127 .build();
246c9e2a 2128
85984a87 2129 p.cargo("test").run();
21d9c4ae
DW
2130 p.process(&p.bin("examples/hello"))
2131 .with_stdout("Hello, World!\n")
2132 .run();
2133 p.process(&p.bin("examples/goodbye"))
2134 .with_stdout("Goodbye, World!\n")
2135 .run();
6950bbb0 2136}
a12a909c 2137
6950bbb0
AC
2138#[test]
2139fn standard_build_no_ndebug() {
f8c9928c 2140 let p = project()
a6dad622 2141 .file("Cargo.toml", &basic_bin_manifest("foo"))
1e682848
AC
2142 .file(
2143 "src/foo.rs",
2144 r#"
a12a909c 2145 fn main() {
c54fc006 2146 if cfg!(debug_assertions) {
a12a909c 2147 println!("slow")
c54fc006
AC
2148 } else {
2149 println!("fast")
a12a909c
SF
2150 }
2151 }
1e682848 2152 "#,
fecb7246
AC
2153 )
2154 .build();
a12a909c 2155
85984a87 2156 p.cargo("build").run();
21d9c4ae 2157 p.process(&p.bin("foo")).with_stdout("slow\n").run();
6950bbb0 2158}
a12a909c 2159
6950bbb0
AC
2160#[test]
2161fn release_build_ndebug() {
f8c9928c 2162 let p = project()
a6dad622 2163 .file("Cargo.toml", &basic_bin_manifest("foo"))
1e682848
AC
2164 .file(
2165 "src/foo.rs",
2166 r#"
a12a909c 2167 fn main() {
c54fc006 2168 if cfg!(debug_assertions) {
a12a909c 2169 println!("slow")
c54fc006
AC
2170 } else {
2171 println!("fast")
a12a909c
SF
2172 }
2173 }
1e682848 2174 "#,
fecb7246
AC
2175 )
2176 .build();
a12a909c 2177
85984a87 2178 p.cargo("build --release").run();
21d9c4ae 2179 p.process(&p.release_bin("foo")).with_stdout("fast\n").run();
6950bbb0 2180}
835e5ac1 2181
6950bbb0
AC
2182#[test]
2183fn inferred_main_bin() {
85984a87 2184 let p = project().file("src/main.rs", "fn main() {}").build();
835e5ac1 2185
85984a87 2186 p.cargo("build").run();
21d9c4ae 2187 p.process(&p.bin("foo")).run();
6950bbb0 2188}
9f5d9b81 2189
6950bbb0
AC
2190#[test]
2191fn deletion_causes_failure() {
7fe2fbc8 2192 let p = project()
1e682848
AC
2193 .file(
2194 "Cargo.toml",
2195 r#"
9f5d9b81
AC
2196 [package]
2197 name = "foo"
2198 version = "0.0.1"
2199 authors = []
2200
2201 [dependencies.bar]
2202 path = "bar"
1e682848 2203 "#,
fecb7246
AC
2204 )
2205 .file("src/main.rs", "extern crate bar; fn main() {}")
ab19c483 2206 .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
d43ee1dd
NK
2207 .file("bar/src/lib.rs", "")
2208 .build();
9f5d9b81 2209
85984a87 2210 p.cargo("build").run();
ab19c483 2211 p.change_file("Cargo.toml", &basic_manifest("foo", "0.0.1"));
f58d107e
EH
2212 p.cargo("build")
2213 .with_status(101)
2214 .with_stderr_contains("[..]can't find crate for `bar`")
2215 .run();
6950bbb0 2216}
5e1c31d5 2217
6950bbb0
AC
2218#[test]
2219fn bad_cargo_toml_in_target_dir() {
f8c9928c 2220 let p = project()
ca7d9ee2 2221 .file("src/main.rs", "fn main() {}")
d43ee1dd
NK
2222 .file("target/Cargo.toml", "bad-toml")
2223 .build();
5e1c31d5 2224
85984a87 2225 p.cargo("build").run();
21d9c4ae 2226 p.process(&p.bin("foo")).run();
6950bbb0 2227}
ed3bb05b 2228
6950bbb0
AC
2229#[test]
2230fn lib_with_standard_name() {
7fe2fbc8 2231 let p = project()
ab19c483 2232 .file("Cargo.toml", &basic_manifest("syntax", "0.0.1"))
ca7d9ee2 2233 .file("src/lib.rs", "pub fn foo() {}")
85984a87
DW
2234 .file(
2235 "src/main.rs",
2236 "extern crate syntax; fn main() { syntax::foo() }",
fecb7246
AC
2237 )
2238 .build();
ed3bb05b 2239
85984a87 2240 p.cargo("build")
2cd9cce6 2241 .with_stderr(
1e682848 2242 "\
89f43938 2243[COMPILING] syntax v0.0.1 ([CWD])
34628b65 2244[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
ed3bb05b 2245",
fecb7246
AC
2246 )
2247 .run();
6950bbb0 2248}
85b0d0a4 2249
6950bbb0
AC
2250#[test]
2251fn simple_staticlib() {
7fe2fbc8 2252 let p = project()
1e682848
AC
2253 .file(
2254 "Cargo.toml",
2255 r#"
85b0d0a4
AC
2256 [package]
2257 name = "foo"
2258 authors = []
2259 version = "0.0.1"
2260
22dfc520 2261 [lib]
85b0d0a4
AC
2262 name = "foo"
2263 crate-type = ["staticlib"]
1e682848 2264 "#,
fecb7246
AC
2265 )
2266 .file("src/lib.rs", "pub fn foo() {}")
d43ee1dd 2267 .build();
85b0d0a4 2268
5955ea8a 2269 // env var is a test for #1381
85984a87 2270 p.cargo("build").env("RUST_LOG", "nekoneko=trace").run();
6950bbb0 2271}
e2720d2b 2272
6950bbb0
AC
2273#[test]
2274fn staticlib_rlib_and_bin() {
7fe2fbc8 2275 let p = project()
1e682848
AC
2276 .file(
2277 "Cargo.toml",
2278 r#"
045254ed
CL
2279 [package]
2280 name = "foo"
2281 authors = []
2282 version = "0.0.1"
2283
2284 [lib]
2285 name = "foo"
2286 crate-type = ["staticlib", "rlib"]
1e682848 2287 "#,
fecb7246
AC
2288 )
2289 .file("src/lib.rs", "pub fn foo() {}")
ca7d9ee2 2290 .file("src/main.rs", "extern crate foo; fn main() { foo::foo(); }")
d43ee1dd 2291 .build();
045254ed 2292
85984a87 2293 p.cargo("build -v").run();
6950bbb0 2294}
045254ed 2295
6950bbb0
AC
2296#[test]
2297fn opt_out_of_bin() {
7fe2fbc8 2298 let p = project()
1e682848
AC
2299 .file(
2300 "Cargo.toml",
2301 r#"
e2720d2b
AC
2302 bin = []
2303
2304 [package]
2305 name = "foo"
2306 authors = []
2307 version = "0.0.1"
1e682848 2308 "#,
fecb7246
AC
2309 )
2310 .file("src/lib.rs", "")
d43ee1dd
NK
2311 .file("src/main.rs", "bad syntax")
2312 .build();
85984a87 2313 p.cargo("build").run();
6950bbb0 2314}
e2720d2b 2315
6950bbb0
AC
2316#[test]
2317fn single_lib() {
7fe2fbc8 2318 let p = project()
1e682848
AC
2319 .file(
2320 "Cargo.toml",
2321 r#"
e2720d2b
AC
2322 [package]
2323 name = "foo"
2324 authors = []
2325 version = "0.0.1"
2326
2327 [lib]
2328 name = "foo"
2329 path = "src/bar.rs"
1e682848 2330 "#,
fecb7246
AC
2331 )
2332 .file("src/bar.rs", "")
d43ee1dd 2333 .build();
85984a87 2334 p.cargo("build").run();
6950bbb0 2335}
22dfc520 2336
6950bbb0
AC
2337#[test]
2338fn freshness_ignores_excluded() {
7fe2fbc8 2339 let foo = project()
1e682848
AC
2340 .file(
2341 "Cargo.toml",
2342 r#"
0e8f8d50
AC
2343 [package]
2344 name = "foo"
2345 version = "0.0.0"
2346 authors = []
0fdaf507 2347 build = "build.rs"
0e8f8d50 2348 exclude = ["src/b*.rs"]
1e682848 2349 "#,
fecb7246
AC
2350 )
2351 .file("build.rs", "fn main() {}")
d43ee1dd
NK
2352 .file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
2353 .build();
763ba535 2354 foo.root().move_into_the_past();
0e8f8d50 2355
85984a87 2356 foo.cargo("build")
2cd9cce6 2357 .with_stderr(
1e682848 2358 "\
89f43938 2359[COMPILING] foo v0.0.0 ([CWD])
34628b65 2360[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1e682848 2361",
fecb7246
AC
2362 )
2363 .run();
0e8f8d50
AC
2364
2365 // Smoke test to make sure it doesn't compile again
2366 println!("first pass");
85984a87 2367 foo.cargo("build").with_stdout("").run();
0e8f8d50
AC
2368
2369 // Modify an ignored file and make sure we don't rebuild
2370 println!("second pass");
9ed3a6ea 2371 File::create(&foo.root().join("src/bar.rs")).unwrap();
85984a87 2372 foo.cargo("build").with_stdout("").run();
6950bbb0 2373}
a216b9f7 2374
6950bbb0
AC
2375#[test]
2376fn rebuild_preserves_out_dir() {
7fe2fbc8 2377 let foo = project()
1e682848
AC
2378 .file(
2379 "Cargo.toml",
2380 r#"
a216b9f7 2381 [package]
0fdaf507
AC
2382 name = "foo"
2383 version = "0.0.0"
2384 authors = []
2385 build = 'build.rs'
1e682848 2386 "#,
fecb7246
AC
2387 )
2388 .file(
1e682848
AC
2389 "build.rs",
2390 r#"
ee5e24ff 2391 use std::env;
964e72ff
AC
2392 use std::fs::File;
2393 use std::path::Path;
a216b9f7 2394
0fdaf507 2395 fn main() {
964e72ff 2396 let path = Path::new(&env::var("OUT_DIR").unwrap()).join("foo");
1384050e 2397 if env::var_os("FIRST").is_some() {
a216b9f7
AC
2398 File::create(&path).unwrap();
2399 } else {
2400 File::create(&path).unwrap();
2401 }
0fdaf507 2402 }
1e682848 2403 "#,
fecb7246
AC
2404 )
2405 .file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
d43ee1dd 2406 .build();
763ba535 2407 foo.root().move_into_the_past();
a216b9f7 2408
85984a87
DW
2409 foo.cargo("build")
2410 .env("FIRST", "1")
2cd9cce6 2411 .with_stderr(
1e682848 2412 "\
89f43938 2413[COMPILING] foo v0.0.0 ([CWD])
34628b65 2414[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1e682848 2415",
fecb7246
AC
2416 )
2417 .run();
a216b9f7 2418
9ed3a6ea 2419 File::create(&foo.root().join("src/bar.rs")).unwrap();
85984a87 2420 foo.cargo("build")
2cd9cce6 2421 .with_stderr(
1e682848 2422 "\
89f43938 2423[COMPILING] foo v0.0.0 ([CWD])
34628b65 2424[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1e682848 2425",
fecb7246
AC
2426 )
2427 .run();
6950bbb0 2428}
ac094e89 2429
6950bbb0
AC
2430#[test]
2431fn dep_no_libs() {
7fe2fbc8 2432 let foo = project()
1e682848
AC
2433 .file(
2434 "Cargo.toml",
2435 r#"
ac094e89
AC
2436 [package]
2437 name = "foo"
2438 version = "0.0.0"
2439 authors = []
2440
2441 [dependencies.bar]
2442 path = "bar"
1e682848 2443 "#,
fecb7246
AC
2444 )
2445 .file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
ab19c483 2446 .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.0"))
d43ee1dd
NK
2447 .file("bar/src/main.rs", "")
2448 .build();
85984a87 2449 foo.cargo("build").run();
6950bbb0 2450}
3efe0704 2451
6950bbb0
AC
2452#[test]
2453fn recompile_space_in_name() {
7fe2fbc8 2454 let foo = project()
1e682848
AC
2455 .file(
2456 "Cargo.toml",
2457 r#"
3efe0704
AC
2458 [package]
2459 name = "foo"
2460 version = "0.0.0"
2461 authors = []
2462
2463 [lib]
2464 name = "foo"
2465 path = "src/my lib.rs"
1e682848 2466 "#,
fecb7246
AC
2467 )
2468 .file("src/my lib.rs", "")
d43ee1dd 2469 .build();
85984a87 2470 foo.cargo("build").run();
763ba535 2471 foo.root().move_into_the_past();
85984a87 2472 foo.cargo("build").with_stdout("").run();
6950bbb0 2473}
f86aaeda 2474
a6dad622 2475#[cfg(unix)]
6950bbb0
AC
2476#[test]
2477fn ignore_bad_directories() {
a6dad622 2478 use std::os::unix::prelude::*;
7fe2fbc8 2479 let foo = project()
ab19c483 2480 .file("Cargo.toml", &basic_manifest("foo", "0.0.0"))
d43ee1dd
NK
2481 .file("src/lib.rs", "")
2482 .build();
a6dad622
AC
2483 let dir = foo.root().join("tmp");
2484 fs::create_dir(&dir).unwrap();
2485 let stat = fs::metadata(&dir).unwrap();
2486 let mut perms = stat.permissions();
2487 perms.set_mode(0o644);
2488 fs::set_permissions(&dir, perms.clone()).unwrap();
85984a87 2489 foo.cargo("build").run();
a6dad622
AC
2490 perms.set_mode(0o755);
2491 fs::set_permissions(&dir, perms).unwrap();
6950bbb0 2492}
670fc47d 2493
6950bbb0
AC
2494#[test]
2495fn bad_cargo_config() {
7fe2fbc8 2496 let foo = project()
ab19c483 2497 .file("Cargo.toml", &basic_manifest("foo", "0.0.0"))
670fc47d 2498 .file("src/lib.rs", "")
ca7d9ee2 2499 .file(".cargo/config", "this is not valid toml")
d43ee1dd 2500 .build();
85984a87
DW
2501 foo.cargo("build -v")
2502 .with_status(101)
2503 .with_stderr(
1e682848 2504 "\
ecd1e5df 2505[ERROR] could not load Cargo configuration
670fc47d
AC
2506
2507Caused by:
0602940f 2508 could not parse TOML configuration in `[..]`
670fc47d
AC
2509
2510Caused by:
ed58e0be 2511 could not parse input as TOML
670fc47d 2512
a5a298f1 2513Caused by:
ca7d9ee2 2514 expected an equals, found an identifier at line 1
1e682848 2515",
fecb7246
AC
2516 )
2517 .run();
6950bbb0 2518}
8495548d 2519
6950bbb0
AC
2520#[test]
2521fn cargo_platform_specific_dependency() {
763ba535 2522 let host = rustc_host();
7fe2fbc8 2523 let p = project()
1e682848
AC
2524 .file(
2525 "Cargo.toml",
2526 &format!(
2527 r#"
8495548d 2528 [project]
8495548d
PK
2529 name = "foo"
2530 version = "0.5.0"
2531 authors = ["wycats@example.com"]
3f0ef765 2532 build = "build.rs"
8495548d 2533
3f0ef765
AC
2534 [target.{host}.dependencies]
2535 dep = {{ path = "dep" }}
2536 [target.{host}.build-dependencies]
2537 build = {{ path = "build" }}
2538 [target.{host}.dev-dependencies]
2539 dev = {{ path = "dev" }}
1e682848
AC
2540 "#,
2541 host = host
2542 ),
fecb7246
AC
2543 )
2544 .file("src/main.rs", "extern crate dep; fn main() { dep::dep() }")
85984a87
DW
2545 .file(
2546 "tests/foo.rs",
2547 "extern crate dev; #[test] fn foo() { dev::dev() }",
fecb7246
AC
2548 )
2549 .file(
85984a87
DW
2550 "build.rs",
2551 "extern crate build; fn main() { build::build(); }",
fecb7246
AC
2552 )
2553 .file("dep/Cargo.toml", &basic_manifest("dep", "0.5.0"))
3f0ef765 2554 .file("dep/src/lib.rs", "pub fn dep() {}")
ab19c483 2555 .file("build/Cargo.toml", &basic_manifest("build", "0.5.0"))
3f0ef765 2556 .file("build/src/lib.rs", "pub fn build() {}")
ab19c483 2557 .file("dev/Cargo.toml", &basic_manifest("dev", "0.5.0"))
d43ee1dd
NK
2558 .file("dev/src/lib.rs", "pub fn dev() {}")
2559 .build();
8495548d 2560
85984a87 2561 p.cargo("build").run();
8495548d 2562
570fe892 2563 assert!(p.bin("foo").is_file());
85984a87 2564 p.cargo("test").run();
6950bbb0 2565}
8495548d 2566
6950bbb0
AC
2567#[test]
2568fn bad_platform_specific_dependency() {
7fe2fbc8 2569 let p = project()
1e682848
AC
2570 .file(
2571 "Cargo.toml",
2572 r#"
8495548d
PK
2573 [project]
2574
2575 name = "foo"
2576 version = "0.5.0"
2577 authors = ["wycats@example.com"]
2578
3f0ef765 2579 [target.wrong-target.dependencies.bar]
8495548d 2580 path = "bar"
1e682848 2581 "#,
fecb7246
AC
2582 )
2583 .file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
ab19c483 2584 .file("bar/Cargo.toml", &basic_manifest("bar", "0.5.0"))
85984a87
DW
2585 .file(
2586 "bar/src/lib.rs",
f58d107e 2587 r#"pub fn gimme() -> String { format!("") }"#,
fecb7246
AC
2588 )
2589 .build();
8495548d 2590
f58d107e
EH
2591 p.cargo("build")
2592 .with_status(101)
2593 .with_stderr_contains("[..]can't find crate for `bar`")
2594 .run();
6950bbb0 2595}
8495548d 2596
6950bbb0
AC
2597#[test]
2598fn cargo_platform_specific_dependency_wrong_platform() {
7fe2fbc8 2599 let p = project()
1e682848
AC
2600 .file(
2601 "Cargo.toml",
2602 r#"
8495548d
PK
2603 [project]
2604
2605 name = "foo"
2606 version = "0.5.0"
2607 authors = ["wycats@example.com"]
2608
2609 [target.non-existing-triplet.dependencies.bar]
2610 path = "bar"
1e682848 2611 "#,
fecb7246
AC
2612 )
2613 .file("src/main.rs", "fn main() {}")
ab19c483 2614 .file("bar/Cargo.toml", &basic_manifest("bar", "0.5.0"))
85984a87
DW
2615 .file(
2616 "bar/src/lib.rs",
2617 "invalid rust file, should not be compiled",
fecb7246
AC
2618 )
2619 .build();
8495548d 2620
66262110 2621 p.cargo("build").run();
8495548d 2622
570fe892 2623 assert!(p.bin("foo").is_file());
21d9c4ae 2624 p.process(&p.bin("foo")).run();
8495548d 2625
a6dad622
AC
2626 let loc = p.root().join("Cargo.lock");
2627 let mut lockfile = String::new();
1e682848
AC
2628 File::open(&loc)
2629 .unwrap()
2630 .read_to_string(&mut lockfile)
2631 .unwrap();
85984a87 2632 assert!(lockfile.contains("bar"));
6950bbb0 2633}
d15aaa82 2634
446985cf
KA
2635#[test]
2636fn example_as_lib() {
7fe2fbc8 2637 let p = project()
1e682848
AC
2638 .file(
2639 "Cargo.toml",
2640 r#"
446985cf
KA
2641 [package]
2642 name = "foo"
2643 version = "0.0.1"
2644 authors = []
2645
2646 [[example]]
2647 name = "ex"
2648 crate-type = ["lib"]
1e682848 2649 "#,
fecb7246
AC
2650 )
2651 .file("src/lib.rs", "")
d43ee1dd
NK
2652 .file("examples/ex.rs", "")
2653 .build();
446985cf 2654
85984a87 2655 p.cargo("build --example=ex").run();
570fe892 2656 assert!(p.example_lib("ex", "lib").is_file());
446985cf
KA
2657}
2658
2659#[test]
2660fn example_as_rlib() {
7fe2fbc8 2661 let p = project()
1e682848
AC
2662 .file(
2663 "Cargo.toml",
2664 r#"
446985cf
KA
2665 [package]
2666 name = "foo"
2667 version = "0.0.1"
2668 authors = []
2669
2670 [[example]]
2671 name = "ex"
2672 crate-type = ["rlib"]
1e682848 2673 "#,
fecb7246
AC
2674 )
2675 .file("src/lib.rs", "")
d43ee1dd
NK
2676 .file("examples/ex.rs", "")
2677 .build();
446985cf 2678
85984a87 2679 p.cargo("build --example=ex").run();
570fe892 2680 assert!(p.example_lib("ex", "rlib").is_file());
446985cf
KA
2681}
2682
2683#[test]
2684fn example_as_dylib() {
7fe2fbc8 2685 let p = project()
1e682848
AC
2686 .file(
2687 "Cargo.toml",
2688 r#"
446985cf
KA
2689 [package]
2690 name = "foo"
2691 version = "0.0.1"
2692 authors = []
2693
2694 [[example]]
2695 name = "ex"
2696 crate-type = ["dylib"]
1e682848 2697 "#,
fecb7246
AC
2698 )
2699 .file("src/lib.rs", "")
d43ee1dd
NK
2700 .file("examples/ex.rs", "")
2701 .build();
446985cf 2702
85984a87 2703 p.cargo("build --example=ex").run();
570fe892 2704 assert!(p.example_lib("ex", "dylib").is_file());
446985cf
KA
2705}
2706
2707#[test]
2708fn example_as_proc_macro() {
865e0e55
KA
2709 if !is_nightly() {
2710 return;
2711 }
2712
7fe2fbc8 2713 let p = project()
1e682848
AC
2714 .file(
2715 "Cargo.toml",
2716 r#"
446985cf
KA
2717 [package]
2718 name = "foo"
2719 version = "0.0.1"
2720 authors = []
2721
2722 [[example]]
2723 name = "ex"
2724 crate-type = ["proc-macro"]
1e682848 2725 "#,
fecb7246
AC
2726 )
2727 .file("src/lib.rs", "")
d43ee1dd
NK
2728 .file("examples/ex.rs", "#![feature(proc_macro)]")
2729 .build();
446985cf 2730
85984a87 2731 p.cargo("build --example=ex").run();
570fe892 2732 assert!(p.example_lib("ex", "proc-macro").is_file());
446985cf
KA
2733}
2734
6950bbb0
AC
2735#[test]
2736fn example_bin_same_name() {
7fe2fbc8 2737 let p = project()
d15aaa82 2738 .file("src/main.rs", "fn main() {}")
d43ee1dd
NK
2739 .file("examples/foo.rs", "fn main() {}")
2740 .build();
d15aaa82 2741
66262110 2742 p.cargo("test --no-run -v").run();
d15aaa82 2743
570fe892 2744 assert!(!p.bin("foo").is_file());
530f2dd4 2745 // We expect a file of the form bin/foo-{metadata_hash}
570fe892 2746 assert!(p.bin("examples/foo").is_file());
d15aaa82 2747
66262110 2748 p.cargo("test --no-run -v").run();
d15aaa82 2749
570fe892 2750 assert!(!p.bin("foo").is_file());
530f2dd4 2751 // We expect a file of the form bin/foo-{metadata_hash}
570fe892 2752 assert!(p.bin("examples/foo").is_file());
6950bbb0 2753}
523f507d 2754
6950bbb0
AC
2755#[test]
2756fn compile_then_delete() {
85984a87 2757 let p = project().file("src/main.rs", "fn main() {}").build();
523f507d 2758
85984a87 2759 p.cargo("run -v").run();
570fe892 2760 assert!(p.bin("foo").is_file());
d8d59e40 2761 if cfg!(windows) {
50c6eb4d 2762 // On windows unlinking immediately after running often fails, so sleep
763ba535 2763 sleep_ms(100);
d8d59e40 2764 }
a6dad622 2765 fs::remove_file(&p.bin("foo")).unwrap();
85984a87 2766 p.cargo("run -v").run();
6950bbb0 2767}
5be647ef 2768
6950bbb0
AC
2769#[test]
2770fn transitive_dependencies_not_available() {
7fe2fbc8 2771 let p = project()
1e682848
AC
2772 .file(
2773 "Cargo.toml",
2774 r#"
5be647ef
AC
2775 [package]
2776 name = "foo"
2777 version = "0.0.1"
2778 authors = []
2779
f2aa5b4c 2780 [dependencies.aaaaa]
5be647ef 2781 path = "a"
1e682848 2782 "#,
fecb7246
AC
2783 )
2784 .file(
1e682848
AC
2785 "src/main.rs",
2786 "extern crate bbbbb; extern crate aaaaa; fn main() {}",
fecb7246
AC
2787 )
2788 .file(
1e682848
AC
2789 "a/Cargo.toml",
2790 r#"
5be647ef 2791 [package]
f2aa5b4c 2792 name = "aaaaa"
5be647ef
AC
2793 version = "0.0.1"
2794 authors = []
2795
f2aa5b4c 2796 [dependencies.bbbbb]
5be647ef 2797 path = "../b"
1e682848 2798 "#,
fecb7246
AC
2799 )
2800 .file("a/src/lib.rs", "extern crate bbbbb;")
ab19c483 2801 .file("b/Cargo.toml", &basic_manifest("bbbbb", "0.0.1"))
d43ee1dd
NK
2802 .file("b/src/lib.rs", "")
2803 .build();
5be647ef 2804
85984a87
DW
2805 p.cargo("build -v")
2806 .with_status(101)
2807 .with_stderr_contains("[..] can't find crate for `bbbbb`[..]")
2808 .run();
6950bbb0 2809}
06f37a09 2810
6950bbb0
AC
2811#[test]
2812fn cyclic_deps_rejected() {
7fe2fbc8 2813 let p = project()
1e682848
AC
2814 .file(
2815 "Cargo.toml",
2816 r#"
06f37a09
AC
2817 [package]
2818 name = "foo"
2819 version = "0.0.1"
2820 authors = []
2821
2822 [dependencies.a]
2823 path = "a"
1e682848 2824 "#,
fecb7246
AC
2825 )
2826 .file("src/lib.rs", "")
1e682848
AC
2827 .file(
2828 "a/Cargo.toml",
2829 r#"
06f37a09
AC
2830 [package]
2831 name = "a"
2832 version = "0.0.1"
2833 authors = []
2834
2835 [dependencies.foo]
2836 path = ".."
1e682848 2837 "#,
fecb7246
AC
2838 )
2839 .file("a/src/lib.rs", "")
d43ee1dd 2840 .build();
06f37a09 2841
d5fc8dc3
DW
2842 p.cargo("build -v")
2843 .with_status(101)
2844 .with_stderr(
89f43938
ZL
2845"[ERROR] cyclic package dependency: package `a v0.0.1 ([CWD]/a)` depends on itself. Cycle:
2846package `a v0.0.1 ([CWD]/a)`
2847 ... which is depended on by `foo v0.0.1 ([CWD])`",
d5fc8dc3 2848 ).run();
6950bbb0 2849}
afe88e0b 2850
6950bbb0
AC
2851#[test]
2852fn predictable_filenames() {
7fe2fbc8 2853 let p = project()
1e682848
AC
2854 .file(
2855 "Cargo.toml",
2856 r#"
27efa7ba
AC
2857 [package]
2858 name = "foo"
2859 version = "0.0.1"
2860 authors = []
2861
2862 [lib]
2863 name = "foo"
3194a23a 2864 crate-type = ["dylib", "rlib"]
1e682848 2865 "#,
fecb7246
AC
2866 )
2867 .file("src/lib.rs", "")
d43ee1dd 2868 .build();
27efa7ba 2869
85984a87 2870 p.cargo("build -v").run();
570fe892 2871 assert!(p.root().join("target/debug/libfoo.rlib").is_file());
1e682848 2872 let dylib_name = format!("{}foo{}", env::consts::DLL_PREFIX, env::consts::DLL_SUFFIX);
570fe892 2873 assert!(p.root().join("target/debug").join(dylib_name).is_file());
6950bbb0 2874}
27efa7ba 2875
6950bbb0
AC
2876#[test]
2877fn dashes_to_underscores() {
7fe2fbc8 2878 let p = project()
ab19c483 2879 .file("Cargo.toml", &basic_manifest("foo-bar", "0.0.1"))
afe88e0b 2880 .file("src/lib.rs", "")
d43ee1dd
NK
2881 .file("src/main.rs", "extern crate foo_bar; fn main() {}")
2882 .build();
afe88e0b 2883
85984a87 2884 p.cargo("build -v").run();
570fe892 2885 assert!(p.bin("foo-bar").is_file());
6950bbb0 2886}
afe88e0b 2887
6950bbb0
AC
2888#[test]
2889fn dashes_in_crate_name_bad() {
7fe2fbc8 2890 let p = project()
1e682848
AC
2891 .file(
2892 "Cargo.toml",
2893 r#"
afe88e0b
AC
2894 [package]
2895 name = "foo"
2896 version = "0.0.1"
2897 authors = []
2898
2899 [lib]
2900 name = "foo-bar"
1e682848 2901 "#,
fecb7246
AC
2902 )
2903 .file("src/lib.rs", "")
d43ee1dd
NK
2904 .file("src/main.rs", "extern crate foo_bar; fn main() {}")
2905 .build();
afe88e0b 2906
f58d107e
EH
2907 p.cargo("build -v")
2908 .with_status(101)
2909 .with_stderr(
2910 "\
2911[ERROR] failed to parse manifest at `[..]/foo/Cargo.toml`
2912
2913Caused by:
2914 library target names cannot contain hyphens: foo-bar
2915",
2916 )
2917 .run();
6950bbb0 2918}
7a1d8d94 2919
6950bbb0
AC
2920#[test]
2921fn rustc_env_var() {
85984a87 2922 let p = project().file("src/lib.rs", "").build();
20ce682e 2923
85984a87
DW
2924 p.cargo("build -v")
2925 .env("RUSTC", "rustc-that-does-not-exist")
2926 .with_status(101)
2927 .with_stderr(
1e682848 2928 "\
5eb80b74 2929[ERROR] could not execute process `rustc-that-does-not-exist -vV` ([..])
20ce682e
AC
2930
2931Caused by:
530e1d18 2932[..]
1e682848 2933",
fecb7246
AC
2934 )
2935 .run();
570fe892 2936 assert!(!p.bin("a").is_file());
6950bbb0 2937}
20ce682e 2938
6950bbb0
AC
2939#[test]
2940fn filtering() {
7fe2fbc8 2941 let p = project()
7a1d8d94
AC
2942 .file("src/lib.rs", "")
2943 .file("src/bin/a.rs", "fn main() {}")
2944 .file("src/bin/b.rs", "fn main() {}")
2945 .file("examples/a.rs", "fn main() {}")
d43ee1dd
NK
2946 .file("examples/b.rs", "fn main() {}")
2947 .build();
7a1d8d94 2948
85984a87 2949 p.cargo("build --lib").run();
570fe892 2950 assert!(!p.bin("a").is_file());
7a1d8d94 2951
85984a87 2952 p.cargo("build --bin=a --example=a").run();
570fe892
DW
2953 assert!(p.bin("a").is_file());
2954 assert!(!p.bin("b").is_file());
2955 assert!(p.bin("examples/a").is_file());
2956 assert!(!p.bin("examples/b").is_file());
6950bbb0 2957}
1727409e 2958
205a50dc
BW
2959#[test]
2960fn filtering_implicit_bins() {
7fe2fbc8 2961 let p = project()
205a50dc
BW
2962 .file("src/lib.rs", "")
2963 .file("src/bin/a.rs", "fn main() {}")
2964 .file("src/bin/b.rs", "fn main() {}")
2965 .file("examples/a.rs", "fn main() {}")
d43ee1dd
NK
2966 .file("examples/b.rs", "fn main() {}")
2967 .build();
205a50dc 2968
85984a87 2969 p.cargo("build --bins").run();
570fe892
DW
2970 assert!(p.bin("a").is_file());
2971 assert!(p.bin("b").is_file());
2972 assert!(!p.bin("examples/a").is_file());
2973 assert!(!p.bin("examples/b").is_file());
205a50dc
BW
2974}
2975
2976#[test]
2977fn filtering_implicit_examples() {
7fe2fbc8 2978 let p = project()
205a50dc
BW
2979 .file("src/lib.rs", "")
2980 .file("src/bin/a.rs", "fn main() {}")
2981 .file("src/bin/b.rs", "fn main() {}")
2982 .file("examples/a.rs", "fn main() {}")
d43ee1dd
NK
2983 .file("examples/b.rs", "fn main() {}")
2984 .build();
205a50dc 2985
85984a87 2986 p.cargo("build --examples").run();
570fe892
DW
2987 assert!(!p.bin("a").is_file());
2988 assert!(!p.bin("b").is_file());
2989 assert!(p.bin("examples/a").is_file());
2990 assert!(p.bin("examples/b").is_file());
205a50dc
BW
2991}
2992
6950bbb0
AC
2993#[test]
2994fn ignore_dotfile() {
7fe2fbc8 2995 let p = project()
1727409e 2996 .file("src/bin/.a.rs", "")
d43ee1dd
NK
2997 .file("src/bin/a.rs", "fn main() {}")
2998 .build();
1727409e 2999
85984a87 3000 p.cargo("build").run();
6950bbb0 3001}
014765f7 3002
6950bbb0
AC
3003#[test]
3004fn ignore_dotdirs() {
7fe2fbc8 3005 let p = project()
11144645
LB
3006 .file("src/bin/a.rs", "fn main() {}")
3007 .file(".git/Cargo.toml", "")
d43ee1dd
NK
3008 .file(".pc/dummy-fix.patch/Cargo.toml", "")
3009 .build();
11144645 3010
85984a87 3011 p.cargo("build").run();
6950bbb0 3012}
11144645 3013
6950bbb0
AC
3014#[test]
3015fn dotdir_root() {
b02ba377 3016 let p = ProjectBuilder::new(root().join(".foo"))
d43ee1dd
NK
3017 .file("src/bin/a.rs", "fn main() {}")
3018 .build();
85984a87 3019 p.cargo("build").run();
6950bbb0 3020}
7d94831f 3021
6950bbb0 3022#[test]
dd0b7a2c 3023fn custom_target_dir_env() {
85984a87 3024 let p = project().file("src/main.rs", "fn main() {}").build();
014765f7
AC
3025
3026 let exe_name = format!("foo{}", env::consts::EXE_SUFFIX);
3027
85984a87 3028 p.cargo("build").env("CARGO_TARGET_DIR", "foo/target").run();
570fe892
DW
3029 assert!(p.root().join("foo/target/debug").join(&exe_name).is_file());
3030 assert!(!p.root().join("target/debug").join(&exe_name).is_file());
014765f7 3031
85984a87 3032 p.cargo("build").run();
570fe892
DW
3033 assert!(p.root().join("foo/target/debug").join(&exe_name).is_file());
3034 assert!(p.root().join("target/debug").join(&exe_name).is_file());
014765f7
AC
3035
3036 fs::create_dir(p.root().join(".cargo")).unwrap();
1e682848
AC
3037 File::create(p.root().join(".cargo/config"))
3038 .unwrap()
3039 .write_all(
3040 br#"
014765f7 3041 [build]
256d9dd2 3042 target-dir = "foo/target"
1e682848 3043 "#,
fecb7246
AC
3044 )
3045 .unwrap();
85984a87 3046 p.cargo("build").env("CARGO_TARGET_DIR", "bar/target").run();
570fe892
DW
3047 assert!(p.root().join("bar/target/debug").join(&exe_name).is_file());
3048 assert!(p.root().join("foo/target/debug").join(&exe_name).is_file());
3049 assert!(p.root().join("target/debug").join(&exe_name).is_file());
6950bbb0 3050}
8230f1f4 3051
dd0b7a2c
SS
3052#[test]
3053fn custom_target_dir_line_parameter() {
85984a87 3054 let p = project().file("src/main.rs", "fn main() {}").build();
dd0b7a2c
SS
3055
3056 let exe_name = format!("foo{}", env::consts::EXE_SUFFIX);
3057
85984a87 3058 p.cargo("build --target-dir foo/target").run();
570fe892
DW
3059 assert!(p.root().join("foo/target/debug").join(&exe_name).is_file());
3060 assert!(!p.root().join("target/debug").join(&exe_name).is_file());
dd0b7a2c 3061
85984a87 3062 p.cargo("build").run();
570fe892
DW
3063 assert!(p.root().join("foo/target/debug").join(&exe_name).is_file());
3064 assert!(p.root().join("target/debug").join(&exe_name).is_file());
dd0b7a2c
SS
3065
3066 fs::create_dir(p.root().join(".cargo")).unwrap();
3067 File::create(p.root().join(".cargo/config"))
3068 .unwrap()
3069 .write_all(
3070 br#"
3071 [build]
3072 target-dir = "foo/target"
3073 "#,
fecb7246
AC
3074 )
3075 .unwrap();
85984a87 3076 p.cargo("build --target-dir bar/target").run();
570fe892
DW
3077 assert!(p.root().join("bar/target/debug").join(&exe_name).is_file());
3078 assert!(p.root().join("foo/target/debug").join(&exe_name).is_file());
3079 assert!(p.root().join("target/debug").join(&exe_name).is_file());
dd0b7a2c 3080
85984a87
DW
3081 p.cargo("build --target-dir foobar/target")
3082 .env("CARGO_TARGET_DIR", "bar/target")
3083 .run();
fecb7246
AC
3084 assert!(p
3085 .root()
3086 .join("foobar/target/debug")
3087 .join(&exe_name)
3088 .is_file());
570fe892
DW
3089 assert!(p.root().join("bar/target/debug").join(&exe_name).is_file());
3090 assert!(p.root().join("foo/target/debug").join(&exe_name).is_file());
3091 assert!(p.root().join("target/debug").join(&exe_name).is_file());
dd0b7a2c
SS
3092}
3093
6950bbb0
AC
3094#[test]
3095fn build_multiple_packages() {
7fe2fbc8 3096 let p = project()
1e682848
AC
3097 .file(
3098 "Cargo.toml",
3099 r#"
b3ade7c7
FH
3100 [package]
3101 name = "foo"
3102 version = "0.0.1"
3103 authors = []
3104
3105 [dependencies.d1]
3106 path = "d1"
3107 [dependencies.d2]
3108 path = "d2"
3109
3110 [[bin]]
3111 name = "foo"
1e682848 3112 "#,
fecb7246
AC
3113 )
3114 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
ab19c483 3115 .file("d1/Cargo.toml", &basic_bin_manifest("d1"))
b3ade7c7
FH
3116 .file("d1/src/lib.rs", "")
3117 .file("d1/src/main.rs", "fn main() { println!(\"d1\"); }")
1e682848
AC
3118 .file(
3119 "d2/Cargo.toml",
3120 r#"
b3ade7c7
FH
3121 [package]
3122 name = "d2"
3123 version = "0.0.1"
3124 authors = []
3125
3126 [[bin]]
3127 name = "d2"
3128 doctest = false
1e682848 3129 "#,
fecb7246
AC
3130 )
3131 .file("d2/src/main.rs", "fn main() { println!(\"d2\"); }")
d43ee1dd 3132 .build();
b3ade7c7 3133
85984a87 3134 p.cargo("build -p d1 -p d2 -p foo").run();
b3ade7c7 3135
570fe892 3136 assert!(p.bin("foo").is_file());
21d9c4ae 3137 p.process(&p.bin("foo")).with_stdout("i am foo\n").run();
b3ade7c7 3138
85984a87
DW
3139 let d1_path = &p
3140 .build_dir()
1e682848
AC
3141 .join("debug")
3142 .join(format!("d1{}", env::consts::EXE_SUFFIX));
85984a87
DW
3143 let d2_path = &p
3144 .build_dir()
1e682848
AC
3145 .join("debug")
3146 .join(format!("d2{}", env::consts::EXE_SUFFIX));
b3ade7c7 3147
570fe892 3148 assert!(d1_path.is_file());
21d9c4ae 3149 p.process(d1_path).with_stdout("d1").run();
d9615d70 3150
570fe892 3151 assert!(d2_path.is_file());
21d9c4ae 3152 p.process(d2_path).with_stdout("d2").run();
6950bbb0 3153}
f0647ea2 3154
6950bbb0
AC
3155#[test]
3156fn invalid_spec() {
7fe2fbc8 3157 let p = project()
1e682848
AC
3158 .file(
3159 "Cargo.toml",
3160 r#"
f0647ea2
FH
3161 [package]
3162 name = "foo"
3163 version = "0.0.1"
3164 authors = []
3165
3166 [dependencies.d1]
3167 path = "d1"
3168
3169 [[bin]]
3170 name = "foo"
1e682848 3171 "#,
fecb7246
AC
3172 )
3173 .file("src/bin/foo.rs", &main_file(r#""i am foo""#, &[]))
ab19c483 3174 .file("d1/Cargo.toml", &basic_bin_manifest("d1"))
f0647ea2 3175 .file("d1/src/lib.rs", "")
d43ee1dd
NK
3176 .file("d1/src/main.rs", "fn main() { println!(\"d1\"); }")
3177 .build();
f0647ea2 3178
85984a87
DW
3179 p.cargo("build -p notAValidDep")
3180 .with_status(101)
3181 .with_stderr("[ERROR] package id specification `notAValidDep` matched no packages")
3182 .run();
f0647ea2 3183
85984a87
DW
3184 p.cargo("build -p d1 -p notAValidDep")
3185 .with_status(101)
3186 .with_stderr("[ERROR] package id specification `notAValidDep` matched no packages")
3187 .run();
6950bbb0 3188}
17cc4337 3189
6950bbb0
AC
3190#[test]
3191fn manifest_with_bom_is_ok() {
7fe2fbc8 3192 let p = project()
1e682848
AC
3193 .file(
3194 "Cargo.toml",
3195 "\u{FEFF}
17cc4337
AC
3196 [package]
3197 name = \"foo\"
3198 version = \"0.0.1\"
3199 authors = []
1e682848 3200 ",
fecb7246
AC
3201 )
3202 .file("src/lib.rs", "")
d43ee1dd 3203 .build();
85984a87 3204 p.cargo("build -v").run();
6950bbb0 3205}
75848a2a 3206
6950bbb0
AC
3207#[test]
3208fn panic_abort_compiles_with_panic_abort() {
7fe2fbc8 3209 let p = project()
1e682848
AC
3210 .file(
3211 "Cargo.toml",
3212 r#"
75848a2a
AC
3213 [package]
3214 name = "foo"
3215 version = "0.0.1"
3216 authors = []
3217
3218 [profile.dev]
3219 panic = 'abort'
1e682848 3220 "#,
fecb7246
AC
3221 )
3222 .file("src/lib.rs", "")
d43ee1dd 3223 .build();
85984a87
DW
3224 p.cargo("build -v")
3225 .with_stderr_contains("[..] -C panic=abort [..]")
3226 .run();
6950bbb0 3227}
76a48a9b
AK
3228
3229#[test]
3230fn explicit_color_config_is_propagated_to_rustc() {
7fe2fbc8 3231 let p = project()
ab19c483 3232 .file("Cargo.toml", &basic_manifest("test", "0.0.0"))
d43ee1dd
NK
3233 .file("src/lib.rs", "")
3234 .build();
85984a87
DW
3235 p.cargo("build -v --color always")
3236 .with_stderr_contains("[..]rustc [..] src/lib.rs --color always[..]")
3237 .run();
76a48a9b 3238
85984a87 3239 p.cargo("clean").run();
5e8eef97 3240
85984a87
DW
3241 p.cargo("build -v --color never")
3242 .with_stderr(
1e682848 3243 "\
76a48a9b 3244[COMPILING] test v0.0.0 ([..])
0863469c 3245[RUNNING] `rustc [..] --color never [..]`
34628b65 3246[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1e682848 3247",
fecb7246
AC
3248 )
3249 .run();
76a48a9b 3250}
ebd630db 3251
9f98208d
AK
3252#[test]
3253fn compiler_json_error_format() {
7fe2fbc8 3254 let p = project()
1e682848
AC
3255 .file(
3256 "Cargo.toml",
3257 r#"
9f98208d
AK
3258 [project]
3259
3260 name = "foo"
3261 version = "0.5.0"
3262 authors = ["wycats@example.com"]
3263
3264 [dependencies.bar]
3265 path = "bar"
1e682848 3266 "#,
fecb7246
AC
3267 )
3268 .file(
85984a87
DW
3269 "build.rs",
3270 "fn main() { println!(\"cargo:rustc-cfg=xyz\") }",
fecb7246
AC
3271 )
3272 .file("src/main.rs", "fn main() { let unused = 92; }")
ab19c483 3273 .file("bar/Cargo.toml", &basic_manifest("bar", "0.5.0"))
d43ee1dd
NK
3274 .file("bar/src/lib.rs", r#"fn dead() {}"#)
3275 .build();
9f98208d 3276
ad3bab1f 3277 // Using jobs=1 to ensure that the order of messages is consistent.
85984a87
DW
3278 p.cargo("build -v --message-format=json --jobs=1")
3279 .with_json(
1e682848 3280 r#"
9f98208d 3281 {
ad3bab1f
EH
3282 "reason":"compiler-artifact",
3283 "package_id":"foo 0.5.0 ([..])",
2268d57b 3284 "target":{
ad3bab1f
EH
3285 "kind":["custom-build"],
3286 "crate_types":["bin"],
5dcc4f17 3287 "edition": "2015",
ad3bab1f
EH
3288 "name":"build-script-build",
3289 "src_path":"[..]build.rs"
2268d57b 3290 },
9f6d7989
AC
3291 "profile": {
3292 "debug_assertions": true,
f9306094 3293 "debuginfo": 2,
9f6d7989 3294 "opt_level": "0",
803d748a 3295 "overflow_checks": true,
9f6d7989
AC
3296 "test": false
3297 },
282f238d 3298 "executable": null,
9f6d7989 3299 "features": [],
ad3bab1f
EH
3300 "filenames": "{...}",
3301 "fresh": false
3302 }
3303
3304 {
3305 "reason":"compiler-message",
9f6d7989 3306 "package_id":"bar 0.5.0 ([..])",
2268d57b
KA
3307 "target":{
3308 "kind":["lib"],
3309 "crate_types":["lib"],
5dcc4f17 3310 "edition": "2015",
2268d57b
KA
3311 "name":"bar",
3312 "src_path":"[..]lib.rs"
3313 },
ad3bab1f 3314 "message":"{...}"
9f6d7989
AC
3315 }
3316
f101d88e 3317 {
3318 "reason":"compiler-artifact",
f101d88e 3319 "profile": {
3320 "debug_assertions": true,
3321 "debuginfo": 2,
3322 "opt_level": "0",
3323 "overflow_checks": true,
3324 "test": false
3325 },
282f238d 3326 "executable": null,
f101d88e 3327 "features": [],
ad3bab1f
EH
3328 "package_id":"bar 0.5.0 ([..])",
3329 "target":{
3330 "kind":["lib"],
3331 "crate_types":["lib"],
5dcc4f17 3332 "edition": "2015",
ad3bab1f
EH
3333 "name":"bar",
3334 "src_path":"[..]lib.rs"
3335 },
3336 "filenames":["[..].rlib"],
f101d88e 3337 "fresh": false
3338 }
3339
3340 {
3341 "reason":"build-script-executed",
3342 "package_id":"foo 0.5.0 ([..])",
3343 "linked_libs":[],
3344 "linked_paths":[],
3345 "env":[],
3346 "cfgs":["xyz"]
3347 }
3348
9f98208d 3349 {
ef94f467 3350 "reason":"compiler-message",
9f98208d 3351 "package_id":"foo 0.5.0 ([..])",
2268d57b
KA
3352 "target":{
3353 "kind":["bin"],
3354 "crate_types":["bin"],
5dcc4f17 3355 "edition": "2015",
2268d57b
KA
3356 "name":"foo",
3357 "src_path":"[..]main.rs"
3358 },
88f6a302 3359 "message":"{...}"
9f98208d 3360 }
9f6d7989
AC
3361
3362 {
3363 "reason":"compiler-artifact",
3364 "package_id":"foo 0.5.0 ([..])",
2268d57b
KA
3365 "target":{
3366 "kind":["bin"],
3367 "crate_types":["bin"],
5dcc4f17 3368 "edition": "2015",
2268d57b
KA
3369 "name":"foo",
3370 "src_path":"[..]main.rs"
3371 },
9f6d7989
AC
3372 "profile": {
3373 "debug_assertions": true,
f9306094 3374 "debuginfo": 2,
9f6d7989 3375 "opt_level": "0",
803d748a 3376 "overflow_checks": true,
9f6d7989
AC
3377 "test": false
3378 },
282f238d 3379 "executable": "[..]/foo/target/debug/foo[EXE]",
9f6d7989 3380 "features": [],
f9306094 3381 "filenames": "{...}",
7bfd7dc7
AK
3382 "fresh": false
3383 }
1e682848 3384"#,
fecb7246
AC
3385 )
3386 .run();
7bfd7dc7
AK
3387
3388 // With fresh build, we should repeat the artifacts,
3389 // but omit compiler warnings.
85984a87
DW
3390 p.cargo("build -v --message-format=json --jobs=1")
3391 .with_json(
1e682848 3392 r#"
f101d88e 3393 {
3394 "reason":"compiler-artifact",
3395 "package_id":"foo 0.5.0 ([..])",
3396 "target":{
3397 "kind":["custom-build"],
3398 "crate_types":["bin"],
5dcc4f17 3399 "edition": "2015",
f101d88e 3400 "name":"build-script-build",
3401 "src_path":"[..]build.rs"
3402 },
3403 "profile": {
3404 "debug_assertions": true,
3405 "debuginfo": 2,
3406 "opt_level": "0",
3407 "overflow_checks": true,
3408 "test": false
3409 },
282f238d 3410 "executable": null,
f101d88e 3411 "features": [],
3412 "filenames": "{...}",
3413 "fresh": true
3414 }
3415
7bfd7dc7
AK
3416 {
3417 "reason":"compiler-artifact",
3418 "profile": {
3419 "debug_assertions": true,
f9306094 3420 "debuginfo": 2,
7bfd7dc7 3421 "opt_level": "0",
803d748a 3422 "overflow_checks": true,
7bfd7dc7
AK
3423 "test": false
3424 },
282f238d 3425 "executable": null,
7bfd7dc7
AK
3426 "features": [],
3427 "package_id":"bar 0.5.0 ([..])",
3428 "target":{
3429 "kind":["lib"],
3430 "crate_types":["lib"],
5dcc4f17 3431 "edition": "2015",
7bfd7dc7
AK
3432 "name":"bar",
3433 "src_path":"[..]lib.rs"
3434 },
3435 "filenames":["[..].rlib"],
3436 "fresh": true
3437 }
3438
f101d88e 3439 {
3440 "reason":"build-script-executed",
3441 "package_id":"foo 0.5.0 ([..])",
3442 "linked_libs":[],
3443 "linked_paths":[],
3444 "env":[],
3445 "cfgs":["xyz"]
3446 }
3447
7bfd7dc7
AK
3448 {
3449 "reason":"compiler-artifact",
3450 "package_id":"foo 0.5.0 ([..])",
3451 "target":{
3452 "kind":["bin"],
3453 "crate_types":["bin"],
5dcc4f17 3454 "edition": "2015",
7bfd7dc7
AK
3455 "name":"foo",
3456 "src_path":"[..]main.rs"
3457 },
3458 "profile": {
3459 "debug_assertions": true,
f9306094 3460 "debuginfo": 2,
7bfd7dc7 3461 "opt_level": "0",
803d748a 3462 "overflow_checks": true,
7bfd7dc7
AK
3463 "test": false
3464 },
282f238d 3465 "executable": "[..]/foo/target/debug/foo[EXE]",
7bfd7dc7 3466 "features": [],
f9306094 3467 "filenames": "{...}",
7bfd7dc7 3468 "fresh": true
9f6d7989 3469 }
1e682848 3470"#,
fecb7246
AC
3471 )
3472 .run();
9f98208d
AK
3473}
3474
3475#[test]
3476fn wrong_message_format_option() {
7fe2fbc8 3477 let p = project()
9f98208d 3478 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
3479 .file("src/main.rs", "fn main() {}")
3480 .build();
9f98208d 3481
85984a87
DW
3482 p.cargo("build --message-format XML")
3483 .with_status(1)
3484 .with_stderr_contains(
1e682848 3485 "\
31ea0d93 3486error: 'XML' isn't a valid value for '--message-format <FMT>'
28a84e02 3487<tab>[possible values: human, json, short]
1e682848 3488",
fecb7246
AC
3489 )
3490 .run();
9f98208d
AK
3491}
3492
3e325037 3493#[test]
3494fn message_format_json_forward_stderr() {
7fe2fbc8 3495 let p = project()
3e325037 3496 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
3497 .file("src/main.rs", "fn main() { let unused = 0; }")
3498 .build();
3e325037 3499
85984a87
DW
3500 p.cargo("rustc --release --bin foo --message-format JSON")
3501 .with_json(
1e682848 3502 r#"
3e325037 3503 {
3504 "reason":"compiler-message",
3505 "package_id":"foo 0.5.0 ([..])",
1182ebeb
KA
3506 "target":{
3507 "kind":["bin"],
3508 "crate_types":["bin"],
5dcc4f17 3509 "edition": "2015",
1182ebeb
KA
3510 "name":"foo",
3511 "src_path":"[..]"
3512 },
88f6a302 3513 "message":"{...}"
3e325037 3514 }
3515
3516 {
3517 "reason":"compiler-artifact",
3518 "package_id":"foo 0.5.0 ([..])",
1182ebeb
KA
3519 "target":{
3520 "kind":["bin"],
3521 "crate_types":["bin"],
5dcc4f17 3522 "edition": "2015",
1182ebeb
KA
3523 "name":"foo",
3524 "src_path":"[..]"
3525 },
3e325037 3526 "profile":{
dfd964a4 3527 "debug_assertions":false,
3528 "debuginfo":null,
3529 "opt_level":"3",
3530 "overflow_checks": false,
3e325037 3531 "test":false
3532 },
282f238d 3533 "executable": "{...}",
3e325037 3534 "features":[],
f9306094 3535 "filenames": "{...}",
7bfd7dc7 3536 "fresh": false
3e325037 3537 }
1e682848 3538"#,
fecb7246
AC
3539 )
3540 .run();
3e325037 3541}
3542
ebd630db
AC
3543#[test]
3544fn no_warn_about_package_metadata() {
7fe2fbc8 3545 let p = project()
1e682848
AC
3546 .file(
3547 "Cargo.toml",
3548 r#"
ebd630db
AC
3549 [package]
3550 name = "foo"
3551 version = "0.0.1"
3552 authors = []
3553
3554 [package.metadata]
3555 foo = "bar"
3556 a = true
3557 b = 3
3558
3559 [package.metadata.another]
3560 bar = 3
1e682848 3561 "#,
fecb7246
AC
3562 )
3563 .file("src/lib.rs", "")
d43ee1dd 3564 .build();
85984a87
DW
3565 p.cargo("build")
3566 .with_stderr(
1e682848
AC
3567 "[..] foo v0.0.1 ([..])\n\
3568 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3569 )
3570 .run();
ebd630db 3571}
84fe0707
MHT
3572
3573#[test]
3574fn cargo_build_empty_target() {
7fe2fbc8 3575 let p = project()
84fe0707 3576 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
3577 .file("src/main.rs", "fn main() {}")
3578 .build();
84fe0707 3579
85984a87
DW
3580 p.cargo("build --target")
3581 .arg("")
3582 .with_status(101)
3583 .with_stderr_contains("[..] target was empty")
3584 .run();
84fe0707 3585}
4df1375c
SD
3586
3587#[test]
3588fn build_all_workspace() {
7fe2fbc8 3589 let p = project()
1e682848
AC
3590 .file(
3591 "Cargo.toml",
3592 r#"
4df1375c
SD
3593 [project]
3594 name = "foo"
3595 version = "0.1.0"
3596
3597 [dependencies]
3598 bar = { path = "bar" }
3599
3600 [workspace]
1e682848 3601 "#,
fecb7246
AC
3602 )
3603 .file("src/main.rs", "fn main() {}")
ab19c483 3604 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
ca7d9ee2 3605 .file("bar/src/lib.rs", "pub fn bar() {}")
d43ee1dd 3606 .build();
4df1375c 3607
85984a87
DW
3608 p.cargo("build --all")
3609 .with_stderr(
1e682848
AC
3610 "[..] Compiling bar v0.1.0 ([..])\n\
3611 [..] Compiling foo v0.1.0 ([..])\n\
3612 [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3613 )
3614 .run();
4df1375c
SD
3615}
3616
b961de25 3617#[test]
3618fn build_all_exclude() {
7fe2fbc8 3619 let p = project()
1e682848
AC
3620 .file(
3621 "Cargo.toml",
3622 r#"
b961de25 3623 [project]
3624 name = "foo"
3625 version = "0.1.0"
3626
3627 [workspace]
3628 members = ["bar", "baz"]
1e682848 3629 "#,
fecb7246
AC
3630 )
3631 .file("src/main.rs", "fn main() {}")
ab19c483 3632 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
ca7d9ee2 3633 .file("bar/src/lib.rs", "pub fn bar() {}")
ab19c483 3634 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
ca7d9ee2 3635 .file("baz/src/lib.rs", "pub fn baz() { break_the_build(); }")
d43ee1dd 3636 .build();
b961de25 3637
85984a87
DW
3638 p.cargo("build --all --exclude baz")
3639 .with_stderr_contains("[..]Compiling foo v0.1.0 [..]")
3640 .with_stderr_contains("[..]Compiling bar v0.1.0 [..]")
3641 .with_stderr_does_not_contain("[..]Compiling baz v0.1.0 [..]")
3642 .run();
b961de25 3643}
3644
01782fe6
BW
3645#[test]
3646fn build_all_workspace_implicit_examples() {
7fe2fbc8 3647 let p = project()
1e682848
AC
3648 .file(
3649 "Cargo.toml",
3650 r#"
01782fe6
BW
3651 [project]
3652 name = "foo"
3653 version = "0.1.0"
3654
3655 [dependencies]
3656 bar = { path = "bar" }
3657
3658 [workspace]
1e682848 3659 "#,
fecb7246
AC
3660 )
3661 .file("src/lib.rs", "")
01782fe6
BW
3662 .file("src/bin/a.rs", "fn main() {}")
3663 .file("src/bin/b.rs", "fn main() {}")
3664 .file("examples/c.rs", "fn main() {}")
3665 .file("examples/d.rs", "fn main() {}")
ab19c483 3666 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
01782fe6
BW
3667 .file("bar/src/lib.rs", "")
3668 .file("bar/src/bin/e.rs", "fn main() {}")
3669 .file("bar/src/bin/f.rs", "fn main() {}")
3670 .file("bar/examples/g.rs", "fn main() {}")
d43ee1dd
NK
3671 .file("bar/examples/h.rs", "fn main() {}")
3672 .build();
01782fe6 3673
85984a87
DW
3674 p.cargo("build --all --examples")
3675 .with_stderr(
1e682848
AC
3676 "[..] Compiling bar v0.1.0 ([..])\n\
3677 [..] Compiling foo v0.1.0 ([..])\n\
3678 [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3679 )
3680 .run();
570fe892
DW
3681 assert!(!p.bin("a").is_file());
3682 assert!(!p.bin("b").is_file());
3683 assert!(p.bin("examples/c").is_file());
3684 assert!(p.bin("examples/d").is_file());
3685 assert!(!p.bin("e").is_file());
3686 assert!(!p.bin("f").is_file());
3687 assert!(p.bin("examples/g").is_file());
3688 assert!(p.bin("examples/h").is_file());
01782fe6
BW
3689}
3690
4df1375c
SD
3691#[test]
3692fn build_all_virtual_manifest() {
f8c9928c 3693 let p = project()
1e682848
AC
3694 .file(
3695 "Cargo.toml",
3696 r#"
4df1375c 3697 [workspace]
f8c9928c 3698 members = ["bar", "baz"]
1e682848 3699 "#,
fecb7246
AC
3700 )
3701 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
ca7d9ee2 3702 .file("bar/src/lib.rs", "pub fn bar() {}")
ab19c483 3703 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
ca7d9ee2 3704 .file("baz/src/lib.rs", "pub fn baz() {}")
d43ee1dd 3705 .build();
4df1375c 3706
f8c9928c 3707 // The order in which bar and baz are built is not guaranteed
85984a87
DW
3708 p.cargo("build --all")
3709 .with_stderr_contains("[..] Compiling baz v0.1.0 ([..])")
3710 .with_stderr_contains("[..] Compiling bar v0.1.0 ([..])")
3711 .with_stderr(
3712 "[..] Compiling [..] v0.1.0 ([..])\n\
3713 [..] Compiling [..] v0.1.0 ([..])\n\
3714 [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3715 )
3716 .run();
4df1375c
SD
3717}
3718
15791c74
RS
3719#[test]
3720fn build_virtual_manifest_all_implied() {
f8c9928c 3721 let p = project()
1e682848
AC
3722 .file(
3723 "Cargo.toml",
3724 r#"
15791c74 3725 [workspace]
f8c9928c 3726 members = ["bar", "baz"]
1e682848 3727 "#,
fecb7246
AC
3728 )
3729 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
ca7d9ee2 3730 .file("bar/src/lib.rs", "pub fn bar() {}")
ab19c483 3731 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
ca7d9ee2 3732 .file("baz/src/lib.rs", "pub fn baz() {}")
d43ee1dd 3733 .build();
15791c74 3734
f8c9928c 3735 // The order in which bar and baz are built is not guaranteed
85984a87
DW
3736 p.cargo("build")
3737 .with_stderr_contains("[..] Compiling baz v0.1.0 ([..])")
3738 .with_stderr_contains("[..] Compiling bar v0.1.0 ([..])")
3739 .with_stderr(
3740 "[..] Compiling [..] v0.1.0 ([..])\n\
3741 [..] Compiling [..] v0.1.0 ([..])\n\
3742 [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3743 )
3744 .run();
15791c74
RS
3745}
3746
33431d71 3747#[test]
3748fn build_virtual_manifest_one_project() {
f8c9928c 3749 let p = project()
1e682848
AC
3750 .file(
3751 "Cargo.toml",
3752 r#"
33431d71 3753 [workspace]
f8c9928c 3754 members = ["bar", "baz"]
1e682848 3755 "#,
fecb7246
AC
3756 )
3757 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
85984a87 3758 .file("bar/src/lib.rs", "pub fn bar() {}")
ab19c483 3759 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
85984a87 3760 .file("baz/src/lib.rs", "pub fn baz() {}")
d43ee1dd 3761 .build();
33431d71 3762
85984a87
DW
3763 p.cargo("build -p bar")
3764 .with_stderr_does_not_contain("[..]baz[..]")
3765 .with_stderr_contains("[..] Compiling bar v0.1.0 ([..])")
3766 .with_stderr(
3767 "[..] Compiling [..] v0.1.0 ([..])\n\
3768 [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3769 )
3770 .run();
33431d71 3771}
3772
01782fe6
BW
3773#[test]
3774fn build_all_virtual_manifest_implicit_examples() {
7fe2fbc8 3775 let p = project()
1e682848
AC
3776 .file(
3777 "Cargo.toml",
3778 r#"
01782fe6 3779 [workspace]
f8c9928c 3780 members = ["bar", "baz"]
1e682848 3781 "#,
fecb7246
AC
3782 )
3783 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
f8c9928c
DW
3784 .file("bar/src/lib.rs", "")
3785 .file("bar/src/bin/a.rs", "fn main() {}")
3786 .file("bar/src/bin/b.rs", "fn main() {}")
3787 .file("bar/examples/c.rs", "fn main() {}")
3788 .file("bar/examples/d.rs", "fn main() {}")
ab19c483 3789 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
f8c9928c
DW
3790 .file("baz/src/lib.rs", "")
3791 .file("baz/src/bin/e.rs", "fn main() {}")
3792 .file("baz/src/bin/f.rs", "fn main() {}")
3793 .file("baz/examples/g.rs", "fn main() {}")
3794 .file("baz/examples/h.rs", "fn main() {}")
d43ee1dd 3795 .build();
01782fe6 3796
f8c9928c 3797 // The order in which bar and baz are built is not guaranteed
85984a87
DW
3798 p.cargo("build --all --examples")
3799 .with_stderr_contains("[..] Compiling baz v0.1.0 ([..])")
3800 .with_stderr_contains("[..] Compiling bar v0.1.0 ([..])")
3801 .with_stderr(
3802 "[..] Compiling [..] v0.1.0 ([..])\n\
3803 [..] Compiling [..] v0.1.0 ([..])\n\
3804 [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3805 )
3806 .run();
570fe892
DW
3807 assert!(!p.bin("a").is_file());
3808 assert!(!p.bin("b").is_file());
3809 assert!(p.bin("examples/c").is_file());
3810 assert!(p.bin("examples/d").is_file());
3811 assert!(!p.bin("e").is_file());
3812 assert!(!p.bin("f").is_file());
3813 assert!(p.bin("examples/g").is_file());
3814 assert!(p.bin("examples/h").is_file());
01782fe6
BW
3815}
3816
4df1375c
SD
3817#[test]
3818fn build_all_member_dependency_same_name() {
f8c9928c 3819 let p = project()
1e682848
AC
3820 .file(
3821 "Cargo.toml",
3822 r#"
4df1375c
SD
3823 [workspace]
3824 members = ["a"]
1e682848 3825 "#,
fecb7246
AC
3826 )
3827 .file(
1e682848
AC
3828 "a/Cargo.toml",
3829 r#"
4df1375c
SD
3830 [project]
3831 name = "a"
3832 version = "0.1.0"
3833
3834 [dependencies]
3835 a = "0.1.0"
1e682848 3836 "#,
fecb7246
AC
3837 )
3838 .file("a/src/lib.rs", "pub fn a() {}")
d43ee1dd 3839 .build();
4df1375c
SD
3840
3841 Package::new("a", "0.1.0").publish();
3842
85984a87
DW
3843 p.cargo("build --all")
3844 .with_stderr(
e2637b65
AC
3845 "[UPDATING] `[..]` index\n\
3846 [DOWNLOADING] crates ...\n\
3847 [DOWNLOADED] a v0.1.0 ([..])\n\
3848 [COMPILING] a v0.1.0\n\
3849 [COMPILING] a v0.1.0 ([..])\n\
3850 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
fecb7246
AC
3851 )
3852 .run();
4df1375c
SD
3853}
3854
8c40d891
JM
3855#[test]
3856fn run_proper_binary() {
7fe2fbc8 3857 let p = project()
1e682848
AC
3858 .file(
3859 "Cargo.toml",
3860 r#"
8c40d891
JM
3861 [package]
3862 name = "foo"
3863 authors = []
3864 version = "0.0.0"
3865 [[bin]]
3866 name = "main"
3867 [[bin]]
3868 name = "other"
1e682848 3869 "#,
fecb7246
AC
3870 )
3871 .file("src/lib.rs", "")
85984a87
DW
3872 .file(
3873 "src/bin/main.rs",
3874 r#"fn main() { panic!("This should never be run."); }"#,
fecb7246
AC
3875 )
3876 .file("src/bin/other.rs", "fn main() {}")
d43ee1dd 3877 .build();
8c40d891 3878
85984a87 3879 p.cargo("run --bin other").run();
8c40d891 3880}
53a32fee
JM
3881
3882#[test]
3883fn run_proper_binary_main_rs() {
7fe2fbc8 3884 let p = project()
ab19c483 3885 .file("Cargo.toml", &basic_bin_manifest("foo"))
53a32fee 3886 .file("src/lib.rs", "")
ca7d9ee2 3887 .file("src/bin/main.rs", "fn main() {}")
d43ee1dd 3888 .build();
53a32fee 3889
85984a87 3890 p.cargo("run --bin foo").run();
53a32fee 3891}
277e0896 3892
f7933732
JM
3893#[test]
3894fn run_proper_alias_binary_from_src() {
7fe2fbc8 3895 let p = project()
1e682848
AC
3896 .file(
3897 "Cargo.toml",
3898 r#"
f7933732
JM
3899 [package]
3900 name = "foo"
3901 authors = []
3902 version = "0.0.0"
3903 [[bin]]
3904 name = "foo"
3905 [[bin]]
3906 name = "bar"
1e682848 3907 "#,
fecb7246
AC
3908 )
3909 .file("src/foo.rs", r#"fn main() { println!("foo"); }"#)
ca7d9ee2 3910 .file("src/bar.rs", r#"fn main() { println!("bar"); }"#)
d43ee1dd 3911 .build();
f7933732 3912
85984a87 3913 p.cargo("build --all").run();
21d9c4ae
DW
3914 p.process(&p.bin("foo")).with_stdout("foo\n").run();
3915 p.process(&p.bin("bar")).with_stdout("bar\n").run();
f7933732
JM
3916}
3917
3918#[test]
3919fn run_proper_alias_binary_main_rs() {
7fe2fbc8 3920 let p = project()
1e682848
AC
3921 .file(
3922 "Cargo.toml",
3923 r#"
f7933732
JM
3924 [package]
3925 name = "foo"
3926 authors = []
3927 version = "0.0.0"
3928 [[bin]]
3929 name = "foo"
3930 [[bin]]
3931 name = "bar"
1e682848 3932 "#,
fecb7246
AC
3933 )
3934 .file("src/main.rs", r#"fn main() { println!("main"); }"#)
d43ee1dd 3935 .build();
f7933732 3936
85984a87 3937 p.cargo("build --all").run();
21d9c4ae
DW
3938 p.process(&p.bin("foo")).with_stdout("main\n").run();
3939 p.process(&p.bin("bar")).with_stdout("main\n").run();
f7933732
JM
3940}
3941
277e0896
JM
3942#[test]
3943fn run_proper_binary_main_rs_as_foo() {
7fe2fbc8 3944 let p = project()
ab19c483 3945 .file("Cargo.toml", &basic_bin_manifest("foo"))
85984a87
DW
3946 .file(
3947 "src/foo.rs",
3948 r#" fn main() { panic!("This should never be run."); }"#,
fecb7246
AC
3949 )
3950 .file("src/main.rs", "fn main() {}")
d43ee1dd 3951 .build();
277e0896 3952
85984a87 3953 p.cargo("run --bin foo").run();
277e0896 3954}
20f23d90
TM
3955
3956#[test]
3957fn rustc_wrapper() {
3958 // We don't have /usr/bin/env on Windows.
1e682848
AC
3959 if cfg!(windows) {
3960 return;
3961 }
20f23d90 3962
7fe2fbc8 3963 let p = project()
20f23d90 3964 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
3965 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
3966 .build();
20f23d90 3967
85984a87
DW
3968 p.cargo("build -v")
3969 .env("RUSTC_WRAPPER", "/usr/bin/env")
3970 .with_stderr_contains("[RUNNING] `/usr/bin/env rustc --crate-name foo [..]")
3971 .run();
20f23d90 3972}
fccaffb1
AC
3973
3974#[test]
3975fn cdylib_not_lifted() {
7fe2fbc8 3976 let p = project()
1e682848
AC
3977 .file(
3978 "Cargo.toml",
3979 r#"
fccaffb1
AC
3980 [project]
3981 name = "foo"
3982 authors = []
3983 version = "0.1.0"
3984
3985 [lib]
3986 crate-type = ["cdylib"]
1e682848 3987 "#,
fecb7246
AC
3988 )
3989 .file("src/lib.rs", "")
d43ee1dd 3990 .build();
fccaffb1 3991
85984a87 3992 p.cargo("build").run();
fccaffb1
AC
3993
3994 let files = if cfg!(windows) {
3995 vec!["foo.dll.lib", "foo.dll.exp", "foo.dll"]
3996 } else if cfg!(target_os = "macos") {
3997 vec!["libfoo.dylib"]
3998 } else {
3999 vec!["libfoo.so"]
4000 };
4001
4002 for file in files {
4003 println!("checking: {}", file);
570fe892 4004 assert!(p.root().join("target/debug/deps").join(&file).is_file());
fccaffb1
AC
4005 }
4006}
7c94358e 4007
c4b56e2a
TK
4008#[test]
4009fn cdylib_final_outputs() {
7fe2fbc8 4010 let p = project()
1e682848
AC
4011 .file(
4012 "Cargo.toml",
4013 r#"
c4b56e2a 4014 [project]
f9a541b5 4015 name = "foo-bar"
c4b56e2a
TK
4016 authors = []
4017 version = "0.1.0"
4018
4019 [lib]
4020 crate-type = ["cdylib"]
1e682848 4021 "#,
fecb7246
AC
4022 )
4023 .file("src/lib.rs", "")
d43ee1dd 4024 .build();
c4b56e2a 4025
85984a87 4026 p.cargo("build").run();
c4b56e2a
TK
4027
4028 let files = if cfg!(windows) {
f9a541b5 4029 vec!["foo_bar.dll.lib", "foo_bar.dll"]
c4b56e2a 4030 } else if cfg!(target_os = "macos") {
f9a541b5 4031 vec!["libfoo_bar.dylib"]
c4b56e2a 4032 } else {
f9a541b5 4033 vec!["libfoo_bar.so"]
c4b56e2a
TK
4034 };
4035
4036 for file in files {
4037 println!("checking: {}", file);
570fe892 4038 assert!(p.root().join("target/debug").join(&file).is_file());
c4b56e2a
TK
4039 }
4040}
4041
7c94358e
AW
4042#[test]
4043fn deterministic_cfg_flags() {
4044 // This bug is non-deterministic
4045
7fe2fbc8 4046 let p = project()
1e682848
AC
4047 .file(
4048 "Cargo.toml",
4049 r#"
7c94358e
AW
4050 [project]
4051 name = "foo"
4052 version = "0.1.0"
4053 authors = []
4054 build = "build.rs"
4055
4056 [features]
4057 default = ["f_a", "f_b", "f_c", "f_d"]
4058 f_a = []
4059 f_b = []
4060 f_c = []
4061 f_d = []
1e682848 4062 "#,
fecb7246
AC
4063 )
4064 .file(
1e682848
AC
4065 "build.rs",
4066 r#"
7c94358e
AW
4067 fn main() {
4068 println!("cargo:rustc-cfg=cfg_a");
4069 println!("cargo:rustc-cfg=cfg_b");
4070 println!("cargo:rustc-cfg=cfg_c");
4071 println!("cargo:rustc-cfg=cfg_d");
4072 println!("cargo:rustc-cfg=cfg_e");
4073 }
1e682848 4074 "#,
fecb7246
AC
4075 )
4076 .file("src/main.rs", "fn main() {}")
d43ee1dd 4077 .build();
7c94358e 4078
85984a87
DW
4079 p.cargo("build -v")
4080 .with_stderr(
1e682848 4081 "\
7c94358e
AW
4082[COMPILING] foo v0.1.0 [..]
4083[RUNNING] [..]
4084[RUNNING] [..]
4085[RUNNING] `rustc --crate-name foo [..] \
408f4e31
AC
4086--cfg[..]default[..]--cfg[..]f_a[..]--cfg[..]f_b[..]\
4087--cfg[..]f_c[..]--cfg[..]f_d[..] \
7c94358e 4088--cfg cfg_a --cfg cfg_b --cfg cfg_c --cfg cfg_d --cfg cfg_e`
1e682848 4089[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
fecb7246
AC
4090 )
4091 .run();
7c94358e 4092}
8a61174b
AK
4093
4094#[test]
4095fn explicit_bins_without_paths() {
7fe2fbc8 4096 let p = project()
1e682848
AC
4097 .file(
4098 "Cargo.toml",
4099 r#"
8a61174b
AK
4100 [package]
4101 name = "foo"
4102 version = "0.1.0"
4103 authors = []
4104
4105 [[bin]]
4106 name = "foo"
4107
4108 [[bin]]
4109 name = "bar"
1e682848 4110 "#,
fecb7246
AC
4111 )
4112 .file("src/lib.rs", "")
8a61174b 4113 .file("src/main.rs", "fn main() {}")
d43ee1dd
NK
4114 .file("src/bin/bar.rs", "fn main() {}")
4115 .build();
8a61174b 4116
85984a87 4117 p.cargo("build").run();
8a61174b 4118}
b7752d1f
AK
4119
4120#[test]
4121fn no_bin_in_src_with_lib() {
7fe2fbc8 4122 let p = project()
ab19c483 4123 .file("Cargo.toml", &basic_bin_manifest("foo"))
b7752d1f 4124 .file("src/lib.rs", "")
d43ee1dd
NK
4125 .file("src/foo.rs", "fn main() {}")
4126 .build();
b7752d1f 4127
85984a87
DW
4128 p.cargo("build")
4129 .with_status(101)
4130 .with_stderr_contains(
1e682848 4131 "\
0ae2b97f
AK
4132[ERROR] failed to parse manifest at `[..]`
4133
4134Caused by:
1e682848 4135 can't find `foo` bin, specify bin.path",
fecb7246
AC
4136 )
4137 .run();
b7752d1f 4138}
10661f3f 4139
10661f3f 4140#[test]
3bffb641 4141fn inferred_bins() {
7fe2fbc8 4142 let p = project()
10661f3f
MS
4143 .file("src/main.rs", "fn main() {}")
4144 .file("src/bin/bar.rs", "fn main() {}")
3bffb641 4145 .file("src/bin/baz/main.rs", "fn main() {}")
d43ee1dd 4146 .build();
10661f3f 4147
85984a87 4148 p.cargo("build").run();
570fe892
DW
4149 assert!(p.bin("foo").is_file());
4150 assert!(p.bin("bar").is_file());
4151 assert!(p.bin("baz").is_file());
5d12c5b4
S
4152}
4153
4154#[test]
3bffb641 4155fn inferred_bins_duplicate_name() {
5d12c5b4 4156 // this should fail, because we have two binaries with the same name
f8c9928c 4157 let p = project()
5d12c5b4 4158 .file("src/main.rs", "fn main() {}")
f8c9928c
DW
4159 .file("src/bin/bar.rs", "fn main() {}")
4160 .file("src/bin/bar/main.rs", "fn main() {}")
d43ee1dd 4161 .build();
5d12c5b4 4162
85984a87 4163 p.cargo("build").with_status(101).with_stderr_contains(
f8c9928c 4164 "[..]found duplicate binary name bar, but all binary targets must have a unique name[..]",
85984a87
DW
4165 )
4166 .run();
5d12c5b4
S
4167}
4168
4169#[test]
3bffb641 4170fn inferred_bin_path() {
7fe2fbc8 4171 let p = project()
1e682848
AC
4172 .file(
4173 "Cargo.toml",
4174 r#"
5d12c5b4
S
4175 [package]
4176 name = "foo"
4177 version = "0.1.0"
4178 authors = []
4179
4180 [[bin]]
4181 name = "bar"
4182 # Note, no `path` key!
1e682848 4183 "#,
fecb7246
AC
4184 )
4185 .file("src/bin/bar/main.rs", "fn main() {}")
d43ee1dd 4186 .build();
5d12c5b4 4187
85984a87 4188 p.cargo("build").run();
570fe892 4189 assert!(p.bin("bar").is_file());
10661f3f 4190}
eb0e4c0b 4191
6b94ed22 4192#[test]
4193fn inferred_examples() {
7fe2fbc8 4194 let p = project()
6b94ed22 4195 .file("src/lib.rs", "fn main() {}")
4196 .file("examples/bar.rs", "fn main() {}")
d43ee1dd
NK
4197 .file("examples/baz/main.rs", "fn main() {}")
4198 .build();
6b94ed22 4199
85984a87 4200 p.cargo("test").run();
570fe892
DW
4201 assert!(p.bin("examples/bar").is_file());
4202 assert!(p.bin("examples/baz").is_file());
6b94ed22 4203}
4204
4205#[test]
4206fn inferred_tests() {
7fe2fbc8 4207 let p = project()
6b94ed22 4208 .file("src/lib.rs", "fn main() {}")
4209 .file("tests/bar.rs", "fn main() {}")
d43ee1dd
NK
4210 .file("tests/baz/main.rs", "fn main() {}")
4211 .build();
6b94ed22 4212
85984a87 4213 p.cargo("test --test=bar --test=baz").run();
6b94ed22 4214}
4215
4216#[test]
3bffb641 4217fn inferred_benchmarks() {
7fe2fbc8 4218 let p = project()
6b94ed22 4219 .file("src/lib.rs", "fn main() {}")
d43ee1dd 4220 .file("benches/bar.rs", "fn main() {}")
3bffb641 4221 .file("benches/baz/main.rs", "fn main() {}")
d43ee1dd 4222 .build();
6b94ed22 4223
85984a87 4224 p.cargo("bench --bench=bar --bench=baz").run();
6b94ed22 4225}
4226
67c52ffe
DW
4227#[test]
4228fn target_edition() {
4229 let p = project()
4230 .file(
4231 "Cargo.toml",
4232 r#"
3d029039
AC
4233 [package]
4234 name = "foo"
4235 version = "0.0.1"
67c52ffe 4236
3d029039
AC
4237 [lib]
4238 edition = "2018"
4239 "#,
fecb7246
AC
4240 )
4241 .file("src/lib.rs", "")
67c52ffe
DW
4242 .build();
4243
85984a87 4244 p.cargo("build -v")
2fc6b3e3 4245 .without_status() // passes on nightly, fails on stable, b/c --edition is nightly-only
85984a87
DW
4246 .with_stderr_contains(
4247 "\
67c52ffe
DW
4248[COMPILING] foo v0.0.1 ([..])
4249[RUNNING] `rustc [..]--edition=2018 [..]
85984a87 4250",
fecb7246
AC
4251 )
4252 .run();
67c52ffe
DW
4253}
4254
4255#[test]
4256fn target_edition_override() {
4257 let p = project()
4258 .file(
4259 "Cargo.toml",
4260 r#"
3d029039
AC
4261 [package]
4262 name = "foo"
4263 version = "0.0.1"
4264 authors = []
4265 edition = "2018"
67c52ffe 4266
3d029039
AC
4267 [lib]
4268 edition = "2015"
4269 "#,
fecb7246
AC
4270 )
4271 .file(
3d029039
AC
4272 "src/lib.rs",
4273 "
4274 pub fn async() {}
4275 pub fn try() {}
4276 pub fn await() {}
fecb7246 4277 ",
3d029039 4278 )
67c52ffe
DW
4279 .build();
4280
3d029039 4281 p.cargo("build -v").run();
67c52ffe
DW
4282}
4283
eb0e4c0b
TH
4284#[test]
4285fn same_metadata_different_directory() {
4286 // A top-level crate built in two different workspaces should have the
4287 // same metadata hash.
85984a87
DW
4288 let p = project()
4289 .at("foo1")
eb0e4c0b 4290 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
4291 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
4292 .build();
eb0e4c0b 4293 let output = t!(String::from_utf8(
af4f1392 4294 t!(p.cargo("build -v").exec_with_output()).stderr,
eb0e4c0b
TH
4295 ));
4296 let metadata = output
4297 .split_whitespace()
23591fe5 4298 .find(|arg| arg.starts_with("metadata="))
eb0e4c0b
TH
4299 .unwrap();
4300
85984a87
DW
4301 let p = project()
4302 .at("foo2")
eb0e4c0b 4303 .file("Cargo.toml", &basic_bin_manifest("foo"))
d43ee1dd
NK
4304 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
4305 .build();
eb0e4c0b 4306
85984a87
DW
4307 p.cargo("build -v")
4308 .with_stderr_contains(format!("[..]{}[..]", metadata))
4309 .run();
eb0e4c0b 4310}
dafab240
AK
4311
4312#[test]
4313fn building_a_dependent_crate_witout_bin_should_fail() {
4314 Package::new("testless", "0.1.0")
1e682848
AC
4315 .file(
4316 "Cargo.toml",
4317 r#"
dafab240
AK
4318 [project]
4319 name = "testless"
4320 version = "0.1.0"
4321
4322 [[bin]]
4323 name = "a_bin"
1e682848 4324 "#,
fecb7246
AC
4325 )
4326 .file("src/lib.rs", "")
dafab240
AK
4327 .publish();
4328
7fe2fbc8 4329 let p = project()
1e682848
AC
4330 .file(
4331 "Cargo.toml",
4332 r#"
dafab240
AK
4333 [project]
4334 name = "foo"
4335 version = "0.1.0"
4336
4337 [dependencies]
4338 testless = "0.1.0"
1e682848 4339 "#,
fecb7246
AC
4340 )
4341 .file("src/lib.rs", "")
d43ee1dd 4342 .build();
dafab240 4343
85984a87
DW
4344 p.cargo("build")
4345 .with_status(101)
4346 .with_stderr_contains("[..]can't find `a_bin` bin, specify bin.path")
4347 .run();
dafab240 4348}
dfd964a4 4349
dfd964a4 4350#[test]
4351fn uplift_dsym_of_bin_on_mac() {
8647a87d 4352 if !cfg!(any(target_os = "macos", target_os = "ios")) {
1e682848 4353 return;
8647a87d 4354 }
7fe2fbc8 4355 let p = project()
dfd964a4 4356 .file("src/main.rs", "fn main() { panic!(); }")
4357 .file("src/bin/b.rs", "fn main() { panic!(); }")
4358 .file("examples/c.rs", "fn main() { panic!(); }")
4359 .file("tests/d.rs", "fn main() { panic!(); }")
4360 .build();
4361
85984a87 4362 p.cargo("build --bins --examples --tests").run();
6fd1b54c
DW
4363 assert!(p.bin("foo.dSYM").is_dir());
4364 assert!(p.bin("b.dSYM").is_dir());
fecb7246
AC
4365 assert!(p
4366 .bin("b.dSYM")
4367 .symlink_metadata()
4368 .expect("read metadata from b.dSYM")
4369 .file_type()
4370 .is_symlink());
6fd1b54c
DW
4371 assert!(!p.bin("c.dSYM").is_dir());
4372 assert!(!p.bin("d.dSYM").is_dir());
dfd964a4 4373}
0acc67ca 4374
9c0d3f29
AK
4375#[test]
4376fn uplift_pdb_of_bin_on_windows() {
4377 if !cfg!(all(target_os = "windows", target_env = "msvc")) {
1e682848 4378 return;
9c0d3f29 4379 }
7fe2fbc8 4380 let p = project()
9c0d3f29
AK
4381 .file("src/main.rs", "fn main() { panic!(); }")
4382 .file("src/bin/b.rs", "fn main() { panic!(); }")
4383 .file("examples/c.rs", "fn main() { panic!(); }")
4384 .file("tests/d.rs", "fn main() { panic!(); }")
4385 .build();
4386
85984a87 4387 p.cargo("build --bins --examples --tests").run();
570fe892
DW
4388 assert!(p.target_debug_dir().join("foo.pdb").is_file());
4389 assert!(p.target_debug_dir().join("b.pdb").is_file());
4390 assert!(!p.target_debug_dir().join("c.pdb").is_file());
4391 assert!(!p.target_debug_dir().join("d.pdb").is_file());
9c0d3f29
AK
4392}
4393
0acc67ca
EH
4394// Make sure that `cargo build` chooses the correct profile for building
4395// targets based on filters (assuming --profile is not specified).
4396#[test]
4397fn build_filter_infer_profile() {
7fe2fbc8 4398 let p = project()
0acc67ca
EH
4399 .file("src/lib.rs", "")
4400 .file("src/main.rs", "fn main() {}")
4401 .file("tests/t1.rs", "")
4402 .file("benches/b1.rs", "")
4403 .file("examples/ex1.rs", "fn main() {}")
4404 .build();
4405
85984a87
DW
4406 p.cargo("build -v")
4407 .with_stderr_contains(
739c272f 4408 "[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
85984a87 4409 --emit=dep-info,link[..]",
fecb7246
AC
4410 )
4411 .with_stderr_contains(
739c272f 4412 "[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
85984a87 4413 --emit=dep-info,link[..]",
fecb7246
AC
4414 )
4415 .run();
0acc67ca
EH
4416
4417 p.root().join("target").rm_rf();
85984a87
DW
4418 p.cargo("build -v --test=t1")
4419 .with_stderr_contains(
739c272f
EH
4420 "[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
4421 --emit=dep-info,link -C debuginfo=2 [..]",
fecb7246
AC
4422 )
4423 .with_stderr_contains(
739c272f 4424 "[RUNNING] `rustc --crate-name t1 tests/t1.rs --color never --emit=dep-info,link \
fecb7246
AC
4425 -C debuginfo=2 [..]",
4426 )
4427 .with_stderr_contains(
739c272f
EH
4428 "[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4429 --emit=dep-info,link -C debuginfo=2 [..]",
fecb7246
AC
4430 )
4431 .run();
0acc67ca
EH
4432
4433 p.root().join("target").rm_rf();
739c272f 4434 // Bench uses test profile without `--release`.
85984a87
DW
4435 p.cargo("build -v --bench=b1")
4436 .with_stderr_contains(
739c272f
EH
4437 "[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
4438 --emit=dep-info,link -C debuginfo=2 [..]",
fecb7246
AC
4439 )
4440 .with_stderr_contains(
739c272f 4441 "[RUNNING] `rustc --crate-name b1 benches/b1.rs --color never --emit=dep-info,link \
fecb7246 4442 -C debuginfo=2 [..]",
739c272f
EH
4443 )
4444 .with_stderr_does_not_contain("opt-level")
4445 .with_stderr_contains(
4446 "[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4447 --emit=dep-info,link -C debuginfo=2 [..]",
fecb7246
AC
4448 )
4449 .run();
0acc67ca 4450}
b9497ab2 4451
0bf8e541 4452#[test]
0bc11557 4453fn targets_selected_default() {
85984a87
DW
4454 let p = project().file("src/main.rs", "fn main() {}").build();
4455 p.cargo("build -v")
0bf8e541 4456 // bin
fecb7246
AC
4457 .with_stderr_contains(
4458 "\
4459 [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4460 --emit=dep-info,link[..]",
4461 )
0bf8e541 4462 // bench
fecb7246
AC
4463 .with_stderr_does_not_contain(
4464 "\
4465 [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
4466 -C opt-level=3 --test [..]",
4467 )
0bf8e541 4468 // unit test
fecb7246
AC
4469 .with_stderr_does_not_contain(
4470 "\
4471 [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
4472 -C debuginfo=2 --test [..]",
4473 )
4474 .run();
0bc11557
XL
4475}
4476
4477#[test]
4478fn targets_selected_all() {
85984a87
DW
4479 let p = project().file("src/main.rs", "fn main() {}").build();
4480 p.cargo("build -v --all-targets")
0bf8e541 4481 // bin
fecb7246
AC
4482 .with_stderr_contains(
4483 "\
4484 [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4485 --emit=dep-info,link[..]",
4486 )
0bf8e541 4487 // unit test
fecb7246
AC
4488 .with_stderr_contains(
4489 "\
4490 [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
4491 -C debuginfo=2 --test [..]",
4492 )
4493 .run();
0bf8e541
XL
4494}
4495
b9497ab2
EH
4496#[test]
4497fn all_targets_no_lib() {
85984a87
DW
4498 let p = project().file("src/main.rs", "fn main() {}").build();
4499 p.cargo("build -v --all-targets")
b9497ab2 4500 // bin
fecb7246
AC
4501 .with_stderr_contains(
4502 "\
4503 [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4504 --emit=dep-info,link[..]",
4505 )
b9497ab2 4506 // unit test
fecb7246
AC
4507 .with_stderr_contains(
4508 "\
4509 [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
4510 -C debuginfo=2 --test [..]",
4511 )
4512 .run();
b9497ab2 4513}
554f3339
LL
4514
4515#[test]
4516fn no_linkable_target() {
4517 // Issue 3169. This is currently not an error as per discussion in PR #4797
7fe2fbc8 4518 let p = project()
1e682848
AC
4519 .file(
4520 "Cargo.toml",
4521 r#"
554f3339
LL
4522 [package]
4523 name = "foo"
4524 version = "0.1.0"
4525 authors = []
4526 [dependencies]
4527 the_lib = { path = "the_lib" }
1e682848 4528 "#,
fecb7246
AC
4529 )
4530 .file("src/main.rs", "fn main() {}")
1e682848
AC
4531 .file(
4532 "the_lib/Cargo.toml",
4533 r#"
554f3339
LL
4534 [package]
4535 name = "the_lib"
4536 version = "0.1.0"
4537 [lib]
4538 name = "the_lib"
4539 crate-type = ["staticlib"]
1e682848 4540 "#,
fecb7246
AC
4541 )
4542 .file("the_lib/src/lib.rs", "pub fn foo() {}")
554f3339 4543 .build();
85984a87
DW
4544 p.cargo("build")
4545 .with_stderr_contains(
1e682848
AC
4546 "\
4547 [WARNING] The package `the_lib` provides no linkable [..] \
4548 while compiling `foo`. [..] in `the_lib`'s Cargo.toml. [..]",
fecb7246
AC
4549 )
4550 .run();
554f3339 4551}
da41b4e0
XL
4552
4553#[test]
4554fn avoid_dev_deps() {
4555 Package::new("foo", "1.0.0").publish();
7fe2fbc8 4556 let p = project()
1e682848
AC
4557 .file(
4558 "Cargo.toml",
4559 r#"
da41b4e0
XL
4560 [package]
4561 name = "bar"
4562 version = "0.1.0"
4563 authors = []
4564
4565 [dev-dependencies]
4566 baz = "1.0.0"
1e682848 4567 "#,
fecb7246
AC
4568 )
4569 .file("src/main.rs", "fn main() {}")
da41b4e0
XL
4570 .build();
4571
f58d107e
EH
4572 p.cargo("build")
4573 .with_status(101)
4574 .with_stderr(
4575 "\
4576[UPDATING] [..]
4577[ERROR] no matching package named `baz` found
4578location searched: registry `https://github.com/rust-lang/crates.io-index`
4579required by package `bar v0.1.0 ([..]/foo)`
4580",
4581 )
4582 .run();
85984a87
DW
4583 p.cargo("build -Zavoid-dev-deps")
4584 .masquerade_as_nightly_cargo()
4585 .run();
da41b4e0 4586}
67968e6b
AK
4587
4588#[test]
4589fn invalid_jobs() {
7fe2fbc8 4590 let p = project()
67968e6b
AK
4591 .file("Cargo.toml", &basic_bin_manifest("foo"))
4592 .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
4593 .build();
4594
85984a87
DW
4595 p.cargo("build --jobs over9000")
4596 .with_status(1)
4597 .with_stderr("error: Invalid value: could not parse `over9000` as a number")
4598 .run();
67968e6b 4599}
771fec3c
EH
4600
4601#[test]
4602fn target_filters_workspace() {
4603 let ws = project()
4604 .at("ws")
4605 .file(
4606 "Cargo.toml",
4607 r#"
4608 [workspace]
4609 members = ["a", "b"]
4610 "#,
fecb7246
AC
4611 )
4612 .file("a/Cargo.toml", &basic_lib_manifest("a"))
771fec3c
EH
4613 .file("a/src/lib.rs", "")
4614 .file("a/examples/ex1.rs", "fn main() {}")
4615 .file("b/Cargo.toml", &basic_bin_manifest("b"))
fa0787aa 4616 .file("b/src/lib.rs", "")
771fec3c 4617 .file("b/src/main.rs", "fn main() {}")
771fec3c
EH
4618 .build();
4619
85984a87
DW
4620 ws.cargo("build -v --example ex")
4621 .with_status(101)
4622 .with_stderr(
771fec3c
EH
4623 "\
4624[ERROR] no example target named `ex`
4625
4626Did you mean `ex1`?",
fecb7246
AC
4627 )
4628 .run();
771fec3c 4629
85984a87
DW
4630 ws.cargo("build -v --lib")
4631 .with_status(0)
4632 .with_stderr_contains("[RUNNING] `rustc [..]a/src/lib.rs[..]")
fa0787aa 4633 .with_stderr_contains("[RUNNING] `rustc [..]b/src/lib.rs[..]")
85984a87 4634 .run();
771fec3c 4635
85984a87
DW
4636 ws.cargo("build -v --example ex1")
4637 .with_status(0)
4638 .with_stderr_contains("[RUNNING] `rustc [..]a/examples/ex1.rs[..]")
85984a87 4639 .run();
771fec3c
EH
4640}
4641
4642#[test]
4643fn target_filters_workspace_not_found() {
4644 let ws = project()
4645 .at("ws")
4646 .file(
4647 "Cargo.toml",
4648 r#"
4649 [workspace]
4650 members = ["a", "b"]
4651 "#,
fecb7246
AC
4652 )
4653 .file("a/Cargo.toml", &basic_bin_manifest("a"))
771fec3c
EH
4654 .file("a/src/main.rs", "fn main() {}")
4655 .file("b/Cargo.toml", &basic_bin_manifest("b"))
4656 .file("b/src/main.rs", "fn main() {}")
4657 .build();
4658
85984a87
DW
4659 ws.cargo("build -v --lib")
4660 .with_status(101)
4661 .with_stderr("[ERROR] no library targets found in packages: a, b")
4662 .run();
771fec3c 4663}
b15dc1ac
EH
4664
4665#[cfg(unix)]
4666#[test]
4667fn signal_display() {
4668 // Cause the compiler to crash with a signal.
4669 let foo = project()
4670 .file(
4671 "Cargo.toml",
4672 r#"
4673 [package]
4674 name = "foo"
4675 version = "0.1.0"
4676 [dependencies]
4677 pm = { path = "pm" }
4678 "#,
4679 )
4680 .file(
4681 "src/lib.rs",
4682 r#"
4683 #[macro_use]
4684 extern crate pm;
4685
4686 #[derive(Foo)]
4687 pub struct S;
4688 "#,
4689 )
4690 .file(
4691 "pm/Cargo.toml",
4692 r#"
4693 [package]
4694 name = "pm"
4695 version = "0.1.0"
4696 [lib]
4697 proc-macro = true
4698 "#,
4699 )
4700 .file(
4701 "pm/src/lib.rs",
4702 r#"
4703 extern crate proc_macro;
4704 use proc_macro::TokenStream;
4705
4706 #[proc_macro_derive(Foo)]
4707 pub fn derive(_input: TokenStream) -> TokenStream {
4708 std::process::abort()
4709 }
4710 "#,
4711 )
4712 .build();
4713
4714 foo.cargo("build")
fecb7246
AC
4715 .with_stderr(
4716 "\
b15dc1ac
EH
4717[COMPILING] pm [..]
4718[COMPILING] foo [..]
4719[ERROR] Could not compile `foo`.
4720
4721Caused by:
4722 process didn't exit successfully: `rustc [..]` (signal: 6, SIGABRT: process abort signal)
fecb7246
AC
4723",
4724 )
b15dc1ac
EH
4725 .with_status(101)
4726 .run();
4727}
3f0c788a
EH
4728
4729#[test]
4730fn json_parse_fail() {
4731 // Ensure when json parsing fails, and rustc exits with non-zero exit
4732 // code, that a useful error message is displayed.
4733 let foo = project()
4734 .file(
4735 "Cargo.toml",
4736 r#"
4737 [package]
4738 name = "foo"
4739 version = "0.1.0"
4740 [dependencies]
4741 pm = { path = "pm" }
4742 "#,
4743 )
4744 .file(
4745 "src/lib.rs",
4746 r#"
4747 #[macro_use]
4748 extern crate pm;
4749
4750 #[derive(Foo)]
4751 pub struct S;
4752 "#,
4753 )
4754 .file(
4755 "pm/Cargo.toml",
4756 r#"
4757 [package]
4758 name = "pm"
4759 version = "0.1.0"
4760 [lib]
4761 proc-macro = true
4762 "#,
4763 )
4764 .file(
4765 "pm/src/lib.rs",
4766 r#"
4767 extern crate proc_macro;
4768 use proc_macro::TokenStream;
4769
4770 #[proc_macro_derive(Foo)]
4771 pub fn derive(_input: TokenStream) -> TokenStream {
4772 eprintln!("{{evil proc macro}}");
4773 panic!("something went wrong");
4774 }
4775 "#,
4776 )
4777 .build();
4778
4779 foo.cargo("build --message-format=json")
4780 .with_stderr(
4781 "\
4782[COMPILING] pm [..]
4783[COMPILING] foo [..]
4784[ERROR] Could not compile `foo`.
4785
4786Caused by:
4787 compiler produced invalid json: `{evil proc macro}`
4788
4789Caused by:
4790 failed to parse process output: `rustc [..]
4791",
4792 )
4793 .with_status(101)
4794 .run();
4795}