1 //! Tests for directory sources.
3 use std
::collections
::HashMap
;
9 use cargo_test_support
::cargo_process
;
10 use cargo_test_support
::git
;
11 use cargo_test_support
::paths
;
12 use cargo_test_support
::registry
::{cksum, Package}
;
13 use cargo_test_support
::{basic_manifest, project, t, ProjectBuilder}
;
16 let root
= paths
::root();
17 t
!(fs
::create_dir(&root
.join(".cargo")));
19 root
.join(".cargo/config"),
22 replace-with = 'my-awesome-local-registry'
24 [source.my-awesome-local-registry]
30 struct VendorPackage
{
31 p
: Option
<ProjectBuilder
>,
37 package
: Option
<String
>,
38 files
: HashMap
<String
, String
>,
42 fn new(name
: &str) -> VendorPackage
{
44 p
: Some(project().at(&format
!("index/{}", name
))),
46 package
: Some(String
::new()),
47 files
: HashMap
::new(),
52 fn file(&mut self, name
: &str, contents
: &str) -> &mut VendorPackage
{
53 self.p
= Some(self.p
.take().unwrap().file(name
, contents
));
56 .insert(name
.to_string(), cksum(contents
.as_bytes()));
60 fn disable_checksum(&mut self) -> &mut VendorPackage
{
61 self.cksum
.package
= None
;
65 fn no_manifest(mut self) -> Self {
66 self.p
= self.p
.map(|pb
| pb
.no_manifest());
71 let p
= self.p
.take().unwrap();
72 let json
= serde_json
::to_string(&self.cksum
).unwrap();
73 let p
= p
.file(".cargo-checksum.json", &json
);
82 VendorPackage
::new("bar")
83 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
84 .file("src/lib.rs", "pub fn bar() {}")
102 "extern crate bar; pub fn foo() { bar::bar(); }",
109 [COMPILING] bar v0.1.0
110 [COMPILING] foo v0.1.0 ([CWD])
118 fn simple_install() {
121 VendorPackage
::new("foo")
122 .file("src/lib.rs", "pub fn foo() {}")
125 VendorPackage
::new("bar")
140 "extern crate foo; pub fn main() { foo::foo(); }",
144 cargo_process("install bar")
147 [INSTALLING] bar v0.1.0
148 [COMPILING] foo v0.0.1
149 [COMPILING] bar v0.1.0
150 [FINISHED] release [optimized] target(s) in [..]s
151 [INSTALLING] [..]bar[..]
152 [INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
153 [WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
160 fn simple_install_fail() {
163 VendorPackage
::new("foo")
164 .file("src/lib.rs", "pub fn foo() {}")
167 VendorPackage
::new("bar")
183 "extern crate foo; pub fn main() { foo::foo(); }",
187 cargo_process("install bar")
190 " Installing bar v0.1.0
191 error: failed to compile `bar v0.1.0`, intermediate artifacts can be found at `[..]`
194 no matching package named `baz` found
195 location searched: registry `https://github.com/rust-lang/crates.io-index`
196 perhaps you meant: bar or foo
197 required by package `bar v0.1.0`
204 fn install_without_feature_dep() {
207 VendorPackage
::new("foo")
208 .file("src/lib.rs", "pub fn foo() {}")
211 VendorPackage
::new("bar")
222 baz = { version = "9.8.7", optional = true }
230 "extern crate foo; pub fn main() { foo::foo(); }",
234 cargo_process("install bar")
237 [INSTALLING] bar v0.1.0
238 [COMPILING] foo v0.0.1
239 [COMPILING] bar v0.1.0
240 [FINISHED] release [optimized] target(s) in [..]s
241 [INSTALLING] [..]bar[..]
242 [INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
243 [WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
253 let _
= project().at("index").build();
270 "extern crate bar; pub fn foo() { bar::bar(); }",
278 error: no matching package named `bar` found
279 location searched: [..]
280 required by package `foo v0.1.0 ([..])`
290 VendorPackage
::new("bar-0.1.0")
291 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
292 .file("src/lib.rs", "pub fn bar() {}")
293 .file(".cargo-checksum", "")
296 VendorPackage
::new("bar-0.2.0")
297 .file("Cargo.toml", &basic_manifest("bar", "0.2.0"))
298 .file("src/lib.rs", "pub fn bar() {}")
299 .file(".cargo-checksum", "")
317 "extern crate bar; pub fn foo() { bar::bar(); }",
324 [COMPILING] bar v0.1.0
325 [COMPILING] foo v0.1.0 ([CWD])
333 fn crates_io_then_directory() {
349 "extern crate bar; pub fn foo() { bar::bar(); }",
353 let cksum
= Package
::new("bar", "0.1.0")
354 .file("src/lib.rs", "pub fn bar() -> u32 { 0 }")
360 [UPDATING] `[..]` index
361 [DOWNLOADING] crates ...
362 [DOWNLOADED] bar v0.1.0 ([..])
363 [COMPILING] bar v0.1.0
364 [COMPILING] foo v0.1.0 ([CWD])
372 let mut v
= VendorPackage
::new("bar");
373 v
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"));
374 v
.file("src/lib.rs", "pub fn bar() -> u32 { 1 }");
375 v
.cksum
.package
= Some(cksum
);
381 [COMPILING] bar v0.1.0
382 [COMPILING] foo v0.1.0 ([CWD])
390 fn crates_io_then_bad_checksum() {
404 .file("src/lib.rs", "")
407 Package
::new("bar", "0.1.0").publish();
409 p
.cargo("build").run();
412 VendorPackage
::new("bar")
413 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
414 .file("src/lib.rs", "")
421 error: checksum for `bar v0.1.0` changed between lock files
423 this could be indicative of a few possible errors:
425 * the lock file is corrupt
426 * a replacement source in use (e.g., a mirror) returned a different checksum
427 * the source itself may be corrupt in one way or another
429 unable to verify that `bar v0.1.0` is the same as when the lockfile was generated
437 fn bad_file_checksum() {
440 VendorPackage
::new("bar")
441 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
442 .file("src/lib.rs", "")
446 paths
::root().join("index/bar/src/lib.rs"),
447 "fn bar() -> u32 { 0 }"
463 .file("src/lib.rs", "")
470 error: the listed checksum of `[..]lib.rs` has changed:
474 directory sources are not intended to be edited, if modifications are \
475 required then it is recommended that [replace] is used with a forked copy of \
483 fn only_dot_files_ok() {
486 VendorPackage
::new("bar")
487 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
488 .file("src/lib.rs", "")
490 VendorPackage
::new("foo")
508 .file("src/lib.rs", "")
511 p
.cargo("build").run();
515 fn random_files_ok() {
518 VendorPackage
::new("bar")
519 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
520 .file("src/lib.rs", "")
522 VendorPackage
::new("foo")
541 .file("src/lib.rs", "")
544 p
.cargo("build").run();
548 fn git_lock_file_doesnt_change() {
549 let git
= git
::new("git", |p
| {
550 p
.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
551 .file("src/lib.rs", "")
554 VendorPackage
::new("git")
555 .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
556 .file("src/lib.rs", "")
571 git = {{ git = '{0}' }}
576 .file("src/lib.rs", "")
579 p
.cargo("build").run();
581 let lock1
= p
.read_lockfile();
583 let root
= paths
::root();
584 t
!(fs
::create_dir(&root
.join(".cargo")));
586 root
.join(".cargo/config"),
591 replace-with = 'my-awesome-local-registry'
593 [source.my-awesome-local-registry]
610 let lock2
= p
.read_lockfile();
611 assert_eq
!(lock1
, lock2
, "lock files changed");
615 fn git_override_requires_lockfile() {
616 VendorPackage
::new("git")
617 .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
618 .file("src/lib.rs", "")
632 git = { git = 'https://example.com/' }
635 .file("src/lib.rs", "")
638 let root
= paths
::root();
639 t
!(fs
::create_dir(&root
.join(".cargo")));
641 root
.join(".cargo/config"),
644 git = 'https://example.com/'
645 replace-with = 'my-awesome-local-registry'
647 [source.my-awesome-local-registry]
656 [ERROR] failed to get `git` as a dependency of package `foo v0.0.1 ([..])`
659 failed to load source for dependency `git`
662 Unable to update [..]
665 the source my-git-repo requires a lock file to be present first before it can be
666 used against vendored source code
668 remove the source replacement configuration, generate a lock file, and then
669 restore the source replacement configuration to continue the build
677 fn workspace_different_locations() {
691 .file("foo/src/lib.rs", "")
692 .file("foo/vendor/baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
693 .file("foo/vendor/baz/src/lib.rs", "")
694 .file("foo/vendor/baz/.cargo-checksum.json", "{\"files\":{}}")
706 .file("bar/src/lib.rs", "")
711 target-dir = './target'
714 replace-with = 'my-awesome-local-registry'
716 [source.my-awesome-local-registry]
717 directory = 'foo/vendor'
722 p
.cargo("build").cwd("foo").run();
735 fn version_missing() {
738 VendorPackage
::new("foo")
739 .file("src/lib.rs", "pub fn foo() {}")
742 VendorPackage
::new("bar")
755 .file("src/main.rs", "fn main() {}")
758 cargo_process("install bar")
761 [INSTALLING] bar v0.1.0
762 error: failed to compile [..]
765 failed to select a version for the requirement `foo = \"^2\"`
766 candidate versions found which didn't match: 0.0.1
767 location searched: directory source `[..] (which is replacing registry `[..]`)
768 required by package `bar v0.1.0`
769 perhaps a crate was updated and forgotten to be re-vendored?