tracel-xtask 4.15.0

Reusable and Extensible xtask commands to manage repositories.
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/// Manage AWS Secrets Manager secrets.
use std::{collections::HashMap, fmt::Write as _, fs, io, path::PathBuf};

use anyhow::Context as _;

use crate::prelude::{Context, Environment};
use crate::utils::aws::cli::{
    secretsmanager_create_empty_secret, secretsmanager_get_secret_string,
    secretsmanager_list_secret_versions_json, secretsmanager_put_secret_string,
};

const FALLBACK_EDITOR: &str = "vi";

#[tracel_xtask_macros::declare_command_args(None, SecretsSubCommand)]
pub struct SecretsCmdArgs {}

impl Default for SecretsSubCommand {
    fn default() -> Self {
        SecretsSubCommand::View(SecretsViewSubCmdArgs::default())
    }
}

#[derive(clap::Args, Default, Clone, PartialEq)]
pub struct SecretsCreateSubCmdArgs {
    /// Region where the secret will be created
    #[arg(long)]
    pub region: String,

    /// Secret name to create with an initial empty JSON value
    #[arg(value_name = "SECRET_ID")]
    pub secret_id: String,

    /// Optional description for the secret
    #[arg(long)]
    pub description: Option<String>,
}

#[derive(clap::Args, Default, Clone, PartialEq)]
pub struct SecretsCopySubCmdArgs {
    /// Region where the secrets live
    #[arg(long)]
    pub region: String,

    /// Source secret identifier (name or ARN)
    #[arg(long, value_name = "FROM_SECRET_ID")]
    pub from: String,

    /// Target secret identifier (name or ARN)
    #[arg(long, value_name = "TO_SECRET_ID")]
    pub to: String,
}

#[derive(clap::Args, Default, Clone, PartialEq)]
pub struct SecretsEditSubCmdArgs {
    /// Region where the secret lives
    #[arg(long)]
    pub region: String,

    /// Secret identifier (name or ARN)
    #[arg(value_name = "SECRET_ID")]
    pub secret_id: String,

    /// Push the new secret version without asking for confirmation
    #[arg(short = 'y', long = "yes")]
    pub yes: bool,
}

#[derive(clap::Args, Default, Clone, PartialEq)]
pub struct SecretsEnvFileSubCmdArgs {
    /// Output file path. If omitted, writes to stdout.
    #[arg(long)]
    pub output: Option<std::path::PathBuf>,

    /// Region where the secrets live
    #[arg(long)]
    pub region: String,

    /// Secret identifiers (names or ARN), can provide multiple ones.
    #[arg(value_name = "SECRET_ID", num_args(1..), required = true)]
    pub secret_ids: Vec<String>,
}

#[derive(clap::Args, Default, Clone, PartialEq)]
pub struct SecretsListSubCmdArgs {
    /// Region where the secret lives
    #[arg(long)]
    pub region: String,

    /// Secret identifier (name or ARN)
    #[arg(value_name = "SECRET_ID")]
    pub secret_id: String,
}

#[derive(clap::Args, Default, Clone, PartialEq)]
pub struct SecretsPushSubCmdArgs {
    /// Region where the secret lives
    #[arg(long)]
    pub region: String,

    /// Secret identifier (name or ARN)
    #[arg(long, value_name = "SECRET_ID")]
    pub secret_id: String,

    /// Push the new secret version without asking for confirmation
    #[arg(short = 'y', long = "yes")]
    pub yes: bool,

    /// Key-value updates in the form KEY=VALUE
    #[arg(value_name = "KEY=VALUE", num_args(1..), required = true)]
    pub kv: Vec<String>,
}

#[derive(clap::Args, Default, Clone, PartialEq)]
pub struct SecretsViewSubCmdArgs {
    /// Region where the secret lives
    #[arg(long)]
    pub region: String,

    /// Secret identifier (name or ARN)
    #[arg(value_name = "SECRET_ID")]
    pub secret_id: String,
}

pub fn handle_command(
    args: SecretsCmdArgs,
    _env: Environment,
    _ctx: Context,
) -> anyhow::Result<()> {
    match args.get_command() {
        SecretsSubCommand::Create(create_args) => create(create_args),
        SecretsSubCommand::Copy(copy_args) => copy(copy_args),
        SecretsSubCommand::Edit(edit_args) => edit(edit_args),
        SecretsSubCommand::EnvFile(env_args) => env_file(env_args),
        SecretsSubCommand::List(list_args) => list(list_args),
        SecretsSubCommand::Push(push_args) => push(push_args),
        SecretsSubCommand::View(view_args) => view(view_args),
    }
}

