Skip to content

Commit

Permalink
test: add test to describe the issue#13280
Browse files Browse the repository at this point in the history
Cargo fails to detect environment variable is newly set and rebuild if the environment variable is set in `config.toml`
  • Loading branch information
linyihai committed Oct 24, 2024
1 parent 6f92aaa commit 4a96a40
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions tests/testsuite/cargo_env_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,166 @@ MAIN ENV_TEST:from-env
"#]])
.run();
}

#[cargo_test]
fn env_changed_defined_in_config_toml() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
use std::env;
fn main() {
println!( "{}", env!("ENV_TEST") );
}
"#,
)
.file(
".cargo/config.toml",
r#"
[env]
ENV_TEST = "from-config"
"#,
)
.build();

p.cargo("run")
.with_stdout_data(str![[r#"
from-config
"#]])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.5.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();

p.cargo("run")
.env("ENV_TEST", "from-env")
.with_stdout_data(str![[r#"
from-config
"#]])
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();

p.cargo("run")
.env("ENV_TEST", "from-env")
.with_stdout_data(str![[r#"
from-config
"#]])
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();
}

#[cargo_test]
fn forced_env_changed_defined_in_config_toml() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
use std::env;
fn main() {
println!( "{}", env!("ENV_TEST") );
}
"#,
)
.file(
".cargo/config.toml",
r#"
[env]
ENV_TEST = {value = "from-config", force = true}
"#,
)
.build();

p.cargo("run")
.with_stdout_data(str![[r#"
from-config
"#]])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.5.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();

p.cargo("run")
.env("ENV_TEST", "from-env")
.with_stdout_data(str![[r#"
from-config
"#]])
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();
}

#[cargo_test]
fn env_changed_defined_in_config_args() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
use std::env;
fn main() {
println!( "{}", env!("ENV_TEST") );
}
"#,
)
.build();
p.cargo(r#"run --config 'env.ENV_TEST="one"'"#)
.with_stdout_data(str![[r#"
one
"#]])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.5.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();

p.cargo(r#"run --config 'env.ENV_TEST="two"'"#)
.with_stdout_data(str![[r#"
one
"#]])
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();

p.cargo(r#"run --config 'env.ENV_TEST="two"'"#)
.with_stdout_data(str![[r#"
one
"#]])
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
"#]])
.run();
}

0 comments on commit 4a96a40

Please sign in to comment.