cargo/tests/testsuite/path.rs

1152 lines
28 KiB
Rust
Raw Normal View History

2019-11-25 02:42:45 +00:00
//! Tests for `path` dependencies.
use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::registry::Package;
use cargo_test_support::{basic_lib_manifest, basic_manifest, main_file, project};
2019-09-12 19:52:46 +00:00
use cargo_test_support::{sleep_ms, t};
use std::fs;
2014-06-18 00:05:29 +00:00
#[cargo_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#"
[package]
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.bar]
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "bar"
"#,
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#"
[package]
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.baz]
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "baz"
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
[lib]
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
name = "bar"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/src/bar.rs",
r#"
2020-09-27 00:59:58 +00:00
extern crate baz;
2014-06-18 00:05:29 +00:00
2020-09-27 00:59:58 +00:00
pub fn gimme() -> String {
baz::gimme()
}
"#,
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#"
2020-09-27 00:59:58 +00:00
pub fn gimme() -> String {
"test passed".to_string()
}
"#,
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");
2023-11-15 22:33:27 +00:00
p.cargo("clean -v")
.with_stderr(
"[REMOVING] [CWD]/target\n\
[REMOVED] [..]",
)
.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();
}
#[cargo_test]
fn cargo_compile_with_root_dev_deps() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2014-07-03 01:13:00 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-07-03 01:13:00 +00:00
2020-09-27 00:59:58 +00:00
[dev-dependencies.bar]
2014-07-03 01:13:00 +00:00
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "../bar"
2014-07-03 01:13:00 +00:00
2020-09-27 00:59:58 +00:00
[[bin]]
name = "foo"
"#,
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#"
2020-09-27 00:59:58 +00:00
pub fn gimme() -> &'static str {
"zoidberg"
}
"#,
2018-12-08 11:19:47 +00:00
)
.build();
2014-07-03 01:13:00 +00:00
p.cargo("check")
.with_status(101)
.with_stderr_contains("[..]can't find crate for `bar`")
.run();
}
2014-07-03 01:13:00 +00:00
#[cargo_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#"
[package]
2014-07-03 01:13:00 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dev-dependencies.bar]
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "../bar"
2020-09-27 00:59:58 +00:00
[[bin]]
name = "foo"
"#,
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#"
2020-09-27 00:59:58 +00:00
pub fn gimme() -> &'static str {
"zoidberg"
}
"#,
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("test")
.with_stderr(
"\
[COMPILING] [..] v0.5.0 ([..])
[COMPILING] [..] v0.5.0 ([..])
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2021-02-21 22:37:42 +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
#[cargo_test]
fn cargo_compile_with_transitive_dev_deps() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "bar"
"#,
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#"
[package]
2020-09-27 00:59:58 +00:00
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dev-dependencies.baz]
2020-09-27 00:59:58 +00:00
git = "git://example.com/path/to/nowhere"
2020-09-27 00:59:58 +00:00
[lib]
2020-09-27 00:59:58 +00:00
name = "bar"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/src/bar.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn gimme() -> &'static str {
"zoidberg"
}
"#,
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();
}
#[cargo_test]
fn no_rebuild_dependency() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
path = "bar"
"#,
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("check")
.with_stderr(
"[CHECKING] bar v0.5.0 ([CWD]/bar)\n\
[CHECKING] 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#"
2020-09-27 00:59:58 +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("check")
.with_stderr(
"[CHECKING] foo v0.5.0 ([..])\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();
}
#[cargo_test]
fn deep_dependencies_trigger_rebuild() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
path = "bar"
"#,
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#"
[package]
2020-09-27 00:59:58 +00:00
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[lib]
name = "bar"
[dependencies.baz]
path = "../baz"
"#,
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("check")
.with_stderr(
"[CHECKING] baz v0.5.0 ([CWD]/baz)\n\
[CHECKING] bar v0.5.0 ([CWD]/bar)\n\
[CHECKING] 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();
2023-11-15 22:33:27 +00:00
p.cargo("check").with_stderr("[FINISHED] [..]").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);
p.change_file("baz/src/baz.rs", r#"pub fn baz() { println!("hello!"); }"#);
sleep_ms(1000);
p.cargo("check")
.with_stderr(
"[CHECKING] baz v0.5.0 ([CWD]/baz)\n\
[CHECKING] bar v0.5.0 ([CWD]/bar)\n\
[CHECKING] 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);
p.change_file(
"bar/src/bar.rs",
r#"
extern crate baz;
pub fn bar() { println!("hello!"); baz::baz(); }
"#,
);
sleep_ms(1000);
p.cargo("check")
.with_stderr(
"[CHECKING] bar v0.5.0 ([CWD]/bar)\n\
[CHECKING] 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();
}
#[cargo_test]
fn no_rebuild_two_deps() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
path = "bar"
[dependencies.baz]
path = "baz"
"#,
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#"
[package]
2020-09-27 00:59:58 +00:00
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[lib]
name = "bar"
[dependencies.baz]
path = "../baz"
"#,
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());
2023-11-15 22:33:27 +00:00
p.cargo("build").with_stderr("[FINISHED] [..]").run();
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
}
#[cargo_test]
fn nested_deps_recompile() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "src/bar"
"#,
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("check")
.with_stderr(
"[CHECKING] bar v0.5.0 ([CWD]/src/bar)\n\
[CHECKING] 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);
p.change_file("src/main.rs", r#"fn main() {}"#);
// This shouldn't recompile `bar`
p.cargo("check")
.with_stderr(
"[CHECKING] 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();
}
#[cargo_test]
fn error_message_for_missing_manifest() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
2020-09-27 00:59:58 +00:00
path = "src/bar"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.file("src/bar/not-a-manifest", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[ERROR] failed to get `bar` as a dependency of package `foo v0.5.0 [..]`
Caused by:
failed to load source for dependency `bar`
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
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();
}
#[cargo_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();
2024-01-26 19:40:46 +00:00
fs::write(
&paths::root().join(".cargo/config.toml"),
r#"paths = ["bar"]"#,
)
.unwrap();
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]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[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("check -v").run();
}
#[cargo_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
2024-01-26 19:40:46 +00:00
.file(
".cargo/config.toml",
&format!("paths = ['{}']", root.display()),
)
.file(
2018-03-14 15:17:44 +00:00
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
path = '{}'
2020-09-27 00:59:58 +00:00
"#,
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("check").run();
}
#[cargo_test]
fn override_path_dep() {
let bar = project()
.at("bar")
2018-03-14 15:17:44 +00:00
.file(
"p1/Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "p1"
version = "0.5.0"
authors = []
2020-09-27 00:59:58 +00:00
[dependencies.p2]
path = "../p2"
"#,
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(
2024-01-26 19:40:46 +00:00
".cargo/config.toml",
2018-03-14 15:17:44 +00:00
&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#"
2020-09-27 00:59:58 +00:00
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.p2]
path = '{}'
2020-09-27 00:59:58 +00:00
"#,
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("check -v").run();
}
#[cargo_test]
fn path_dep_build_cmd() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "bar"
"#,
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#"
[package]
2020-09-27 00:59:58 +00:00
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
build = "build.rs"
2020-09-27 00:59:58 +00:00
[lib]
name = "bar"
path = "src/bar.rs"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/build.rs",
r#"
2020-09-27 00:59:58 +00:00
use std::fs;
fn main() {
fs::copy("src/bar.rs.in", "src/bar.rs").unwrap();
}
"#,
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.
p.change_file("bar/src/bar.rs.in", "pub fn gimme() -> i32 { 1 }");
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();
}
#[cargo_test]
fn dev_deps_no_rebuild_lib() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
2020-09-27 00:59:58 +00:00
[dev-dependencies.bar]
path = "bar"
2020-09-27 00:59:58 +00:00
[lib]
name = "foo"
doctest = false
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/lib.rs",
r#"
2020-09-27 00:59:58 +00:00
#[cfg(test)] #[allow(unused_extern_crates)] extern crate bar;
#[cfg(not(test))] pub fn foo() { env!("FOO"); }
"#,
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] test [unoptimized + debuginfo] target(s) in [..]
2021-02-21 22:37:42 +00:00
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])",
2018-12-08 11:19:47 +00:00
)
.with_stdout_contains("running 0 tests")
.run();
}
#[cargo_test]
fn custom_target_no_rebuild() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
a = { path = "a" }
[workspace]
members = ["a", "b"]
"#,
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#"
[package]
2020-09-27 00:59:58 +00:00
name = "b"
version = "0.5.0"
authors = []
[dependencies]
a = { path = "../a" }
"#,
2018-12-08 11:19:47 +00:00
)
.file("b/src/lib.rs", "")
.build();
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[CHECKING] a v0.5.0 ([..])
[CHECKING] 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("check --manifest-path=b/Cargo.toml")
.env("CARGO_TARGET_DIR", "target_moved")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[CHECKING] 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();
}
#[cargo_test]
fn override_and_depend() {
let p = project()
.no_manifest()
2018-03-14 15:17:44 +00:00
.file(
"a/a1/Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "a1"
version = "0.5.0"
authors = []
[dependencies]
a2 = { path = "../a2" }
"#,
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#"
[package]
2020-09-27 00:59:58 +00:00
name = "b"
version = "0.5.0"
authors = []
[dependencies]
a1 = { path = "../a/a1" }
a2 = { path = "../a/a2" }
"#,
2018-12-08 11:19:47 +00:00
)
.file("b/src/lib.rs", "")
2024-01-26 19:40:46 +00:00
.file("b/.cargo/config.toml", r#"paths = ["../a"]"#)
.build();
p.cargo("check")
.cwd("b")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[WARNING] skipping duplicate package `a2` found at `[..]`
[CHECKING] a2 v0.5.0 ([..])
[CHECKING] a1 v0.5.0 ([..])
[CHECKING] 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();
}
#[cargo_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(
2024-01-26 19:40:46 +00:00
".cargo/config.toml",
r#"paths = ["../whoa-this-does-not-exist"]"#,
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] 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();
}
#[cargo_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#"
[package]
2020-09-27 00:59:58 +00:00
name = "top"
version = "0.5.0"
authors = []
2020-09-27 00:59:58 +00:00
[workspace]
2020-09-27 00:59:58 +00:00
[dependencies]
foo = { path = "foo" }
"#,
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#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
2020-09-27 00:59:58 +00:00
[dependencies]
bar = "*"
"#,
2018-12-08 11:19:47 +00:00
)
.file("foo/src/lib.rs", "")
.build();
// Generate a lock file
p.cargo("check").run();
// Change the dependency on `bar` to an invalid path
p.change_file(
"foo/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
bar = { path = "" }
"#,
);
// Make sure we get a nice error. In the past this actually stack
// overflowed!
p.cargo("check")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
error: no matching package found
searched package name: `bar`
perhaps you meant: foo
location searched: [..]
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();
}
#[cargo_test]
fn workspace_produces_rlib() {
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "top"
version = "0.5.0"
authors = []
2020-09-27 00:59:58 +00:00
[workspace]
2020-09-27 00:59:58 +00:00
[dependencies]
foo = { path = "foo" }
"#,
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());
}
#[cargo_test]
fn deep_path_error() {
// Test for an error loading a path deep in the dependency graph.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
a = {path="a"}
"#,
)
.file("src/lib.rs", "")
.file(
"a/Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "a"
version = "0.1.0"
[dependencies]
b = {path="../b"}
"#,
)
.file("a/src/lib.rs", "")
.file(
"b/Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "b"
version = "0.1.0"
[dependencies]
c = {path="../c"}
"#,
)
.file("b/src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr(
"\
[ERROR] failed to get `c` as a dependency of package `b v0.1.0 [..]`
... which satisfies path dependency `b` of package `a v0.1.0 [..]`
... which satisfies path dependency `a` of package `foo v0.1.0 [..]`
Caused by:
failed to load source for dependency `c`
Caused by:
Unable to update [..]/foo/c
Caused by:
failed to read `[..]/foo/c/Cargo.toml`
Caused by:
[..]
",
)
.run();
}
#[cargo_test]
fn catch_tricky_cycle() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "message"
version = "0.1.0"
[dev-dependencies]
test = { path = "test" }
"#,
)
.file("src/lib.rs", "")
.file(
"tangle/Cargo.toml",
r#"
[package]
name = "tangle"
version = "0.1.0"
[dependencies]
message = { path = ".." }
snapshot = { path = "../snapshot" }
"#,
)
.file("tangle/src/lib.rs", "")
.file(
"snapshot/Cargo.toml",
r#"
[package]
name = "snapshot"
version = "0.1.0"
[dependencies]
ledger = { path = "../ledger" }
"#,
)
.file("snapshot/src/lib.rs", "")
.file(
"ledger/Cargo.toml",
r#"
[package]
name = "ledger"
version = "0.1.0"
[dependencies]
tangle = { path = "../tangle" }
"#,
)
.file("ledger/src/lib.rs", "")
.file(
"test/Cargo.toml",
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
snapshot = { path = "../snapshot" }
"#,
)
.file("test/src/lib.rs", "")
.build();
p.cargo("test")
.with_stderr_contains("[..]cyclic package dependency[..]")
.with_status(101)
.run();
}