fakeenv 0.1.0

A simple wrapper of `std::env` which allows faking the environment
Documentation
  • Coverage
  • 100%
    18 out of 18 items documented13 out of 18 items with examples
  • Size
  • Source code size: 35.67 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.92 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • qnighy/fakeenv
    5 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • qnighy

fakeenv

A simple wrapper of std::env which allows faking the environment.

Example

Using the real environment

use fakeenv::EnvStore;

fn answer(env: &EnvStore) -> i32 {
    env.var("THE_ANSWER").unwrap().parse().unwrap()
}

fn main() {
    std::env::set_var("THE_ANSWER", "42");

    let env = EnvStore::real();
    assert_eq!(answer(&env), 42);
}

Making a fake environment

Fake is only turned on when the fake feature is enabled.

As this is mostly for testing purpose, you might want to enable the feature like this:

[dependencies]
fakeenv = "0.1.0"

[dev-dependencies]
fakeenv = { version = "0.1.0", features = ["fake"] }

Then you can generate a fake environment using EnvStore::fake:

use fakeenv::EnvStore;

fn answer(env: &EnvStore) -> i32 {
    env.var("THE_ANSWER").unwrap().parse().unwrap()
}

fn main() {
    let env = EnvStore::fake();
    env.set_var("THE_ANSWER", "42");
    assert_eq!(answer(&env), 42);
}

Faking user directories

The dirs feature enables faking the dirs functions.

[dependencies]
fakeenv = { version = "0.1.0", features = ["dirs"] }
let env = EnvStore::real();
println!("home directory = {:?}", env.home_dir());