Skip to content

Commit 9b3e223

Browse files
committed
chore: use the fakeenv crate for testing environment variable specific functionality - crate serial_test is no longer needed
1 parent 29f1efa commit 9b3e223

7 files changed

Lines changed: 49 additions & 31 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ sdiff = { version = "0.1", optional = true }
4343

4444
[dev-dependencies]
4545
anyhow = "1"
46+
fakeenv = { version = "0.1", default-features = false, features = ["fake"] }
4647
proptest = "1"
47-
serial_test = "3"
4848
time = { version = "0.3", default-features = false, features = ["macros"] }
4949
version-sync = "0.9"
5050

examples/fixture/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod dummy_extern_uses {
44
use anyhow as _;
55
#[cfg(feature = "bigdecimal")]
66
use bigdecimal as _;
7+
use fakeenv as _;
78
#[cfg(feature = "float-cmp")]
89
use float_cmp as _;
910
use hashbrown as _;
@@ -18,7 +19,6 @@ mod dummy_extern_uses {
1819
use rust_decimal as _;
1920
#[cfg(feature = "colored")]
2021
use sdiff as _;
21-
use serial_test as _;
2222
use time as _;
2323
use version_sync as _;
2424
}

src/colored/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ mod with_colored_feature {
699699
/// Returns true if the environment variable `NO_COLOR` is set.
700700
#[cfg(feature = "std")]
701701
fn is_no_color_env_var_set() -> bool {
702-
use crate::std::env;
702+
use crate::env;
703703

704704
match env::var(ENV_VAR_NO_COLOR) {
705705
Ok(value) => !value.is_empty(),
@@ -718,7 +718,7 @@ mod with_colored_feature {
718718
#[must_use]
719719
#[inline]
720720
pub fn configured_diff_format_impl() -> DiffFormat {
721-
use crate::std::env;
721+
use crate::env;
722722

723723
match env::var(ENV_VAR_HIGHLIGHT_DIFFS) {
724724
Ok(value) => {

src/colored/tests.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,11 @@ mod with_colored_but_not_std_feature {
9090
mod with_colored_and_std_features {
9191
use super::*;
9292
use crate::colored::with_colored_feature::ENV_VAR_HIGHLIGHT_DIFFS;
93-
use crate::std::env;
93+
use crate::env;
9494
use hashbrown::HashMap;
9595
use proptest::prelude::*;
96-
use serial_test::serial;
9796

9897
#[test]
99-
#[serial]
10098
fn get_configured_diff_format_when_env_var_not_set() {
10199
env::remove_var(ENV_VAR_HIGHLIGHT_DIFFS);
102100

@@ -106,7 +104,6 @@ mod with_colored_and_std_features {
106104
}
107105

108106
#[test]
109-
#[serial]
110107
fn get_configured_diff_format_when_env_var_not_set_and_no_color_env_var_set() {
111108
env::remove_var(ENV_VAR_HIGHLIGHT_DIFFS);
112109
env::set_var("NO_COLOR", "1");
@@ -119,7 +116,6 @@ mod with_colored_and_std_features {
119116
}
120117

121118
#[test]
122-
#[serial]
123119
fn get_configured_diff_format_when_env_var_set_to_unknown_mode() {
124120
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "not-valid");
125121

@@ -129,7 +125,6 @@ mod with_colored_and_std_features {
129125
}
130126

131127
#[test]
132-
#[serial]
133128
fn get_configured_diff_format_when_env_var_set_to_bold_mode() {
134129
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "bold");
135130

@@ -139,7 +134,6 @@ mod with_colored_and_std_features {
139134
}
140135

141136
#[test]
142-
#[serial]
143137
fn get_configured_diff_format_when_env_var_set_to_bold_mode_and_no_color_env_var_set() {
144138
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "bold");
145139
env::set_var("NO_COLOR", "1");
@@ -152,7 +146,6 @@ mod with_colored_and_std_features {
152146
}
153147

154148
#[test]
155-
#[serial]
156149
fn get_configured_diff_format_when_env_var_set_to_red_green_mode() {
157150
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-green");
158151

@@ -162,7 +155,6 @@ mod with_colored_and_std_features {
162155
}
163156

164157
#[test]
165-
#[serial]
166158
fn get_configured_diff_format_when_env_var_set_to_red_green_mode_and_no_color_env_var_set() {
167159
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-green");
168160
env::set_var("NO_COLOR", "1");
@@ -175,7 +167,6 @@ mod with_colored_and_std_features {
175167
}
176168

177169
#[test]
178-
#[serial]
179170
fn get_configured_diff_format_when_env_var_set_to_red_blue_mode() {
180171
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-blue");
181172

@@ -185,7 +176,6 @@ mod with_colored_and_std_features {
185176
}
186177

187178
#[test]
188-
#[serial]
189179
fn get_configured_diff_format_when_env_var_set_to_red_blue_mode_and_no_color_env_var_set() {
190180
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-blue");
191181
env::set_var("NO_COLOR", "1");
@@ -198,7 +188,6 @@ mod with_colored_and_std_features {
198188
}
199189

200190
#[test]
201-
#[serial]
202191
fn get_configured_diff_format_when_env_var_set_to_red_yellow_mode() {
203192
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-yellow");
204193

@@ -208,7 +197,6 @@ mod with_colored_and_std_features {
208197
}
209198

210199
#[test]
211-
#[serial]
212200
fn get_configured_diff_format_when_env_var_set_to_red_yellow_mode_and_no_color_env_var_set() {
213201
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-yellow");
214202
env::set_var("NO_COLOR", "1");
@@ -221,7 +209,6 @@ mod with_colored_and_std_features {
221209
}
222210

223211
#[test]
224-
#[serial]
225212
fn get_configured_diff_format_when_env_var_set_to_off() {
226213
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "off");
227214

@@ -232,47 +219,43 @@ mod with_colored_and_std_features {
232219

233220
proptest! {
234221
#[test]
235-
#[serial]
236222
fn setting_env_var_to_bold_is_case_insensitive(
237223
mode in "[bB][oO][lL][dD]"
238224
) {
239-
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, mode);
225+
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, &mode);
240226

241227
let diff_format = configured_diff_format();
242228

243229
prop_assert_eq!(diff_format, DIFF_FORMAT_BOLD);
244230
}
245231

246232
#[test]
247-
#[serial]
248233
fn setting_env_var_to_red_blue_is_case_insensitive(
249234
mode in "[rR][eE][dD]-[bB][lL][uU][eE]"
250235
) {
251-
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, mode);
236+
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, &mode);
252237

253238
let diff_format = configured_diff_format();
254239

255240
prop_assert_eq!(diff_format, DIFF_FORMAT_RED_BLUE);
256241
}
257242

258243
#[test]
259-
#[serial]
260244
fn setting_env_var_to_red_yellow_is_case_insensitive(
261245
mode in "[rR][eE][dD]-[yY][eE][lL][lL][oO][wW]"
262246
) {
263-
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, mode);
247+
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, &mode);
264248

265249
let diff_format = configured_diff_format();
266250

267251
prop_assert_eq!(diff_format, DIFF_FORMAT_RED_YELLOW);
268252
}
269253

270254
#[test]
271-
#[serial]
272255
fn setting_env_var_to_off_is_case_insensitive(
273256
mode in "[oO][fF][fF]"
274257
) {
275-
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, mode);
258+
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, &mode);
276259

277260
let diff_format = configured_diff_format();
278261

@@ -281,7 +264,6 @@ mod with_colored_and_std_features {
281264
}
282265

283266
#[test]
284-
#[serial]
285267
fn assert_that_sets_the_diff_format_to_red_green() {
286268
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-green");
287269

@@ -291,7 +273,6 @@ mod with_colored_and_std_features {
291273
}
292274

293275
#[test]
294-
#[serial]
295276
fn verify_that_sets_the_diff_format_to_no_highlighting() {
296277
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-green");
297278

@@ -301,7 +282,6 @@ mod with_colored_and_std_features {
301282
}
302283

303284
#[test]
304-
#[serial]
305285
fn assert_that_code_sets_the_diff_format_to_red_green() {
306286
env::set_var(ENV_VAR_HIGHLIGHT_DIFFS, "red-green");
307287

src/env.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Fakeable `env` module for tests.
2+
//!
3+
//! inspired by
4+
//! [Testing code that uses environment variables](https://www.reddit.com/r/rust/comments/1jd8sxg/testing_code_that_uses_environment_variables/)
5+
//! - post on Reddit
6+
7+
pub use std::env::VarError;
8+
9+
#[cfg(not(test))]
10+
pub use std::env::var;
11+
12+
#[cfg(test)]
13+
pub use fake_env::*;
14+
15+
#[cfg(test)]
16+
mod fake_env {
17+
use fakeenv::EnvStore;
18+
use std::cell::RefCell;
19+
use std::env::VarError;
20+
21+
thread_local! {
22+
static ENV_STORE: RefCell<EnvStore> = RefCell::new(EnvStore::fake());
23+
}
24+
25+
pub fn var(key: &str) -> Result<String, VarError> {
26+
ENV_STORE.with(|env| env.borrow().var(key))
27+
}
28+
29+
pub fn set_var(key: &str, value: &str) {
30+
ENV_STORE.with(|env| env.borrow_mut().set_var(key, value));
31+
}
32+
33+
pub fn remove_var(key: &str) {
34+
ENV_STORE.with(|env| env.borrow_mut().remove_var(key));
35+
}
36+
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ mod c_string;
678678
mod char;
679679
mod char_count;
680680
mod collection;
681+
#[cfg(feature = "std")]
682+
mod env;
681683
mod equality;
682684
mod error;
683685
mod float;
@@ -713,8 +715,8 @@ type TestCodeSnippetsInReadme = ();
713715
// Rust issue [#95513](https://github.com/rust-lang/rust/issues/95513) is fixed
714716
#[cfg(test)]
715717
mod dummy_extern_uses {
718+
use fakeenv as _;
716719
use proptest as _;
717-
use serial_test as _;
718720
use time as _;
719721
use version_sync as _;
720722
}

tests/version_numbers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod dummy_extern_uses {
88
use asserting as _;
99
#[cfg(feature = "bigdecimal")]
1010
use bigdecimal as _;
11+
use fakeenv as _;
1112
#[cfg(feature = "float-cmp")]
1213
use float_cmp as _;
1314
use hashbrown as _;
@@ -22,7 +23,6 @@ mod dummy_extern_uses {
2223
use rust_decimal as _;
2324
#[cfg(feature = "colored")]
2425
use sdiff as _;
25-
use serial_test as _;
2626
use time as _;
2727
}
2828

0 commit comments

Comments
 (0)