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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use colored::*;
use lazy_static::lazy_static;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
pub enum BuildSystem {
Buck,
Buck2,
Cargo,
}
impl BuildSystem {
pub fn is_buck(&self) -> bool {
match self {
Self::Buck | Self::Buck2 => true,
Self::Cargo => false,
}
}
}
pub struct Config {
pub assertions_will_panic: AtomicBool,
pub terminal_width_override: AtomicUsize,
pub update_mode: bool,
pub build_system: BuildSystem,
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(),
build_system: build_system(),
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 build_system() -> BuildSystem {
if std::env::var("BUCK_BUILD_ID").is_ok() {
BuildSystem::Buck
} else if std::env::var("BUCK2_DAEMON_UUID").is_ok() {
BuildSystem::Buck2
} else {
BuildSystem::Cargo
}
}
fn is_update_mode() -> bool {
std::env::var("K9_UPDATE_SNAPSHOTS").map_or(false, |_| true)
}
fn should_force_enable_colors() -> bool {
if build_system().is_buck() {
return true;
}
if let Ok(force_colors) = std::env::var("K9_FORCE_COLORS") {
match force_colors.as_str() {
"1" => return true,
"0" => return false,
_ => (),
}
}
false
}
pub fn update_instructions() -> colored::ColoredString {
match CONFIG.build_system {
BuildSystem::Buck => {
"buck test //path/to/your/buck/target/... -- --env K9_UPDATE_SNAPSHOTS=1".yellow()
}
BuildSystem::Buck2 => {
"buck2 test //path/to/your/buck/target/... -- --env K9_UPDATE_SNAPSHOTS=1".yellow()
}
BuildSystem::Cargo => {
"run with `K9_UPDATE_SNAPSHOTS=1` to update/create snapshots".yellow()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_building() {
let _b = format!("{}", CONFIG.force_enable_colors);
}
}