premortem 0.6.2

A configuration library that performs a premortem on your app's config—finding all the ways it would die before it ever runs
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
481
# Testing Configuration

This guide covers testing patterns for configuration with premortem. The key feature enabling testability is the `MockEnv` type, which allows testing configuration loading without real files or environment variables.

## Table of Contents

- [Using MockEnv]#using-mockenv
- [Testing Error Cases]#testing-error-cases
- [Testing Validation]#testing-validation
- [Testing Source Priority]#testing-source-priority
- [Testing Nested Configuration]#testing-nested-configuration
- [Advanced Patterns]#advanced-patterns

## Using MockEnv

The `MockEnv` type allows testing file-based configuration without creating actual files:

```rust
use premortem::prelude::*;

#[test]
fn test_config_loading() {
    let env = MockEnv::new()
        .with_file("config.toml", r#"
            host = "localhost"
            port = 8080
        "#)
        .with_env("APP_DEBUG", "true");

    let config = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .source(Env::prefix("APP_"))
        .build_with_env(&env)
        .expect("should load");

    assert_eq!(config.host, "localhost");
    assert_eq!(config.port, 8080);
    assert!(config.debug);
}
```

### MockEnv Methods

| Method | Description |
|--------|-------------|
| `with_file(path, content)` | Add a mock file with content |
| `with_env(name, value)` | Set an environment variable |
| `with_envs(iter)` | Set multiple environment variables |
| `with_missing_file(path)` | Mark a file as explicitly missing |
| `with_unreadable_file(path)` | Simulate permission denied error |
| `with_directory(path)` | Add a mock directory |
| `set_file(path, content)` | Update file content during test |
| `remove_file(path)` | Remove a file during test |
| `set_env(name, value)` | Update env var during test |
| `remove_env(name)` | Remove env var during test |

## Testing Error Cases

### Missing Required File

```rust
#[test]
fn test_missing_required_file() {
    let env = MockEnv::new();

    let result = Config::<AppConfig>::builder()
        .source(Toml::file("missing.toml"))
        .build_with_env(&env);

    assert!(result.is_err());

    let errors = result.unwrap_err();
    match errors.first() {
        ConfigError::SourceError { kind, .. } => {
            assert!(matches!(kind, SourceErrorKind::NotFound { .. }));
        }
        other => panic!("Expected SourceError, got: {:?}", other),
    }
}
```

### Optional File Missing

```rust
#[test]
fn test_optional_file_missing() {
    let env = MockEnv::new()
        .with_env("APP_HOST", "localhost")
        .with_env("APP_PORT", "8080");

    // Optional file doesn't error when missing
    let config = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml").optional())
        .source(Env::prefix("APP_"))
        .build_with_env(&env)
        .expect("should load from env only");

    assert_eq!(config.host, "localhost");
}
```

### Permission Denied

```rust
#[test]
fn test_permission_denied() {
    let env = MockEnv::new()
        .with_unreadable_file("secrets.toml");

    let result = Config::<AppConfig>::builder()
        .source(Toml::file("secrets.toml"))
        .build_with_env(&env);

    assert!(result.is_err());

    let errors = result.unwrap_err();
    match errors.first() {
        ConfigError::SourceError { kind, .. } => {
            assert!(matches!(kind, SourceErrorKind::IoError { .. }));
        }
        _ => panic!("Expected IoError"),
    }
}
```

### Parse Errors

```rust
#[test]
fn test_invalid_toml() {
    let env = MockEnv::new()
        .with_file("config.toml", "this is not valid toml {{{");

    let result = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env);

    assert!(result.is_err());

    let errors = result.unwrap_err();
    match errors.first() {
        ConfigError::SourceError { kind, .. } => {
            assert!(matches!(kind, SourceErrorKind::ParseError { .. }));
        }
        _ => panic!("Expected ParseError"),
    }
}
```

## Testing Validation

### Error Accumulation

Premortem's key feature is collecting ALL errors:

```rust
#[test]
fn test_all_errors_accumulated() {
    let env = MockEnv::new().with_file("config.toml", r#"
        host = ""     # Invalid: empty
        port = 0      # Invalid: out of range
    "#);

    let result = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env);

    assert!(result.is_err());
    let errors = result.unwrap_err();

    // Both validation errors should be present
    assert_eq!(errors.len(), 2);
}
```

### Specific Validation Errors

```rust
#[test]
fn test_specific_validation_error() {
    let env = MockEnv::new().with_file("config.toml", r#"
        host = "localhost"
        port = 99999  # Invalid: exceeds max port
    "#);

    let result = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env);

    assert!(result.is_err());
    let errors = result.unwrap_err();

    // Check the error is for the port field
    let has_port_error = errors.iter().any(|e| {
        matches!(e, ConfigError::ValidationError { path, .. } if path == "port")
    });
    assert!(has_port_error, "Expected port validation error");
}
```

### Cross-Field Validation

```rust
#[test]
fn test_cross_field_validation() {
    #[derive(Debug, Deserialize)]
    struct RangeConfig {
        min: i32,
        max: i32,
    }

    impl Validate for RangeConfig {
        fn validate(&self) -> ConfigValidation<()> {
            if self.min > self.max {
                Validation::Failure(ConfigErrors::single(
                    ConfigError::CrossFieldError {
                        paths: vec!["min".to_string(), "max".to_string()],
                        message: "min cannot exceed max".to_string(),
                    }
                ))
            } else {
                Validation::Success(())
            }
        }
    }

    let env = MockEnv::new().with_file("config.toml", r#"
        min = 100
        max = 50
    "#);

    let result = Config::<RangeConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env);

    assert!(result.is_err());
    let errors = result.unwrap_err();
    assert!(matches!(errors.first(), ConfigError::CrossFieldError { .. }));
}
```