/// Create a secret and attach an initial empty JSON (`{}`) version as plain text.
fn create(args: SecretsCreateSubCmdArgs) -> anyhow::Result<()> {
    // create the secret metadata
    secretsmanager_create_empty_secret(&args.secret_id, &args.region, args.description.as_deref())?;
    // add a first version as an empty JSON object.
    secretsmanager_put_secret_string(&args.secret_id, &args.region, "{}")?;
    eprintln!(
        "✅ Created secret '{}' in region '{}' with an initial empty JSON value.",
        args.secret_id, args.region
    );
    Ok(())
}

/// Copy a secret value from one secret ID to another in the same region.
pub fn copy(args: SecretsCopySubCmdArgs) -> anyhow::Result<()> {
    if args.from == args.to {
        eprintln!(
            "Source and target secrets are identical ('{}'), nothing to do.",
            args.from
        );
        return Ok(());
    }

    eprintln!(
        "Fetching source secret '{}' in region '{}'...",
        args.from, args.region
    );
    let value = secretsmanager_get_secret_string(&args.from, &args.region, "text")?;

    // Check if the target already has a current version.
    // If we can successfully fetch it, we consider that a current version exists
    // and ask for confirmation before creating a new one.
    let target_has_version =
        secretsmanager_get_secret_string(&args.to, &args.region, "text").is_ok();
    if target_has_version {
        eprintln!(
            "Secret '{}' already has a current version in region '{}'.",
            args.to, args.region
        );
        if !confirm_push()? {
            eprintln!("Aborting: new secret version was not pushed.");
            return Ok(());
        }
    }

    eprintln!(
        "Writing target secret '{}' in region '{}'...",
        args.to, args.region
    );
    secretsmanager_put_secret_string(&args.to, &args.region, &value)?;
    eprintln!(
        "✅ Copied secret value from '{}' to '{}'.",
        args.from, args.to
    );

    Ok(())
}

/// Fetch secret into a temp file, open editor,
/// ask to commit or discard on close and then push a new version if confirmed.
///
/// Behavior:
/// - If the secret is JSON, it is pretty-printed for editing and stored back
///   minified on a single line.
/// - If the secret is not JSON, it is treated as an opaque string.
fn edit(args: SecretsEditSubCmdArgs) -> anyhow::Result<()> {
    // 1) fetch current secret value
    let original_raw = secretsmanager_get_secret_string(&args.secret_id, &args.region, "text")?;
    let original_raw_trimmed = original_raw.trim_end_matches('\n');
    // 2) make things pretty if possible
    let to_edit =
        pretty_json(original_raw_trimmed).unwrap_or_else(|| original_raw_trimmed.to_string());
    // 3) write the secrets to a temp file for editing
    let tmp_path = temp_file_path(&args.secret_id);
    fs::write(&tmp_path, &to_edit)?;
    eprintln!(
        "Editing secret '{}' in region '{}' using temporary file:\n  {}",
        args.secret_id,
        args.region,
        tmp_path.display()
    );
    // 4) open editor
    let editor = detect_editor();
    let mut parts = editor.split_whitespace();
    let cmd = parts.next().unwrap_or(FALLBACK_EDITOR);
    let mut command = std::process::Command::new(cmd);
    for arg in parts {
        command.arg(arg);
    }
    command.arg(&tmp_path);
    let status = command
        .status()
        .map_err(|e| anyhow::anyhow!("launching editor '{editor}' should succeed: {e}"))?;
    if !status.success() {
        fs::remove_file(&tmp_path).ok();
        return Err(anyhow::anyhow!(
            "editor '{editor}' should exit successfully (exit status {status})"
        ));
    }
    // 5) read updated contents of file and do some cleanup
    let edited_raw = fs::read_to_string(&tmp_path)?;
    fs::remove_file(&tmp_path).ok();
    let edited_raw_trimmed = edited_raw.trim_end_matches('\n');
    // Try to treat content as JSON on both sides
    let original_norm_json = normalize_json(original_raw_trimmed);
    let edited_norm_json = normalize_json(edited_raw_trimmed);
    // If both are valid JSON, compare and store minified JSON
    if let (Some(orig_norm), Some(edited_norm)) = (original_norm_json, edited_norm_json) {
        if orig_norm == edited_norm {
            eprintln!(
                "No changes detected (JSON content unchanged), not pushing a new secret version."
            );
            return Ok(());
        }
        eprintln!("Secret JSON content has changed.");
        if !args.yes && !confirm_push()? {
            eprintln!("Aborting: new secret version was not pushed.");
            return Ok(());
        }
        // Store minified JSON
        secretsmanager_put_secret_string(&args.secret_id, &args.region, &edited_norm)?;
        eprintln!(
            "✅ New JSON version pushed for secret '{}' in region '{}'.",
            args.secret_id, args.region
        );
        return Ok(());
    }
    // 6) Fallback for non-JSON secrets
    if edited_raw_trimmed == original_raw_trimmed {
        eprintln!("No changes detected, not pushing a new secret version.");
        return Ok(());
    }
    eprintln!("Secret content has changed.");
    if !args.yes && !confirm_push()? {
        eprintln!("Aborting: new secret version was not pushed.");
        return Ok(());
    }
    secretsmanager_put_secret_string(&args.secret_id, &args.region, edited_raw_trimmed)?;
    eprintln!(
        "✅ New version pushed for secret '{}' in region '{}'.",
        args.secret_id, args.region
    );

    Ok(())
}

