reinhardt-conf 0.3.13

Configuration management framework for Reinhardt - Django-inspired settings with encryption and secrets management
Documentation
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Integration tests for EnvSource type inference, LowPriorityEnvSource, and cross-priority merging.
// Covers: smart parsing, prefix handling, priority chain (Default → LowPriorityEnv → Toml → Env).

use reinhardt_conf::settings::builder::SettingsBuilder;
use reinhardt_conf::settings::sources::{
	ConfigSource, DefaultSource, EnvSource, HighPriorityEnvSource, LowPriorityEnvSource,
	TomlFileSource,
};
use rstest::rstest;
use serde_json::Value;
use serial_test::serial;
use std::io::Write;
use std::path::PathBuf;
use tempfile::TempDir;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn write_toml_file(content: &str) -> (TempDir, PathBuf) {
	let dir = TempDir::new().unwrap();
	let path = dir.path().join("config.toml");
	let mut file = std::fs::File::create(&path).unwrap();
	file.write_all(content.as_bytes()).unwrap();
	(dir, path)
}

/// Set environment variables for test. Returns the keys for cleanup.
///
/// # Safety
/// Caller must ensure exclusive access to environment variables (use `#[serial]`).
unsafe fn set_env_vars(vars: &[(&str, &str)]) {
	for (key, value) in vars {
		unsafe { std::env::set_var(key, value) };
	}
}

/// Remove environment variables after test.
///
/// # Safety
/// Caller must ensure exclusive access to environment variables (use `#[serial]`).
unsafe fn remove_env_vars(vars: &[&str]) {
	for key in vars {
		unsafe { std::env::remove_var(key) };
	}
}

// ===========================================================================
// EnvSource – type inference and smart parsing
// ===========================================================================

