Skip to main content

sentry_options/
testing.rs

1//! Testing utilities for overriding option values in tests.
2//!
3//! # Example
4//!
5//! ```rust,ignore
6//! use sentry_options::Options;
7//! use sentry_options::testing::override_options;
8//! use serde_json::json;
9//!
10//! #[test]
11//! fn test_feature() {
12//!     let opts = Options::from_directory(test_dir).unwrap();
13//!
14//!     let _guard = override_options(&[("seer", "feature.enabled", json!(true))]).unwrap();
15//!     assert_eq!(opts.get("seer", "feature.enabled").unwrap(), json!(true));
16//! }
17//! ```
18
19use 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
30/// Set an override value for a specific namespace and key.
31pub 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
40/// Get an override value if one exists.
41pub 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
49/// Clear an override for a specific namespace and key.
50pub 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
58/// Guard that restores overrides when dropped.
59pub 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
83/// Set overrides for the lifetime of the returned guard.
84///
85/// Requires that options have been initialized via `init()`.
86/// Validates that each key exists in the schema and the value matches the expected type.
87/// When the guard is dropped (goes out of scope), the overrides are restored
88/// to their previous values.
89///
90/// # Note
91///
92/// Overrides are thread-local. They won't apply to spawned threads.
93///
94/// # Errors
95///
96/// Returns an error if:
97/// - Any namespace doesn't exist
98/// - Any key doesn't exist in the schema
99/// - Any value doesn't match the expected type
100///
101/// # Example
102///
103/// ```rust,ignore
104/// use sentry_options::testing::override_options;
105/// use sentry_options::init;
106/// use serde_json::json;
107///
108/// crate::init().unwrap();
109/// let _guard = override_options(&[
110///     ("namespace", "key1", json!(true)),
111///     ("namespace", "key2", json!(42)),
112/// ]).unwrap();
113/// // overrides are active here
114/// // when _guard goes out of scope, overrides are restored
115/// ```
116pub fn override_options(overrides: &[(&str, &str, Value)]) -> Result<OverrideGuard> {
117    // Validate all overrides before applying any
118    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}