pub fn env_file(args: SecretsEnvFileSubCmdArgs) -> anyhow::Result<()> {
    if args.secret_ids.is_empty() {
        eprintln!("No secrets provided.");
        return Ok(());
    }
    // In case of multiple same secret names, the latest wins
    let mut merged: HashMap<String, String> = HashMap::new();
    for id in &args.secret_ids {
        eprintln!("Fetching secret '{id}'...");
        let s = secretsmanager_get_secret_string(id, &args.region, "text")?;
        let s = s.trim();
        if s.is_empty() {
            continue;
        }
        // 1) try JSON object
        if let Ok(value) = serde_json::from_str::<serde_json::Value>(s) {
            if let Some(obj) = value.as_object() {
                for (k, v) in obj {
                    let v_str = v
                        .as_str()
                        .map(|x| x.to_string())
                        .unwrap_or_else(|| v.to_string());
                    merged.insert(k.clone(), v_str);
                }
                continue;
            }
        }
        // 2) fallback to .env style format
        for line in s.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            if let Some((k, v)) = line.split_once('=') {
                merged.insert(k.trim().to_string(), v.trim().to_string());
            }
        }
    }

    // Expand variable inside values
    let merged = expand_env_map(&merged);

    // Sort the env vars for deterministic ordering
    let mut entries: Vec<(String, String)> = merged.into_iter().collect();
    entries.sort_by(|a, b| a.0.cmp(&b.0));
    // Write to passed path or to stdout if no path has been passed
    let mut buf = String::new();
    for (k, v) in entries {
        writeln!(&mut buf, "{k}={v}")?;
    }
    if let Some(path) = args.output {
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir)?;
        }
        std::fs::write(&path, buf)?;
        eprintln!("Wrote env file to {}", path.display());
    } else {
        print!("{buf}");
    }

    Ok(())
}

/// list all versions of the secrets.
fn list(args: SecretsListSubCmdArgs) -> anyhow::Result<()> {
    eprintln!(
        "Listing versions for secret '{}' in region '{}'...",
        args.secret_id, args.region
    );
    let json = secretsmanager_list_secret_versions_json(&args.secret_id, &args.region)?;
    let v: serde_json::Value = serde_json::from_str(&json).context(
        "Parsing Secrets Manager list-secret-version-ids response as JSON should succeed",
    )?;
    let versions = v
        .get("Versions")
        .and_then(|v| v.as_array())
        .ok_or_else(|| {
            anyhow::anyhow!(
                "AWS response for secret '{}' should contain a 'Versions' array",
                args.secret_id
            )
        })?;

    if versions.is_empty() {
        println!("No versions found for secret '{}'.", args.secret_id);
        return Ok(());
    }

    struct Row {
        id: String,
        created: String,
        stages: String,
    }

    let mut rows: Vec<Row> = Vec::new();
    let mut id_w = "VersionId".len();
    let mut created_w = "Created".len();
    let mut stages_w = "Stages".len();

    for ver in versions {
        let id = ver
            .get("VersionId")
            .and_then(|x| x.as_str())
            .unwrap_or("")
            .to_string();

        let created = match ver.get("CreatedDate") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(serde_json::Value::Number(n)) => n.to_string(),
            Some(other) => other.to_string(),
            None => "".to_string(),
        };

        let stages = ver
            .get("VersionStages")
            .and_then(|x| x.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str())
                    .collect::<Vec<_>>()
                    .join(",")
            })
            .unwrap_or_default();

        id_w = id_w.max(id.len());
        created_w = created_w.max(created.len());
        stages_w = stages_w.max(stages.len());

        rows.push(Row {
            id,
            created,
            stages,
        });
    }

    // Header
    println!(
        "{:<id_w$}  {:<created_w$}  {:<stages_w$}",
        "VersionId",
        "Created",
        "Stages",
        id_w = id_w,
        created_w = created_w,
        stages_w = stages_w,
    );

    // Separator
    println!(
        "{:-<id_w$}  {:-<created_w$}  {:-<stages_w$}",
        "",
        "",
        "",
        id_w = id_w,
        created_w = created_w,
        stages_w = stages_w,
    );

    // Rows
    for r in rows {
        println!(
            "{:<id_w$}  {:<created_w$}  {:<stages_w$}",
            r.id,
            r.created,
            r.stages,
            id_w = id_w,
            created_w = created_w,
            stages_w = stages_w,
        );
    }

    Ok(())
}

