]> git.proxmox.com Git - cargo.git/blame - tests/testsuite/update.rs
Auto merge of #9212 - vidbina:fix-spelling-worksapce-to-workspace, r=ehuss
[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
f299d1c1
AH
395// cargo update should respect its arguments even without a lockfile.
396// See issue "Running cargo update without a Cargo.lock ignores arguments"
397// at <https://github.com/rust-lang/cargo/issues/6872>.
0e0d9688 398#[cargo_test]
f299d1c1
AH
399fn update_precise_first_run() {
400 Package::new("serde", "0.1.0").publish();
401 Package::new("serde", "0.2.0").publish();
402 Package::new("serde", "0.2.1").publish();
403
404 let p = project()
405 .file(
406 "Cargo.toml",
407 r#"
408 [package]
409 name = "bar"
410 version = "0.0.1"
411
412 [dependencies]
413 serde = "0.2"
414 "#,
415 )
416 .file("src/lib.rs", "")
417 .build();
418
419 p.cargo("update -p serde --precise 0.2.0")
420 .with_stderr(
421 "\
422[UPDATING] `[..]` index
1bcf7f11 423[UPDATING] serde v0.2.1 -> v0.2.0
f299d1c1
AH
424",
425 )
426 .run();
427
428 // Assert `cargo metadata` shows serde 0.2.0
429 p.cargo("metadata")
430 .with_json(
431 r#"{
432 "packages": [
433 {
434 "authors": [],
435 "categories": [],
58869e5c
AS
436 "dependencies": [
437 {
438 "features": [],
439 "kind": null,
440 "name": "serde",
441 "optional": false,
442 "registry": null,
443 "rename": null,
444 "req": "^0.2",
445 "source": "registry+https://github.com/rust-lang/crates.io-index",
446 "target": null,
447 "uses_default_features": true
448 }
449 ],
f299d1c1 450 "description": null,
72694e80 451 "documentation": null,
f299d1c1
AH
452 "edition": "2015",
453 "features": {},
72694e80 454 "homepage": null,
58869e5c 455 "id": "bar 0.0.1 (path+file://[..]/foo)",
f299d1c1
AH
456 "keywords": [],
457 "license": null,
458 "license_file": null,
459 "links": null,
58869e5c 460 "manifest_path": "[..]/foo/Cargo.toml",
f299d1c1 461 "metadata": null,
7d41d454 462 "publish": null,
58869e5c 463 "name": "bar",
f299d1c1
AH
464 "readme": null,
465 "repository": null,
58869e5c 466 "source": null,
f299d1c1
AH
467 "targets": [
468 {
469 "crate_types": [
470 "lib"
471 ],
e831dd12 472 "doc": true,
e1d433d3 473 "doctest": true,
95b22d28 474 "test": true,
f299d1c1
AH
475 "edition": "2015",
476 "kind": [
477 "lib"
478 ],
58869e5c
AS
479 "name": "bar",
480 "src_path": "[..]/foo/src/lib.rs"
f299d1c1
AH
481 }
482 ],
58869e5c 483 "version": "0.0.1"
f299d1c1
AH
484 },
485 {
486 "authors": [],
487 "categories": [],
58869e5c 488 "dependencies": [],
f299d1c1 489 "description": null,
72694e80 490 "documentation": null,
f299d1c1
AH
491 "edition": "2015",
492 "features": {},
72694e80 493 "homepage": null,
58869e5c 494 "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
f299d1c1
AH
495 "keywords": [],
496 "license": null,
497 "license_file": null,
498 "links": null,
58869e5c 499 "manifest_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/Cargo.toml",
f299d1c1 500 "metadata": null,
7d41d454 501 "publish": null,
58869e5c 502 "name": "serde",
f299d1c1
AH
503 "readme": null,
504 "repository": null,
58869e5c 505 "source": "registry+https://github.com/rust-lang/crates.io-index",
f299d1c1
AH
506 "targets": [
507 {
508 "crate_types": [
509 "lib"
510 ],
e831dd12 511 "doc": true,
e1d433d3 512 "doctest": true,
f299d1c1
AH
513 "edition": "2015",
514 "kind": [
515 "lib"
516 ],
58869e5c 517 "name": "serde",
95b22d28
OS
518 "src_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/src/lib.rs",
519 "test": true
f299d1c1
AH
520 }
521 ],
58869e5c 522 "version": "0.2.0"
f299d1c1
AH
523 }
524 ],
525 "resolve": {
526 "nodes": [
527 {
528 "dependencies": [
529 "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
530 ],
531 "deps": [
532 {
a7faecc3
EH
533 "dep_kinds": [
534 {
535 "kind": null,
536 "target": null
537 }
538 ],
f299d1c1
AH
539 "name": "serde",
540 "pkg": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
541 }
542 ],
543 "features": [],
544 "id": "bar 0.0.1 (path+file://[..]/foo)"
545 },
546 {
547 "dependencies": [],
548 "deps": [],
549 "features": [],
550 "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
551 }
552 ],
553 "root": "bar 0.0.1 (path+file://[..]/foo)"
554 },
555 "target_directory": "[..]/foo/target",
556 "version": 1,
557 "workspace_members": [
558 "bar 0.0.1 (path+file://[..]/foo)"
559 ],
866d4316
BC
560 "workspace_root": "[..]/foo",
561 "metadata": null
f299d1c1
AH
562}"#,
563 )
564 .run();
565
566 p.cargo("update -p serde --precise 0.2.0")
567 .with_stderr(
568 "\
569[UPDATING] `[..]` index
570",
571 )
572 .run();
f299d1c1
AH
573}
574
0e0d9688 575#[cargo_test]
851e20b2
DW
576fn preserve_top_comment() {
577 let p = project().file("src/lib.rs", "").build();
578
579 p.cargo("update").run();
580
bd0e4a08
DW
581 let lockfile = p.read_lockfile();
582 assert!(lockfile.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n"));
583
584 let mut lines = lockfile.lines().collect::<Vec<_>>();
585 lines.insert(2, "# some other comment");
586 let mut lockfile = lines.join("\n");
7dd9872c 587 lockfile.push_str("\n"); // .lines/.join loses the last newline
851e20b2
DW
588 println!("saving Cargo.lock contents:\n{}", lockfile);
589
590 p.change_file("Cargo.lock", &lockfile);
591
592 p.cargo("update").run();
593
bd0e4a08 594 let lockfile2 = p.read_lockfile();
851e20b2
DW
595 println!("loaded Cargo.lock contents:\n{}", lockfile2);
596
bd0e4a08 597 assert_eq!(lockfile, lockfile2);
851e20b2 598}
7be09e3c 599
0e0d9688 600#[cargo_test]
7be09e3c
AK
601fn dry_run_update() {
602 Package::new("log", "0.1.0").publish();
603 Package::new("serde", "0.1.0").dep("log", "0.1").publish();
604
605 let p = project()
606 .file(
607 "Cargo.toml",
608 r#"
609 [package]
610 name = "bar"
611 version = "0.0.1"
612 authors = []
613
614 [dependencies]
615 serde = "0.1"
616 log = "0.1"
617 foo = { path = "foo" }
618 "#,
619 )
620 .file("src/lib.rs", "")
621 .file(
622 "foo/Cargo.toml",
623 r#"
624 [package]
625 name = "foo"
626 version = "0.0.1"
627 authors = []
628
629 [dependencies]
630 serde = "0.1"
631 "#,
632 )
633 .file("foo/src/lib.rs", "")
634 .build();
635
636 p.cargo("build").run();
4ae79d2f 637 let old_lockfile = p.read_lockfile();
7be09e3c
AK
638
639 Package::new("log", "0.1.1").publish();
640 Package::new("serde", "0.1.1").dep("log", "0.1").publish();
641
642 p.cargo("update -p serde --dry-run")
643 .with_stderr(
644 "\
645[UPDATING] `[..]` index
646[UPDATING] serde v0.1.0 -> v0.1.1
647[WARNING] not updating lockfile due to dry run
648",
649 )
650 .run();
4ae79d2f 651 let new_lockfile = p.read_lockfile();
7be09e3c
AK
652 assert_eq!(old_lockfile, new_lockfile)
653}
9589d2cb
CO
654
655#[cargo_test]
656fn workspace_only() {
657 let p = project().file("src/main.rs", "fn main() {}").build();
658 p.cargo("generate-lockfile").run();
659 let lock1 = p.read_lockfile();
660
661 p.change_file(
662 "Cargo.toml",
663 r#"
664 [package]
665 name = "foo"
666 authors = []
667 version = "0.0.2"
668 "#,
669 );
670 p.cargo("update --workspace").run();
671 let lock2 = p.read_lockfile();
672
673 assert_ne!(lock1, lock2);
674 assert!(lock1.contains("0.0.1"));
675 assert!(lock2.contains("0.0.2"));
676 assert!(!lock1.contains("0.0.2"));
677 assert!(!lock2.contains("0.0.1"));
678}