Add tests for --env usage with tracked_env::var

This commit is contained in:
Guillaume Gomez 2023-12-11 16:43:57 +01:00
parent 5967ceb48d
commit 453ff1efd5
2 changed files with 45 additions and 0 deletions

View file

@ -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()
}

View file

@ -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);
}