From 453ff1efd5b7644f62bcd2b594eb83225abb58be Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 11 Dec 2023 16:43:57 +0100 Subject: [PATCH] Add tests for `--env` usage with `tracked_env::var` --- tests/ui/proc-macro/auxiliary/env.rs | 28 ++++++++++++++++++++++++++++ tests/ui/proc-macro/env.rs | 17 +++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/ui/proc-macro/auxiliary/env.rs create mode 100644 tests/ui/proc-macro/env.rs diff --git a/tests/ui/proc-macro/auxiliary/env.rs b/tests/ui/proc-macro/auxiliary/env.rs new file mode 100644 index 00000000000..58bcb08bf06 --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/env.rs @@ -0,0 +1,28 @@ +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] +#![feature(proc_macro_tracked_env)] + +extern crate proc_macro; + +use proc_macro::TokenStream; +use proc_macro::tracked_env::var; + +#[proc_macro] +pub fn generate_const(input: TokenStream) -> TokenStream { + let the_const = match var("THE_CONST") { + Ok(x) if x == "12" => { + "const THE_CONST: u32 = 12;" + } + _ => { + "const THE_CONST: u32 = 0;" + } + }; + let another = if var("ANOTHER").is_ok() { + "const ANOTHER: u32 = 1;" + } else { + "const ANOTHER: u32 = 2;" + }; + format!("{the_const}{another}").parse().unwrap() +} diff --git a/tests/ui/proc-macro/env.rs b/tests/ui/proc-macro/env.rs new file mode 100644 index 00000000000..1b1d1873eb3 --- /dev/null +++ b/tests/ui/proc-macro/env.rs @@ -0,0 +1,17 @@ +// aux-build:env.rs +// run-pass +// rustc-env: THE_CONST=1 +// compile-flags: -Zunstable-options --env THE_CONST=12 --env ANOTHER=4 + +#![crate_name = "foo"] + +extern crate env; + +use env::generate_const; + +generate_const!(); + +fn main() { + assert_eq!(THE_CONST, 12); + assert_eq!(ANOTHER, 1); +}