pub struct Jail { /* private fields */ }
Available on crate feature 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:

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

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

Creates a new jail that calls f, passing itself to f. Returns the result from f if f does not panic.

Panics

Panics if f panics.

Example
let result = figment::Jail::try_with(|jail| {
    /* in the jail */

    Ok(())
});

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

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

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

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.