-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #9175 - wickerwaka:env-section, r=alexcrichton
Add support for [env] section in .cargo/config.toml This adds support for an `[env]` section in the config.toml files. Environment variables set in this section will be applied to the environment of any processes executed by cargo. This is implemented to follow the recommendations in #8839 (comment) Variables have optional `force` and `relative` flags. `force` means the variable can override an existing environment variable. `relative` means the variable represents a path relative to the location of the directory that contains the `.cargo/` directory that contains the `config.toml` file. A relative variable will have an absolute path prepended to it before setting it in the environment. ``` [env] FOOBAR = "Apple" PATH_TO_SOME_TOOL = { value = "bin/tool", relative = true } USERNAME = { value = "test_user", force = true } ``` Fixes #4121
- Loading branch information
Showing
5 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//! Tests for `[env]` config. | ||
|
||
use cargo_test_support::{basic_bin_manifest, project}; | ||
|
||
#[cargo_test] | ||
fn env_basic() { | ||
let p = project() | ||
.file("Cargo.toml", &basic_bin_manifest("foo")) | ||
.file( | ||
"src/main.rs", | ||
r#" | ||
use std::env; | ||
fn main() { | ||
println!( "compile-time:{}", env!("ENV_TEST_1233") ); | ||
println!( "run-time:{}", env::var("ENV_TEST_1233").unwrap()); | ||
} | ||
"#, | ||
) | ||
.file( | ||
".cargo/config", | ||
r#" | ||
[env] | ||
ENV_TEST_1233 = "Hello" | ||
"#, | ||
) | ||
.build(); | ||
|
||
p.cargo("run -Zconfigurable-env") | ||
.masquerade_as_nightly_cargo() | ||
.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")) | ||
.file( | ||
"src/main.rs", | ||
r#" | ||
fn main() { | ||
} | ||
"#, | ||
) | ||
.file( | ||
".cargo/config", | ||
r#" | ||
[env] | ||
ENV_TEST_BOOL = false | ||
"#, | ||
) | ||
.build(); | ||
|
||
p.cargo("build -Zconfigurable-env") | ||
.masquerade_as_nightly_cargo() | ||
.with_status(101) | ||
.with_stderr_contains("[..]could not load config key `env.ENV_TEST_BOOL`") | ||
.run(); | ||
} | ||
|
||
#[cargo_test] | ||
fn env_force() { | ||
let p = project() | ||
.file("Cargo.toml", &basic_bin_manifest("foo")) | ||
.file( | ||
"src/main.rs", | ||
r#" | ||
use std::env; | ||
fn main() { | ||
println!( "ENV_TEST_FORCED:{}", env!("ENV_TEST_FORCED") ); | ||
println!( "ENV_TEST_UNFORCED:{}", env!("ENV_TEST_UNFORCED") ); | ||
println!( "ENV_TEST_UNFORCED_DEFAULT:{}", env!("ENV_TEST_UNFORCED_DEFAULT") ); | ||
} | ||
"#, | ||
) | ||
.file( | ||
".cargo/config", | ||
r#" | ||
[env] | ||
ENV_TEST_UNFORCED_DEFAULT = "from-config" | ||
ENV_TEST_UNFORCED = { value = "from-config", force = false } | ||
ENV_TEST_FORCED = { value = "from-config", force = true } | ||
"#, | ||
) | ||
.build(); | ||
|
||
p.cargo("run -Zconfigurable-env") | ||
.masquerade_as_nightly_cargo() | ||
.env("ENV_TEST_FORCED", "from-env") | ||
.env("ENV_TEST_UNFORCED", "from-env") | ||
.env("ENV_TEST_UNFORCED_DEFAULT", "from-env") | ||
.with_stdout_contains("ENV_TEST_FORCED:from-config") | ||
.with_stdout_contains("ENV_TEST_UNFORCED:from-env") | ||
.with_stdout_contains("ENV_TEST_UNFORCED_DEFAULT:from-env") | ||
.run(); | ||
} | ||
|
||
#[cargo_test] | ||
fn env_relative() { | ||
let p = project() | ||
.file("Cargo.toml", &basic_bin_manifest("foo2")) | ||
.file( | ||
"src/main.rs", | ||
r#" | ||
use std::env; | ||
use std::path::Path; | ||
fn main() { | ||
println!( "ENV_TEST_REGULAR:{}", env!("ENV_TEST_REGULAR") ); | ||
println!( "ENV_TEST_REGULAR_DEFAULT:{}", env!("ENV_TEST_REGULAR_DEFAULT") ); | ||
println!( "ENV_TEST_RELATIVE:{}", env!("ENV_TEST_RELATIVE") ); | ||
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() ); | ||
} | ||
"#, | ||
) | ||
.file( | ||
".cargo/config", | ||
r#" | ||
[env] | ||
ENV_TEST_REGULAR = { value = "Cargo.toml", relative = false } | ||
ENV_TEST_REGULAR_DEFAULT = "Cargo.toml" | ||
ENV_TEST_RELATIVE = { value = "Cargo.toml", relative = true } | ||
"#, | ||
) | ||
.build(); | ||
|
||
p.cargo("run -Zconfigurable-env") | ||
.masquerade_as_nightly_cargo() | ||
.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters