secretspec-derive 0.20.0

Derive macros for SecretSpec type-safe code generation
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Integration tests that verify the complete macro output

use secretspec_derive::declare_secrets;

mod basic_generation {
    use super::*;

    declare_secrets!("tests/fixtures/basic.toml");

    #[test]
    fn test_struct_fields_exist() {
        // This verifies that the struct has the expected fields
        fn _test_field_types(s: SecretSpec) {
            let _: String = s.api_key;
            let _: String = s.database_url;
            let _: String = s.optional_secret; // Supplied by its manifest default
        }
    }
}

/// Exercises `.prompt_missing(...)` end to end. Follows the same
/// isolated-child-process shape as `as_path_lifetime` above (rather than
/// mutating this process's current directory), since these tests would
/// otherwise race with every other test in this binary over `cwd`.
mod prompt_missing {
    use super::*;
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    declare_secrets!("tests/fixtures/basic.toml");

    const CHILD_CASE_VAR: &str = "SECRETSPEC_DERIVE_PROMPT_MISSING_CHILD";

    fn run_in_isolated_project(case: &str, env_file: Option<&str>) -> bool {
        let project = TempDir::new().expect("create isolated project");
        fs::write(
            project.path().join("secretspec.toml"),
            include_str!("fixtures/basic.toml"),
        )
        .expect("write project manifest");
        if let Some(contents) = env_file {
            fs::write(project.path().join(".env"), contents).expect("write dotenv provider");
        }

        Command::new(std::env::current_exe().expect("locate integration test binary"))
            .args([&format!("prompt_missing::{case}"), "--exact", "--nocapture"])
            .current_dir(project.path())
            .env(CHILD_CASE_VAR, "1")
            .env("HOME", project.path())
            .env("XDG_CONFIG_HOME", project.path())
            .env_remove("SECRETSPEC_PROFILE")
            .env_remove("SECRETSPEC_PROVIDER")
            .status()
            .expect("run isolated child test")
            .success()
    }

    #[test]
    fn prompt_missing_load_succeeds_when_nothing_is_missing() {
        if std::env::var_os(CHILD_CASE_VAR).is_none() {
            assert!(run_in_isolated_project(
                "prompt_missing_load_succeeds_when_nothing_is_missing",
                Some("API_KEY=key-value\nDATABASE_URL=postgres://localhost/db\n"),
            ));
            return;
        }

        // `.prompt_missing(true)` only changes behavior when a required
        // secret is missing; when every secret already resolves, it must
        // load exactly like the default (`prompt_missing` unset).
        let resolved = SecretSpec::builder()
            .with_provider("dotenv://.env")
            .with_reason("integration test")
            .prompt_missing(true)
            .load()
            .expect("load with prompt_missing(true) and no missing secrets");
        assert_eq!(resolved.secrets.api_key, "key-value");
        assert_eq!(resolved.secrets.database_url, "postgres://localhost/db");
    }

    #[test]
    fn default_fails_fast_on_missing_secret() {
        if std::env::var_os(CHILD_CASE_VAR).is_none() {
            assert!(run_in_isolated_project(
                "default_fails_fast_on_missing_secret",
                None
            ));
            return;
        }

        // Left unset, `prompt_missing` defaults to `false`: a missing
        // required secret still fails fast with `RequiredSecretMissing`,
        // exactly like before `prompt_missing` existed.
        let result = SecretSpec::builder()
            .with_provider("dotenv://.env")
            .with_reason("integration test")
            .load();
        assert!(matches!(
            result,
            Err(secretspec::SecretSpecError::RequiredSecretMissing(_))
        ));
    }

    #[test]
    fn prompt_missing_without_a_terminal_fails_with_required_secret_missing() {
        if std::env::var_os(CHILD_CASE_VAR).is_none() {
            assert!(run_in_isolated_project(
                "prompt_missing_without_a_terminal_fails_with_required_secret_missing",
                None,
            ));
            return;
        }

        // `Secrets::ensure_secrets` only prompts when stdin is a real
        // terminal; the child process's stdin here isn't one (inherited
        // from `cargo test`, itself piped), so `prompt_missing(true)`
        // must degrade to the identical `RequiredSecretMissing` a
        // non-interactive load gives, never hang waiting for input.
        let result = SecretSpec::builder()
            .with_provider("dotenv://.env")
            .with_reason("integration test")
            .prompt_missing(true)
            .load();
        assert!(matches!(
            result,
            Err(secretspec::SecretSpecError::RequiredSecretMissing(_))
        ));
    }
}

/// A constraint violation (an `at_least_one`/`exactly_one` group with no/too
/// many members present) is a validation error with an empty
/// `missing_required` list, so it must keep failing as `ValidationFailed`
/// even with `prompt_missing(true)` — the interactive branch only triggers
/// when `missing_required` is non-empty. Runs in its own isolated project,
/// same as `prompt_missing` above, since `Secrets::load()` finds
/// `secretspec.toml` by walking up from `cwd`.
mod prompt_missing_constraint_violation {
    use super::*;
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    declare_secrets!("tests/fixtures/constraint_violation.toml");

    const CHILD_CASE_VAR: &str = "SECRETSPEC_DERIVE_PROMPT_MISSING_CONSTRAINT_CHILD";

    #[test]
    fn prompt_missing_does_not_intercept_constraint_violations() {
        if std::env::var_os(CHILD_CASE_VAR).is_none() {
            let project = TempDir::new().expect("create isolated project");
            fs::write(
                project.path().join("secretspec.toml"),
                include_str!("fixtures/constraint_violation.toml"),
            )
            .expect("write project manifest");

            let status = Command::new(std::env::current_exe().expect("locate integration test binary"))
                .args([
                    "prompt_missing_constraint_violation::prompt_missing_does_not_intercept_constraint_violations",
                    "--exact",
                    "--nocapture",
                ])
                .current_dir(project.path())
                .env(CHILD_CASE_VAR, "1")
                .env("HOME", project.path())
                .env("XDG_CONFIG_HOME", project.path())
                .env_remove("SECRETSPEC_PROFILE")
                .env_remove("SECRETSPEC_PROVIDER")
                .status()
                .expect("run isolated child test");
            assert!(status.success());
            return;
        }

        let result = SecretSpec::builder()
            .with_provider("dotenv://.env")
            .with_reason("integration test")
            .prompt_missing(true)
            .load();
        assert!(matches!(
            result,
            Err(secretspec::SecretSpecError::ValidationFailed(_))
        ));
    }
}

/// `load_profile()` threads `prompt_missing` through its own call to
/// `load_internal`, separate from `load()`'s — the "development" profile's
/// defaults satisfy every field, so this only needs to prove that wiring
/// doesn't break a successful `load_profile()`, mirroring
/// `prompt_missing_load_succeeds_when_nothing_is_missing` above for `load()`.
mod prompt_missing_load_profile {
    use super::*;
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    declare_secrets!("tests/fixtures/profiles.toml");

    const CHILD_CASE_VAR: &str = "SECRETSPEC_DERIVE_PROMPT_MISSING_LOAD_PROFILE_CHILD";

    #[test]
    fn prompt_missing_load_profile_succeeds_when_nothing_is_missing() {
        if std::env::var_os(CHILD_CASE_VAR).is_none() {
            let project = TempDir::new().expect("create isolated project");
            fs::write(
                project.path().join("secretspec.toml"),
                include_str!("fixtures/profiles.toml"),
            )
            .expect("write project manifest");

            let status = Command::new(std::env::current_exe().expect("locate integration test binary"))
                .args([
                    "prompt_missing_load_profile::prompt_missing_load_profile_succeeds_when_nothing_is_missing",
                    "--exact",
                    "--nocapture",
                ])
                .current_dir(project.path())
                .env(CHILD_CASE_VAR, "1")
                .env("HOME", project.path())
                .env("XDG_CONFIG_HOME", project.path())
                .env_remove("SECRETSPEC_PROFILE")
                .env_remove("SECRETSPEC_PROVIDER")
                .status()
                .expect("run isolated child test");
            assert!(status.success());
            return;
        }

        let resolved = SecretSpec::builder()
            .with_provider("dotenv://.env")
            .with_profile("development")
            .with_reason("integration test")
            .prompt_missing(true)
            .load_profile()
            .expect("load_profile with prompt_missing(true) and no missing secrets");
        match resolved.secrets {
            SecretSpecProfile::Development { api_key, .. } => {
                assert_eq!(api_key, "dev-api-key");
            }
            _ => panic!("Expected Development variant"),
        }
    }
}

mod profile_generation {
    use super::*;

    declare_secrets!("tests/fixtures/profiles.toml");

    #[test]
    fn test_profile_enum_variants() {
        // Verify Profile enum has the expected variants
        let _dev = Profile::Development;
        let _staging = Profile::Staging;
        let _prod = Profile::Production;
    }

