Struct rocket_db_pools::figment::Jail
[−]pub struct Jail { /* private fields */ }
test
only.Expand description
A “sandboxed” environment with isolated env and file system namespace.
Jail
creates a pseudo-sandboxed (not actually sandboxed) environment for
testing configurations. Specifically, Jail
:
- Synchronizes all calls to
Jail::expect_with()
andJail::try_with()
to prevent environment variables races. - Switches into a fresh temporary directory (
Jail::directory()
) where files can be created withJail::create_file()
. - Keeps track of environment variables created with
Jail::set_env()
and clears them when theJail
exits. - Deletes the temporary directory and all of its contents when exiting.
Additionally, because Jail
expects functions that return a Result
,
the ?
operator can be used liberally in a jail:
use figment::{Figment, Jail, providers::{Format, Toml, Env}};
figment::Jail::expect_with(|jail| {
jail.create_file("Cargo.toml", r#"
name = "test"
authors = ["bob"]
publish = false
"#)?;
jail.set_env("CARGO_NAME", "env-test");
let config: Config = Figment::new()
.merge(Toml::file("Cargo.toml"))
.merge(Env::prefixed("CARGO_"))
.extract()?;
Ok(())
});
Implementations
impl Jail
impl Jail
pub fn expect_with<F>(f: F) where
F: FnOnce(&mut Jail) -> Result<(), Error>,
pub fn expect_with<F>(f: F) where
F: FnOnce(&mut Jail) -> Result<(), Error>,
Creates a new jail that calls f
, passing itself to f
.
Panics
Panics if f
panics or if Jail::try_with(f)
returns
an Err
; prints the error message.
Example
figment::Jail::expect_with(|jail| {
/* in the jail */
Ok(())
});
pub fn directory(&self) -> &Path
pub fn directory(&self) -> &Path
Returns the directory the jail has switched into. The contents of this
directory will be cleared when Jail
is dropped.
Example
figment::Jail::expect_with(|jail| {
let tmp_directory = jail.directory();
Ok(())
});
pub fn create_file<P>(&self, path: P, contents: &str) -> Result<File, Error> where
P: AsRef<Path>,
pub fn create_file<P>(&self, path: P, contents: &str) -> Result<File, Error> where
P: AsRef<Path>,
Creates a file with contents contents
in the jail’s directory. The
file will be deleted with the jail is dropped.
Example
figment::Jail::expect_with(|jail| {
jail.create_file("MyConfig.json", "contents...");
Ok(())
});
pub fn set_env<K, V>(&mut self, k: K, v: V) where
K: AsRef<str>,
V: Display,
pub fn set_env<K, V>(&mut self, k: K, v: V) where
K: AsRef<str>,
V: Display,
Set the environment variable k
to value v
. The variable will be
removed when the jail is dropped.
Example
const VAR_NAME: &str = "my-very-special-figment-var";
assert!(std::env::var(VAR_NAME).is_err());
figment::Jail::expect_with(|jail| {
jail.set_env(VAR_NAME, "value");
assert!(std::env::var(VAR_NAME).is_ok());
Ok(())
});
assert!(std::env::var(VAR_NAME).is_err());
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Jail
impl Send for Jail
impl Sync for Jail
impl Unpin for Jail
impl UnwindSafe for Jail
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more