2 use std
::collections
::HashMap
;
3 use std
::fs
::{self, File}
;
4 use std
::io
::prelude
::*;
7 use crate::support
::cargo_process
;
8 use crate::support
::git
;
9 use crate::support
::paths
;
10 use crate::support
::registry
::{cksum, Package}
;
11 use crate::support
::{basic_manifest, project, ProjectBuilder}
;
14 let root
= paths
::root();
15 t
!(fs
::create_dir(&root
.join(".cargo")));
16 t
!(t
!(File
::create(root
.join(".cargo/config"))).write_all(
19 replace-with = 'my-awesome-local-registry'
21 [source.my-awesome-local-registry]
27 struct VendorPackage
{
28 p
: Option
<ProjectBuilder
>,
34 package
: Option
<String
>,
35 files
: HashMap
<String
, String
>,
39 fn new(name
: &str) -> VendorPackage
{
41 p
: Some(project().at(&format
!("index/{}", name
))),
43 package
: Some(String
::new()),
44 files
: HashMap
::new(),
49 fn file(&mut self, name
: &str, contents
: &str) -> &mut VendorPackage
{
50 self.p
= Some(self.p
.take().unwrap().file(name
, contents
));
53 .insert(name
.to_string(), cksum(contents
.as_bytes()));
57 fn disable_checksum(&mut self) -> &mut VendorPackage
{
58 self.cksum
.package
= None
;
62 fn no_manifest(mut self) -> Self {
63 self.p
= self.p
.map(|pb
| pb
.no_manifest());
68 let p
= self.p
.take().unwrap();
69 let json
= serde_json
::to_string(&self.cksum
).unwrap();
70 let p
= p
.file(".cargo-checksum.json", &json
);
79 VendorPackage
::new("bar")
80 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
81 .file("src/lib.rs", "pub fn bar() {}")
99 "extern crate bar; pub fn foo() { bar::bar(); }",
106 [COMPILING] bar v0.1.0
107 [COMPILING] foo v0.1.0 ([CWD])
115 fn simple_install() {
118 VendorPackage
::new("foo")
119 .file("src/lib.rs", "pub fn foo() {}")
122 VendorPackage
::new("bar")
137 "extern crate foo; pub fn main() { foo::foo(); }",
141 cargo_process("install bar")
143 " Installing bar v0.1.0
146 Finished release [optimized] target(s) in [..]s
147 Installing [..]bar[..]
148 warning: be sure to add `[..]` to your PATH to be able to run the installed binaries
155 fn simple_install_fail() {
158 VendorPackage
::new("foo")
159 .file("src/lib.rs", "pub fn foo() {}")
162 VendorPackage
::new("bar")
178 "extern crate foo; pub fn main() { foo::foo(); }",
182 cargo_process("install bar")
185 " Installing bar v0.1.0
186 error: failed to compile `bar v0.1.0`, intermediate artifacts can be found at `[..]`
189 no matching package named `baz` found
190 location searched: registry `https://github.com/rust-lang/crates.io-index`
191 did you mean: bar, foo
192 required by package `bar v0.1.0`
199 fn install_without_feature_dep() {
202 VendorPackage
::new("foo")
203 .file("src/lib.rs", "pub fn foo() {}")
206 VendorPackage
::new("bar")
217 baz = { version = "9.8.7", optional = true }
225 "extern crate foo; pub fn main() { foo::foo(); }",
229 cargo_process("install bar")
231 " Installing bar v0.1.0
234 Finished release [optimized] target(s) in [..]s
235 Installing [..]bar[..]
236 warning: be sure to add `[..]` to your PATH to be able to run the installed binaries
246 let _
= project().at("index").build();
263 "extern crate bar; pub fn foo() { bar::bar(); }",
271 error: no matching package named `bar` found
272 location searched: [..]
273 required by package `foo v0.1.0 ([..])`
283 VendorPackage
::new("bar-0.1.0")
284 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
285 .file("src/lib.rs", "pub fn bar() {}")
286 .file(".cargo-checksum", "")
289 VendorPackage
::new("bar-0.2.0")
290 .file("Cargo.toml", &basic_manifest("bar", "0.2.0"))
291 .file("src/lib.rs", "pub fn bar() {}")
292 .file(".cargo-checksum", "")
310 "extern crate bar; pub fn foo() { bar::bar(); }",
317 [COMPILING] bar v0.1.0
318 [COMPILING] foo v0.1.0 ([CWD])
326 fn crates_io_then_directory() {
342 "extern crate bar; pub fn foo() { bar::bar(); }",
346 let cksum
= Package
::new("bar", "0.1.0")
347 .file("src/lib.rs", "pub fn bar() -> u32 { 0 }")
353 [UPDATING] `[..]` index
354 [DOWNLOADING] crates ...
355 [DOWNLOADED] bar v0.1.0 ([..])
356 [COMPILING] bar v0.1.0
357 [COMPILING] foo v0.1.0 ([CWD])
365 let mut v
= VendorPackage
::new("bar");
366 v
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"));
367 v
.file("src/lib.rs", "pub fn bar() -> u32 { 1 }");
368 v
.cksum
.package
= Some(cksum
);
374 [COMPILING] bar v0.1.0
375 [COMPILING] foo v0.1.0 ([CWD])
383 fn crates_io_then_bad_checksum() {
397 .file("src/lib.rs", "")
400 Package
::new("bar", "0.1.0").publish();
402 p
.cargo("build").run();
405 VendorPackage
::new("bar")
406 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
407 .file("src/lib.rs", "")
414 error: checksum for `bar v0.1.0` changed between lock files
416 this could be indicative of a few possible errors:
418 * the lock file is corrupt
419 * a replacement source in use (e.g. a mirror) returned a different checksum
420 * the source itself may be corrupt in one way or another
422 unable to verify that `bar v0.1.0` is the same as when the lockfile was generated
430 fn bad_file_checksum() {
433 VendorPackage
::new("bar")
434 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
435 .file("src/lib.rs", "")
438 let mut f
= t
!(File
::create(paths
::root().join("index/bar/src/lib.rs")));
439 t
!(f
.write_all(b
"fn bar() -> u32 { 0 }"));
454 .file("src/lib.rs", "")
461 error: the listed checksum of `[..]lib.rs` has changed:
465 directory sources are not intended to be edited, if modifications are \
466 required then it is recommended that [replace] is used with a forked copy of \
474 fn only_dot_files_ok() {
477 VendorPackage
::new("bar")
478 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
479 .file("src/lib.rs", "")
481 VendorPackage
::new("foo")
499 .file("src/lib.rs", "")
502 p
.cargo("build").run();
506 fn random_files_ok() {
509 VendorPackage
::new("bar")
510 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
511 .file("src/lib.rs", "")
513 VendorPackage
::new("foo")
532 .file("src/lib.rs", "")
535 p
.cargo("build").run();
539 fn git_lock_file_doesnt_change() {
540 let git
= git
::new("git", |p
| {
541 p
.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
542 .file("src/lib.rs", "")
546 VendorPackage
::new("git")
547 .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
548 .file("src/lib.rs", "")
563 git = {{ git = '{0}' }}
568 .file("src/lib.rs", "")
571 p
.cargo("build").run();
573 let mut lock1
= String
::new();
574 t
!(t
!(File
::open(p
.root().join("Cargo.lock"))).read_to_string(&mut lock1
));
576 let root
= paths
::root();
577 t
!(fs
::create_dir(&root
.join(".cargo")));
578 t
!(t
!(File
::create(root
.join(".cargo/config"))).write_all(
583 replace-with = 'my-awesome-local-registry'
585 [source.my-awesome-local-registry]
603 let mut lock2
= String
::new();
604 t
!(t
!(File
::open(p
.root().join("Cargo.lock"))).read_to_string(&mut lock2
));
605 assert_eq
!(lock1
, lock2
, "lock files changed");
609 fn git_override_requires_lockfile() {
610 VendorPackage
::new("git")
611 .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
612 .file("src/lib.rs", "")
626 git = { git = 'https://example.com/' }
629 .file("src/lib.rs", "")
632 let root
= paths
::root();
633 t
!(fs
::create_dir(&root
.join(".cargo")));
634 t
!(t
!(File
::create(root
.join(".cargo/config"))).write_all(
637 git = 'https://example.com/'
638 replace-with = 'my-awesome-local-registry'
640 [source.my-awesome-local-registry]
649 error: failed to load source for a dependency on `git`
652 Unable to update [..]
655 the source my-git-repo requires a lock file to be present first before it can be
656 used against vendored source code
658 remove the source replacement configuration, generate a lock file, and then
659 restore the source replacement configuration to continue the build
667 fn workspace_different_locations() {
681 .file("foo/src/lib.rs", "")
682 .file("foo/vendor/baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
683 .file("foo/vendor/baz/src/lib.rs", "")
684 .file("foo/vendor/baz/.cargo-checksum.json", "{\"files\":{}}")
696 .file("bar/src/lib.rs", "")
701 target-dir = './target'
704 replace-with = 'my-awesome-local-registry'
706 [source.my-awesome-local-registry]
707 directory = 'foo/vendor'
712 p
.cargo("build").cwd(p
.root().join("foo")).run();
714 .cwd(p
.root().join("bar"))
726 fn version_missing() {
729 VendorPackage
::new("foo")
730 .file("src/lib.rs", "pub fn foo() {}")
733 VendorPackage
::new("bar")
746 .file("src/main.rs", "fn main() {}")
749 cargo_process("install bar")
752 [INSTALLING] bar v0.1.0
753 error: failed to compile [..]
756 failed to select a version for the requirement `foo = \"^2\"`
757 candidate versions found which didn't match: 0.0.1
758 location searched: directory source `[..] (which is replacing registry `[..]`)
759 required by package `bar v0.1.0`
760 perhaps a crate was updated and forgotten to be re-vendored?