cargo/tests/testsuite/path.rs

1048 lines
24 KiB
Rust
Raw Normal View History

use std::fs::{self, File};
use std::io::prelude::*;
2018-12-06 19:17:36 +00:00
use crate::support::paths::{self, CargoPathExt};
use crate::support::registry::Package;
use crate::support::sleep_ms;
use crate::support::{basic_lib_manifest, basic_manifest, main_file, project};
2014-06-18 00:05:29 +00:00
#[test]
2019-02-03 04:01:23 +00:00
// I have no idea why this is failing spuriously on Windows;
// for more info, see #3466.
#[cfg(not(windows))]
fn cargo_compile_with_nested_deps_shorthand() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2014-06-18 00:05:29 +00:00
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
version = "0.5.0"
path = "bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
2018-03-14 15:17:44 +00:00
.file(
"bar/Cargo.toml",
r#"
2014-06-18 00:05:29 +00:00
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.baz]
version = "0.5.0"
path = "baz"
2014-08-14 06:08:02 +00:00
[lib]
2014-06-18 00:05:29 +00:00
name = "bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/src/bar.rs",
r#"
2014-06-18 00:05:29 +00:00
extern crate baz;
pub fn gimme() -> String {
baz::gimme()
}
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("bar/baz/Cargo.toml", &basic_lib_manifest("baz"))
2018-03-14 15:17:44 +00:00
.file(
"bar/baz/src/baz.rs",
r#"
2014-06-18 00:05:29 +00:00
pub fn gimme() -> String {
"test passed".to_string()
2014-06-18 00:05:29 +00:00
}
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.build();
2014-06-18 00:05:29 +00:00
p.cargo("build")
.with_stderr(
"[COMPILING] baz v0.5.0 ([CWD]/bar/baz)\n\
[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2014-06-18 00:05:29 +00:00
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
2014-06-18 00:05:29 +00:00
p.process(&p.bin("foo")).with_stdout("test passed\n").run();
println!("cleaning");
p.cargo("clean -v").with_stdout("").run();
println!("building baz");
p.cargo("build -p baz")
.with_stderr(
"[COMPILING] baz v0.5.0 ([CWD]/bar/baz)\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
println!("building foo");
p.cargo("build -p foo")
.with_stderr(
"[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn cargo_compile_with_root_dev_deps() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2014-07-03 01:13:00 +00:00
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dev-dependencies.bar]
version = "0.5.0"
2014-07-10 19:08:13 +00:00
path = "../bar"
2014-07-03 01:13:00 +00:00
[[bin]]
name = "foo"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
.build();
let _p2 = project()
.at("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file(
"src/lib.rs",
r#"
2014-07-03 01:13:00 +00:00
pub fn gimme() -> &'static str {
"zoidberg"
}
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.build();
2014-07-03 01:13:00 +00:00
p.cargo("build")
.with_status(101)
.with_stderr_contains("[..]can't find crate for `bar`")
.run();
}
2014-07-03 01:13:00 +00:00
#[test]
fn cargo_compile_with_root_dev_deps_with_testing() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
2014-07-03 01:13:00 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dev-dependencies.bar]
version = "0.5.0"
path = "../bar"
[[bin]]
name = "foo"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
.build();
let _p2 = project()
.at("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file(
"src/lib.rs",
r#"
pub fn gimme() -> &'static str {
"zoidberg"
}
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("test")
.with_stderr(
"\
[COMPILING] [..] v0.5.0 ([..])
[COMPILING] [..] v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-08-02 09:18:48 +00:00
[RUNNING] target/debug/deps/foo-[..][EXE]",
2018-12-08 11:19:47 +00:00
)
.with_stdout_contains("running 0 tests")
.run();
}
2014-07-03 01:13:00 +00:00
#[test]
fn cargo_compile_with_transitive_dev_deps() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
version = "0.5.0"
path = "bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
2018-03-14 15:17:44 +00:00
.file(
"bar/Cargo.toml",
r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
[dev-dependencies.baz]
git = "git://example.com/path/to/nowhere"
2014-08-14 06:08:02 +00:00
[lib]
name = "bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/src/bar.rs",
r#"
pub fn gimme() -> &'static str {
"zoidberg"
}
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("build")
.with_stderr(
"[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in \
[..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
p.process(&p.bin("foo")).with_stdout("zoidberg\n").run();
}
#[test]
fn no_rebuild_dependency() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
path = "bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "extern crate bar; fn main() { bar::bar() }")
2018-07-24 22:35:01 +00:00
.file("bar/Cargo.toml", &basic_lib_manifest("bar"))
.file("bar/src/bar.rs", "pub fn bar() {}")
.build();
// First time around we should compile both foo and bar
p.cargo("build")
.with_stderr(
"[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2017-02-16 11:18:55 +00:00
sleep_ms(1000);
2018-03-14 15:17:44 +00:00
p.change_file(
"src/main.rs",
r#"
2017-02-16 11:18:55 +00:00
extern crate bar;
fn main() { bar::bar(); }
2018-03-14 15:17:44 +00:00
"#,
);
2017-02-16 11:18:55 +00:00
// Don't compile bar, but do recompile foo.
p.cargo("build")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] foo v0.5.0 ([..])\n\
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn deep_dependencies_trigger_rebuild() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
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
[dependencies.bar]
path = "bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "extern crate bar; fn main() { bar::bar() }")
2018-03-14 15:17:44 +00:00
.file(
"bar/Cargo.toml",
r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-08-14 06:08:02 +00:00
[lib]
name = "bar"
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
[dependencies.baz]
path = "../baz"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"bar/src/bar.rs",
"extern crate baz; pub fn bar() { baz::baz() }",
2018-12-08 11:19:47 +00:00
)
.file("baz/Cargo.toml", &basic_lib_manifest("baz"))
.file("baz/src/baz.rs", "pub fn baz() {}")
.build();
p.cargo("build")
.with_stderr(
"[COMPILING] baz v0.5.0 ([CWD]/baz)\n\
[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
p.cargo("build").with_stdout("").run();
// Make sure an update to baz triggers a rebuild of bar
//
// We base recompilation off mtime, so sleep for at least a second to ensure
// that this write will change the mtime.
2019-01-04 20:47:22 +00:00
sleep_ms(1000);
2018-03-14 15:17:44 +00:00
File::create(&p.root().join("baz/src/baz.rs"))
.unwrap()
.write_all(br#"pub fn baz() { println!("hello!"); }"#)
2018-03-14 15:17:44 +00:00
.unwrap();
sleep_ms(1000);
p.cargo("build")
.with_stderr(
"[COMPILING] baz v0.5.0 ([CWD]/baz)\n\
[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
// Make sure an update to bar doesn't trigger baz
2019-01-04 20:47:22 +00:00
sleep_ms(1000);
2018-03-14 15:17:44 +00:00
File::create(&p.root().join("bar/src/bar.rs"))
.unwrap()
.write_all(
br#"
extern crate baz;
pub fn bar() { println!("hello!"); baz::baz(); }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.unwrap();
sleep_ms(1000);
p.cargo("build")
.with_stderr(
"[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn no_rebuild_two_deps() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
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
[dependencies.bar]
path = "bar"
[dependencies.baz]
path = "baz"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "extern crate bar; fn main() { bar::bar() }")
2018-03-14 15:17:44 +00:00
.file(
"bar/Cargo.toml",
r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-08-14 06:08:02 +00:00
[lib]
name = "bar"
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
[dependencies.baz]
path = "../baz"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("bar/src/bar.rs", "pub fn bar() {}")
2018-07-24 22:35:01 +00:00
.file("baz/Cargo.toml", &basic_lib_manifest("baz"))
.file("baz/src/baz.rs", "pub fn baz() {}")
.build();
p.cargo("build")
.with_stderr(
"[COMPILING] baz v0.5.0 ([CWD]/baz)\n\
[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
p.cargo("build").with_stdout("").run();
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
}
#[test]
fn nested_deps_recompile() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
version = "0.5.0"
path = "src/bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
2018-07-24 22:35:01 +00:00
.file("src/bar/Cargo.toml", &basic_lib_manifest("bar"))
.file("src/bar/src/bar.rs", "pub fn gimme() -> i32 { 92 }")
.build();
p.cargo("build")
.with_stderr(
"[COMPILING] bar v0.5.0 ([CWD]/src/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
sleep_ms(1000);
2018-03-14 15:17:44 +00:00
File::create(&p.root().join("src/main.rs"))
.unwrap()
.write_all(br#"fn main() {}"#)
2018-03-14 15:17:44 +00:00
.unwrap();
// This shouldn't recompile `bar`
p.cargo("build")
.with_stderr(
"[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn error_message_for_missing_manifest() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
path = "src/bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.file("src/bar/not-a-manifest", "")
.build();
p.cargo("build")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
Add flags to assert lock/cache behavior to Cargo If a lock file is generated and some equivalent of `cargo fetch` is run then Cargo shouldn't ever touch the network or modify `Cargo.lock` until any `Cargo.toml` later changes, but this often wants to be asserted in some build environments where it's a programmer error if Cargo attempts to access the network. The `--locked` flag added here will assert that `Cargo.lock` does not need to change to proceed. That is, if `Cargo.lock` would be modified (as it automatically is by default) this is turned into a hard error instead. This `--frozen` will not only assert that `Cargo.lock` doesn't change (the same behavior as `--locked`), but it will also will manually prevent Cargo from touching the network by ensuring that all network requests return an error. These flags can be used in environments where it is *expected* that no network access happens (or no lockfile changes happen) because it has been pre-arranged for Cargo to not happen. Examples of this include: * CI for projects want to pass `--locked` to ensure that `Cargo.lock` is up to date before changes are checked in. * Environments with vendored dependencies want to pass `--frozen` as touching the network indicates a programmer error that something wasn't vendored correctly. A crucial property of these two flags is that **they do not change the behavior of Cargo**. They are simply assertions at a few locations in Cargo to ensure that actions expected to not happen indeed don't happen. Some documentation has also been added to this effect. Closes #2111
2016-06-28 17:39:46 +00:00
[ERROR] failed to load source for a dependency on `bar`
Caused by:
Unable to update [CWD]/src/bar
Caused by:
2018-08-02 09:18:48 +00:00
failed to read `[..]bar/Cargo.toml`
Caused by:
[..] (os error [..])
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn override_relative() {
let bar = project()
.at("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "")
.build();
fs::create_dir(&paths::root().join(".cargo")).unwrap();
2018-03-14 15:17:44 +00:00
File::create(&paths::root().join(".cargo/config"))
.unwrap()
.write_all(br#"paths = ["bar"]"#)
.unwrap();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
path = '{}'
2018-03-14 15:17:44 +00:00
"#,
bar.root().display()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-03-14 15:17:44 +00:00
.build();
p.cargo("build -v").run();
}
#[test]
fn override_self() {
let bar = project()
.at("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
.file("src/lib.rs", "")
.build();
let p = project();
2019-03-27 01:51:13 +00:00
let root = p.root();
let p = p
.file(".cargo/config", &format!("paths = ['{}']", root.display()))
.file(
2018-03-14 15:17:44 +00:00
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
path = '{}'
2018-03-14 15:17:44 +00:00
"#,
bar.root().display()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-03-14 15:17:44 +00:00
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("build").run();
}
#[test]
fn override_path_dep() {
let bar = project()
.at("bar")
2018-03-14 15:17:44 +00:00
.file(
"p1/Cargo.toml",
r#"
[package]
name = "p1"
version = "0.5.0"
authors = []
[dependencies.p2]
path = "../p2"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("p1/src/lib.rs", "")
2018-07-24 22:35:01 +00:00
.file("p2/Cargo.toml", &basic_manifest("p2", "0.5.0"))
.file("p2/src/lib.rs", "")
.build();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
".cargo/config",
&format!(
"paths = ['{}', '{}']",
2018-03-14 15:17:44 +00:00
bar.root().join("p1").display(),
bar.root().join("p2").display()
),
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.p2]
path = '{}'
2018-03-14 15:17:44 +00:00
"#,
bar.root().join("p2").display()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-03-14 15:17:44 +00:00
.build();
p.cargo("build -v").run();
}
#[test]
fn path_dep_build_cmd() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
version = "0.5.0"
path = "bar"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
2018-03-14 15:17:44 +00:00
.file(
"bar/Cargo.toml",
r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
build = "build.rs"
[lib]
name = "bar"
2017-07-09 10:31:42 +00:00
path = "src/bar.rs"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/build.rs",
r#"
use std::fs;
fn main() {
fs::copy("src/bar.rs.in", "src/bar.rs").unwrap();
}
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("bar/src/bar.rs.in", "pub fn gimme() -> i32 { 0 }")
2018-03-14 15:17:44 +00:00
.build();
p.root().join("bar").move_into_the_past();
p.cargo("build")
.with_stderr(
"[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in \
[..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
p.process(&p.bin("foo")).with_stdout("0\n").run();
// Touching bar.rs.in should cause the `build` command to run again.
{
let file = fs::File::create(&p.root().join("bar/src/bar.rs.in"));
2018-03-14 15:17:44 +00:00
file.unwrap()
.write_all(br#"pub fn gimme() -> i32 { 1 }"#)
.unwrap();
}
p.cargo("build")
.with_stderr(
"[COMPILING] bar v0.5.0 ([CWD]/bar)\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in \
[..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2018-03-14 15:17:44 +00:00
p.process(&p.bin("foo")).with_stdout("1\n").run();
}
#[test]
fn dev_deps_no_rebuild_lib() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = []
[dev-dependencies.bar]
path = "bar"
[lib]
name = "foo"
doctest = false
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/lib.rs",
r#"
2017-08-27 07:31:16 +00:00
#[cfg(test)] #[allow(unused_extern_crates)] extern crate bar;
2016-05-20 14:19:49 +00:00
#[cfg(not(test))] pub fn foo() { env!("FOO"); }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("bar/Cargo.toml", &basic_manifest("bar", "0.5.0"))
.file("bar/src/lib.rs", "pub fn bar() {}")
.build();
p.cargo("build")
.env("FOO", "bar")
.with_stderr(
"[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2018-03-14 15:17:44 +00:00
p.cargo("test")
.with_stderr(
"\
[COMPILING] [..] v0.5.0 ([CWD][..])
[COMPILING] [..] v0.5.0 ([CWD][..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-08-02 09:18:48 +00:00
[RUNNING] target/debug/deps/foo-[..][EXE]",
2018-12-08 11:19:47 +00:00
)
.with_stdout_contains("running 0 tests")
.run();
}
#[test]
fn custom_target_no_rebuild() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
a = { path = "a" }
[workspace]
members = ["a", "b"]
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-07-24 22:35:01 +00:00
.file("a/Cargo.toml", &basic_manifest("a", "0.5.0"))
.file("a/src/lib.rs", "")
2018-03-14 15:17:44 +00:00
.file(
"b/Cargo.toml",
r#"
[project]
name = "b"
version = "0.5.0"
authors = []
[dependencies]
a = { path = "../a" }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("b/src/lib.rs", "")
.build();
p.cargo("build")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] a v0.5.0 ([..])
[COMPILING] foo v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2018-03-14 15:17:44 +00:00
t!(fs::rename(
p.root().join("target"),
p.root().join("target_moved")
));
p.cargo("build --manifest-path=b/Cargo.toml")
.env("CARGO_TARGET_DIR", "target_moved")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] b v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn override_and_depend() {
let p = project()
.no_manifest()
2018-03-14 15:17:44 +00:00
.file(
"a/a1/Cargo.toml",
r#"
[project]
name = "a1"
version = "0.5.0"
authors = []
[dependencies]
a2 = { path = "../a2" }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("a/a1/src/lib.rs", "")
2018-07-24 22:35:01 +00:00
.file("a/a2/Cargo.toml", &basic_manifest("a2", "0.5.0"))
.file("a/a2/src/lib.rs", "")
2018-03-14 15:17:44 +00:00
.file(
"b/Cargo.toml",
r#"
[project]
name = "b"
version = "0.5.0"
authors = []
[dependencies]
a1 = { path = "../a/a1" }
a2 = { path = "../a/a2" }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("b/src/lib.rs", "")
.file("b/.cargo/config", r#"paths = ["../a"]"#)
.build();
p.cargo("build")
.cwd("b")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] a2 v0.5.0 ([..])
[COMPILING] a1 v0.5.0 ([..])
[COMPILING] b v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn missing_path_dependency() {
let p = project()
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("a", "0.5.0"))
.file("src/lib.rs", "")
.file(
".cargo/config",
r#"paths = ["../whoa-this-does-not-exist"]"#,
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("build")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[ERROR] failed to update path override `[..]../whoa-this-does-not-exist` \
(defined in `[..]`)
Caused by:
failed to read directory `[..]`
Caused by:
[..] (os error [..])
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn invalid_path_dep_in_workspace_with_lockfile() {
Package::new("bar", "1.0.0").publish();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "top"
version = "0.5.0"
authors = []
[workspace]
[dependencies]
foo = { path = "foo" }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-03-14 15:17:44 +00:00
.file(
"foo/Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
bar = "*"
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("foo/src/lib.rs", "")
.build();
// Generate a lock file
p.cargo("build").run();
// Change the dependency on `bar` to an invalid path
2018-03-14 15:17:44 +00:00
File::create(&p.root().join("foo/Cargo.toml"))
.unwrap()
.write_all(
br#"
[project]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
bar = { path = "" }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.unwrap();
// Make sure we get a nice error. In the past this actually stack
// overflowed!
p.cargo("build")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
2018-02-07 16:51:40 +00:00
error: no matching package named `bar` found
location searched: [..]
2019-01-14 19:34:46 +00:00
perhaps you meant: foo
2018-02-07 16:51:40 +00:00
required by package `foo v0.5.0 ([..])`
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn workspace_produces_rlib() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "top"
version = "0.5.0"
authors = []
[workspace]
[dependencies]
foo = { path = "foo" }
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-07-24 22:35:01 +00:00
.file("foo/Cargo.toml", &basic_manifest("foo", "0.5.0"))
.file("foo/src/lib.rs", "")
.build();
p.cargo("build").run();
2018-08-29 06:11:10 +00:00
assert!(p.root().join("target/debug/libtop.rlib").is_file());
assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
}
#[test]
fn thin_lto_works() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[project]
name = "top"
version = "0.5.0"
authors = []
[profile.release]
lto = 'thin'
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("build --release -v")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] top [..]
[RUNNING] `rustc [..] -C lto=thin [..]`
[FINISHED] [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}