/// Push updates to a JSON secret by setting one or more KEY=VALUE pairs.
/// The secret must be a JSON object and updated value is stored on a single line.
pub fn push(args: SecretsPushSubCmdArgs) -> anyhow::Result<()> {
    // 1) fetch current secrets
    eprintln!(
        "Fetching secret '{}' in region '{}'...",
        args.secret_id, args.region
    );
    let original = secretsmanager_get_secret_string(&args.secret_id, &args.region, "text")?;
    let original_trimmed = original.trim_end_matches('\n');
    let mut value: serde_json::Value =
        serde_json::from_str(original_trimmed).with_context(|| {
            format!(
                "Parsing secret '{}' as JSON should succeed to use the 'push' subcommand",
                args.secret_id
            )
        })?;
    let obj = value.as_object_mut().ok_or_else(|| {
        anyhow::anyhow!(
            "Secret '{}' should be a JSON object to use the 'push' subcommand",
            args.secret_id
        )
    })?;

    // 2) Add key-value pairs to secret
    let mut changed = false;
    eprintln!("Changed entries to update:");
    for kv in &args.kv {
        let (key, val) = kv.split_once('=').ok_or_else(|| {
            anyhow::anyhow!(
                "Key/value argument '{kv}' should use the KEY=VALUE format for secret '{}'",
                args.secret_id
            )
        })?;
        let key = key.trim();
        let val = val.trim();
        if key.is_empty() {
            anyhow::bail!(
                "Key in '{kv}' should not be empty for secret '{}'",
                args.secret_id
            );
        }
        let existing = obj.get(key);
        // If existing value is the same string, skip
        if let Some(existing_val) = existing {
            if existing_val.is_string() && existing_val.as_str() == Some(val) {
                // skip value if it has not changed
                continue;
            }
        }
        obj.insert(key.to_string(), serde_json::Value::String(val.to_string()));
        changed = true;
        eprintln!("  - {key}");
    }
    if !changed {
        eprintln!("None.");
        eprintln!(
            "No changes detected (JSON content unchanged), not pushing a new secret version."
        );
        return Ok(());
    }

    // 3) Confirmation prompt
    eprintln!("Secret JSON content has changed.");
    if !args.yes && !confirm_push()? {
        eprintln!("Aborting: new secret version was not pushed.");
        return Ok(());
    }

    // 4) Store the new version of the secrets as a minified JSON format
    let normalized =
        serde_json::to_string(&value).context("Serializing updated JSON secret should succeed")?;
    secretsmanager_put_secret_string(&args.secret_id, &args.region, &normalized)?;
    eprintln!(
        "✅ Updated secret '{}' in region '{}' with {} key(s).",
        args.secret_id,
        args.region,
        args.kv.len()
    );

    Ok(())
}

/// fetch and print the secret.
fn view(args: SecretsViewSubCmdArgs) -> anyhow::Result<()> {
    let value = secretsmanager_get_secret_string(&args.secret_id, &args.region, "text")?;
    let trimmed = value.trim_end_matches('\n');

    if let Some(pretty) = pretty_json(trimmed) {
        println!("{pretty}");
    } else {
        println!("{value}");
    }

    Ok(())
}

