1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use colored::*;
use lazy_static::lazy_static;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
pub struct Config {
pub assertions_will_panic: AtomicBool,
pub terminal_width_override: AtomicUsize,
pub update_mode: bool,
pub built_with_buck: bool,
pub force_enable_colors: bool,
}
lazy_static! {
pub static ref CONFIG: Config = Config {
assertions_will_panic: AtomicBool::new(true),
terminal_width_override: AtomicUsize::new(0),
update_mode: is_update_mode(),
built_with_buck: is_buck_build(),
force_enable_colors: should_force_enable_colors(),
};
}
pub fn set_panic(v: bool) {
CONFIG.assertions_will_panic.store(v, Ordering::Relaxed)
}
pub fn should_panic() -> bool {
CONFIG.assertions_will_panic.load(Ordering::Relaxed)
}
pub fn set_terminal_with_override(width: usize) {
CONFIG
.terminal_width_override
.store(width, Ordering::Relaxed);
}
pub fn terminal_width_override() -> usize {
CONFIG.terminal_width_override.load(Ordering::Relaxed)
}
fn is_buck_build() -> bool {
std::env::var("BUCK_BUILD_ID").is_ok()
}
fn is_update_mode() -> bool {
let runtime_var = std::env::var("K9_UPDATE_SNAPSHOTS").map_or(false, |_| true);
if !runtime_var && is_buck_build() {
if option_env!("K9_UPDATE_SNAPSHOTS").is_some() {
return true;
}
}
runtime_var
}
fn should_force_enable_colors() -> bool {
is_buck_build()
}
pub fn update_instructions() -> colored::ColoredString {
if is_buck_build() {
"buck test //path/to/your/buck/target/... -- --env K9_UPDATE_SNAPSHOTS=1".yellow()
} else {
"run with `K9_UPDATE_SNAPSHOTS=1` to update/create snapshots".yellow()
}
}