Skip to content

Commit 2bebec4

Browse files
committed
docs: crate
1 parent 9ec21f0 commit 2bebec4

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
1+
//! Test kit for logics which involves environment variables.
2+
//!
3+
//! # Usage
4+
//!
5+
//! To test environment-related logics:
6+
//! ```
7+
//! use envtestkit::set_env;
8+
//! use envtestkit::lock::lock_read;
9+
//! use std::ffi::OsString;
10+
//!
11+
//! let _lock = lock_test();
12+
//! let _test = set_env(OsString::from("ENV_KEY"), "value");
13+
//! // Do your test here ...
14+
//! ```
15+
//!
16+
//! Complete examples can be found in `examples/` directory of this crate.
17+
//!
18+
//! # Race Condition
19+
//!
20+
//! Environment variables are global states. These things need to be ensured for building robust tests:
21+
//! - Run environment-modifying tests in serial.
22+
//! - Run environment-sensitive tests while no environment-modifying tests are running at the moment.
23+
//!
24+
//! For that, please use appropriate locks in your tests. See the [lock](lock/index.html) module.
25+
26+
#![warn(missing_docs)]
27+
#![warn(missing_doc_code_examples)]
28+
129
#[cfg(feature = "lock")]
230
#[macro_use]
331
extern crate lazy_static;
432

33+
/// Tools for serializing environment-modifying tests.
534
#[cfg(feature = "lock")]
635
pub mod lock;
736

src/lock.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,32 @@ lazy_static! {
66
static ref LOCK: RwLock<()> = RwLock::new(());
77
}
88

9+
/// Ensure no environment-modifying tests are running at the moment.
10+
///
11+
/// This method should be called before running environment-sensitive tests.
12+
///
13+
/// # Examples
14+
///
15+
/// ```
16+
/// use envtestkit::lock::lock_read;
17+
///
18+
/// let _lock = lock_read();
19+
/// ```
920
pub fn lock_read<'a>() -> RwLockReadGuard<'a, RawRwLock, ()> {
1021
LOCK.read()
1122
}
1223

24+
/// Serialize environment-modifying tests.
25+
///
26+
/// This method should be called before running environment-modifying tests.
27+
///
28+
/// # Examples
29+
///
30+
/// ```
31+
/// use envtestkit::lock::lock_test;
32+
///
33+
/// let _lock = lock_test();
34+
/// ```
1335
pub fn lock_test<'a>() -> RwLockWriteGuard<'a, RawRwLock, ()> {
1436
LOCK.write()
1537
}

src/testkit.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
use std::env;
22
use std::ffi::{OsStr, OsString};
33

4+
/// Guard temporary environment modification.
5+
///
6+
/// Clean up environment modification after this guard is out of scope.
47
#[must_use = "if unused environment will immediately be cleaned up"]
58
pub struct EnvironmentTestGuard {
69
key: OsString,
710
value: Option<OsString>,
811
}
912

13+
/// Set environment variable temporarily for testing.
14+
///
15+
/// # Examples
16+
///
17+
/// ```
18+
/// use envtestkit::set_env;
19+
/// use std::ffi::OsString;
20+
///
21+
/// let _test = set_env(OsString::from("ENV_KEY"), "value");
22+
/// ```
1023
pub fn set_env<V: AsRef<OsStr>>(key: OsString, value: V) -> EnvironmentTestGuard {
1124
let prev_value = env::var_os(&key);
1225
env::set_var(&key, value);

0 commit comments

Comments
 (0)