-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for [env] section in .cargo/config.toml #9175
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,126 @@ | ||
//! 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("[..]`env.ENV_TEST_BOOL` expected a string, but found a boolean") | ||
.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") ); | ||
} | ||
"#, | ||
) | ||
.file( | ||
".cargo/config", | ||
r#" | ||
[env] | ||
ENV_TEST_UNFORCED = "from-config" | ||
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") | ||
.with_stdout_contains("ENV_TEST_FORCED:from-config") | ||
.with_stdout_contains("ENV_TEST_UNFORCED: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_RELATIVE:{}", env!("ENV_TEST_RELATIVE") ); | ||
println!( "ENV_TEST_ABSOLUTE:{}", env!("ENV_TEST_ABSOLUTE") ); | ||
|
||
assert!( Path::new(env!("ENV_TEST_ABSOLUTE")).is_absolute() ); | ||
assert!( !Path::new(env!("ENV_TEST_RELATIVE")).is_absolute() ); | ||
} | ||
"#, | ||
) | ||
.file( | ||
".cargo/config", | ||
r#" | ||
[env] | ||
ENV_TEST_RELATIVE = "Cargo.toml" | ||
ENV_TEST_ABSOLUTE = { 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to do custom serde deserialization (e.g. via
#[serde(untagged)]
) instead of having custom deserialization logic here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I tried that originally and could not get it to work. I had something like:
The problem was serde would never parse the
Value<T>
type in theWithOptions
variant, it worked if it was a regularString
. I'm not sure why, maybe the entire "path" from the root of the deserialization needs to beValue<T>
types in order for them to be handle correctly in some inner struct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this structure may work instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that doesn't work either. It seems like
untagged
and theValue<T>
types do not play nicely together, it will never select a variant with that type. Latest commit does contain a struct/enum layout that does work though. So no more custom logic.