[−][src]Struct env_test_util::TempEnvVar
Temporary environment variable manager
When initialising the variable manager with new
, the actual content will be removed and stored
in initial_value
. You can then set a temporary value using the method with
. The environment
variable will then be reset to it's initial value when it will be dropped.
Examples
use env_test_util::TempEnvVar; std::env::set_var("MY_VARIABLE", "ORIGINAL"); // set the variable to "ORIGINAL" let variable = TempEnvVar::new("MY_VARIABLE"); // read the variable and stores it assert_eq!(std::env::var("MY_VARIABLE").ok(), None); let variable = variable.with("NEW_CONTENT"); // set the environment variable with a new content assert_eq!(std::env::var("MY_VARIABLE").ok(), Some("NEW_CONTENT".into())); drop(variable); assert_eq!(std::env::var("MY_VARIABLE").ok(), Some("ORIGINAL".into()));
Don't forget to assign the variable in your tests, otherwise the drop
function will be called right away
use env_test_util::TempEnvVar; std::env::set_var("MY_VARIABLE", "ORIGINAL"); // set the variable to "ORIGINAL" TempEnvVar::new("MY_VARIABLE").with("SOMETHING_ELSE"); // read the variable and stores it assert_eq!(std::env::var("MY_VARIABLE").ok(), Some("ORIGINAL".into())); let _variable = TempEnvVar::new("MY_VARIABLE").with("SOMETHING_ELSE"); // Instead, store it in a variable assert_eq!(std::env::var("MY_VARIABLE").ok(), Some("SOMETHING_ELSE".into()));
Fields
key: String
name of the environment variable
initial_value: Option<String>
initial value of the environment variable
Implementations
impl TempEnvVar
[src]
pub fn new(key: &str) -> Self
[src]
creates a new temporary environment variable manager
pub fn with(self, value: &str) -> Self
[src]
set the environment with a new temporary value
Trait Implementations
impl Drop for TempEnvVar
[src]
Auto Trait Implementations
impl RefUnwindSafe for TempEnvVar
impl Send for TempEnvVar
impl Sync for TempEnvVar
impl Unpin for TempEnvVar
impl UnwindSafe for TempEnvVar
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,