2021-02-24 19:49:23 +00:00
|
|
|
//! Tests for build.rs rerun-if-env-changed and rustc-env
|
2019-11-25 02:42:45 +00:00
|
|
|
|
2021-04-16 16:24:20 +00:00
|
|
|
use cargo_test_support::basic_manifest;
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::project;
|
|
|
|
use cargo_test_support::sleep_ms;
|
2017-06-05 14:52:31 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2017-06-05 14:52:31 +00:00
|
|
|
fn rerun_if_env_changes() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
2018-07-25 00:30:32 +00:00
|
|
|
.file("src/main.rs", "fn main() {}")
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"build.rs",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
fn main() {
|
|
|
|
println!("cargo:rerun-if-env-changed=FOO");
|
|
|
|
}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.build();
|
2017-06-05 14:52:31 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-07-24 13:01:56 +00:00
|
|
|
[COMPILING] foo v0.0.1 ([..])
|
2017-06-05 14:52:31 +00:00
|
|
|
[FINISHED] [..]
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.env("FOO", "bar")
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-07-24 13:01:56 +00:00
|
|
|
[COMPILING] foo v0.0.1 ([..])
|
2017-06-05 14:52:31 +00:00
|
|
|
[FINISHED] [..]
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.env("FOO", "baz")
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-07-24 13:01:56 +00:00
|
|
|
[COMPILING] foo v0.0.1 ([..])
|
2017-06-05 14:52:31 +00:00
|
|
|
[FINISHED] [..]
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.env("FOO", "baz")
|
|
|
|
.with_stderr("[FINISHED] [..]")
|
|
|
|
.run();
|
|
|
|
p.cargo("build")
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-07-24 13:01:56 +00:00
|
|
|
[COMPILING] foo v0.0.1 ([..])
|
2017-06-05 14:52:31 +00:00
|
|
|
[FINISHED] [..]
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2017-06-05 14:52:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2017-06-05 14:52:31 +00:00
|
|
|
fn rerun_if_env_or_file_changes() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
2018-07-25 00:30:32 +00:00
|
|
|
.file("src/main.rs", "fn main() {}")
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"build.rs",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
fn main() {
|
|
|
|
println!("cargo:rerun-if-env-changed=FOO");
|
|
|
|
println!("cargo:rerun-if-changed=foo");
|
|
|
|
}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file("foo", "")
|
2017-07-22 03:12:21 +00:00
|
|
|
.build();
|
2017-06-05 14:52:31 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-07-24 13:01:56 +00:00
|
|
|
[COMPILING] foo v0.0.1 ([..])
|
2017-06-05 14:52:31 +00:00
|
|
|
[FINISHED] [..]
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.env("FOO", "bar")
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-07-24 13:01:56 +00:00
|
|
|
[COMPILING] foo v0.0.1 ([..])
|
2017-06-05 14:52:31 +00:00
|
|
|
[FINISHED] [..]
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.env("FOO", "bar")
|
|
|
|
.with_stderr("[FINISHED] [..]")
|
|
|
|
.run();
|
2017-06-05 14:52:31 +00:00
|
|
|
sleep_ms(1000);
|
2020-04-17 04:10:11 +00:00
|
|
|
p.change_file("foo", "");
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.env("FOO", "bar")
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-07-24 13:01:56 +00:00
|
|
|
[COMPILING] foo v0.0.1 ([..])
|
2017-06-05 14:52:31 +00:00
|
|
|
[FINISHED] [..]
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2017-06-05 14:52:31 +00:00
|
|
|
}
|
2020-08-28 22:49:34 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
2021-02-17 17:14:28 +00:00
|
|
|
fn rustc_bootstrap() {
|
2021-04-17 15:31:48 +00:00
|
|
|
let build_rs = r#"
|
|
|
|
fn main() {
|
|
|
|
println!("cargo:rustc-env=RUSTC_BOOTSTRAP=1");
|
|
|
|
}
|
|
|
|
"#;
|
2020-08-28 22:49:34 +00:00
|
|
|
let p = project()
|
2021-04-16 16:24:20 +00:00
|
|
|
.file("Cargo.toml", &basic_manifest("has-dashes", "0.0.1"))
|
2021-04-17 15:31:48 +00:00
|
|
|
.file("src/lib.rs", "#![feature(rustc_attrs)]")
|
|
|
|
.file("build.rs", build_rs)
|
2020-08-28 22:49:34 +00:00
|
|
|
.build();
|
2021-04-17 15:31:48 +00:00
|
|
|
// RUSTC_BOOTSTRAP unset on stable should error
|
2020-08-28 22:49:34 +00:00
|
|
|
p.cargo("build")
|
2021-02-17 17:14:28 +00:00
|
|
|
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
|
2021-04-16 16:24:20 +00:00
|
|
|
.with_stderr_contains(
|
|
|
|
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
|
|
|
|
)
|
2020-08-28 22:49:34 +00:00
|
|
|
.with_status(101)
|
|
|
|
.run();
|
2021-04-17 15:59:41 +00:00
|
|
|
// nightly should warn whether or not RUSTC_BOOTSTRAP is set
|
2021-02-17 17:14:28 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.masquerade_as_nightly_cargo()
|
2021-04-17 15:59:41 +00:00
|
|
|
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
|
|
|
|
// (this matters when tests are being run with a beta or stable cargo)
|
|
|
|
.env("RUSTC_BOOTSTRAP", "1")
|
2021-02-17 17:14:28 +00:00
|
|
|
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
|
|
|
|
.run();
|
2021-04-17 15:31:48 +00:00
|
|
|
// RUSTC_BOOTSTRAP set to the name of the library should warn
|
2021-04-16 15:48:44 +00:00
|
|
|
p.cargo("build")
|
2021-04-16 16:24:20 +00:00
|
|
|
.env("RUSTC_BOOTSTRAP", "has_dashes")
|
2021-04-16 15:48:44 +00:00
|
|
|
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
|
|
|
|
.run();
|
2021-04-17 15:31:48 +00:00
|
|
|
// RUSTC_BOOTSTRAP set to some random value should error
|
2021-04-16 15:48:44 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.env("RUSTC_BOOTSTRAP", "bar")
|
|
|
|
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
|
2021-04-16 16:24:20 +00:00
|
|
|
.with_stderr_contains(
|
|
|
|
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
|
|
|
|
)
|
2021-04-16 15:48:44 +00:00
|
|
|
.with_status(101)
|
|
|
|
.run();
|
2021-04-17 15:31:48 +00:00
|
|
|
|
|
|
|
// Tests for binaries instead of libraries
|
|
|
|
let p = project()
|
|
|
|
.file("Cargo.toml", &basic_manifest("foo", "0.0.1"))
|
2021-04-17 15:59:41 +00:00
|
|
|
.file("src/main.rs", "#![feature(rustc_attrs)] fn main() {}")
|
2021-04-17 15:31:48 +00:00
|
|
|
.file("build.rs", build_rs)
|
|
|
|
.build();
|
2021-04-17 15:59:41 +00:00
|
|
|
// nightly should warn when there's no library whether or not RUSTC_BOOTSTRAP is set
|
2021-04-17 15:31:48 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.masquerade_as_nightly_cargo()
|
2021-04-17 15:59:41 +00:00
|
|
|
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
|
|
|
|
// (this matters when tests are being run with a beta or stable cargo)
|
|
|
|
.env("RUSTC_BOOTSTRAP", "1")
|
2021-04-17 15:31:48 +00:00
|
|
|
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
|
|
|
|
.run();
|
|
|
|
// RUSTC_BOOTSTRAP conditionally set when there's no library should error (regardless of the value)
|
|
|
|
p.cargo("build")
|
|
|
|
.env("RUSTC_BOOTSTRAP", "foo")
|
|
|
|
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
|
2021-04-17 15:59:41 +00:00
|
|
|
.with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=1` [..]")
|
2021-04-17 15:31:48 +00:00
|
|
|
.with_status(101)
|
|
|
|
.run();
|
2020-08-28 22:49:34 +00:00
|
|
|
}
|