2019-11-25 02:42:45 +00:00
|
|
|
//! Tests for the `cargo generate-lockfile` command.
|
|
|
|
|
2023-02-28 22:46:45 +00:00
|
|
|
use cargo_test_support::registry::{Package, RegistryBuilder};
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::{basic_manifest, paths, project, ProjectBuilder};
|
2020-04-17 04:10:11 +00:00
|
|
|
use std::fs;
|
2014-08-04 01:38:25 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn adding_and_removing_packages() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
Implement a registry source
# cargo upload
The cargo-upload command will take the local package and upload it to the
specified registry. The local package is uploaded as a tarball compressed with
gzip under maximum compression. Most of this is done by just delegating to
`cargo package` The host to upload to is specified, in order of priority, by a
command line `--host` flag, the `registry.host` config key, and then the default
registry. The default registry is still `example.com`
The registry itself is still a work in progress, but the general plumbing for a
command such as this would look like:
1. Ensure the local package has been compressed into an archive.
2. Fetch the relevant registry and login token from config files.
3. Ensure all dependencies for a package are listed as coming from the same
registry.
4. Upload the archive to the registry with the login token.
5. The registry will verify the package is under 2MB (configurable).
6. The registry will upload the archive to S3, calculating a checksum in the
process.
7. The registry will add an entry to the registry's index (a git repository).
The entry will include the name of the package, the version uploaded, the
checksum of the upload, and then the list of dependencies (name/version req)
8. The local `cargo upload` command will succeed.
# cargo login
Uploading requires a token from the api server, and this token follows the same
config chain for the host except that there is no fallback. To implement login,
the `cargo login` command is used. With 0 arguments, the command will request
that a site be visited for a login token, and with an argument it will set the
argument as the new login token.
The `util::config` module was modified to allow writing configuration as well as
reading it. The support is a little lacking in that comments are blown away, but
the support is there at least.
# RegistrySource
An implementation of `RegistrySource` has been created (deleting the old
`DummyRegistrySource`). This implementation only needs a URL to be constructed,
and it is assumed that the URL is running an instance of the cargo registry.
## RegistrySource::update
Currently this will unconditionally update the registry's index (a git
repository). Tuning is necessary to prevent updating the index each time (more
coming soon).
## RegistrySource::query
This is called in the resolve phase of cargo. This function is given a
dependency to query for, and the source will simply look into the index to see
if any package with the name is present. If found, the package's index file will
be loaded and parsed into a list of summaries.
The main optimization of this function is to not require the entire registry to
ever be resident in memory. Instead, only necessary packages are loaded into
memory and parsed.
## RegistrySource::download
This is also called during the resolve phase of cargo, but only when a package
has been selected to be built (actually resolved). This phase of the source will
actually download and unpack the tarball for the package.
Currently a configuration file is located in the root of a registry's index
describing the root url to download packages from.
This function is optimized for two different metrics:
1. If a tarball is downloaded, it is not downloaded again. It is assumed that
once a tarball is successfully downloaded it will never change.
2. If the unpacking destination has a `.cargo-ok` file, it is assumed that the
unpacking has already occurred and does not need to happen again.
With these in place, a rebuild should take almost no time at all.
## RegistrySource::get
This function is simply implemented in terms of a PathSource's `get` function by
creating a `PathSource` for all unpacked tarballs as part of the `download`
stage.
## Filesystem layout
There are a few new directories as part of the `.cargo` home folder:
* `.cargo/registry/index/$hostname-$hash` - This is the directory containing the
actual index of the registry. `$hostname` comes from its url, and `$hash` is
the hash of the entire url.
* `.cargo/registry/cache/$hostname-$hash/$pkg-$vers.tar.gz` - This is a
directory used to cache the downloads of packages from the registry.
* `.cargo/registry/src/$hostname-$hash/$pkg-$vers` - This is the location of the
unpacked packages. They will be compiled from this location.
# New Dependencies
Cargo has picked up a new dependency on the `curl-rust` package in order to send
HTTP requests to the registry as well as send HTTP requests to download
tarballs.
2014-07-18 15:40:45 +00:00
|
|
|
.file("src/main.rs", "fn main() {}")
|
2018-07-24 22:35:01 +00:00
|
|
|
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
|
2017-07-22 03:12:21 +00:00
|
|
|
.file("bar/src/lib.rs", "")
|
|
|
|
.build();
|
2014-08-04 17:50:32 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2014-08-04 17:50:32 +00:00
|
|
|
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock1 = p.read_lockfile();
|
2014-08-04 17:50:32 +00:00
|
|
|
|
|
|
|
// add a dep
|
2020-04-17 04:10:11 +00:00
|
|
|
p.change_file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
authors = []
|
|
|
|
version = "0.0.1"
|
|
|
|
|
|
|
|
[dependencies.bar]
|
|
|
|
path = "bar"
|
|
|
|
"#,
|
|
|
|
);
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock2 = p.read_lockfile();
|
2018-02-23 23:27:53 +00:00
|
|
|
assert_ne!(lock1, lock2);
|
2014-08-04 17:50:32 +00:00
|
|
|
|
|
|
|
// change the dep
|
2020-04-17 04:10:11 +00:00
|
|
|
p.change_file("bar/Cargo.toml", &basic_manifest("bar", "0.0.2"));
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock3 = p.read_lockfile();
|
2018-02-23 23:27:53 +00:00
|
|
|
assert_ne!(lock1, lock3);
|
|
|
|
assert_ne!(lock2, lock3);
|
2014-08-04 17:50:32 +00:00
|
|
|
|
|
|
|
// remove the dep
|
2016-04-05 23:32:13 +00:00
|
|
|
println!("lock4");
|
2020-04-17 04:10:11 +00:00
|
|
|
p.change_file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
authors = []
|
|
|
|
version = "0.0.1"
|
|
|
|
"#,
|
|
|
|
);
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock4 = p.read_lockfile();
|
2014-08-04 17:50:32 +00:00
|
|
|
assert_eq!(lock1, lock4);
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2014-10-15 23:18:43 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2023-02-28 22:46:45 +00:00
|
|
|
fn no_index_update_sparse() {
|
|
|
|
let _registry = RegistryBuilder::new().http_index().build();
|
|
|
|
no_index_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn no_index_update_git() {
|
|
|
|
no_index_update();
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:12:19 +00:00
|
|
|
fn no_index_update() {
|
2018-03-04 20:51:31 +00:00
|
|
|
Package::new("serde", "1.0.0").publish();
|
|
|
|
|
2018-07-20 11:47:47 +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"
|
|
|
|
authors = []
|
|
|
|
version = "0.0.1"
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
serde = "1.0"
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file("src/main.rs", "fn main() {}")
|
2018-01-31 21:12:19 +00:00
|
|
|
.build();
|
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile")
|
2018-09-08 09:23:57 +00:00
|
|
|
.with_stderr("[UPDATING] `[..]` index")
|
2018-08-28 09:20:03 +00:00
|
|
|
.run();
|
2018-01-31 21:12:19 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile -Zno-index-update")
|
2022-07-16 02:32:23 +00:00
|
|
|
.masquerade_as_nightly_cargo(&["no-index-update"])
|
2018-08-28 09:20:03 +00:00
|
|
|
.with_stdout("")
|
|
|
|
.with_stderr("")
|
|
|
|
.run();
|
2018-01-31 21:12:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn preserve_metadata() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
2014-10-15 23:18:43 +00:00
|
|
|
.file("src/main.rs", "fn main() {}")
|
2018-07-24 22:35:01 +00:00
|
|
|
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
|
2017-07-22 03:12:21 +00:00
|
|
|
.file("bar/src/lib.rs", "")
|
|
|
|
.build();
|
2014-10-15 23:18:43 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2014-10-15 23:18:43 +00:00
|
|
|
|
|
|
|
let metadata = r#"
|
|
|
|
[metadata]
|
|
|
|
bar = "baz"
|
|
|
|
foo = "bar"
|
|
|
|
"#;
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock = p.read_lockfile();
|
|
|
|
let data = lock + metadata;
|
2020-04-17 04:10:11 +00:00
|
|
|
p.change_file("Cargo.lock", &data);
|
2014-10-15 23:18:43 +00:00
|
|
|
|
|
|
|
// Build and make sure the metadata is still there
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build").run();
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock = p.read_lockfile();
|
2015-02-27 01:04:25 +00:00
|
|
|
assert!(lock.contains(metadata.trim()), "{}", lock);
|
2014-10-15 23:18:43 +00:00
|
|
|
|
|
|
|
// Update and make sure the metadata is still there
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("update").run();
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock = p.read_lockfile();
|
2015-02-27 01:04:25 +00:00
|
|
|
assert!(lock.contains(metadata.trim()), "{}", lock);
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2015-10-30 10:39:35 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn preserve_line_endings_issue_2076() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
2015-10-30 10:39:35 +00:00
|
|
|
.file("src/main.rs", "fn main() {}")
|
2018-07-24 22:35:01 +00:00
|
|
|
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
|
2017-07-22 03:12:21 +00:00
|
|
|
.file("bar/src/lib.rs", "")
|
|
|
|
.build();
|
2015-10-30 10:39:35 +00:00
|
|
|
|
|
|
|
let lockfile = p.root().join("Cargo.lock");
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2018-08-29 06:11:10 +00:00
|
|
|
assert!(lockfile.is_file());
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2015-10-30 10:39:35 +00:00
|
|
|
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock0 = p.read_lockfile();
|
2015-10-30 10:39:35 +00:00
|
|
|
|
2019-01-14 10:20:10 +00:00
|
|
|
assert!(lock0.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n"));
|
2015-10-30 10:39:35 +00:00
|
|
|
|
|
|
|
let lock1 = lock0.replace("\n", "\r\n");
|
2020-04-17 04:10:11 +00:00
|
|
|
p.change_file("Cargo.lock", &lock1);
|
2015-10-30 10:39:35 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("generate-lockfile").run();
|
2015-10-30 10:39:35 +00:00
|
|
|
|
2016-08-25 08:34:25 +00:00
|
|
|
let lock2 = p.read_lockfile();
|
2015-10-30 10:39:35 +00:00
|
|
|
|
2019-01-14 10:20:10 +00:00
|
|
|
assert!(lock2.starts_with("# This file is automatically @generated by Cargo.\r\n# It is not intended for manual editing.\r\n"));
|
2015-10-30 10:39:35 +00:00
|
|
|
assert_eq!(lock1, lock2);
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2016-03-20 20:12:52 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn cargo_update_generate_lockfile() {
|
2018-08-28 09:20:03 +00:00
|
|
|
let p = project().file("src/main.rs", "fn main() {}").build();
|
2016-03-20 20:12:52 +00:00
|
|
|
|
|
|
|
let lockfile = p.root().join("Cargo.lock");
|
2018-08-29 06:11:10 +00:00
|
|
|
assert!(!lockfile.is_file());
|
2023-11-15 22:33:27 +00:00
|
|
|
p.cargo("update").with_stderr("").run();
|
2018-08-29 06:11:10 +00:00
|
|
|
assert!(lockfile.is_file());
|
2016-03-20 20:12:52 +00:00
|
|
|
|
|
|
|
fs::remove_file(p.root().join("Cargo.lock")).unwrap();
|
|
|
|
|
2018-08-29 06:11:10 +00:00
|
|
|
assert!(!lockfile.is_file());
|
2023-11-15 22:33:27 +00:00
|
|
|
p.cargo("update").with_stderr("").run();
|
2018-08-29 06:11:10 +00:00
|
|
|
assert!(lockfile.is_file());
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2018-05-28 17:02:42 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-28 17:02:42 +00:00
|
|
|
fn duplicate_entries_in_lockfile() {
|
2018-07-14 01:49:26 +00:00
|
|
|
let _a = ProjectBuilder::new(paths::root().join("a"))
|
2018-05-28 17:02:42 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "a"
|
|
|
|
authors = []
|
|
|
|
version = "0.0.1"
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
common = {path="common"}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file("src/lib.rs", "")
|
2018-05-28 17:02:42 +00:00
|
|
|
.build();
|
|
|
|
|
2018-07-24 22:35:01 +00:00
|
|
|
let common_toml = &basic_manifest("common", "0.0.1");
|
2018-05-28 17:02:42 +00:00
|
|
|
|
2018-07-14 01:49:26 +00:00
|
|
|
let _common_in_a = ProjectBuilder::new(paths::root().join("a/common"))
|
2018-05-28 17:02:42 +00:00
|
|
|
.file("Cargo.toml", common_toml)
|
|
|
|
.file("src/lib.rs", "")
|
|
|
|
.build();
|
|
|
|
|
2018-07-14 01:49:26 +00:00
|
|
|
let b = ProjectBuilder::new(paths::root().join("b"))
|
2018-05-28 17:02:42 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "b"
|
|
|
|
authors = []
|
|
|
|
version = "0.0.1"
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
common = {path="common"}
|
|
|
|
a = {path="../a"}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file("src/lib.rs", "")
|
2018-05-28 17:02:42 +00:00
|
|
|
.build();
|
|
|
|
|
2018-07-14 01:49:26 +00:00
|
|
|
let _common_in_b = ProjectBuilder::new(paths::root().join("b/common"))
|
2018-05-28 17:02:42 +00:00
|
|
|
.file("Cargo.toml", common_toml)
|
|
|
|
.file("src/lib.rs", "")
|
|
|
|
.build();
|
|
|
|
|
2019-02-03 04:01:23 +00:00
|
|
|
// should fail due to a duplicate package `common` in the lock file
|
2018-08-28 09:20:03 +00:00
|
|
|
b.cargo("build")
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr_contains(
|
2018-06-03 21:40:27 +00:00
|
|
|
"[..]package collision in the lockfile: packages common [..] and \
|
|
|
|
common [..] are different, but only one can be written to \
|
2018-09-25 07:02:52 +00:00
|
|
|
lockfile unambiguously",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2018-05-28 17:02:42 +00:00
|
|
|
}
|