2021-02-15 23:17:00 +00:00
|
|
|
//! Tests for `[env]` config.
|
|
|
|
|
2023-04-24 18:44:52 +00:00
|
|
|
use cargo_test_support::basic_manifest;
|
2021-02-15 23:17:00 +00:00
|
|
|
use cargo_test_support::{basic_bin_manifest, project};
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn env_basic() {
|
|
|
|
let p = project()
|
|
|
|
.file("Cargo.toml", &basic_bin_manifest("foo"))
|
2021-02-15 23:29:08 +00:00
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
use std::env;
|
|
|
|
fn main() {
|
|
|
|
println!( "compile-time:{}", env!("ENV_TEST_1233") );
|
|
|
|
println!( "run-time:{}", env::var("ENV_TEST_1233").unwrap());
|
|
|
|
}
|
2021-02-15 23:29:08 +00:00
|
|
|
"#,
|
|
|
|
)
|
2021-02-15 23:17:00 +00:00
|
|
|
.file(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2021-02-15 23:29:08 +00:00
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
[env]
|
|
|
|
ENV_TEST_1233 = "Hello"
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2021-04-25 04:55:52 +00:00
|
|
|
p.cargo("run")
|
2021-02-15 23:17:00 +00:00
|
|
|
.with_stdout_contains("compile-time:Hello")
|
|
|
|
.with_stdout_contains("run-time:Hello")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn env_invalid() {
|
|
|
|
let p = project()
|
|
|
|
.file("Cargo.toml", &basic_bin_manifest("foo"))
|
2021-02-15 23:29:08 +00:00
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
fn main() {
|
|
|
|
}
|
2021-02-15 23:29:08 +00:00
|
|
|
"#,
|
|
|
|
)
|
2021-02-15 23:17:00 +00:00
|
|
|
.file(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2021-02-15 23:29:08 +00:00
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
[env]
|
|
|
|
ENV_TEST_BOOL = false
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2023-02-15 21:57:30 +00:00
|
|
|
p.cargo("check")
|
2021-02-15 23:17:00 +00:00
|
|
|
.with_status(101)
|
2021-02-20 21:49:08 +00:00
|
|
|
.with_stderr_contains("[..]could not load config key `env.ENV_TEST_BOOL`")
|
2021-02-15 23:17:00 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:32:22 +00:00
|
|
|
#[cargo_test]
|
2023-05-07 17:01:43 +00:00
|
|
|
fn env_no_disallowed() {
|
|
|
|
// Checks for keys that are not allowed in the [env] table.
|
2023-01-28 11:32:22 +00:00
|
|
|
let p = project()
|
2023-05-07 17:01:43 +00:00
|
|
|
.file("Cargo.toml", &basic_manifest("foo", "1.0.0"))
|
|
|
|
.file("src/lib.rs", "")
|
2023-01-28 11:32:22 +00:00
|
|
|
.build();
|
|
|
|
|
2023-05-08 16:30:59 +00:00
|
|
|
for disallowed in &["CARGO_HOME", "RUSTUP_HOME", "RUSTUP_TOOLCHAIN"] {
|
2023-05-07 17:01:43 +00:00
|
|
|
p.change_file(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2023-05-07 17:01:43 +00:00
|
|
|
&format!(
|
|
|
|
r#"
|
|
|
|
[env]
|
|
|
|
{disallowed} = "foo"
|
|
|
|
"#
|
|
|
|
),
|
|
|
|
);
|
|
|
|
p.cargo("check")
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr(&format!(
|
|
|
|
"[ERROR] setting the `{disallowed}` environment variable \
|
|
|
|
is not supported in the `[env]` configuration table"
|
|
|
|
))
|
|
|
|
.run();
|
|
|
|
}
|
2023-01-28 11:32:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 23:17:00 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn env_force() {
|
|
|
|
let p = project()
|
|
|
|
.file("Cargo.toml", &basic_bin_manifest("foo"))
|
2021-02-15 23:29:08 +00:00
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
use std::env;
|
|
|
|
fn main() {
|
|
|
|
println!( "ENV_TEST_FORCED:{}", env!("ENV_TEST_FORCED") );
|
|
|
|
println!( "ENV_TEST_UNFORCED:{}", env!("ENV_TEST_UNFORCED") );
|
2021-02-20 21:49:08 +00:00
|
|
|
println!( "ENV_TEST_UNFORCED_DEFAULT:{}", env!("ENV_TEST_UNFORCED_DEFAULT") );
|
2021-02-15 23:17:00 +00:00
|
|
|
}
|
2021-02-15 23:29:08 +00:00
|
|
|
"#,
|
|
|
|
)
|
2021-02-15 23:17:00 +00:00
|
|
|
.file(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2021-02-15 23:29:08 +00:00
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
[env]
|
2021-02-20 21:49:08 +00:00
|
|
|
ENV_TEST_UNFORCED_DEFAULT = "from-config"
|
|
|
|
ENV_TEST_UNFORCED = { value = "from-config", force = false }
|
2021-02-15 23:17:00 +00:00
|
|
|
ENV_TEST_FORCED = { value = "from-config", force = true }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2021-04-25 04:55:52 +00:00
|
|
|
p.cargo("run")
|
2021-02-15 23:17:00 +00:00
|
|
|
.env("ENV_TEST_FORCED", "from-env")
|
|
|
|
.env("ENV_TEST_UNFORCED", "from-env")
|
2021-02-20 21:49:08 +00:00
|
|
|
.env("ENV_TEST_UNFORCED_DEFAULT", "from-env")
|
2021-02-15 23:17:00 +00:00
|
|
|
.with_stdout_contains("ENV_TEST_FORCED:from-config")
|
|
|
|
.with_stdout_contains("ENV_TEST_UNFORCED:from-env")
|
2021-02-20 21:49:08 +00:00
|
|
|
.with_stdout_contains("ENV_TEST_UNFORCED_DEFAULT:from-env")
|
2021-02-15 23:17:00 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn env_relative() {
|
|
|
|
let p = project()
|
|
|
|
.file("Cargo.toml", &basic_bin_manifest("foo2"))
|
2021-02-15 23:29:08 +00:00
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
|
|
|
fn main() {
|
2021-02-20 21:49:08 +00:00
|
|
|
println!( "ENV_TEST_REGULAR:{}", env!("ENV_TEST_REGULAR") );
|
|
|
|
println!( "ENV_TEST_REGULAR_DEFAULT:{}", env!("ENV_TEST_REGULAR_DEFAULT") );
|
2021-02-15 23:17:00 +00:00
|
|
|
println!( "ENV_TEST_RELATIVE:{}", env!("ENV_TEST_RELATIVE") );
|
|
|
|
|
2021-02-20 21:49:08 +00:00
|
|
|
assert!( Path::new(env!("ENV_TEST_RELATIVE")).is_absolute() );
|
|
|
|
assert!( !Path::new(env!("ENV_TEST_REGULAR")).is_absolute() );
|
|
|
|
assert!( !Path::new(env!("ENV_TEST_REGULAR_DEFAULT")).is_absolute() );
|
2021-02-15 23:17:00 +00:00
|
|
|
}
|
2021-02-15 23:29:08 +00:00
|
|
|
"#,
|
|
|
|
)
|
2021-02-15 23:17:00 +00:00
|
|
|
.file(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2021-02-15 23:29:08 +00:00
|
|
|
r#"
|
2021-02-15 23:17:00 +00:00
|
|
|
[env]
|
2021-02-20 21:49:08 +00:00
|
|
|
ENV_TEST_REGULAR = { value = "Cargo.toml", relative = false }
|
|
|
|
ENV_TEST_REGULAR_DEFAULT = "Cargo.toml"
|
|
|
|
ENV_TEST_RELATIVE = { value = "Cargo.toml", relative = true }
|
2021-02-15 23:17:00 +00:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2021-04-25 04:55:52 +00:00
|
|
|
p.cargo("run").run();
|
2021-02-15 23:17:00 +00:00
|
|
|
}
|
2021-06-12 15:48:23 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn env_no_override() {
|
|
|
|
let p = project()
|
|
|
|
.file("Cargo.toml", &basic_bin_manifest("unchanged"))
|
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
|
|
|
use std::env;
|
|
|
|
fn main() {
|
|
|
|
println!( "CARGO_PKG_NAME:{}", env!("CARGO_PKG_NAME") );
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2021-06-12 15:48:23 +00:00
|
|
|
r#"
|
|
|
|
[env]
|
|
|
|
CARGO_PKG_NAME = { value = "from-config", force = true }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2022-07-15 00:55:44 +00:00
|
|
|
p.cargo("run")
|
2021-06-12 15:48:23 +00:00
|
|
|
.with_stdout_contains("CARGO_PKG_NAME:unchanged")
|
|
|
|
.run();
|
|
|
|
}
|
2023-04-24 18:44:52 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn env_applied_to_target_info_discovery_rustc() {
|
|
|
|
let wrapper = project()
|
|
|
|
.at("wrapper")
|
|
|
|
.file("Cargo.toml", &basic_manifest("wrapper", "1.0.0"))
|
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let mut args = std::env::args().skip(1);
|
|
|
|
let env_test = std::env::var("ENV_TEST").unwrap();
|
|
|
|
eprintln!("WRAPPER ENV_TEST:{env_test}");
|
|
|
|
let status = std::process::Command::new(&args.next().unwrap())
|
|
|
|
.args(args).status().unwrap();
|
|
|
|
std::process::exit(status.code().unwrap_or(1));
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
wrapper.cargo("build").run();
|
|
|
|
let wrapper = &wrapper.bin("wrapper");
|
|
|
|
|
|
|
|
let p = project()
|
|
|
|
.file("Cargo.toml", &basic_bin_manifest("foo"))
|
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
eprintln!( "MAIN ENV_TEST:{}", std::env!("ENV_TEST") );
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2023-04-24 18:44:52 +00:00
|
|
|
r#"
|
|
|
|
[env]
|
|
|
|
ENV_TEST = "from-config"
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
p.cargo("run")
|
|
|
|
.env("RUSTC_WORKSPACE_WRAPPER", wrapper)
|
2023-04-24 17:59:01 +00:00
|
|
|
.with_stderr_contains("WRAPPER ENV_TEST:from-config")
|
|
|
|
.with_stderr_contains("MAIN ENV_TEST:from-config")
|
2023-04-24 18:44:52 +00:00
|
|
|
.run();
|
|
|
|
|
|
|
|
// Ensure wrapper also maintains the same overridden priority for envs.
|
|
|
|
p.cargo("clean").run();
|
|
|
|
p.cargo("run")
|
|
|
|
.env("ENV_TEST", "from-env")
|
|
|
|
.env("RUSTC_WORKSPACE_WRAPPER", wrapper)
|
|
|
|
.with_stderr_contains("WRAPPER ENV_TEST:from-env")
|
|
|
|
.with_stderr_contains("MAIN ENV_TEST:from-env")
|
|
|
|
.run();
|
|
|
|
}
|