File tree Expand file tree Collapse file tree 3 files changed +64
-0
lines changed
Expand file tree Collapse file tree 3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 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]
331extern crate lazy_static;
432
33+ /// Tools for serializing environment-modifying tests.
534#[ cfg( feature = "lock" ) ]
635pub mod lock;
736
Original file line number Diff line number Diff 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+ /// ```
920pub 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+ /// ```
1335pub fn lock_test < ' a > ( ) -> RwLockWriteGuard < ' a , RawRwLock , ( ) > {
1436 LOCK . write ( )
1537}
Original file line number Diff line number Diff line change 11use std:: env;
22use 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" ]
58pub 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+ /// ```
1023pub 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) ;
You can’t perform that action at this time.
0 commit comments