]> git.proxmox.com Git - cargo.git/blame - tests/testsuite/update.rs
Don't rely on a thread local to uniquely create test roots
[cargo.git] / tests / testsuite / update.rs
CommitLineData
1408eecc 1use std::fs::File;
51d23560
AC
2use std::io::prelude::*;
3
04ddd4d0
DW
4use crate::support::registry::Package;
5use crate::support::{basic_manifest, project};
51d23560
AC
6
7#[test]
8fn minor_update_two_places() {
9 Package::new("log", "0.1.0").publish();
7fe2fbc8 10 let p = project()
51d23560
AC
11 .file(
12 "Cargo.toml",
13 r#"
14 [package]
15 name = "bar"
16 version = "0.0.1"
17 authors = []
18
19 [dependencies]
20 log = "0.1"
21 foo = { path = "foo" }
22 "#,
fecb7246
AC
23 )
24 .file("src/lib.rs", "")
51d23560
AC
25 .file(
26 "foo/Cargo.toml",
27 r#"
28 [package]
29 name = "foo"
30 version = "0.0.1"
31 authors = []
32
33 [dependencies]
34 log = "0.1"
35 "#,
fecb7246
AC
36 )
37 .file("foo/src/lib.rs", "")
51d23560
AC
38 .build();
39
85984a87 40 p.cargo("build").run();
51d23560
AC
41 Package::new("log", "0.1.1").publish();
42
43 File::create(p.root().join("foo/Cargo.toml"))
44 .unwrap()
45 .write_all(
46 br#"
47 [package]
48 name = "foo"
49 version = "0.0.1"
50 authors = []
51
52 [dependencies]
53 log = "0.1.1"
54 "#,
fecb7246
AC
55 )
56 .unwrap();
51d23560 57
85984a87 58 p.cargo("build").run();
51d23560
AC
59}
60
61#[test]
62fn transitive_minor_update() {
63 Package::new("log", "0.1.0").publish();
64 Package::new("serde", "0.1.0").dep("log", "0.1").publish();
65
7fe2fbc8 66 let p = project()
51d23560
AC
67 .file(
68 "Cargo.toml",
69 r#"
70 [package]
71 name = "bar"
72 version = "0.0.1"
73 authors = []
74
75 [dependencies]
76 serde = "0.1"
77 log = "0.1"
78 foo = { path = "foo" }
79 "#,
fecb7246
AC
80 )
81 .file("src/lib.rs", "")
51d23560
AC
82 .file(
83 "foo/Cargo.toml",
84 r#"
85 [package]
86 name = "foo"
87 version = "0.0.1"
88 authors = []
89
90 [dependencies]
91 serde = "0.1"
92 "#,
fecb7246
AC
93 )
94 .file("foo/src/lib.rs", "")
51d23560
AC
95 .build();
96
85984a87 97 p.cargo("build").run();
51d23560
AC
98
99 Package::new("log", "0.1.1").publish();
100 Package::new("serde", "0.1.1").dep("log", "0.1.1").publish();
101
102 // Note that `serde` isn't actually updated here! The default behavior for
103 // `update` right now is to as conservatively as possible attempt to satisfy
104 // an update. In this case we previously locked the dependency graph to `log
105 // 0.1.0`, but nothing on the command line says we're allowed to update
106 // that. As a result the update of `serde` here shouldn't update to `serde
107 // 0.1.1` as that would also force an update to `log 0.1.1`.
108 //
109 // Also note that this is probably counterintuitive and weird. We may wish
110 // to change this one day.
85984a87
DW
111 p.cargo("update -p serde")
112 .with_stderr(
51d23560 113 "\
41aa6fba 114[UPDATING] `[..]` index
51d23560 115",
fecb7246
AC
116 )
117 .run();
51d23560
AC
118}
119
120#[test]
121fn conservative() {
122 Package::new("log", "0.1.0").publish();
123 Package::new("serde", "0.1.0").dep("log", "0.1").publish();
124
7fe2fbc8 125 let p = project()
51d23560
AC
126 .file(
127 "Cargo.toml",
128 r#"
129 [package]
130 name = "bar"
131 version = "0.0.1"
132 authors = []
133
134 [dependencies]
135 serde = "0.1"
136 log = "0.1"
137 foo = { path = "foo" }
138 "#,
fecb7246
AC
139 )
140 .file("src/lib.rs", "")
51d23560
AC
141 .file(
142 "foo/Cargo.toml",
143 r#"
144 [package]
145 name = "foo"
146 version = "0.0.1"
147 authors = []
148
149 [dependencies]
150 serde = "0.1"
151 "#,
fecb7246
AC
152 )
153 .file("foo/src/lib.rs", "")
51d23560
AC
154 .build();
155
85984a87 156 p.cargo("build").run();
51d23560
AC
157
158 Package::new("log", "0.1.1").publish();
159 Package::new("serde", "0.1.1").dep("log", "0.1").publish();
160
85984a87
DW
161 p.cargo("update -p serde")
162 .with_stderr(
51d23560 163 "\
41aa6fba 164[UPDATING] `[..]` index
51d23560
AC
165[UPDATING] serde v0.1.0 -> v0.1.1
166",
fecb7246
AC
167 )
168 .run();
51d23560
AC
169}
170
171#[test]
172fn update_via_new_dep() {
173 Package::new("log", "0.1.0").publish();
7fe2fbc8 174 let p = project()
51d23560
AC
175 .file(
176 "Cargo.toml",
177 r#"
178 [package]
179 name = "bar"
180 version = "0.0.1"
181 authors = []
182
183 [dependencies]
184 log = "0.1"
185 # foo = { path = "foo" }
186 "#,
fecb7246
AC
187 )
188 .file("src/lib.rs", "")
51d23560
AC
189 .file(
190 "foo/Cargo.toml",
191 r#"
192 [package]
193 name = "foo"
194 version = "0.0.1"
195 authors = []
196
197 [dependencies]
198 log = "0.1.1"
199 "#,
fecb7246
AC
200 )
201 .file("foo/src/lib.rs", "")
51d23560
AC
202 .build();
203
85984a87 204 p.cargo("build").run();
51d23560
AC
205 Package::new("log", "0.1.1").publish();
206
207 p.uncomment_root_manifest();
782266aa 208 p.cargo("build").env("CARGO_LOG", "cargo=trace").run();
51d23560
AC
209}
210
211#[test]
212fn update_via_new_member() {
213 Package::new("log", "0.1.0").publish();
7fe2fbc8 214 let p = project()
51d23560
AC
215 .file(
216 "Cargo.toml",
217 r#"
218 [package]
219 name = "bar"
220 version = "0.0.1"
221 authors = []
222
223 [workspace]
224 # members = [ "foo" ]
225
226 [dependencies]
227 log = "0.1"
228 "#,
fecb7246
AC
229 )
230 .file("src/lib.rs", "")
51d23560
AC
231 .file(
232 "foo/Cargo.toml",
233 r#"
234 [package]
235 name = "foo"
236 version = "0.0.1"
237 authors = []
238
239 [dependencies]
240 log = "0.1.1"
241 "#,
fecb7246
AC
242 )
243 .file("foo/src/lib.rs", "")
51d23560
AC
244 .build();
245
85984a87 246 p.cargo("build").run();
51d23560
AC
247 Package::new("log", "0.1.1").publish();
248
249 p.uncomment_root_manifest();
85984a87 250 p.cargo("build").run();
51d23560
AC
251}
252
253#[test]
254fn add_dep_deep_new_requirement() {
255 Package::new("log", "0.1.0").publish();
7fe2fbc8 256 let p = project()
51d23560
AC
257 .file(
258 "Cargo.toml",
259 r#"
260 [package]
261 name = "bar"
262 version = "0.0.1"
263 authors = []
264
265 [dependencies]
266 log = "0.1"
267 # bar = "0.1"
268 "#,
fecb7246
AC
269 )
270 .file("src/lib.rs", "")
51d23560
AC
271 .build();
272
85984a87 273 p.cargo("build").run();
51d23560
AC
274
275 Package::new("log", "0.1.1").publish();
276 Package::new("bar", "0.1.0").dep("log", "0.1.1").publish();
277
278 p.uncomment_root_manifest();
85984a87 279 p.cargo("build").run();
51d23560
AC
280}
281
282#[test]
283fn everything_real_deep() {
284 Package::new("log", "0.1.0").publish();
285 Package::new("foo", "0.1.0").dep("log", "0.1").publish();
7fe2fbc8 286 let p = project()
51d23560
AC
287 .file(
288 "Cargo.toml",
289 r#"
290 [package]
291 name = "bar"
292 version = "0.0.1"
293 authors = []
294
295 [dependencies]
296 foo = "0.1"
297 # bar = "0.1"
298 "#,
fecb7246
AC
299 )
300 .file("src/lib.rs", "")
51d23560
AC
301 .build();
302
85984a87 303 p.cargo("build").run();
51d23560
AC
304
305 Package::new("log", "0.1.1").publish();
306 Package::new("bar", "0.1.0").dep("log", "0.1.1").publish();
307
308 p.uncomment_root_manifest();
85984a87 309 p.cargo("build").run();
51d23560 310}
0deaae9e
AC
311
312#[test]
313fn change_package_version() {
7fe2fbc8 314 let p = project()
0deaae9e
AC
315 .file(
316 "Cargo.toml",
317 r#"
318 [package]
319 name = "a-foo"
320 version = "0.2.0-alpha"
321 authors = []
322
323 [dependencies]
324 bar = { path = "bar", version = "0.2.0-alpha" }
325 "#,
fecb7246
AC
326 )
327 .file("src/lib.rs", "")
ab19c483 328 .file("bar/Cargo.toml", &basic_manifest("bar", "0.2.0-alpha"))
0deaae9e
AC
329 .file("bar/src/lib.rs", "")
330 .file(
331 "Cargo.lock",
332 r#"
333 [[package]]
334 name = "foo"
335 version = "0.2.0"
336 dependencies = ["bar 0.2.0"]
337
338 [[package]]
339 name = "bar"
340 version = "0.2.0"
341 "#,
fecb7246
AC
342 )
343 .build();
0deaae9e 344
85984a87 345 p.cargo("build").run();
0deaae9e 346}
1a26e86c 347
348#[test]
349fn update_precise() {
350 Package::new("log", "0.1.0").publish();
351 Package::new("serde", "0.1.0").publish();
352 Package::new("serde", "0.2.1").publish();
353
7fe2fbc8 354 let p = project()
1a26e86c 355 .file(
356 "Cargo.toml",
357 r#"
358 [package]
359 name = "bar"
360 version = "0.0.1"
361 authors = []
362
363 [dependencies]
364 serde = "0.2"
365 foo = { path = "foo" }
366 "#,
fecb7246
AC
367 )
368 .file("src/lib.rs", "")
1a26e86c 369 .file(
370 "foo/Cargo.toml",
371 r#"
372 [package]
373 name = "foo"
374 version = "0.0.1"
375 authors = []
376
377 [dependencies]
378 serde = "0.1"
379 "#,
fecb7246
AC
380 )
381 .file("foo/src/lib.rs", "")
1a26e86c 382 .build();
383
85984a87 384 p.cargo("build").run();
1a26e86c 385
386 Package::new("serde", "0.2.0").publish();
387
85984a87
DW
388 p.cargo("update -p serde:0.2.1 --precise 0.2.0")
389 .with_stderr(
1a26e86c 390 "\
41aa6fba 391[UPDATING] `[..]` index
1a26e86c 392[UPDATING] serde v0.2.1 -> v0.2.0
393",
fecb7246
AC
394 )
395 .run();
1a26e86c 396}
851e20b2 397
f299d1c1
AH
398// cargo update should respect its arguments even without a lockfile.
399// See issue "Running cargo update without a Cargo.lock ignores arguments"
400// at <https://github.com/rust-lang/cargo/issues/6872>.
401#[test]
402fn update_precise_first_run() {
403 Package::new("serde", "0.1.0").publish();
404 Package::new("serde", "0.2.0").publish();
405 Package::new("serde", "0.2.1").publish();
406
407 let p = project()
408 .file(
409 "Cargo.toml",
410 r#"
411 [package]
412 name = "bar"
413 version = "0.0.1"
414
415 [dependencies]
416 serde = "0.2"
417 "#,
418 )
419 .file("src/lib.rs", "")
420 .build();
421
422 p.cargo("update -p serde --precise 0.2.0")
423 .with_stderr(
424 "\
425[UPDATING] `[..]` index
1bcf7f11 426[UPDATING] serde v0.2.1 -> v0.2.0
f299d1c1
AH
427",
428 )
429 .run();
430
431 // Assert `cargo metadata` shows serde 0.2.0
432 p.cargo("metadata")
433 .with_json(
434 r#"{
435 "packages": [
436 {
437 "authors": [],
438 "categories": [],
439 "dependencies": [],
440 "description": null,
441 "edition": "2015",
442 "features": {},
443 "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
444 "keywords": [],
445 "license": null,
446 "license_file": null,
447 "links": null,
448 "manifest_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/Cargo.toml",
449 "metadata": null,
450 "name": "serde",
451 "readme": null,
452 "repository": null,
453 "source": "registry+https://github.com/rust-lang/crates.io-index",
454 "targets": [
455 {
456 "crate_types": [
457 "lib"
458 ],
e1d433d3 459 "doctest": true,
f299d1c1
AH
460 "edition": "2015",
461 "kind": [
462 "lib"
463 ],
464 "name": "serde",
465 "src_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/src/lib.rs"
466 }
467 ],
468 "version": "0.2.0"
469 },
470 {
471 "authors": [],
472 "categories": [],
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 ],
487 "description": null,
488 "edition": "2015",
489 "features": {},
490 "id": "bar 0.0.1 (path+file://[..]/foo)",
491 "keywords": [],
492 "license": null,
493 "license_file": null,
494 "links": null,
495 "manifest_path": "[..]/foo/Cargo.toml",
496 "metadata": null,
497 "name": "bar",
498 "readme": null,
499 "repository": null,
500 "source": null,
501 "targets": [
502 {
503 "crate_types": [
504 "lib"
505 ],
e1d433d3 506 "doctest": true,
f299d1c1
AH
507 "edition": "2015",
508 "kind": [
509 "lib"
510 ],
511 "name": "bar",
512 "src_path": "[..]/foo/src/lib.rs"
513 }
514 ],
515 "version": "0.0.1"
516 }
517 ],
518 "resolve": {
519 "nodes": [
520 {
521 "dependencies": [
522 "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
523 ],
524 "deps": [
525 {
526 "name": "serde",
527 "pkg": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
528 }
529 ],
530 "features": [],
531 "id": "bar 0.0.1 (path+file://[..]/foo)"
532 },
533 {
534 "dependencies": [],
535 "deps": [],
536 "features": [],
537 "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
538 }
539 ],
540 "root": "bar 0.0.1 (path+file://[..]/foo)"
541 },
542 "target_directory": "[..]/foo/target",
543 "version": 1,
544 "workspace_members": [
545 "bar 0.0.1 (path+file://[..]/foo)"
546 ],
547 "workspace_root": "[..]/foo"
548}"#,
549 )
550 .run();
551
552 p.cargo("update -p serde --precise 0.2.0")
553 .with_stderr(
554 "\
555[UPDATING] `[..]` index
556",
557 )
558 .run();
f299d1c1
AH
559}
560
851e20b2
DW
561#[test]
562fn preserve_top_comment() {
563 let p = project().file("src/lib.rs", "").build();
564
565 p.cargo("update").run();
566
bd0e4a08
DW
567 let lockfile = p.read_lockfile();
568 assert!(lockfile.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n"));
569
570 let mut lines = lockfile.lines().collect::<Vec<_>>();
571 lines.insert(2, "# some other comment");
572 let mut lockfile = lines.join("\n");
573 lockfile.push_str("\n"); // .lines/.join loses the last newline
851e20b2
DW
574 println!("saving Cargo.lock contents:\n{}", lockfile);
575
576 p.change_file("Cargo.lock", &lockfile);
577
578 p.cargo("update").run();
579
bd0e4a08 580 let lockfile2 = p.read_lockfile();
851e20b2
DW
581 println!("loaded Cargo.lock contents:\n{}", lockfile2);
582
bd0e4a08 583 assert_eq!(lockfile, lockfile2);
851e20b2 584}
7be09e3c
AK
585
586#[test]
587fn dry_run_update() {
588 Package::new("log", "0.1.0").publish();
589 Package::new("serde", "0.1.0").dep("log", "0.1").publish();
590
591 let p = project()
592 .file(
593 "Cargo.toml",
594 r#"
595 [package]
596 name = "bar"
597 version = "0.0.1"
598 authors = []
599
600 [dependencies]
601 serde = "0.1"
602 log = "0.1"
603 foo = { path = "foo" }
604 "#,
605 )
606 .file("src/lib.rs", "")
607 .file(
608 "foo/Cargo.toml",
609 r#"
610 [package]
611 name = "foo"
612 version = "0.0.1"
613 authors = []
614
615 [dependencies]
616 serde = "0.1"
617 "#,
618 )
619 .file("foo/src/lib.rs", "")
620 .build();
621
622 p.cargo("build").run();
623 let old_lockfile = p.read_file("Cargo.lock");
624
625 Package::new("log", "0.1.1").publish();
626 Package::new("serde", "0.1.1").dep("log", "0.1").publish();
627
628 p.cargo("update -p serde --dry-run")
629 .with_stderr(
630 "\
631[UPDATING] `[..]` index
632[UPDATING] serde v0.1.0 -> v0.1.1
633[WARNING] not updating lockfile due to dry run
634",
635 )
636 .run();
637 let new_lockfile = p.read_file("Cargo.lock");
638 assert_eq!(old_lockfile, new_lockfile)
639}