/// Build a temp file path for editing a secret.
fn temp_file_path(secret_id: &str) -> PathBuf {
    let mut base: String = secret_id
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
        .collect();
    if base.len() > 64 {
        base.truncate(64);
    }
    let pid = std::process::id();
    let filename = format!("tracel-secret-{base}-{pid}.tmp");
    std::env::temp_dir().join(filename)
}

/// Detect the editor to use $VISUAL then $EDITOR then falling back to "vi".
fn detect_editor() -> String {
    std::env::var("VISUAL")
        .or_else(|_| std::env::var("EDITOR"))
        .unwrap_or_else(|_| FALLBACK_EDITOR.to_string())
}

/// Try to pretty-print JSON
fn pretty_json(s: &str) -> Option<String> {
    let value: serde_json::Value = serde_json::from_str(s).ok()?;
    serde_json::to_string_pretty(&value).ok()
}

/// Normalize JSON to a canonical minified form.
fn normalize_json(s: &str) -> Option<String> {
    let value: serde_json::Value = serde_json::from_str(s).ok()?;
    serde_json::to_string(&value).ok()
}

/// Ask user to confirm pushing a new secret version.
fn confirm_push() -> anyhow::Result<bool> {
    use std::io::Write as _;

    print!("Do you want to push a new secret version? [y/N]: ");
    io::stdout().flush().ok();

    let mut answer = String::new();
    io::stdin().read_line(&mut answer)?;
    let answer = answer.trim().to_lowercase();
    Ok(answer == "y" || answer == "yes")
}

/// Expand `${VAR}` placeholders in values using the given map.
fn expand_value(input: &str, vars: &HashMap<String, String>) -> String {
    let mut out = String::new();
    let mut rest = input;

    while let Some(start) = rest.find("${") {
        // keep everything before the placeholder
        out.push_str(&rest[..start]);
        let after = &rest[start + 2..];
        // find the closing brace
        if let Some(end_rel) = after.find('}') {
            let var_name = &after[..end_rel];
            if let Some(val) = vars.get(var_name) {
                // known var: substitute
                out.push_str(val);
            } else {
                // unknown var: keep the placeholder as-is
                out.push_str("${");
                out.push_str(var_name);
                out.push('}');
            }
            // continue after the closing brace
            rest = &after[end_rel + 1..];
        } else {
            // no closing brace, keep the rest as-is
            out.push_str(&rest[start..]);
            rest = "";
            break;
        }
    }
    // trailing part without placeholders
    out.push_str(rest);
    out
}