    #[test]
    fn test_profile_specific_types() {
        // This verifies the profile-specific enum variants have correct field types
        fn _test_development(profile: SecretSpecProfile) {
            match profile {
                SecretSpecProfile::Development {
                    api_key,
                    database_url,
                    redis_url,
                } => {
                    let _: String = api_key; // Supplied by its manifest default
                    let _: String = database_url; // Required but has default
                    let _: Option<String> = redis_url; // Optional
                }
                _ => panic!("Expected Development variant"),
            }
        }

        fn _test_production(profile: SecretSpecProfile) {
            match profile {
                SecretSpecProfile::Production {
                    api_key,
                    database_url,
                    redis_url,
                } => {
                    let _: String = api_key; // Required in prod
                    let _: String = database_url; // Required in prod
                    let _: String = redis_url; // Required in prod
                }
                _ => panic!("Expected Production variant"),
            }
        }
    }

    #[test]
    fn test_union_type_fields() {
        // Verify the union struct has Option for fields that are optional in any profile
        fn _test_field_types(s: SecretSpec) {
            let _: String = s.api_key; // Defaulted in development, required elsewhere
            let _: String = s.database_url; // Has default in dev but still required
            let _: Option<String> = s.redis_url; // Optional by default
        }
    }
}

mod complex_generation {
    use super::*;

    declare_secrets!("tests/fixtures/complex.toml");

    #[test]
    fn test_complex_field_types() {
        fn _test_field_types(s: SecretSpec) {
            let _: String = s.always_required;
            let _: String = s.required_with_default; // Its default guarantees a value
            let _: Option<String> = s.always_optional;
            let _: Option<String> = s.complex_secret; // Optional in dev and test
            let _: Option<String> = s.multi_profile; // Optional in base
        }
    }

    #[test]
    fn test_all_profiles_generated() {
        // Verify all profiles from the TOML are generated
        let _dev = Profile::Development;
        let _staging = Profile::Staging;
        let _prod = Profile::Production;
        let _test = Profile::Test;
    }
}

mod as_path_lifetime {
    use super::*;
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    declare_secrets!("tests/fixtures/as_path.toml");

    const CHILD_PROCESS: &str = "SECRETSPEC_DERIVE_AS_PATH_LIFETIME_CHILD";

    #[test]
    fn typed_as_path_lifetime_child() {
        if std::env::var_os(CHILD_PROCESS).is_none() {
            return;
        }

        let resolved =
            SecretSpec::load(Some("dotenv://.env"), None).expect("load the typed as_path secret");
        let path = resolved.secrets.cert_data.clone();

        assert!(
            path.exists(),
            "the generated loader must keep an as_path file alive while Resolved is alive"
        );

        drop(resolved);

        assert!(
            !path.exists(),
            "dropping Resolved must remove its as_path temporary file"
        );
    }

    #[test]
    fn typed_as_path_file_lives_as_long_as_resolved() {
        if std::env::var_os(CHILD_PROCESS).is_some() {
            return;
        }

        let project = TempDir::new().expect("create isolated project");
        fs::write(
            project.path().join("secretspec.toml"),
            include_str!("fixtures/as_path.toml"),
        )
        .expect("write project manifest");
        fs::write(
            project.path().join(".env"),
            "CERT_DATA=certificate-content\n",
        )
        .expect("write dotenv provider");

        let status = Command::new(std::env::current_exe().expect("locate integration test binary"))
            .args([
                "as_path_lifetime::typed_as_path_lifetime_child",
                "--exact",
                "--nocapture",
            ])
            .current_dir(project.path())
            .env(CHILD_PROCESS, "1")
            .env("HOME", project.path())
            .env("XDG_CONFIG_HOME", project.path())
            .env_remove("SECRETSPEC_PROFILE")
            .env_remove("SECRETSPEC_PROVIDER")
            .status()
            .expect("run isolated child test");

        assert!(status.success(), "child lifetime assertion failed");
    }
}

mod json_serialization {
    use super::*;

    declare_secrets!("tests/fixtures/basic.toml");

    #[test]
    fn test_secret_spec_secrets_json_serialization() {
        use secretspec::Resolved;

        // Create a mock SecretSpec instance
        let spec = SecretSpec {
            api_key: "test_key".to_string(),
            database_url: "postgres://localhost/db".to_string(),
            optional_secret: "optional".to_string(),
        };

        let secrets_wrapper = Resolved::new(spec, "dotenv".to_string(), "production".to_string());

        // Test serialization to JSON
        let json = serde_json::to_string(&secrets_wrapper).expect("Failed to serialize Resolved");

        // Verify JSON contains expected fields
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("Failed to parse JSON");
        assert_eq!(parsed["provider"], "dotenv");
        assert_eq!(parsed["profile"], "production");
        assert_eq!(parsed["secrets"]["api_key"], "test_key");

        // Test round-trip deserialization
        let deserialized: Resolved<SecretSpec> =
            serde_json::from_str(&json).expect("Failed to deserialize Resolved");
        assert_eq!(deserialized.provider, "dotenv");
        assert_eq!(deserialized.profile, "production");
        assert_eq!(deserialized.secrets.api_key, "test_key");
    }
}

