about summary refs log tree commit diff
path: root/tests/ui/env-macro
diff options
context:
space:
mode:
authorbeetrees <b@beetr.ee>2024-03-17 19:04:42 +0000
committerbeetrees <b@beetr.ee>2024-03-17 21:59:40 +0000
commit36514015ffc58ede3995e96dc0b057fc8a32de0b (patch)
treed5b9776e3a47441c2613987944ab50c06ed8ebc3 /tests/ui/env-macro
parent35dfc67d94c47a6c6ae28c46e7dc1c547f772485 (diff)
downloadrust-36514015ffc58ede3995e96dc0b057fc8a32de0b.tar.gz
rust-36514015ffc58ede3995e96dc0b057fc8a32de0b.zip
Move `option_env!` and `env!` tests to the `env-macro` directory
Diffstat (limited to 'tests/ui/env-macro')
-rw-r--r--tests/ui/env-macro/env-arg-2-not-string-literal.rs1
-rw-r--r--tests/ui/env-macro/env-arg-2-not-string-literal.stderr8
-rw-r--r--tests/ui/env-macro/env-env-overload.rs9
-rw-r--r--tests/ui/env-macro/env-env.rs5
-rw-r--r--tests/ui/env-macro/env-escaped-var.rs3
-rw-r--r--tests/ui/env-macro/env-escaped-var.stderr11
-rw-r--r--tests/ui/env-macro/env-no-args.rs1
-rw-r--r--tests/ui/env-macro/env-no-args.stderr8
-rw-r--r--tests/ui/env-macro/env-not-defined-custom.rs1
-rw-r--r--tests/ui/env-macro/env-not-defined-custom.stderr10
-rw-r--r--tests/ui/env-macro/env-not-defined-default.rs4
-rw-r--r--tests/ui/env-macro/env-not-defined-default.stderr11
-rw-r--r--tests/ui/env-macro/env-not-env.rs7
-rw-r--r--tests/ui/env-macro/env-not-string-literal.rs1
-rw-r--r--tests/ui/env-macro/env-not-string-literal.stderr8
-rw-r--r--tests/ui/env-macro/env-too-many-args.rs1
-rw-r--r--tests/ui/env-macro/env-too-many-args.stderr8
-rw-r--r--tests/ui/env-macro/error-recovery-issue-55897.rs19
-rw-r--r--tests/ui/env-macro/error-recovery-issue-55897.stderr38
-rw-r--r--tests/ui/env-macro/name-whitespace-issue-110547.rs7
-rw-r--r--tests/ui/env-macro/name-whitespace-issue-110547.stderr29
-rw-r--r--tests/ui/env-macro/option_env-no-args.rs1
-rw-r--r--tests/ui/env-macro/option_env-no-args.stderr8
-rw-r--r--tests/ui/env-macro/option_env-not-defined.rs5
-rw-r--r--tests/ui/env-macro/option_env-not-string-literal.rs1
-rw-r--r--tests/ui/env-macro/option_env-not-string-literal.stderr8
-rw-r--r--tests/ui/env-macro/option_env-too-many-args.rs1
-rw-r--r--tests/ui/env-macro/option_env-too-many-args.stderr8
28 files changed, 222 insertions, 0 deletions
diff --git a/tests/ui/env-macro/env-arg-2-not-string-literal.rs b/tests/ui/env-macro/env-arg-2-not-string-literal.rs
new file mode 100644
index 00000000000..66dced478f9
--- /dev/null
+++ b/tests/ui/env-macro/env-arg-2-not-string-literal.rs
@@ -0,0 +1 @@
+fn main() { env!("one", 10); } //~ ERROR: expected string literal
diff --git a/tests/ui/env-macro/env-arg-2-not-string-literal.stderr b/tests/ui/env-macro/env-arg-2-not-string-literal.stderr
new file mode 100644
index 00000000000..843ce4823ea
--- /dev/null
+++ b/tests/ui/env-macro/env-arg-2-not-string-literal.stderr
@@ -0,0 +1,8 @@
+error: expected string literal
+  --> $DIR/env-arg-2-not-string-literal.rs:1:25
+   |
+LL | fn main() { env!("one", 10); }
+   |                         ^^
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/env-env-overload.rs b/tests/ui/env-macro/env-env-overload.rs
new file mode 100644
index 00000000000..b3497ffbc88
--- /dev/null
+++ b/tests/ui/env-macro/env-env-overload.rs
@@ -0,0 +1,9 @@
+//@ run-pass
+//@ rustc-env:MY_VAR=tadam
+//@ compile-flags: --env-set 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/env-macro/env-env.rs b/tests/ui/env-macro/env-env.rs
new file mode 100644
index 00000000000..18226256b64
--- /dev/null
+++ b/tests/ui/env-macro/env-env.rs
@@ -0,0 +1,5 @@
+//@ compile-flags: --env-set FOO=123abc -Zunstable-options
+//@ run-pass
+fn main() {
+    assert_eq!(env!("FOO"), "123abc");
+}
diff --git a/tests/ui/env-macro/env-escaped-var.rs b/tests/ui/env-macro/env-escaped-var.rs
new file mode 100644
index 00000000000..d898feb78c6
--- /dev/null
+++ b/tests/ui/env-macro/env-escaped-var.rs
@@ -0,0 +1,3 @@
+fn main() {
+    env!("\t"); //~ERROR environment variable `\t` not defined at compile time
+}
diff --git a/tests/ui/env-macro/env-escaped-var.stderr b/tests/ui/env-macro/env-escaped-var.stderr
new file mode 100644
index 00000000000..53a2ead6742
--- /dev/null
+++ b/tests/ui/env-macro/env-escaped-var.stderr
@@ -0,0 +1,11 @@
+error: environment variable `\t` not defined at compile time
+  --> $DIR/env-escaped-var.rs:2:5
+   |
+LL |     env!("\t");
+   |     ^^^^^^^^^^
+   |
+   = help: use `std::env::var("\t")` to read the variable at run time
+   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/env-no-args.rs b/tests/ui/env-macro/env-no-args.rs
new file mode 100644
index 00000000000..2ff6d242b27
--- /dev/null
+++ b/tests/ui/env-macro/env-no-args.rs
@@ -0,0 +1 @@
+fn main() { env!(); } //~ ERROR: `env!()` takes 1 or 2 arguments
diff --git a/tests/ui/env-macro/env-no-args.stderr b/tests/ui/env-macro/env-no-args.stderr
new file mode 100644
index 00000000000..3a94b36cd0f
--- /dev/null
+++ b/tests/ui/env-macro/env-no-args.stderr
@@ -0,0 +1,8 @@
+error: `env!()` takes 1 or 2 arguments
+  --> $DIR/env-no-args.rs:1:13
+   |
+LL | fn main() { env!(); }
+   |             ^^^^^^
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/env-not-defined-custom.rs b/tests/ui/env-macro/env-not-defined-custom.rs
new file mode 100644
index 00000000000..30b72783f5c
--- /dev/null
+++ b/tests/ui/env-macro/env-not-defined-custom.rs
@@ -0,0 +1 @@
+fn main() { env!("__HOPEFULLY_NOT_DEFINED__", "my error message"); } //~ ERROR: my error message
diff --git a/tests/ui/env-macro/env-not-defined-custom.stderr b/tests/ui/env-macro/env-not-defined-custom.stderr
new file mode 100644
index 00000000000..70c41bcc52e
--- /dev/null
+++ b/tests/ui/env-macro/env-not-defined-custom.stderr
@@ -0,0 +1,10 @@
+error: my error message
+  --> $DIR/env-not-defined-custom.rs:1:13
+   |
+LL | fn main() { env!("__HOPEFULLY_NOT_DEFINED__", "my error message"); }
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/env-not-defined-default.rs b/tests/ui/env-macro/env-not-defined-default.rs
new file mode 100644
index 00000000000..1fb046c78f2
--- /dev/null
+++ b/tests/ui/env-macro/env-not-defined-default.rs
@@ -0,0 +1,4 @@
+fn main() {
+    env!("CARGO__HOPEFULLY_NOT_DEFINED__");
+    //~^ ERROR: environment variable `CARGO__HOPEFULLY_NOT_DEFINED__` not defined
+}
diff --git a/tests/ui/env-macro/env-not-defined-default.stderr b/tests/ui/env-macro/env-not-defined-default.stderr
new file mode 100644
index 00000000000..efd7fdb4e53
--- /dev/null
+++ b/tests/ui/env-macro/env-not-defined-default.stderr
@@ -0,0 +1,11 @@
+error: environment variable `CARGO__HOPEFULLY_NOT_DEFINED__` not defined at compile time
+  --> $DIR/env-not-defined-default.rs:2:5
+   |
+LL |     env!("CARGO__HOPEFULLY_NOT_DEFINED__");
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO__HOPEFULLY_NOT_DEFINED__")` instead
+   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/env-not-env.rs b/tests/ui/env-macro/env-not-env.rs
new file mode 100644
index 00000000000..e903eab4e96
--- /dev/null
+++ b/tests/ui/env-macro/env-not-env.rs
@@ -0,0 +1,7 @@
+//@ run-pass
+//@ rustc-env:MY_ENV=/
+// Ensures that variables not defined through `--env-set` are still available.
+
+fn main() {
+    assert!(!env!("MY_ENV").is_empty());
+}
diff --git a/tests/ui/env-macro/env-not-string-literal.rs b/tests/ui/env-macro/env-not-string-literal.rs
new file mode 100644
index 00000000000..3eaa0b5daaf
--- /dev/null
+++ b/tests/ui/env-macro/env-not-string-literal.rs
@@ -0,0 +1 @@
+fn main() { env!(10, "two"); } //~ ERROR: expected string literal
diff --git a/tests/ui/env-macro/env-not-string-literal.stderr b/tests/ui/env-macro/env-not-string-literal.stderr
new file mode 100644
index 00000000000..0985459eff9
--- /dev/null
+++ b/tests/ui/env-macro/env-not-string-literal.stderr
@@ -0,0 +1,8 @@
+error: expected string literal
+  --> $DIR/env-not-string-literal.rs:1:18
+   |
+LL | fn main() { env!(10, "two"); }
+   |                  ^^
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/env-too-many-args.rs b/tests/ui/env-macro/env-too-many-args.rs
new file mode 100644
index 00000000000..ffad1c51303
--- /dev/null
+++ b/tests/ui/env-macro/env-too-many-args.rs
@@ -0,0 +1 @@
+fn main() { env!("one", "two", "three"); } //~ ERROR: `env!()` takes 1 or 2 arguments
diff --git a/tests/ui/env-macro/env-too-many-args.stderr b/tests/ui/env-macro/env-too-many-args.stderr
new file mode 100644
index 00000000000..f156026846a
--- /dev/null
+++ b/tests/ui/env-macro/env-too-many-args.stderr
@@ -0,0 +1,8 @@
+error: `env!()` takes 1 or 2 arguments
+  --> $DIR/env-too-many-args.rs:1:13
+   |
+LL | fn main() { env!("one", "two", "three"); }
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/error-recovery-issue-55897.rs b/tests/ui/env-macro/error-recovery-issue-55897.rs
new file mode 100644
index 00000000000..b6500e54059
--- /dev/null
+++ b/tests/ui/env-macro/error-recovery-issue-55897.rs
@@ -0,0 +1,19 @@
+use prelude::*; //~ ERROR unresolved import `prelude`
+
+mod unresolved_env {
+    use env; //~ ERROR unresolved import `env`
+
+    include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
+}
+
+mod nonexistent_env {
+    include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
+    //~^ ERROR environment variable `NON_EXISTENT` not defined
+}
+
+mod erroneous_literal {
+    include!(concat!("NON_EXISTENT"suffix, "/data.rs"));
+    //~^ ERROR suffixes on string literals are invalid
+}
+
+fn main() {}
diff --git a/tests/ui/env-macro/error-recovery-issue-55897.stderr b/tests/ui/env-macro/error-recovery-issue-55897.stderr
new file mode 100644
index 00000000000..5a20bf8b168
--- /dev/null
+++ b/tests/ui/env-macro/error-recovery-issue-55897.stderr
@@ -0,0 +1,38 @@
+error: environment variable `NON_EXISTENT` not defined at compile time
+  --> $DIR/error-recovery-issue-55897.rs:10:22
+   |
+LL |     include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
+   |                      ^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: use `std::env::var("NON_EXISTENT")` to read the variable at run time
+   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: suffixes on string literals are invalid
+  --> $DIR/error-recovery-issue-55897.rs:15:22
+   |
+LL |     include!(concat!("NON_EXISTENT"suffix, "/data.rs"));
+   |                      ^^^^^^^^^^^^^^^^^^^^ invalid suffix `suffix`
+
+error[E0432]: unresolved import `prelude`
+  --> $DIR/error-recovery-issue-55897.rs:1:5
+   |
+LL | use prelude::*;
+   |     ^^^^^^^
+   |     |
+   |     unresolved import
+   |     help: a similar path exists: `std::prelude`
+
+error[E0432]: unresolved import `env`
+  --> $DIR/error-recovery-issue-55897.rs:4:9
+   |
+LL |     use env;
+   |         ^^^ no `env` in the root
+   |
+help: consider importing this module instead
+   |
+LL |     use std::env;
+   |         ~~~~~~~~
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0432`.
diff --git a/tests/ui/env-macro/name-whitespace-issue-110547.rs b/tests/ui/env-macro/name-whitespace-issue-110547.rs
new file mode 100644
index 00000000000..2acfb2e671e
--- /dev/null
+++ b/tests/ui/env-macro/name-whitespace-issue-110547.rs
@@ -0,0 +1,7 @@
+//@ compile-flags: -C debug-assertions
+
+fn main() {
+    env!{"\t"}; //~ ERROR not defined at compile time
+    env!("\t"); //~ ERROR not defined at compile time
+    env!("\u{2069}"); //~ ERROR not defined at compile time
+}
diff --git a/tests/ui/env-macro/name-whitespace-issue-110547.stderr b/tests/ui/env-macro/name-whitespace-issue-110547.stderr
new file mode 100644
index 00000000000..5f34904d4ae
--- /dev/null
+++ b/tests/ui/env-macro/name-whitespace-issue-110547.stderr
@@ -0,0 +1,29 @@
+error: environment variable `\t` not defined at compile time
+  --> $DIR/name-whitespace-issue-110547.rs:4:5
+   |
+LL |     env!{"\t"};
+   |     ^^^^^^^^^^
+   |
+   = help: use `std::env::var("\t")` to read the variable at run time
+   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: environment variable `\t` not defined at compile time
+  --> $DIR/name-whitespace-issue-110547.rs:5:5
+   |
+LL |     env!("\t");
+   |     ^^^^^^^^^^
+   |
+   = help: use `std::env::var("\t")` to read the variable at run time
+   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: environment variable `\u{2069}` not defined at compile time
+  --> $DIR/name-whitespace-issue-110547.rs:6:5
+   |
+LL |     env!("\u{2069}");
+   |     ^^^^^^^^^^^^^^^^
+   |
+   = help: use `std::env::var("\u{2069}")` to read the variable at run time
+   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 3 previous errors
+
diff --git a/tests/ui/env-macro/option_env-no-args.rs b/tests/ui/env-macro/option_env-no-args.rs
new file mode 100644
index 00000000000..bc5f77bc62e
--- /dev/null
+++ b/tests/ui/env-macro/option_env-no-args.rs
@@ -0,0 +1 @@
+fn main() { option_env!(); } //~ ERROR: option_env! takes 1 argument
diff --git a/tests/ui/env-macro/option_env-no-args.stderr b/tests/ui/env-macro/option_env-no-args.stderr
new file mode 100644
index 00000000000..d621aff770d
--- /dev/null
+++ b/tests/ui/env-macro/option_env-no-args.stderr
@@ -0,0 +1,8 @@
+error: option_env! takes 1 argument
+  --> $DIR/option_env-no-args.rs:1:13
+   |
+LL | fn main() { option_env!(); }
+   |             ^^^^^^^^^^^^^
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/option_env-not-defined.rs b/tests/ui/env-macro/option_env-not-defined.rs
new file mode 100644
index 00000000000..90a01a80313
--- /dev/null
+++ b/tests/ui/env-macro/option_env-not-defined.rs
@@ -0,0 +1,5 @@
+//@ run-pass
+
+pub fn main() {
+    assert!(option_env!("__HOPEFULLY_DOESNT_EXIST__").is_none());
+}
diff --git a/tests/ui/env-macro/option_env-not-string-literal.rs b/tests/ui/env-macro/option_env-not-string-literal.rs
new file mode 100644
index 00000000000..27c3a8e83db
--- /dev/null
+++ b/tests/ui/env-macro/option_env-not-string-literal.rs
@@ -0,0 +1 @@
+fn main() { option_env!(10); } //~ ERROR: argument must be a string literal
diff --git a/tests/ui/env-macro/option_env-not-string-literal.stderr b/tests/ui/env-macro/option_env-not-string-literal.stderr
new file mode 100644
index 00000000000..3d2542a0e6c
--- /dev/null
+++ b/tests/ui/env-macro/option_env-not-string-literal.stderr
@@ -0,0 +1,8 @@
+error: argument must be a string literal
+  --> $DIR/option_env-not-string-literal.rs:1:25
+   |
+LL | fn main() { option_env!(10); }
+   |                         ^^
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/env-macro/option_env-too-many-args.rs b/tests/ui/env-macro/option_env-too-many-args.rs
new file mode 100644
index 00000000000..ecc8b61ac85
--- /dev/null
+++ b/tests/ui/env-macro/option_env-too-many-args.rs
@@ -0,0 +1 @@
+fn main() { option_env!("one", "two"); } //~ ERROR: option_env! takes 1 argument
diff --git a/tests/ui/env-macro/option_env-too-many-args.stderr b/tests/ui/env-macro/option_env-too-many-args.stderr
new file mode 100644
index 00000000000..b4da3670787
--- /dev/null
+++ b/tests/ui/env-macro/option_env-too-many-args.stderr
@@ -0,0 +1,8 @@
+error: option_env! takes 1 argument
+  --> $DIR/option_env-too-many-args.rs:1:13
+   |
+LL | fn main() { option_env!("one", "two"); }
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 1 previous error
+