## Testing Source Priority

### Environment Overrides File

```rust
#[test]
fn test_env_overrides_file() {
    let env = MockEnv::new()
        .with_file("config.toml", r#"
            host = "from-file"
            port = 8080
        "#)
        .with_env("APP_HOST", "from-env");

    let config = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .source(Env::prefix("APP_"))  // Higher priority
        .build_with_env(&env)
        .expect("should load");

    assert_eq!(config.host, "from-env");  // Env wins
    assert_eq!(config.port, 8080);        // From file
}
```

### Multiple File Sources

```rust
#[test]
fn test_file_layering() {
    let env = MockEnv::new()
        .with_file("base.toml", r#"
            host = "base-host"
            port = 8080
            debug = false
        "#)
        .with_file("override.toml", r#"
            host = "override-host"
            debug = true
        "#);

    let config = Config::<AppConfig>::builder()
        .source(Toml::file("base.toml"))
        .source(Toml::file("override.toml"))  // Higher priority
        .build_with_env(&env)
        .expect("should load");

    assert_eq!(config.host, "override-host");  // Overridden
    assert_eq!(config.port, 8080);             // From base
    assert!(config.debug);                      // Overridden
}
```

## Testing Nested Configuration

```rust
#[derive(Debug, Deserialize, Validate)]
struct ServerConfig {
    #[validate(non_empty)]
    host: String,
    #[validate(range(1..=65535))]
    port: u16,
}

#[derive(Debug, Deserialize, Validate)]
struct AppConfig {
    #[validate(nested)]
    server: ServerConfig,
}

#[test]
fn test_nested_config() {
    let env = MockEnv::new().with_file("config.toml", r#"
        [server]
        host = "localhost"
        port = 8080
    "#);

    let config = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env)
        .expect("should load");

    assert_eq!(config.server.host, "localhost");
    assert_eq!(config.server.port, 8080);
}

#[test]
fn test_nested_validation_error_paths() {
    let env = MockEnv::new().with_file("config.toml", r#"
        [server]
        host = ""
        port = 8080
    "#);

    let result = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env);

    assert!(result.is_err());
    let errors = result.unwrap_err();

    // Error path should include nested path
    let paths: Vec<_> = errors.iter().filter_map(|e| e.path()).collect();
    assert!(paths.iter().any(|p| p.contains("server")));
}
```

## Advanced Patterns

### Dynamic File Changes

MockEnv supports changing content during test execution:

```rust
#[test]
fn test_dynamic_changes() {
    let env = MockEnv::new()
        .with_file("config.toml", r#"
            host = "initial"
            port = 8080
        "#);

    // First load
    let config1 = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env)
        .unwrap();
    assert_eq!(config1.host, "initial");

    // Change file content
    env.set_file("config.toml", r#"
        host = "updated"
        port = 9000
    "#);

    // Second load sees new content
    let config2 = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env)
        .unwrap();
    assert_eq!(config2.host, "updated");
    assert_eq!(config2.port, 9000);
}
```

### Testing with Traced Builds

```rust
#[test]
fn test_value_tracing() {
    let env = MockEnv::new()
        .with_file("config.toml", "host = \"from-file\"")
        .with_env("APP_HOST", "from-env");

    let traced = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .source(Env::prefix("APP_"))
        .build_traced_with_env(&env)
        .expect("should load");

    // Verify override was tracked
    assert!(traced.was_overridden("host"));

    let host_trace = traced.trace("host").unwrap();
    assert_eq!(host_trace.history.len(), 2);
    assert_eq!(host_trace.final_value.value.as_str(), Some("from-env"));
}
```

### Testing Error Messages

```rust
#[test]
fn test_error_messages_are_helpful() {
    let env = MockEnv::new().with_file("config.toml", r#"
        host = ""
        port = -1
    "#);

    let result = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .build_with_env(&env);

    let errors = result.unwrap_err();

    for error in errors.iter() {
        let msg = error.to_string();
        // Errors should mention the field name
        assert!(
            msg.contains("host") || msg.contains("port"),
            "Error should mention field: {}",
            msg
        );
    }
}
```

### Helper Functions for Tests

Create test utilities to reduce boilerplate:

```rust
mod test_helpers {
    use super::*;

    pub fn mock_config(toml: &str) -> MockEnv {
        MockEnv::new().with_file("config.toml", toml)
    }

    pub fn load_test_config(env: &MockEnv) -> Result<Config<AppConfig>, ConfigErrors> {
        Config::<AppConfig>::builder()
            .source(Toml::file("config.toml"))
            .source(Env::prefix("APP_"))
            .build_with_env(env)
    }

    pub fn assert_validation_error_for(errors: &ConfigErrors, path: &str) {
        let has_error = errors.iter().any(|e| {
            e.path().map(|p| p.contains(path)).unwrap_or(false)
        });
        assert!(has_error, "Expected error for path '{}' in {:?}", path, errors);
    }
}

#[test]
fn test_with_helpers() {
    use test_helpers::*;

    let env = mock_config(r#"
        host = ""
        port = 8080
    "#);

    let result = load_test_config(&env);
    assert!(result.is_err());

    assert_validation_error_for(&result.unwrap_err(), "host");
}
```