sentry_options/
testing.rs1use std::cell::RefCell;
20use std::collections::HashMap;
21
22use serde_json::Value;
23
24use crate::{GLOBAL_OPTIONS, Result};
25
26thread_local! {
27 static OVERRIDES: RefCell<HashMap<String, HashMap<String, Value>>> = RefCell::new(HashMap::new());
28}
29
30pub fn set_override(namespace: &str, key: &str, value: Value) {
32 OVERRIDES.with(|o| {
33 o.borrow_mut()
34 .entry(namespace.to_string())
35 .or_default()
36 .insert(key.to_string(), value);
37 });
38}
39
40pub fn get_override(namespace: &str, key: &str) -> Option<Value> {
42 OVERRIDES.with(|o| {
43 o.borrow()
44 .get(namespace)
45 .and_then(|ns| ns.get(key).cloned())
46 })
47}
48
49pub fn clear_override(namespace: &str, key: &str) {
51 OVERRIDES.with(|o| {
52 if let Some(ns_map) = o.borrow_mut().get_mut(namespace) {
53 ns_map.remove(key);
54 }
55 });
56}
57
58pub struct OverrideGuard {
60 previous: Vec<(String, String, Option<Value>)>,
61}
62
63impl Drop for OverrideGuard {
64 fn drop(&mut self) {
65 OVERRIDES.with(|o| {
66 let mut map = o.borrow_mut();
67 for (ns, key, prev_value) in self.previous.drain(..) {
68 match prev_value {
69 Some(v) => {
70 map.entry(ns).or_default().insert(key, v);
71 }
72 None => {
73 if let Some(ns_map) = map.get_mut(&ns) {
74 ns_map.remove(&key);
75 }
76 }
77 }
78 }
79 });
80 }
81}
82
83pub fn override_options(overrides: &[(&str, &str, Value)]) -> Result<OverrideGuard> {
117 let opts = GLOBAL_OPTIONS
119 .get()
120 .ok_or(crate::OptionsError::NotInitialized)?;
121 for (ns, key, value) in overrides {
122 opts.validate_override(ns, key, value)?;
123 }
124
125 let mut previous = Vec::with_capacity(overrides.len());
126
127 OVERRIDES.with(|o| {
128 let mut map = o.borrow_mut();
129 for (ns, key, value) in overrides {
130 let prev = map.get(*ns).and_then(|m| m.get(*key).cloned());
131 previous.push((ns.to_string(), key.to_string(), prev));
132 map.entry(ns.to_string())
133 .or_default()
134 .insert(key.to_string(), value.clone());
135 }
136 });
137
138 Ok(OverrideGuard { previous })
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144 use serde_json::json;
145
146 #[test]
147 fn test_set_get_clear_override() {
148 set_override("ns", "key", json!(true));
149 assert_eq!(get_override("ns", "key"), Some(json!(true)));
150 clear_override("ns", "key");
151 assert_eq!(get_override("ns", "key"), None);
152 }
153
154 #[test]
155 fn test_override_guard_restores() {
156 crate::init().unwrap();
157 set_override("sentry-options-testing", "int-option", json!(1));
158
159 {
160 let _guard =
161 override_options(&[("sentry-options-testing", "int-option", json!(2))]).unwrap();
162 assert_eq!(
163 get_override("sentry-options-testing", "int-option"),
164 Some(json!(2))
165 );
166 }
167
168 assert_eq!(
169 get_override("sentry-options-testing", "int-option"),
170 Some(json!(1))
171 );
172 clear_override("sentry-options-testing", "int-option");
173 }
174
175 #[test]
176 fn test_override_guard_clears_new_key() {
177 crate::init().unwrap();
178 assert_eq!(get_override("sentry-options-testing", "bool-option"), None);
179
180 {
181 let _guard =
182 override_options(&[("sentry-options-testing", "bool-option", json!(true))])
183 .unwrap();
184 assert_eq!(
185 get_override("sentry-options-testing", "bool-option"),
186 Some(json!(true))
187 );
188 }
189
190 assert_eq!(get_override("sentry-options-testing", "bool-option"), None);
191 }
192
193 #[test]
194 fn test_nested_overrides() {
195 crate::init().unwrap();
196 {
197 let _outer =
198 override_options(&[("sentry-options-testing", "int-option", json!(100))]).unwrap();
199 assert_eq!(
200 get_override("sentry-options-testing", "int-option"),
201 Some(json!(100))
202 );
203
204 {
205 let _inner =
206 override_options(&[("sentry-options-testing", "int-option", json!(200))])
207 .unwrap();
208 assert_eq!(
209 get_override("sentry-options-testing", "int-option"),
210 Some(json!(200))
211 );
212 }
213
214 assert_eq!(
215 get_override("sentry-options-testing", "int-option"),
216 Some(json!(100))
217 );
218 }
219
220 assert_eq!(get_override("sentry-options-testing", "int-option"), None);
221 }
222}