about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-12-10 21:48:53 +0000
committerbors <bors@rust-lang.org>2023-12-10 21:48:53 +0000
commitd86d65bbc19b928387f68427fcc3a0da498d8a19 (patch)
tree4cb4a17ba1d40e64b5a126b044742176c49eace9 /tests
parentec4176167b964debee77015d0c67720639f9d673 (diff)
parentdc2f77aad617400b68ba607ca59bbf0121000b1f (diff)
downloadrust-d86d65bbc19b928387f68427fcc3a0da498d8a19.tar.gz
rust-d86d65bbc19b928387f68427fcc3a0da498d8a19.zip
Auto merge of #118368 - GuillaumeGomez:env-flag, r=Nilstrieb
Implement `--env` compiler flag (without `tracked_env` support)

Part of https://github.com/rust-lang/rust/issues/80792.
Implementation of https://github.com/rust-lang/compiler-team/issues/653.
Not an implementation of https://github.com/rust-lang/rfcs/pull/2794.

It adds the `--env` compiler flag option which allows to set environment values used by `env!` and `option_env!`.

Important to note: When trying to retrieve an environment variable value, it will first look into the ones defined with `--env`, and if there isn't one, then only it will look into the environment variables. So if you use `--env PATH=a`, then `env!("PATH")` will return `"a"` and not the actual `PATH` value.

As mentioned in the title, `tracked_env` support is not added here. I'll do it in a follow-up PR.

r? rust-lang/compiler
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/extenv/extenv-env-overload.rs9
-rw-r--r--tests/ui/extenv/extenv-env.rs5
-rw-r--r--tests/ui/extenv/extenv-not-env.rs7
-rw-r--r--tests/ui/feature-gates/env-flag.rs3
-rw-r--r--tests/ui/feature-gates/env-flag.stderr2
5 files changed, 26 insertions, 0 deletions
diff --git a/tests/ui/extenv/extenv-env-overload.rs b/tests/ui/extenv/extenv-env-overload.rs
new file mode 100644
index 00000000000..b82bb2fe966
--- /dev/null
+++ b/tests/ui/extenv/extenv-env-overload.rs
@@ -0,0 +1,9 @@
+// run-pass
+// rustc-env:MY_VAR=tadam
+// compile-flags: --env MY_VAR=123abc -Zunstable-options
+
+// This test ensures that variables provided with `--env` take precedence over
+// variables from environment.
+fn main() {
+    assert_eq!(env!("MY_VAR"), "123abc");
+}
diff --git a/tests/ui/extenv/extenv-env.rs b/tests/ui/extenv/extenv-env.rs
new file mode 100644
index 00000000000..9fda52b8941
--- /dev/null
+++ b/tests/ui/extenv/extenv-env.rs
@@ -0,0 +1,5 @@
+// compile-flags: --env FOO=123abc -Zunstable-options
+// run-pass
+fn main() {
+    assert_eq!(env!("FOO"), "123abc");
+}
diff --git a/tests/ui/extenv/extenv-not-env.rs b/tests/ui/extenv/extenv-not-env.rs
new file mode 100644
index 00000000000..d6c4a43b003
--- /dev/null
+++ b/tests/ui/extenv/extenv-not-env.rs
@@ -0,0 +1,7 @@
+// run-pass
+// rustc-env:MY_ENV=/
+// Ensures that variables not defined through `--env` are still available.
+
+fn main() {
+    assert!(!env!("MY_ENV").is_empty());
+}
diff --git a/tests/ui/feature-gates/env-flag.rs b/tests/ui/feature-gates/env-flag.rs
new file mode 100644
index 00000000000..9dfda2584fb
--- /dev/null
+++ b/tests/ui/feature-gates/env-flag.rs
@@ -0,0 +1,3 @@
+// compile-flags: --env A=B
+
+fn main() {}
diff --git a/tests/ui/feature-gates/env-flag.stderr b/tests/ui/feature-gates/env-flag.stderr
new file mode 100644
index 00000000000..5cb18cef9fb
--- /dev/null
+++ b/tests/ui/feature-gates/env-flag.stderr
@@ -0,0 +1,2 @@
+error: the `-Z unstable-options` flag must also be passed to enable the flag `env`
+