]> git.proxmox.com Git - cargo.git/blame - tests/testsuite/update.rs
Warning when precise or aggressive without -p flag
[cargo.git] / tests / testsuite / update.rs
CommitLineData
83571aee
EH
1//! Tests for the `cargo update` command.
2
9115b2c3
AC
3use cargo_test_support::registry::Package;
4use cargo_test_support::{basic_manifest, project};
51d23560 5
0e0d9688 6#[cargo_test]
51d23560
AC
7fn minor_update_two_places() {
8 Package::new("log", "0.1.0").publish();
7fe2fbc8 9 let p = project()
51d23560
AC
10 .file(
11 "Cargo.toml",
12 r#"
13 [package]
14 name = "bar"
15 version = "0.0.1"
16 authors = []
17
18 [dependencies]
19 log = "0.1"
20 foo = { path = "foo" }
21 "#,
fecb7246
AC
22 )
23 .file("src/lib.rs", "")
51d23560
AC
24 .file(
25 "foo/Cargo.toml",
26 r#"
27 [package]
28 name = "foo"
29 version = "0.0.1"
30 authors = []
31
32 [dependencies]
33 log = "0.1"
34 "#,
fecb7246
AC
35 )
36 .file("foo/src/lib.rs", "")
51d23560
AC
37 .build();
38
85984a87 39 p.cargo("build").run();
51d23560
AC
40 Package::new("log", "0.1.1").publish();
41
4ae79d2f
EH
42 p.change_file(
43 "foo/Cargo.toml",
44 r#"
45 [package]
46 name = "foo"
47 version = "0.0.1"
48 authors = []
51d23560 49
4ae79d2f
EH
50 [dependencies]
51 log = "0.1.1"
52 "#,
53 );
51d23560 54
85984a87 55 p.cargo("build").run();
51d23560
AC
56}
57
0e0d9688 58#[cargo_test]
51d23560
AC
59fn transitive_minor_update() {
60 Package::new("log", "0.1.0").publish();
61 Package::new("serde", "0.1.0").dep("log", "0.1").publish();
62
7fe2fbc8 63 let p = project()
51d23560
AC
64 .file(
65 "Cargo.toml",
66 r#"
67 [package]
68 name = "bar"
69 version = "0.0.1"
70 authors = []
71
72 [dependencies]
73 serde = "0.1"
74 log = "0.1"
75 foo = { path = "foo" }
76 "#,
fecb7246
AC
77 )
78 .file("src/lib.rs", "")
51d23560
AC
79 .file(
80 "foo/Cargo.toml",
81 r#"
82 [package]
83 name = "foo"
84 version = "0.0.1"
85 authors = []
86
87 [dependencies]
88 serde = "0.1"
89 "#,
fecb7246
AC
90 )
91 .file("foo/src/lib.rs", "")
51d23560
AC
92 .build();
93
85984a87 94 p.cargo("build").run();
51d23560
AC
95
96 Package::new("log", "0.1.1").publish();
97 Package::new("serde", "0.1.1").dep("log", "0.1.1").publish();
98
99 // Note that `serde` isn't actually updated here! The default behavior for
100 // `update` right now is to as conservatively as possible attempt to satisfy
101 // an update. In this case we previously locked the dependency graph to `log
102 // 0.1.0`, but nothing on the command line says we're allowed to update
103 // that. As a result the update of `serde` here shouldn't update to `serde
104 // 0.1.1` as that would also force an update to `log 0.1.1`.
105 //
106 // Also note that this is probably counterintuitive and weird. We may wish
107 // to change this one day.
85984a87
DW
108 p.cargo("update -p serde")
109 .with_stderr(
51d23560 110 "\
41aa6fba 111[UPDATING] `[..]` index
51d23560 112",
fecb7246
AC
113 )
114 .run();
51d23560
AC
115}
116
0e0d9688 117#[cargo_test]
51d23560
AC
118fn conservative() {
119 Package::new("log", "0.1.0").publish();
120 Package::new("serde", "0.1.0").dep("log", "0.1").publish();
121
7fe2fbc8 122 let p = project()
51d23560
AC
123 .file(
124 "Cargo.toml",
125 r#"
126 [package]
127 name = "bar"
128 version = "0.0.1"
129 authors = []
130
131 [dependencies]
132 serde = "0.1"
133 log = "0.1"
134 foo = { path = "foo" }
135 "#,
fecb7246
AC
136 )
137 .file("src/lib.rs", "")
51d23560
AC
138 .file(
139 "foo/Cargo.toml",
140 r#"
141 [package]
142 name = "foo"
143 version = "0.0.1"
144 authors = []
145
146 [dependencies]
147 serde = "0.1"
148 "#,
fecb7246
AC
149 )
150 .file("foo/src/lib.rs", "")
51d23560
AC
151 .build();
152
85984a87 153 p.cargo("build").run();
51d23560
AC
154
155 Package::new("log", "0.1.1").publish();
156 Package::new("serde", "0.1.1").dep("log", "0.1").publish();
157
85984a87
DW
158 p.cargo("update -p serde")
159 .with_stderr(
51d23560 160 "\
41aa6fba 161[UPDATING] `[..]` index
51d23560
AC
162[UPDATING] serde v0.1.0 -> v0.1.1
163",
fecb7246
AC
164 )
165 .run();
51d23560
AC
166}
167
0e0d9688 168#[cargo_test]
51d23560
AC
169fn update_via_new_dep() {
170 Package::new("log", "0.1.0").publish();
7fe2fbc8 171 let p = project()
51d23560
AC
172 .file(
173 "Cargo.toml",
174 r#"
175 [package]
176 name = "bar"
177 version = "0.0.1"
178 authors = []
179
180 [dependencies]
181 log = "0.1"
182 # foo = { path = "foo" }
183 "#,
fecb7246
AC
184 )
185 .file("src/lib.rs", "")
51d23560
AC
186 .file(
187 "foo/Cargo.toml",
188 r#"
189 [package]
190 name = "foo"
191 version = "0.0.1"
192 authors = []
193
194 [dependencies]
195 log = "0.1.1"
196 "#,
fecb7246
AC
197 )
198 .file("foo/src/lib.rs", "")
51d23560
AC
199 .build();
200
85984a87 201 p.cargo("build").run();
51d23560
AC
202 Package::new("log", "0.1.1").publish();
203
204 p.uncomment_root_manifest();
782266aa 205 p.cargo("build").env("CARGO_LOG", "cargo=trace").run();
51d23560
AC
206}
207
0e0d9688 208#[cargo_test]
51d23560
AC
209fn update_via_new_member() {
210 Package::new("log", "0.1.0").publish();
7fe2fbc8 211 let p = project()
51d23560
AC
212 .file(
213 "Cargo.toml",
214 r#"
215 [package]
216 name = "bar"
217 version = "0.0.1"
218 authors = []
219
220 [workspace]
221 # members = [ "foo" ]
222
223 [dependencies]
224 log = "0.1"
225 "#,
fecb7246
AC
226 )
227 .file("src/lib.rs", "")
51d23560
AC
228 .file(
229 "foo/Cargo.toml",
230 r#"
231 [package]
232 name = "foo"
233 version = "0.0.1"
234 authors = []
235
236 [dependencies]
237 log = "0.1.1"
238 "#,
fecb7246
AC
239 )
240 .file("foo/src/lib.rs", "")
51d23560
AC
241 .build();
242
85984a87 243 p.cargo("build").run();
51d23560
AC
244 Package::new("log", "0.1.1").publish();
245
246 p.uncomment_root_manifest();
85984a87 247 p.cargo("build").run();
51d23560
AC
248}
249
0e0d9688 250#[cargo_test]
51d23560
AC
251fn add_dep_deep_new_requirement() {
252 Package::new("log", "0.1.0").publish();
7fe2fbc8 253 let p = project()
51d23560
AC
254 .file(
255 "Cargo.toml",
256 r#"
257 [package]
258 name = "bar"
259 version = "0.0.1"
260 authors = []
261
262 [dependencies]
263 log = "0.1"
264 # bar = "0.1"
265 "#,
fecb7246
AC
266 )
267 .file("src/lib.rs", "")
51d23560
AC
268 .build();
269
85984a87 270 p.cargo("build").run();
51d23560
AC
271
272 Package::new("log", "0.1.1").publish();
273 Package::new("bar", "0.1.0").dep("log", "0.1.1").publish();
274
275 p.uncomment_root_manifest();
85984a87 276 p.cargo("build").run();
51d23560
AC
277}
278
0e0d9688 279#[cargo_test]
51d23560
AC
280fn everything_real_deep() {
281 Package::new("log", "0.1.0").publish();
282 Package::new("foo", "0.1.0").dep("log", "0.1").publish();
7fe2fbc8 283 let p = project()
51d23560
AC
284 .file(
285 "Cargo.toml",
286 r#"
287 [package]
288 name = "bar"
289 version = "0.0.1"
290 authors = []
291
292 [dependencies]
293 foo = "0.1"
294 # bar = "0.1"
295 "#,
fecb7246
AC
296 )
297 .file("src/lib.rs", "")
51d23560
AC
298 .build();
299
85984a87 300 p.cargo("build").run();
51d23560
AC
301
302 Package::new("log", "0.1.1").publish();
303 Package::new("bar", "0.1.0").dep("log", "0.1.1").publish();
304
305 p.uncomment_root_manifest();
85984a87 306 p.cargo("build").run();
51d23560 307}
0deaae9e 308
0e0d9688 309#[cargo_test]
0deaae9e 310fn change_package_version() {
7fe2fbc8 311 let p = project()
0deaae9e
AC
312 .file(
313 "Cargo.toml",
314 r#"
315 [package]
316 name = "a-foo"
317 version = "0.2.0-alpha"
318 authors = []
319
320 [dependencies]
321 bar = { path = "bar", version = "0.2.0-alpha" }
322 "#,
fecb7246
AC
323 )
324 .file("src/lib.rs", "")
ab19c483 325 .file("bar/Cargo.toml", &basic_manifest("bar", "0.2.0-alpha"))
0deaae9e
AC
326 .file("bar/src/lib.rs", "")
327 .file(
328 "Cargo.lock",
329 r#"
330 [[package]]
331 name = "foo"
332 version = "0.2.0"
333 dependencies = ["bar 0.2.0"]
334
335 [[package]]
336 name = "bar"
337 version = "0.2.0"
338 "#,
fecb7246
AC
339 )
340 .build();
0deaae9e 341
85984a87 342 p.cargo("build").run();
0deaae9e 343}
1a26e86c 344
0e0d9688 345#[cargo_test]
1a26e86c 346fn update_precise() {
347 Package::new("log", "0.1.0").publish();
348 Package::new("serde", "0.1.0").publish();
349 Package::new("serde", "0.2.1").publish();
350
7fe2fbc8 351 let p = project()
1a26e86c 352 .file(
353 "Cargo.toml",
354 r#"
355 [package]
356 name = "bar"
357 version = "0.0.1"
358 authors = []
359
360 [dependencies]
361 serde = "0.2"
362 foo = { path = "foo" }
363 "#,
fecb7246
AC
364 )
365 .file("src/lib.rs", "")
1a26e86c 366 .file(
367 "foo/Cargo.toml",
368 r#"
369 [package]
370 name = "foo"
371 version = "0.0.1"
372 authors = []
373
374 [dependencies]
375 serde = "0.1"
376 "#,
fecb7246
AC
377 )
378 .file("foo/src/lib.rs", "")
1a26e86c 379 .build();
380
85984a87 381 p.cargo("build").run();
1a26e86c 382
383 Package::new("serde", "0.2.0").publish();
384
85984a87
DW
385 p.cargo("update -p serde:0.2.1 --precise 0.2.0")
386 .with_stderr(
1a26e86c 387 "\
41aa6fba 388[UPDATING] `[..]` index
1a26e86c 389[UPDATING] serde v0.2.1 -> v0.2.0
390",
fecb7246
AC
391 )
392 .run();
1a26e86c 393}
851e20b2 394
f95deaaf 395#[cargo_test]
396fn update_precise_without_package() {
397 Package::new("serde", "0.2.0").publish();
398
399 let p = project()
400 .file(
401 "Cargo.toml",
402 r#"
403 [package]
404 name = "bar"
405 version = "0.0.1"
406 authors = []
407
408 [dependencies]
409 serde = "0.2"
410 "#,
411 )
412 .file("src/lib.rs", "")
413 .build();
414
415 p.cargo("build").run();
416
417 Package::new("serde", "0.2.1").publish();
418 Package::new("serde", "0.3.0").publish();
419
420 p.cargo("update --precise 0.3.0")
421 .with_stderr(
422 "\
423[WARNING] precise is only supported with \"--package <SPEC>\", this will become a hard error in a future release.
424[UPDATING] `[..]` index
425[UPDATING] serde v0.2.0 -> v0.2.1
426",
427 )
428 .run();
429}
430
f299d1c1
AH
431// cargo update should respect its arguments even without a lockfile.
432// See issue "Running cargo update without a Cargo.lock ignores arguments"
433// at <https://github.com/rust-lang/cargo/issues/6872>.
0e0d9688 434#[cargo_test]
f299d1c1
AH
435fn update_precise_first_run() {
436 Package::new("serde", "0.1.0").publish();
437 Package::new("serde", "0.2.0").publish();
438 Package::new("serde", "0.2.1").publish();
439
440 let p = project()
441 .file(
442 "Cargo.toml",
443 r#"
444 [package]
445 name = "bar"
446 version = "0.0.1"
447
448 [dependencies]
449 serde = "0.2"
450 "#,
451 )
452 .file("src/lib.rs", "")
453 .build();
454
455 p.cargo("update -p serde --precise 0.2.0")
456 .with_stderr(
457 "\
458[UPDATING] `[..]` index
1bcf7f11 459[UPDATING] serde v0.2.1 -> v0.2.0
f299d1c1
AH
460",
461 )
462 .run();
463
464 // Assert `cargo metadata` shows serde 0.2.0
465 p.cargo("metadata")
466 .with_json(
467 r#"{
468 "packages": [
469 {
470 "authors": [],
471 "categories": [],
f9a56257 472 "default_run": null,
58869e5c
AS
473 "dependencies": [
474 {
475 "features": [],
476 "kind": null,
477 "name": "serde",
478 "optional": false,
479 "registry": null,
480 "rename": null,
481 "req": "^0.2",
482 "source": "registry+https://github.com/rust-lang/crates.io-index",
483 "target": null,
484 "uses_default_features": true
485 }
486 ],
f299d1c1 487 "description": null,
72694e80 488 "documentation": null,
f299d1c1
AH
489 "edition": "2015",
490 "features": {},
72694e80 491 "homepage": null,
58869e5c 492 "id": "bar 0.0.1 (path+file://[..]/foo)",
f299d1c1
AH
493 "keywords": [],
494 "license": null,
495 "license_file": null,
496 "links": null,
58869e5c 497 "manifest_path": "[..]/foo/Cargo.toml",
f299d1c1 498 "metadata": null,
7d41d454 499 "publish": null,
58869e5c 500 "name": "bar",
f299d1c1
AH
501 "readme": null,
502 "repository": null,
390af271 503 "rust_version": null,
58869e5c 504 "source": null,
f299d1c1
AH
505 "targets": [
506 {
507 "crate_types": [
508 "lib"
509 ],
e831dd12 510 "doc": true,
e1d433d3 511 "doctest": true,
95b22d28 512 "test": true,
f299d1c1
AH
513 "edition": "2015",
514 "kind": [
515 "lib"
516 ],
58869e5c
AS
517 "name": "bar",
518 "src_path": "[..]/foo/src/lib.rs"
f299d1c1
AH
519 }
520 ],
58869e5c 521 "version": "0.0.1"
f299d1c1
AH
522 },
523 {
524 "authors": [],
525 "categories": [],
f9a56257 526 "default_run": null,
58869e5c 527 "dependencies": [],
f299d1c1 528 "description": null,
72694e80 529 "documentation": null,
f299d1c1
AH
530 "edition": "2015",
531 "features": {},
72694e80 532 "homepage": null,
58869e5c 533 "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
f299d1c1
AH
534 "keywords": [],
535 "license": null,
536 "license_file": null,
537 "links": null,
58869e5c 538 "manifest_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/Cargo.toml",
f299d1c1 539 "metadata": null,
7d41d454 540 "publish": null,
58869e5c 541 "name": "serde",
f299d1c1
AH
542 "readme": null,
543 "repository": null,
390af271 544 "rust_version": null,
58869e5c 545 "source": "registry+https://github.com/rust-lang/crates.io-index",
f299d1c1
AH
546 "targets": [
547 {
548 "crate_types": [
549 "lib"
550 ],
e831dd12 551 "doc": true,
e1d433d3 552 "doctest": true,
f299d1c1
AH
553 "edition": "2015",
554 "kind": [
555 "lib"
556 ],
58869e5c 557 "name": "serde",
95b22d28
OS
558 "src_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/src/lib.rs",
559 "test": true
f299d1c1
AH
560 }
561 ],
58869e5c 562 "version": "0.2.0"
f299d1c1
AH
563 }
564 ],
565 "resolve": {
566 "nodes": [
567 {
568 "dependencies": [
569 "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
570 ],
571 "deps": [
572 {
a7faecc3
EH
573 "dep_kinds": [
574 {
575 "kind": null,
576 "target": null
577 }
578 ],
f299d1c1
AH
579 "name": "serde",
580 "pkg": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
581 }
582 ],
583 "features": [],
584 "id": "bar 0.0.1 (path+file://[..]/foo)"
585 },
586 {
587 "dependencies": [],
588 "deps": [],
589 "features": [],
590 "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
591 }
592 ],
593 "root": "bar 0.0.1 (path+file://[..]/foo)"
594 },
595 "target_directory": "[..]/foo/target",
596 "version": 1,
597 "workspace_members": [
598 "bar 0.0.1 (path+file://[..]/foo)"
599 ],
866d4316
BC
600 "workspace_root": "[..]/foo",
601 "metadata": null
f299d1c1
AH
602}"#,
603 )
604 .run();
605
606 p.cargo("update -p serde --precise 0.2.0")
607 .with_stderr(
608 "\
609[UPDATING] `[..]` index
610",
611 )
612 .run();
f299d1c1
AH
613}
614
0e0d9688 615#[cargo_test]
851e20b2
DW
616fn preserve_top_comment() {
617 let p = project().file("src/lib.rs", "").build();
618
619 p.cargo("update").run();
620
bd0e4a08
DW
621 let lockfile = p.read_lockfile();
622 assert!(lockfile.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n"));
623
624 let mut lines = lockfile.lines().collect::<Vec<_>>();
625 lines.insert(2, "# some other comment");
626 let mut lockfile = lines.join("\n");
e58c544f 627 lockfile.push('\n'); // .lines/.join loses the last newline
851e20b2
DW
628 println!("saving Cargo.lock contents:\n{}", lockfile);
629
630 p.change_file("Cargo.lock", &lockfile);
631
632 p.cargo("update").run();
633
bd0e4a08 634 let lockfile2 = p.read_lockfile();
851e20b2
DW
635 println!("loaded Cargo.lock contents:\n{}", lockfile2);
636
bd0e4a08 637 assert_eq!(lockfile, lockfile2);
851e20b2 638}
7be09e3c 639
0e0d9688 640#[cargo_test]
7be09e3c
AK
641fn dry_run_update() {
642 Package::new("log", "0.1.0").publish();
643 Package::new("serde", "0.1.0").dep("log", "0.1").publish();
644
645 let p = project()
646 .file(
647 "Cargo.toml",
648 r#"
649 [package]
650 name = "bar"
651 version = "0.0.1"
652 authors = []
653
654 [dependencies]
655 serde = "0.1"
656 log = "0.1"
657 foo = { path = "foo" }
658 "#,
659 )
660 .file("src/lib.rs", "")
661 .file(
662 "foo/Cargo.toml",
663 r#"
664 [package]
665 name = "foo"
666 version = "0.0.1"
667 authors = []
668
669 [dependencies]
670 serde = "0.1"
671 "#,
672 )
673 .file("foo/src/lib.rs", "")
674 .build();
675
676 p.cargo("build").run();
4ae79d2f 677 let old_lockfile = p.read_lockfile();
7be09e3c
AK
678
679 Package::new("log", "0.1.1").publish();
680 Package::new("serde", "0.1.1").dep("log", "0.1").publish();
681
682 p.cargo("update -p serde --dry-run")
683 .with_stderr(
684 "\
685[UPDATING] `[..]` index
686[UPDATING] serde v0.1.0 -> v0.1.1
687[WARNING] not updating lockfile due to dry run
688",
689 )
690 .run();
4ae79d2f 691 let new_lockfile = p.read_lockfile();
7be09e3c
AK
692 assert_eq!(old_lockfile, new_lockfile)
693}
9589d2cb
CO
694
695#[cargo_test]
696fn workspace_only() {
697 let p = project().file("src/main.rs", "fn main() {}").build();
698 p.cargo("generate-lockfile").run();
699 let lock1 = p.read_lockfile();
700
701 p.change_file(
702 "Cargo.toml",
703 r#"
704 [package]
705 name = "foo"
706 authors = []
707 version = "0.0.2"
708 "#,
709 );
710 p.cargo("update --workspace").run();
711 let lock2 = p.read_lockfile();
712
713 assert_ne!(lock1, lock2);
714 assert!(lock1.contains("0.0.1"));
715 assert!(lock2.contains("0.0.2"));
716 assert!(!lock1.contains("0.0.2"));
717 assert!(!lock2.contains("0.0.1"));
718}
8fe3bde2
EH
719
720#[cargo_test]
721fn precise_with_build_metadata() {
722 // +foo syntax shouldn't be necessary with --precise
723 Package::new("bar", "0.1.0+extra-stuff.0").publish();
724 let p = project()
725 .file(
726 "Cargo.toml",
727 r#"
728 [package]
729 name = "foo"
730 version = "0.1.0"
731
732 [dependencies]
733 bar = "0.1"
734 "#,
735 )
736 .file("src/lib.rs", "")
737 .build();
738 p.cargo("generate-lockfile").run();
739 Package::new("bar", "0.1.1+extra-stuff.1").publish();
740 Package::new("bar", "0.1.2+extra-stuff.2").publish();
741
742 p.cargo("update -p bar --precise 0.1")
743 .with_status(101)
744 .with_stderr(
745 "\
746error: invalid version format for precise version `0.1`
747
748Caused by:
749 unexpected end of input while parsing minor version number
750",
751 )
752 .run();
753
754 p.cargo("update -p bar --precise 0.1.1+does-not-match")
755 .with_status(101)
756 .with_stderr(
757 "\
758[UPDATING] [..] index
759error: no matching package named `bar` found
760location searched: registry `crates-io`
761required by package `foo v0.1.0 ([ROOT]/foo)`
762",
763 )
764 .run();
765
766 p.cargo("update -p bar --precise 0.1.1")
767 .with_stderr(
768 "\
769[UPDATING] [..] index
770[UPDATING] bar v0.1.0+extra-stuff.0 -> v0.1.1+extra-stuff.1
771",
772 )
773 .run();
774
775 Package::new("bar", "0.1.3").publish();
776 p.cargo("update -p bar --precise 0.1.3+foo")
777 .with_status(101)
778 .with_stderr(
779 "\
780[UPDATING] [..] index
781error: no matching package named `bar` found
782location searched: registry `crates-io`
783required by package `foo v0.1.0 ([ROOT]/foo)`
784",
785 )
786 .run();
787
788 p.cargo("update -p bar --precise 0.1.3")
789 .with_stderr(
790 "\
791[UPDATING] [..] index
792[UPDATING] bar v0.1.1+extra-stuff.1 -> v0.1.3
793",
794 )
795 .run();
796}