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