#[rstest]
#[serial(env)]
fn env_source_infers_integer_type() {
	// Arrange
	unsafe { set_env_vars(&[("SRCTEST_PORT", "8080")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	assert_eq!(config.get("port").unwrap(), &Value::Number(8080.into()));

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_PORT"]) };
}

#[rstest]
#[serial(env)]
fn env_source_infers_bool_type() {
	// Arrange
	unsafe { set_env_vars(&[("SRCTEST_ENABLED", "true")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	assert_eq!(config.get("enabled").unwrap(), &Value::Bool(true));

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_ENABLED"]) };
}

#[rstest]
#[serial(env)]
fn env_source_debug_key_smart_parsing_numeric() {
	// Arrange – "1" should be parsed as Bool(true) for the debug key
	unsafe { set_env_vars(&[("SRCTEST_DEBUG", "1")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	assert_eq!(config.get("debug").unwrap(), &Value::Bool(true));

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_DEBUG"]) };
}

#[rstest]
#[serial(env)]
fn env_source_debug_key_parses_yes() {
	// Arrange
	unsafe { set_env_vars(&[("SRCTEST_DEBUG", "yes")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	assert_eq!(config.get("debug").unwrap(), &Value::Bool(true));

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_DEBUG"]) };
}

#[rstest]
#[serial(env)]
fn env_source_debug_key_parses_off() {
	// Arrange
	unsafe { set_env_vars(&[("SRCTEST_DEBUG", "off")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	assert_eq!(config.get("debug").unwrap(), &Value::Bool(false));

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_DEBUG"]) };
}

#[rstest]
#[serial(env)]
fn env_source_allowed_hosts_parsed_as_array() {
	// Arrange
	unsafe { set_env_vars(&[("SRCTEST_ALLOWED_HOSTS", "a.com,b.com")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	let hosts = config.get("allowed_hosts").unwrap();
	assert!(hosts.is_array());
	let arr = hosts.as_array().unwrap();
	assert_eq!(arr.len(), 2);
	assert_eq!(arr[0], Value::String("a.com".to_string()));
	assert_eq!(arr[1], Value::String("b.com".to_string()));

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_ALLOWED_HOSTS"]) };
}

#[rstest]
#[serial(env)]
fn env_source_string_value_remains_string() {
	// Arrange
	unsafe { set_env_vars(&[("SRCTEST_NAME", "myapp")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	assert_eq!(
		config.get("name").unwrap(),
		&Value::String("myapp".to_string())
	);

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_NAME"]) };
}

#[rstest]
#[serial(env)]
fn env_source_strips_prefix_and_lowercases() {
	// Arrange
	unsafe { set_env_vars(&[("SRCTEST_MY_KEY", "val")]) };

	// Act
	let source = EnvSource::new().with_prefix("SRCTEST_");
	let config = source.load().unwrap();

	// Assert
	assert!(config.contains_key("my_key"));
	assert_eq!(
		config.get("my_key").unwrap(),
		&Value::String("val".to_string())
	);

	// Cleanup
	unsafe { remove_env_vars(&["SRCTEST_MY_KEY"]) };
}

// ===========================================================================
// LowPriorityEnvSource
// ===========================================================================

#[rstest]
fn low_priority_env_source_priority_is_40() {
	// Assert
	assert_eq!(LowPriorityEnvSource::new().priority(), 40);
}

#[rstest]
#[serial(env)]
fn low_priority_env_source_loads_env_vars() {
	// Arrange
	unsafe { set_env_vars(&[("LPTEST_PORT", "9090")]) };

	// Act
	let source = LowPriorityEnvSource::new().with_prefix("LPTEST_");
	let config = source.load().unwrap();

	// Assert
	assert_eq!(config.get("port").unwrap(), &Value::Number(9090.into()));

	// Cleanup
	unsafe { remove_env_vars(&["LPTEST_PORT"]) };
}

#[rstest]
#[serial(env)]
fn low_priority_env_source_with_prefix_filters() {
	// Arrange
	unsafe { set_env_vars(&[("LPTEST_INSIDE", "yes_val"), ("OTHER_OUTSIDE", "no_val")]) };

	// Act
	let source = LowPriorityEnvSource::new().with_prefix("LPTEST_");
	let config = source.load().unwrap();

	// Assert
	assert!(config.contains_key("inside"));
	assert!(!config.contains_key("outside"));
	assert!(!config.contains_key("other_outside"));

	// Cleanup
	unsafe { remove_env_vars(&["LPTEST_INSIDE", "OTHER_OUTSIDE"]) };
}

#[rstest]
fn low_priority_env_source_description_contains_low_priority() {
	// Act
	let desc = LowPriorityEnvSource::new().description();

	// Assert
	assert!(
		desc.to_lowercase().contains("low"),
		"description should contain 'low', got: {desc}"
	);
}

// ===========================================================================
// Cross-priority merging
// ===========================================================================

#[rstest]
#[serial(env)]
fn env_overrides_toml() {
	// Arrange
	let (_dir, toml_path) = write_toml_file("port = 3000\n");
	unsafe { set_env_vars(&[("MPTEST_PORT", "9000")]) };

	// Act
	let settings = SettingsBuilder::new()
		.add_source(TomlFileSource::new(&toml_path))
		.add_source(EnvSource::new().with_prefix("MPTEST_"))
		.build()
		.unwrap();

	// Assert – Env(100) wins over Toml(50)
	let port: u16 = settings.get("port").unwrap();
	assert_eq!(port, 9000);

	// Cleanup
	unsafe { remove_env_vars(&["MPTEST_PORT"]) };
}

#[rstest]
fn toml_overrides_default() {
	// Arrange
	let (_dir, toml_path) = write_toml_file("debug = false\n");
	let default = DefaultSource::new().with_value("debug", Value::Bool(true));

	// Act
	let settings = SettingsBuilder::new()
		.add_source(default)
		.add_source(TomlFileSource::new(&toml_path))
		.build()
		.unwrap();

	// Assert – Toml(50) wins over Default(0)
	let debug: bool = settings.get("debug").unwrap();
	assert!(!debug);
}

#[rstest]
#[serial(env)]
fn toml_overrides_low_priority_env() {
	// Arrange
	let (_dir, toml_path) = write_toml_file("port = 5000\n");
	unsafe { set_env_vars(&[("LPMPTEST_PORT", "4000")]) };

	// Act
	let settings = SettingsBuilder::new()
		.add_source(LowPriorityEnvSource::new().with_prefix("LPMPTEST_"))
		.add_source(TomlFileSource::new(&toml_path))
		.build()
		.unwrap();

	// Assert – Toml(50) wins over LowPriorityEnv(40)
	let port: u16 = settings.get("port").unwrap();
	assert_eq!(port, 5000);

	// Cleanup
	unsafe { remove_env_vars(&["LPMPTEST_PORT"]) };
}

#[rstest]
#[serial(env)]
fn low_priority_env_overrides_default() {
	// Arrange
	let default = DefaultSource::new().with_value("timeout", Value::Number(30.into()));
	unsafe { set_env_vars(&[("LPMPTEST2_TIMEOUT", "60")]) };

	// Act
	let settings = SettingsBuilder::new()
		.add_source(default)
		.add_source(LowPriorityEnvSource::new().with_prefix("LPMPTEST2_"))
		.build()
		.unwrap();

	// Assert – LowPriorityEnv(40) wins over Default(0)
	let timeout: u64 = settings.get("timeout").unwrap();
	assert_eq!(timeout, 60);

	// Cleanup
	unsafe { remove_env_vars(&["LPMPTEST2_TIMEOUT"]) };
}

#[rstest]
#[serial(env)]
fn full_chain_same_key_highest_priority_wins() {
	// Arrange – all sources provide "port" with different values
	let default = DefaultSource::new().with_value("port", Value::Number(1000.into()));
	let (_dir, toml_path) = write_toml_file("port = 3000\n");
	unsafe { set_env_vars(&[("FCTEST_PORT", "9999")]) };

	// LowPriorityEnv will also pick up FCTEST_PORT
	let low_env = LowPriorityEnvSource::new().with_prefix("FCTEST_");
	let high_env = EnvSource::new().with_prefix("FCTEST_");

	// Act
	let settings = SettingsBuilder::new()
		.add_source(default) // priority 0
		.add_source(low_env) // priority 40
		.add_source(TomlFileSource::new(&toml_path)) // priority 50
		.add_source(high_env) // priority 100
		.build()
		.unwrap();

	// Assert – Env(100) wins
	let port: u16 = settings.get("port").unwrap();
	assert_eq!(port, 9999);

	// Cleanup
	unsafe { remove_env_vars(&["FCTEST_PORT"]) };
}

#[rstest]
#[serial(env)]
fn full_chain_disjoint_keys_all_present() {
	// Arrange – each source provides a unique key
	let default = DefaultSource::new().with_value("default_key", Value::String("dv".to_string()));
	let (_dir, toml_path) = write_toml_file("toml_key = \"tv\"\n");
	unsafe { set_env_vars(&[("FCTEST2_ENV_KEY", "ev")]) };

	// Act
	let settings = SettingsBuilder::new()
		.add_source(default)
		.add_source(TomlFileSource::new(&toml_path))
		.add_source(EnvSource::new().with_prefix("FCTEST2_"))
		.build()
		.unwrap();

	// Assert – all disjoint keys present
	assert!(settings.contains_key("default_key"));
	assert!(settings.contains_key("toml_key"));
	assert!(settings.contains_key("env_key"));

	let dv: String = settings.get("default_key").unwrap();
	assert_eq!(dv, "dv");
	let tv: String = settings.get("toml_key").unwrap();
	assert_eq!(tv, "tv");
	let ev: String = settings.get("env_key").unwrap();
	assert_eq!(ev, "ev");

	// Cleanup
	unsafe { remove_env_vars(&["FCTEST2_ENV_KEY"]) };
}

#[rstest]
#[serial(env)]
fn full_chain_partial_overlap() {
	// Arrange – "port" overlaps between default and toml; "name" is unique to default;
	// "host" is unique to env
	let default = DefaultSource::new()
		.with_value("port", Value::Number(1111.into()))
		.with_value("name", Value::String("default_app".to_string()));
	let (_dir, toml_path) = write_toml_file("port = 2222\n");
	unsafe { set_env_vars(&[("FCTEST3_HOST", "env.local")]) };

	// Act
	let settings = SettingsBuilder::new()
		.add_source(default) // priority 0
		.add_source(TomlFileSource::new(&toml_path)) // priority 50
		.add_source(EnvSource::new().with_prefix("FCTEST3_")) // priority 100
		.build()
		.unwrap();

	// Assert – overlapping "port" won by Toml(50) over Default(0)
	let port: u16 = settings.get("port").unwrap();
	assert_eq!(port, 2222);

	// Assert – unique keys all present
	let name: String = settings.get("name").unwrap();
	assert_eq!(name, "default_app");
	let host: String = settings.get("host").unwrap();
	assert_eq!(host, "env.local");

	// Cleanup
	unsafe { remove_env_vars(&["FCTEST3_HOST"]) };
}

// ===========================================================================
// Interpolation × priority composition
// ===========================================================================

#[rstest]
#[serial(env)]
fn high_priority_env_overrides_interpolated_toml() {
	// Verifies that HighPriorityEnvSource (priority 60) wins against
	// values produced by an interpolated TomlFileSource (priority 50).

	// Arrange — reuse the file-local set/remove helpers for consistency
	// with the other 17 tests in this file.
	unsafe {
		set_env_vars(&[
			("IT_PG_PORT_PRIO", "8080"), // for TOML interpolation
			("PRIO_TEST_PORT", "9999"),  // for HighPriorityEnvSource override
		])
	};
	let (_dir, path) = write_toml_file(r#"port = "${IT_PG_PORT_PRIO:-5432}""#);

	// Act
	let settings = SettingsBuilder::new()
		.add_source(TomlFileSource::new(&path).with_interpolation()) // priority 50
		.add_source(HighPriorityEnvSource::new().with_prefix("PRIO_TEST_")) // priority 60
		.build()
		.unwrap();

	// Assert — HighPriorityEnvSource (60) beats interpolated TOML (50).
	// Note: HighPriorityEnvSource performs smart type inference and parses
	// "9999" as a number, so we read it back as `u16`. The interpolated TOML
	// value is a string ("8080"), but it gets overridden before deserialization.
	let port: u16 = settings.get("port").unwrap();

	// Cleanup
	unsafe { remove_env_vars(&["IT_PG_PORT_PRIO", "PRIO_TEST_PORT"]) };

	assert_eq!(
		port, 9999,
		"HighPriorityEnvSource (60) must override interpolated TOML (50)"
	);
}