cargo/tests/testsuite/directory.rs

779 lines
17 KiB
Rust
Raw Normal View History

2019-11-25 02:42:45 +00:00
//! Tests for directory sources.
2016-07-05 17:28:51 +00:00
use std::collections::HashMap;
use std::fs;
2016-07-05 17:28:51 +00:00
use std::str;
use serde::Serialize;
use cargo_test_support::cargo_process;
use cargo_test_support::git;
use cargo_test_support::paths;
use cargo_test_support::registry::{cksum, Package};
2019-09-12 19:52:46 +00:00
use cargo_test_support::{basic_manifest, project, t, ProjectBuilder};
2016-07-05 17:28:51 +00:00
fn setup() {
let root = paths::root();
t!(fs::create_dir(&root.join(".cargo")));
t!(fs::write(
2024-01-26 19:40:46 +00:00
root.join(".cargo/config.toml"),
r#"
[source.crates-io]
replace-with = 'my-awesome-local-registry'
2016-07-05 17:28:51 +00:00
[source.my-awesome-local-registry]
directory = 'index'
"#
2018-03-14 15:17:44 +00:00
));
2016-07-05 17:28:51 +00:00
}
struct VendorPackage {
p: Option<ProjectBuilder>,
cksum: Checksum,
}
2017-06-08 20:08:59 +00:00
#[derive(Serialize)]
2016-07-05 17:28:51 +00:00
struct Checksum {
package: Option<String>,
2016-07-05 17:28:51 +00:00
files: HashMap<String, String>,
}
impl VendorPackage {
fn new(name: &str) -> VendorPackage {
VendorPackage {
p: Some(project().at(&format!("index/{}", name))),
2016-07-05 17:28:51 +00:00
cksum: Checksum {
package: Some(String::new()),
2016-07-05 17:28:51 +00:00
files: HashMap::new(),
},
}
}
fn file(&mut self, name: &str, contents: &str) -> &mut VendorPackage {
self.p = Some(self.p.take().unwrap().file(name, contents));
2018-03-14 15:17:44 +00:00
self.cksum
.files
.insert(name.to_string(), cksum(contents.as_bytes()));
2016-07-05 17:28:51 +00:00
self
}
fn disable_checksum(&mut self) -> &mut VendorPackage {
self.cksum.package = None;
self
}
fn no_manifest(mut self) -> Self {
self.p = self.p.map(|pb| pb.no_manifest());
self
}
2016-07-05 17:28:51 +00:00
fn build(&mut self) {
let p = self.p.take().unwrap();
2017-06-08 20:08:59 +00:00
let json = serde_json::to_string(&self.cksum).unwrap();
2016-07-05 17:28:51 +00:00
let p = p.file(".cargo-checksum.json", &json);
let _ = p.build();
2016-07-05 17:28:51 +00:00
}
}
#[cargo_test]
2016-07-05 17:28:51 +00:00
fn simple() {
setup();
VendorPackage::new("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("src/lib.rs", "pub fn bar() {}")
2016-07-05 17:28:51 +00:00
.build();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2016-07-05 17:28:51 +00:00
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"src/lib.rs",
"extern crate bar; pub fn foo() { bar::bar(); }",
2018-12-08 11:19:47 +00:00
)
.build();
2016-07-05 17:28:51 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[CHECKING] bar v0.1.0
[CHECKING] foo v0.1.0 ([CWD])
2016-07-05 17:28:51 +00:00
[FINISHED] [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2016-07-05 17:28:51 +00:00
}
#[cargo_test]
fn simple_install() {
setup();
VendorPackage::new("foo")
.file("src/lib.rs", "pub fn foo() {}")
.build();
VendorPackage::new("bar")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "bar"
version = "0.1.0"
authors = []
2020-09-27 00:59:58 +00:00
[dependencies]
foo = "0.0.1"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"src/main.rs",
"extern crate foo; pub fn main() { foo::foo(); }",
2018-12-08 11:19:47 +00:00
)
.build();
cargo_process("install bar")
.with_stderr(
2019-03-31 00:33:35 +00:00
"\
[INSTALLING] bar v0.1.0
[COMPILING] foo v0.0.1
[COMPILING] bar v0.1.0
[FINISHED] release [optimized] target(s) in [..]s
[INSTALLING] [..]bar[..]
[INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn simple_install_fail() {
setup();
VendorPackage::new("foo")
.file("src/lib.rs", "pub fn foo() {}")
.build();
VendorPackage::new("bar")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "bar"
version = "0.1.0"
authors = []
[dependencies]
foo = "0.1.0"
baz = "9.8.7"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"src/main.rs",
"extern crate foo; pub fn main() { foo::foo(); }",
2018-12-08 11:19:47 +00:00
)
.build();
cargo_process("install bar")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
" Installing bar v0.1.0
2023-06-05 09:27:31 +00:00
error: failed to compile `bar v0.1.0`, intermediate artifacts can be found at `[..]`.
To reuse those artifacts with a future compilation, set the environment variable \
`CARGO_TARGET_DIR` to that path.
Caused by:
no matching package found
searched package name: `baz`
perhaps you meant: bar or foo
2021-06-27 19:18:36 +00:00
location searched: registry `crates-io`
required by package `bar v0.1.0`
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn install_without_feature_dep() {
setup();
VendorPackage::new("foo")
.file("src/lib.rs", "pub fn foo() {}")
.build();
VendorPackage::new("bar")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "bar"
version = "0.1.0"
authors = []
2020-09-27 00:59:58 +00:00
[dependencies]
foo = "0.0.1"
baz = { version = "9.8.7", optional = true }
2020-09-27 00:59:58 +00:00
[features]
wantbaz = ["baz"]
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"src/main.rs",
"extern crate foo; pub fn main() { foo::foo(); }",
2018-12-08 11:19:47 +00:00
)
.build();
cargo_process("install bar")
.with_stderr(
2019-03-31 00:33:35 +00:00
"\
[INSTALLING] bar v0.1.0
[COMPILING] foo v0.0.1
[COMPILING] bar v0.1.0
[FINISHED] release [optimized] target(s) in [..]s
[INSTALLING] [..]bar[..]
[INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
2016-07-05 17:28:51 +00:00
fn not_there() {
setup();
let _ = project().at("index").build();
2016-07-05 17:28:51 +00:00
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2016-07-05 17:28:51 +00:00
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"src/lib.rs",
"extern crate bar; pub fn foo() { bar::bar(); }",
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("check")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
error: no matching package named `bar` found
2016-07-05 17:28:51 +00:00
location searched: [..]
required by package `foo v0.1.0 ([..])`
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2016-07-05 17:28:51 +00:00
}
#[cargo_test]
2016-07-05 17:28:51 +00:00
fn multiple() {
setup();
VendorPackage::new("bar-0.1.0")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("src/lib.rs", "pub fn bar() {}")
2016-07-05 17:28:51 +00:00
.file(".cargo-checksum", "")
.build();
VendorPackage::new("bar-0.2.0")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.2.0"))
.file("src/lib.rs", "pub fn bar() {}")
2016-07-05 17:28:51 +00:00
.file(".cargo-checksum", "")
.build();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2016-07-05 17:28:51 +00:00
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"src/lib.rs",
"extern crate bar; pub fn foo() { bar::bar(); }",
2018-12-08 11:19:47 +00:00
)
.build();
2016-07-05 17:28:51 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[CHECKING] bar v0.1.0
[CHECKING] foo v0.1.0 ([CWD])
2016-07-05 17:28:51 +00:00
[FINISHED] [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2016-07-05 17:28:51 +00:00
}
#[cargo_test]
2016-07-05 17:28:51 +00:00
fn crates_io_then_directory() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2016-07-05 17:28:51 +00:00
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"src/lib.rs",
"extern crate bar; pub fn foo() { bar::bar(); }",
2018-12-08 11:19:47 +00:00
)
.build();
2016-07-05 17:28:51 +00:00
let cksum = Package::new("bar", "0.1.0")
.file("src/lib.rs", "pub fn bar() -> u32 { 0 }")
2018-03-14 15:17:44 +00:00
.publish();
2016-07-05 17:28:51 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] `[..]` index
2018-09-14 20:33:18 +00:00
[DOWNLOADING] crates ...
[DOWNLOADED] bar v0.1.0 ([..])
[CHECKING] bar v0.1.0
[CHECKING] foo v0.1.0 ([CWD])
2016-07-05 17:28:51 +00:00
[FINISHED] [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2016-07-05 17:28:51 +00:00
setup();
let mut v = VendorPackage::new("bar");
2018-07-24 22:35:01 +00:00
v.file("Cargo.toml", &basic_manifest("bar", "0.1.0"));
v.file("src/lib.rs", "pub fn bar() -> u32 { 1 }");
v.cksum.package = Some(cksum);
2016-07-05 17:28:51 +00:00
v.build();
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[CHECKING] bar v0.1.0
[CHECKING] foo v0.1.0 ([CWD])
2016-07-05 17:28:51 +00:00
[FINISHED] [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2016-07-05 17:28:51 +00:00
}
#[cargo_test]
2016-07-05 17:28:51 +00:00
fn crates_io_then_bad_checksum() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2016-07-05 17:28:51 +00:00
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
2016-07-05 17:28:51 +00:00
Package::new("bar", "0.1.0").publish();
2016-07-05 17:28:51 +00:00
p.cargo("check").run();
2016-07-05 17:28:51 +00:00
setup();
VendorPackage::new("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
2016-07-05 17:28:51 +00:00
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
error: checksum for `bar v0.1.0` changed between lock files
2016-07-05 17:28:51 +00:00
this could be indicative of a few possible errors:
* the lock file is corrupt
2019-02-03 04:01:23 +00:00
* a replacement source in use (e.g., a mirror) returned a different checksum
2016-07-05 17:28:51 +00:00
* the source itself may be corrupt in one way or another
unable to verify that `bar v0.1.0` is the same as when the lockfile was generated
2016-07-05 17:28:51 +00:00
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2016-07-05 17:28:51 +00:00
}
#[cargo_test]
2016-07-05 17:28:51 +00:00
fn bad_file_checksum() {
setup();
VendorPackage::new("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
2016-07-05 17:28:51 +00:00
.file("src/lib.rs", "")
.build();
t!(fs::write(
paths::root().join("index/bar/src/lib.rs"),
"fn bar() -> u32 { 0 }"
));
2016-07-05 17:28:51 +00:00
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2016-07-05 17:28:51 +00:00
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
2016-07-05 17:28:51 +00:00
p.cargo("check")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
2016-07-05 17:28:51 +00:00
error: the listed checksum of `[..]lib.rs` has changed:
expected: [..]
actual: [..]
directory sources are not intended to be edited, if modifications are \
required then it is recommended that `[patch]` is used with a forked copy of \
2016-07-05 17:28:51 +00:00
the source
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2016-07-05 17:28:51 +00:00
}
#[cargo_test]
fn only_dot_files_ok() {
setup();
VendorPackage::new("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("src/lib.rs", "")
.build();
VendorPackage::new("foo")
.no_manifest()
.file(".bar", "")
.build();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
p.cargo("check").run();
}
#[cargo_test]
fn random_files_ok() {
setup();
VendorPackage::new("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("src/lib.rs", "")
.build();
VendorPackage::new("foo")
.no_manifest()
.file("bar", "")
.file("../test", "")
.build();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.1.0"
authors = []
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "0.1.0"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
p.cargo("check").run();
}
#[cargo_test]
fn git_lock_file_doesnt_change() {
let git = git::new("git", |p| {
2018-07-24 22:35:01 +00:00
p.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
.file("src/lib.rs", "")
});
VendorPackage::new("git")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
.file("src/lib.rs", "")
.disable_checksum()
.build();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
git = {{ git = '{0}' }}
"#,
2018-03-14 15:17:44 +00:00
git.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
p.cargo("check").run();
let lock1 = p.read_lockfile();
let root = paths::root();
t!(fs::create_dir(&root.join(".cargo")));
t!(fs::write(
2024-01-26 19:40:46 +00:00
root.join(".cargo/config.toml"),
2019-03-27 00:53:53 +00:00
format!(
2018-03-14 15:17:44 +00:00
r#"
[source.my-git-repo]
git = '{}'
replace-with = 'my-awesome-local-registry'
[source.my-awesome-local-registry]
directory = 'index'
"#,
2018-03-14 15:17:44 +00:00
git.url()
2018-12-08 11:19:47 +00:00
)
));
2018-03-14 15:17:44 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[CHECKING] [..]
[CHECKING] [..]
[FINISHED] [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
let lock2 = p.read_lockfile();
assert_eq!(lock1, lock2, "lock files changed");
}
#[cargo_test]
fn git_override_requires_lockfile() {
VendorPackage::new("git")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
.file("src/lib.rs", "")
.disable_checksum()
.build();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
git = { git = 'https://example.com/' }
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
let root = paths::root();
t!(fs::create_dir(&root.join(".cargo")));
t!(fs::write(
2024-01-26 19:40:46 +00:00
root.join(".cargo/config.toml"),
r#"
[source.my-git-repo]
git = 'https://example.com/'
replace-with = 'my-awesome-local-registry'
[source.my-awesome-local-registry]
directory = 'index'
"#
2018-03-14 15:17:44 +00:00
));
p.cargo("check")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[ERROR] failed to get `git` as a dependency of package `foo v0.0.1 ([..])`
Caused by:
failed to load source for dependency `git`
Caused by:
Unable to update [..]
Caused by:
the source my-git-repo requires a lock file to be present first before it can be
used against vendored source code
remove the source replacement configuration, generate a lock file, and then
restore the source replacement configuration to continue the build
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn workspace_different_locations() {
let p = project()
.no_manifest()
.file(
"foo/Cargo.toml",
r#"
[package]
name = 'foo'
version = '0.1.0'
[dependencies]
baz = "*"
"#,
2018-12-08 11:19:47 +00:00
)
.file("foo/src/lib.rs", "")
.file("foo/vendor/baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
.file("foo/vendor/baz/src/lib.rs", "")
.file("foo/vendor/baz/.cargo-checksum.json", "{\"files\":{}}")
.file(
"bar/Cargo.toml",
r#"
[package]
name = 'bar'
version = '0.1.0'
[dependencies]
baz = "*"
"#,
2018-12-08 11:19:47 +00:00
)
.file("bar/src/lib.rs", "")
.file(
2024-01-26 19:40:46 +00:00
".cargo/config.toml",
r#"
[build]
target-dir = './target'
[source.crates-io]
replace-with = 'my-awesome-local-registry'
[source.my-awesome-local-registry]
directory = 'foo/vendor'
"#,
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("check").cwd("foo").run();
p.cargo("check")
.cwd("bar")
.with_stderr(
"\
[CHECKING] bar [..]
[FINISHED] [..]
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn version_missing() {
setup();
VendorPackage::new("foo")
.file("src/lib.rs", "pub fn foo() {}")
.build();
VendorPackage::new("bar")
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.1.0"
authors = []
[dependencies]
foo = "2"
"#,
)
.file("src/main.rs", "fn main() {}")
.build();
cargo_process("install bar")
.with_stderr(
"\
[INSTALLING] bar v0.1.0
2023-06-05 09:27:31 +00:00
error: failed to compile [..], intermediate artifacts can be found at `[..]`.
To reuse those artifacts with a future compilation, set the environment variable \
`CARGO_TARGET_DIR` to that path.
Caused by:
failed to select a version for the requirement `foo = \"^2\"`
candidate versions found which didn't match: 0.0.1
location searched: directory source `[..] (which is replacing registry `[..]`)
required by package `bar v0.1.0`
perhaps a crate was updated and forgotten to be re-vendored?
",
)
.with_status(101)
.run();
}