mod profile_inheritance {
    use super::*;

    declare_secrets!("tests/fixtures/profile_inheritance.toml");

    #[test]
    fn test_profile_inheritance_compilation() {
        // This test verifies that the macro successfully processes a TOML file
        // where profiles have partial secret definitions that rely on field-level inheritance

        // Verify all expected profiles are generated
        let _default = Profile::Default;
        let _dev = Profile::Development;
        let _prod = Profile::Production;
        let _staging = Profile::Staging;
    }

    #[test]
    fn test_union_type_with_inheritance() {
        // Verify the union struct has all secrets from all profiles
        fn _test_field_types(s: SecretSpec) {
            let _: String = s.database_url;
            let _: String = s.api_key;
            let _: String = s.session_secret;
            let _: String = s.cache_ttl;
            let _: Option<String> = s.debug_mode;
            let _: Option<String> = s.enable_profiling;
        }
    }

    #[test]
    fn test_profile_specific_with_inheritance() {
        // Test that each profile variant has the expected fields
        fn _test_default(profile: SecretSpecProfile) {
            match profile {
                SecretSpecProfile::Default {
                    database_url,
                    api_key,
                    session_secret,
                    cache_ttl,
                } => {
                    let _: String = database_url; // Required
                    let _: String = api_key; // Required
                    let _: String = session_secret; // Required
                    let _: String = cache_ttl; // Guaranteed by default
                }
                _ => panic!("Expected Default variant"),
            }
        }

        fn _test_development(profile: SecretSpecProfile) {
            match profile {
                SecretSpecProfile::Development {
                    database_url,
                    session_secret,
                    debug_mode,
                    api_key,
                    cache_ttl,
                } => {
                    let _: String = database_url; // Guaranteed by override default
                    let _: String = session_secret; // Guaranteed by development-only default
                    let _: String = debug_mode; // Guaranteed by its default
                    let _: String = api_key; // Inherited from default
                    let _: String = cache_ttl; // Inherited from default
                }
                _ => panic!("Expected Development variant"),
            }
        }

        fn _test_production(profile: SecretSpecProfile) {
            match profile {
                SecretSpecProfile::Production {
                    database_url,
                    api_key,
                    session_secret,
                    cache_ttl,
                } => {
                    let _: String = database_url; // Override: required
                    let _: String = api_key; // Override: required
                    let _: String = session_secret; // Override: required
                    let _: String = cache_ttl; // Inherited from default
                }
                _ => panic!("Expected Production variant"),
            }
        }

        fn _test_staging(profile: SecretSpecProfile) {
            match profile {
                SecretSpecProfile::Staging {
                    database_url,
                    session_secret,
                    enable_profiling,
                    api_key,
                    cache_ttl,
                } => {
                    let _: String = database_url; // Override: required
                    let _: String = session_secret; // Override: required
                    let _: String = enable_profiling; // Guaranteed by its default
                    let _: String = api_key; // Inherited from default
                    let _: String = cache_ttl; // Inherited from default
                }
                _ => panic!("Expected Staging variant"),
            }
        }
    }

    #[test]
    fn test_builder_works_with_inherited_profiles() {
        // Verify the builder is generated correctly
        let _builder = SecretSpec::builder();

        // Test that we can specify different profiles
        // (We're not actually loading, just verifying the API exists)
        let _ = SecretSpec::builder()
            .with_profile("development")
            .with_provider("dotenv://.env");

        let _ = SecretSpec::builder()
            .with_profile(Profile::Production)
            .with_provider("keyring://");

        // The builder exposes with_reason so typed SDK callers can satisfy the
        // require_reason policy (default "agents") without relying on the
        // SECRETSPEC_REASON env var. (Not loading; just verifying the API exists.)
        let _ = SecretSpec::builder()
            .with_reason("running database migrations")
            .with_provider("dotenv://.env");

        // Caller context identifies an integration without standing in for a
        // user-supplied reason (0.20+).
        let _ = SecretSpec::builder()
            .with_caller(
                secretspec::CallerContext::new("git")
                    .with_operation("credential_get")
                    .with_resource("github.com"),
            )
            .with_provider("dotenv://.env");

        // `prompt_missing` opts the typed loader into the same
        // prompt-and-store behavior `Secrets::ensure_secrets` gives the
        // untyped `Secrets` API. (Not loading; just verifying the API exists.)
        let _ = SecretSpec::builder()
            .prompt_missing(true)
            .with_provider("dotenv://.env");
    }
}