/// Expand `${VAR}` placeholders in a merged env map.
/// Note: this is a single pass expansion, preserving quotes and formatting.
fn expand_env_map(merged: &HashMap<String, String>) -> HashMap<String, String> {
    let mut expanded = HashMap::new();
    for (key, value) in merged {
        let new_val = expand_value(value, merged);
        expanded.insert(key.clone(), new_val);
    }
    expanded
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use std::env;

    #[test]
    #[serial]
    fn test_expand_env_map_simple_expansion() {
        // Clean any prior values that might confuse debugging
        unsafe {
            env::remove_var("LOG_LEVEL_TEST");
            env::remove_var("RUST_LOG_TEST");
        }

        let mut merged: HashMap<String, String> = HashMap::new();
        merged.insert("LOG_LEVEL_TEST".to_string(), "info".to_string());
        merged.insert(
            "RUST_LOG_TEST".to_string(),
            "xtask=${LOG_LEVEL_TEST},server=${LOG_LEVEL_TEST}".to_string(),
        );

        let expanded = expand_env_map(&merged);

        let log_level = expanded
            .get("LOG_LEVEL_TEST")
            .expect("LOG_LEVEL_TEST should be present after expansion");
        let rust_log = expanded
            .get("RUST_LOG_TEST")
            .expect("RUST_LOG_TEST should be present after expansion");

        assert_eq!(
            log_level, "info",
            "LOG_LEVEL_TEST should keep its literal value after expansion"
        );
        assert!(
            !rust_log.contains("${LOG_LEVEL_TEST}"),
            "RUST_LOG_TEST should not contain the raw placeholder '${{LOG_LEVEL_TEST}}', got: {rust_log}"
        );
        assert!(
            rust_log.contains(log_level),
            "RUST_LOG_TEST should contain the expanded LOG_LEVEL_TEST value; LOG_LEVEL_TEST={log_level}, RUST_LOG_TEST={rust_log}"
        );
    }

    #[test]
    #[serial]
    fn test_expand_env_map_mixed_values_and_non_expanded_keys() {
        unsafe {
            env::remove_var("LOG_LEVEL_TEST");
            env::remove_var("RUST_LOG_TEST");
            env::remove_var("PLAIN_KEY_TEST");
        }

        let mut merged: HashMap<String, String> = HashMap::new();
        merged.insert("LOG_LEVEL_TEST".to_string(), "debug".to_string());
        merged.insert(
            "RUST_LOG_TEST".to_string(),
            "xtask=${LOG_LEVEL_TEST},other=${LOG_LEVEL_TEST}".to_string(),
        );
        merged.insert("PLAIN_KEY_TEST".to_string(), "no_placeholders".to_string());

        let expanded = expand_env_map(&merged);

        let log_level = expanded
            .get("LOG_LEVEL_TEST")
            .expect("LOG_LEVEL_TEST should be present after expansion");
        let rust_log = expanded
            .get("RUST_LOG_TEST")
            .expect("RUST_LOG_TEST should be present after expansion");
        let plain = expanded
            .get("PLAIN_KEY_TEST")
            .expect("PLAIN_KEY_TEST should be present after expansion");

        assert_eq!(log_level, "debug");
        assert!(
            !rust_log.contains("${LOG_LEVEL_TEST}"),
            "RUST_LOG_TEST should not contain the raw placeholder '${{LOG_LEVEL_TEST}}', got: {rust_log}"
        );
        assert!(
            rust_log.contains(log_level),
            "RUST_LOG_TEST should contain the expanded LOG_LEVEL_TEST value; LOG_LEVEL_TEST={log_level}, RUST_LOG_TEST={rust_log}"
        );
        assert_eq!(
            plain, "no_placeholders",
            "PLAIN_KEY_TEST should remain unchanged when there are no placeholders"
        );
    }

    #[test]
    #[serial]
    fn test_expand_env_map_unknown_placeholder_is_left_intact() {
        unsafe {
            env::remove_var("UNKNOWN_PLACEHOLDER_TEST");
            env::remove_var("USES_UNKNOWN_TEST");
        }

        let mut merged: HashMap<String, String> = HashMap::new();
        merged.insert(
            "USES_UNKNOWN_TEST".to_string(),
            "value=${UNKNOWN_PLACEHOLDER_TEST}".to_string(),
        );

        let expanded = expand_env_map(&merged);

        let uses_unknown = expanded
            .get("USES_UNKNOWN_TEST")
            .expect("USES_UNKNOWN_TEST should be present after expansion");

        // Unknown placeholders should be preserved exactly
        assert_eq!(
            uses_unknown, "value=${UNKNOWN_PLACEHOLDER_TEST}",
            "Unknown placeholder should be left intact"
        );
    }

    #[test]
    #[serial]
    fn test_expand_env_map_preserves_quotes_around_values() {
        unsafe {
            env::remove_var("LOG_LEVEL_TEST");
            env::remove_var("RUST_LOG_QUOTED_TEST");
            env::remove_var("CRON_TEST");
        }

        let mut merged: HashMap<String, String> = HashMap::new();
        merged.insert("LOG_LEVEL_TEST".to_string(), "info".to_string());
        // placeholder inside double quotes
        merged.insert(
            "RUST_LOG_QUOTED_TEST".to_string(),
            " \"xtask=${LOG_LEVEL_TEST},server=${LOG_LEVEL_TEST}\" ".to_string(),
        );
        // value that contains spaces and is already quoted
        merged.insert("CRON_TEST".to_string(), "'0 0 0 * * *'".to_string());

        let expanded = expand_env_map(&merged);

        let rust_log = expanded
            .get("RUST_LOG_QUOTED_TEST")
            .expect("RUST_LOG_QUOTED_TEST should be present after expansion");
        let cron = expanded
            .get("CRON_TEST")
            .expect("CRON_TEST should be present after expansion");

        // RUST_LOG_QUOTED_TEST should still start and end with a double quote (after trimming)
        let rust_trimmed = rust_log.trim();
        assert!(
            rust_trimmed.starts_with('"') && rust_trimmed.ends_with('"'),
            "RUST_LOG_QUOTED_TEST should still be double-quoted, got: {rust_log}"
        );
        assert!(
            rust_trimmed.contains("xtask=info"),
            "RUST_LOG_QUOTED_TEST should contain the expanded value; got: {rust_trimmed}"
        );

        // CRON_TEST should be unchanged, quotes preserved
        assert_eq!(
            cron, "'0 0 0 * * *'",
            "CRON_TEST should keep its single quotes and content"
        );
    }
}