1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
//! Tests for --offline flag.
use cargo_test_support::{
basic_manifest, git, main_file, path2url, project,
registry::{Package, RegistryBuilder},
Execs,
};
use std::fs;
#[cargo_test]
fn offline_unused_target_dep() {
// --offline with a target dependency that is not used and not downloaded.
Package::new("unused_dep", "1.0.0").publish();
Package::new("used_dep", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
used_dep = "1.0"
[target.'cfg(unused)'.dependencies]
unused_dep = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
// Do a build that downloads only what is necessary.
p.cargo("check")
.with_stderr_contains("[DOWNLOADED] used_dep [..]")
.with_stderr_does_not_contain("[DOWNLOADED] unused_dep [..]")
.run();
p.cargo("clean").run();
// Build offline, make sure it works.
p.cargo("check --offline").run();
}
#[cargo_test]
fn offline_missing_optional() {
Package::new("opt_dep", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
opt_dep = { version = "1.0", optional = true }
"#,
)
.file("src/lib.rs", "")
.build();
// Do a build that downloads only what is necessary.
p.cargo("check")
.with_stderr_does_not_contain("[DOWNLOADED] opt_dep [..]")
.run();
p.cargo("clean").run();
// Build offline, make sure it works.
p.cargo("check --offline").run();
p.cargo("check --offline --features=opt_dep")
.with_stderr(
"\
[ERROR] failed to download `opt_dep v1.0.0`
Caused by:
attempting to make an HTTP request, but --offline was specified
",
)
.with_status(101)
.run();
}
#[cargo_test]
fn cargo_compile_path_with_offline() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies.bar]
path = "bar"
"#,
)
.file("src/lib.rs", "")
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("bar/src/lib.rs", "")
.build();
p.cargo("check --offline").run();
}
#[cargo_test]
fn cargo_compile_with_downloaded_dependency_with_offline() {
Package::new("present_dep", "1.2.3")
.file("Cargo.toml", &basic_manifest("present_dep", "1.2.3"))
.file("src/lib.rs", "")
.publish();
// make package downloaded
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
present_dep = "1.2.3"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check").run();
let p2 = project()
.at("bar")
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.1.0"
[dependencies]
present_dep = "1.2.3"
"#,
)
.file("src/lib.rs", "")
.build();
p2.cargo("check --offline")
.with_stderr(
"\
[CHECKING] present_dep v1.2.3
[CHECKING] bar v0.1.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();
}
#[cargo_test]
fn cargo_compile_offline_not_try_update() {
// When --offline needs to download the registry, provide a reasonable
// error hint to run without --offline.
let p = project()
.at("bar")
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.1.0"
[dependencies]
not_cached_dep = "1.2.5"
"#,
)
.file("src/lib.rs", "")
.build();
let msg = "\
[ERROR] no matching package named `not_cached_dep` found
location searched: registry `crates-io`
required by package `bar v0.1.0 ([..]/bar)`
As a reminder, you're using offline mode (--offline) which can sometimes cause \
surprising resolution failures, if this error is too confusing you may wish to \
retry without the offline flag.
";
p.cargo("check --offline")
.with_status(101)
.with_stderr(msg)
.run();
// While we're here, also check the config works.
p.change_file(".cargo/config", "net.offline = true");
p.cargo("check").with_status(101).with_stderr(msg).run();
}
#[cargo_test]
fn compile_offline_without_maxvers_cached() {
Package::new("present_dep", "1.2.1").publish();
Package::new("present_dep", "1.2.2").publish();
Package::new("present_dep", "1.2.3")
.file("Cargo.toml", &basic_manifest("present_dep", "1.2.3"))
.file(
"src/lib.rs",
r#"pub fn get_version()->&'static str {"1.2.3"}"#,
)
.publish();
Package::new("present_dep", "1.2.5")
.file("Cargo.toml", &basic_manifest("present_dep", "1.2.5"))
.file("src/lib.rs", r#"pub fn get_version(){"1.2.5"}"#)
.publish();
// make package cached
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
present_dep = "=1.2.3"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("build").run();
let p2 = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
present_dep = "1.2"
"#,
)
.file(
"src/main.rs",
"\
extern crate present_dep;
fn main(){
println!(\"{}\", present_dep::get_version());
}",
)
.build();
p2.cargo("run --offline")
.with_stderr(
"\
[COMPILING] present_dep v1.2.3
[COMPILING] foo v0.1.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
Running `[..]`",
)
.with_stdout("1.2.3")
.run();
}
#[cargo_test]
fn cargo_compile_forbird_git_httpsrepo_offline() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.5.0"
authors = ["chabapok@example.com"]
[dependencies.dep1]
git = 'https://github.com/some_user/dep1.git'
"#,
)
.file("src/main.rs", "")
.build();
p.cargo("check --offline").with_status(101).with_stderr("\
[ERROR] failed to get `dep1` as a dependency of package `foo v0.5.0 [..]`
Caused by:
failed to load source for dependency `dep1`
Caused by:
Unable to update https://github.com/some_user/dep1.git
Caused by:
can't checkout from 'https://github.com/some_user/dep1.git': you are in the offline mode (--offline)").run();
}
#[cargo_test]
fn compile_offline_while_transitive_dep_not_cached() {
let baz = Package::new("baz", "1.0.0");
let baz_path = baz.archive_dst();
baz.publish();
let baz_content = fs::read(&baz_path).unwrap();
// Truncate the file to simulate a download failure.
fs::write(&baz_path, &[]).unwrap();
Package::new("bar", "0.1.0").dep("baz", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = "0.1.0"
"#,
)
.file("src/main.rs", "fn main(){}")
.build();
// simulate download bar, but fail to download baz
p.cargo("check")
.with_status(101)
.with_stderr_contains("[..]failed to verify the checksum of `baz[..]")
.run();
// Restore the file contents.
fs::write(&baz_path, &baz_content).unwrap();
p.cargo("check --offline")
.with_status(101)
.with_stderr(
"\
[ERROR] failed to download `bar v0.1.0`
Caused by:
attempting to make an HTTP request, but --offline was specified
",
)
.run();
}
fn update_offline_not_cached() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
bar = "*"
"#,
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("update --offline")
.with_status(101)
.with_stderr(
"\
[ERROR] no matching package named `bar` found
location searched: registry `[..]`
required by package `foo v0.0.1 ([..]/foo)`
As a reminder, you're using offline mode (--offline) which can sometimes cause \
surprising resolution failures, if this error is too confusing you may wish to \
retry without the offline flag.",
)
.run();
}
#[cargo_test]
fn update_offline_not_cached_sparse() {
let _registry = RegistryBuilder::new().http_index().build();
update_offline_not_cached()
}
#[cargo_test]
fn update_offline_not_cached_git() {
update_offline_not_cached()
}
#[cargo_test]
fn cargo_compile_offline_with_cached_git_dep() {
compile_offline_with_cached_git_dep(false)
}
#[cargo_test]
fn gitoxide_cargo_compile_offline_with_cached_git_dep_shallow_dep() {
compile_offline_with_cached_git_dep(true)
}
fn compile_offline_with_cached_git_dep(shallow: bool) {
let git_project = git::new("dep1", |project| {
project
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
.file(
"src/lib.rs",
r#"
pub static COOL_STR:&str = "cached git repo rev1";
"#,
)
});
let repo = git2::Repository::open(&git_project.root()).unwrap();
let rev1 = repo.revparse_single("HEAD").unwrap().id();
// Commit the changes and make sure we trigger a recompile
git_project.change_file(
"src/lib.rs",
r#"pub static COOL_STR:&str = "cached git repo rev2";"#,
);
git::add(&repo);
let rev2 = git::commit(&repo);
// cache to registry rev1 and rev2
let prj = project()
.at("cache_git_dep")
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "cache_git_dep"
version = "0.5.0"
[dependencies.dep1]
git = '{}'
rev = "{}"
"#,
git_project.url(),
rev1
),
)
.file("src/main.rs", "fn main(){}")
.build();
let maybe_use_shallow = |mut cargo: Execs| -> Execs {
if shallow {
cargo
.arg("-Zgitoxide=fetch,shallow-deps")
.masquerade_as_nightly_cargo(&[
"unstable features must be available for -Z gitoxide",
]);
}
cargo
};
maybe_use_shallow(prj.cargo("build")).run();
prj.change_file(
"Cargo.toml",
&format!(
r#"
[package]
name = "cache_git_dep"
version = "0.5.0"
[dependencies.dep1]
git = '{}'
rev = "{}"
"#,
git_project.url(),
rev2
),
);
maybe_use_shallow(prj.cargo("build")).run();
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
[dependencies.dep1]
git = '{}'
"#,
git_project.url()
),
)
.file(
"src/main.rs",
&main_file(r#""hello from {}", dep1::COOL_STR"#, &["dep1"]),
)
.build();
let git_root = git_project.root();
let mut cargo = p.cargo("build --offline");
cargo.with_stderr(format!(
"\
[COMPILING] dep1 v0.5.0 ({}#[..])
[COMPILING] foo v0.5.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
path2url(git_root),
));
maybe_use_shallow(cargo).run();
assert!(p.bin("foo").is_file());
p.process(&p.bin("foo"))
.with_stdout("hello from cached git repo rev2\n")
.run();
p.change_file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
[dependencies.dep1]
git = '{}'
rev = "{}"
"#,
git_project.url(),
rev1
),
);
maybe_use_shallow(p.cargo("build --offline")).run();
p.process(&p.bin("foo"))
.with_stdout("hello from cached git repo rev1\n")
.run();
}
#[cargo_test]
fn offline_resolve_optional_fail() {
// Example where resolve fails offline.
//
// This happens if at least 1 version of an optional dependency is
// available, but none of them satisfy the requirements. The current logic
// that handles this is `RegistryIndex::query_inner`, and it doesn't know
// if the package being queried is an optional one. This is not ideal, it
// would be best if it just ignored optional (unselected) dependencies.
Package::new("dep", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
dep = { version = "1.0", optional = true }
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("fetch").run();
// Change dep to 2.0.
p.change_file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
dep = { version = "2.0", optional = true }
"#,
);
p.cargo("check --offline")
.with_status(101)
.with_stderr(
"\
[ERROR] failed to select a version for the requirement `dep = \"^2.0\"`
candidate versions found which didn't match: 1.0.0
location searched: `[..]` index (which is replacing registry `crates-io`)
required by package `foo v0.1.0 ([..]/foo)`
perhaps a crate was updated and forgotten to be re-vendored?
As a reminder, you're using offline mode (--offline) which can sometimes cause \
surprising resolution failures, if this error is too confusing you may wish to \
retry without the offline flag.
",
)
.run();
}
#[cargo_test]
fn offline_with_all_patched() {
// Offline works if everything is patched.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
dep = "1.0"
[patch.crates-io]
dep = {path = "dep"}
"#,
)
.file("src/lib.rs", "pub fn f() { dep::foo(); }")
.file("dep/Cargo.toml", &basic_manifest("dep", "1.0.0"))
.file("dep/src/lib.rs", "pub fn foo() {}")
.build();
p.cargo("check --offline").run();
}
#[cargo_test]
fn update_offline_cached() {
// Cache a few versions to update against
let p = project().file("src/lib.rs", "").build();
let versions = ["1.2.3", "1.2.5", "1.2.9"];
for vers in versions.iter() {
Package::new("present_dep", vers)
.file("Cargo.toml", &basic_manifest("present_dep", vers))
.file(
"src/lib.rs",
format!(r#"pub fn get_version()->&'static str {{ "{}" }}"#, vers).as_str(),
)
.publish();
// make package cached
p.change_file(
"Cargo.toml",
format!(
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
present_dep = "={}"
"#,
vers
)
.as_str(),
);
p.cargo("build").run();
}
let p2 = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
present_dep = "1.2"
"#,
)
.file(
"src/main.rs",
"\
extern crate present_dep;
fn main(){
println!(\"{}\", present_dep::get_version());
}",
)
.build();
p2.cargo("build --offline")
.with_stderr(
"\
[COMPILING] present_dep v1.2.9
[COMPILING] foo v0.1.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
.run();
p2.rename_run("foo", "with_1_2_9")
.with_stdout("1.2.9")
.run();
// updates happen without updating the index
p2.cargo("update present_dep --precise 1.2.3 --offline")
.with_status(0)
.with_stderr(
"\
[DOWNGRADING] present_dep v1.2.9 -> v1.2.3
",
)
.run();
p2.cargo("build --offline")
.with_stderr(
"\
[COMPILING] present_dep v1.2.3
[COMPILING] foo v0.1.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
.run();
p2.rename_run("foo", "with_1_2_3")
.with_stdout("1.2.3")
.run();
// Offline update should only print package details and not index updating
p2.cargo("update --offline")
.with_status(0)
.with_stderr(
"\
[UPDATING] present_dep v1.2.3 -> v1.2.9
",
)
.run();
// No v1.2.8 loaded into the cache so expect failure.
p2.cargo("update present_dep --precise 1.2.8 --offline")
.with_status(101)
.with_stderr(
"\
[ERROR] no matching package named `present_dep` found
location searched: registry `[..]`
required by package `foo v0.1.0 ([..]/foo)`
As a reminder, you're using offline mode (--offline) which can sometimes cause \
surprising resolution failures, if this error is too confusing you may wish to \
retry without the offline flag.
",
)
.run();
}
#[cargo_test]
fn offline_and_frozen_and_no_lock() {
let p = project().file("src/lib.rs", "").build();
p.cargo("check --frozen --offline")
.with_status(101)
.with_stderr("\
error: the lock file [ROOT]/foo/Cargo.lock needs to be updated but --frozen was passed to prevent this
If you want to try to generate the lock file without accessing the network, \
remove the --frozen flag and use --offline instead.
")
.run();
}
#[cargo_test]
fn offline_and_locked_and_no_frozen() {
let p = project().file("src/lib.rs", "").build();
p.cargo("check --locked --offline")
.with_status(101)
.with_stderr("\
error: the lock file [ROOT]/foo/Cargo.lock needs to be updated but --locked was passed to prevent this
If you want to try to generate the lock file without accessing the network, \
remove the --locked flag and use --offline instead.
")
.run();
}
|