anodizer-core 0.15.3

Core configuration, context, and template engine for the anodizer release tool
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
//! Shared test infrastructure for the anodizer workspace.
//!
//! This module is gated behind the `test-helpers` feature so that other crates
//! in the workspace can pull it in as a dev-dependency:
//!
//! ```toml
//! [dev-dependencies]
//! anodizer-core = { workspace = true, features = ["test-helpers"] }
//! ```
//!
//! Provides:
//! - [`TestContextBuilder`] — fluent builder for [`Context`] with sensible defaults
//! - [`Context::test_fixture`] - stable minimally-populated Context for cross-crate unit tests
//! - [`CwdGuard`] — RAII helper that restores the original cwd on Drop (panic-safe)
//! - [`create_test_project`] — creates a minimal Cargo project on disk
//! - [`init_git_repo`] — initializes a git repo with config, initial commit, and tag
//! - [`init_git_repo_with_commits`] — initializes a git repo with multiple commits
//! - [`create_config`] — writes `.anodizer.yaml`
//! - [`make_git_info`] — creates a [`GitInfo`] with sensible defaults
//! - [`create_fake_binary`] — creates a dummy binary file for archive/checksum tests
//! - [`responder`] — shared in-process HTTP responder for unit tests
//!   (consolidates ~11 inline copies; fixes the v0.3.0 chocolatey /
//!   v0.3.0 github-rate-limit CI flakes)

pub mod artifact_set;
pub mod env;
pub mod fake_tool;
pub mod https_responder;
pub mod responder;
pub mod scripted_responder;

use crate::config::{Config, CrateConfig, Defaults, SignConfig, UpxConfig, WorkspaceConfig};
use crate::context::{Context, ContextOptions};
use crate::git::{GitInfo, SemVer};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

// ---------------------------------------------------------------------------
// CwdGuard — panic-safe cwd restore
// ---------------------------------------------------------------------------

/// RAII guard that captures the current working directory on construction,
/// switches to `target`, and restores the original cwd on Drop.
///
/// This makes cwd-mutating tests panic-safe: if the test body panics between
/// `CwdGuard::new(...)` and the end of the scope, the original cwd is still
/// restored when the guard unwinds — preventing one test's failure from
/// contaminating subsequent tests in the same process.
///
/// Pair with `serial_test` whenever more than one test in the same test
/// binary touches cwd, since changing cwd is a process-wide side effect.
/// Within ONE test binary, *every* cwd-touching test — a swapper like this
/// guard, or a test that spawns a cwd-sensitive subprocess (e.g. `rustc -vV`
/// in `partial.rs`) — must share a SINGLE serial key. If two cwd swappers land
/// in different serial groups they run concurrently and one captures the
/// other's soon-to-be-deleted tempdir as its restore target, so the restore
/// fails `NotFound`. The workspace-canonical key is
/// `#[serial_test::serial(cwd)]`; a binary whose cwd tests already co-vary with
/// PATH/env under a broader unnamed `#[serial]` (e.g. the `FakeToolDir` tests
/// in `stage-notarize`) keeps that one key for all of them. The rule is
/// one-key-per-binary, enforced for raw `set_current_dir` call sites by
/// `.claude/scripts/audit-test-isolation.sh`.
///
/// # Example
///
/// ```rust,ignore
/// use anodizer_core::test_helpers::CwdGuard;
///
/// #[test]
/// #[serial_test::serial(cwd)]
/// fn my_test() {
///     let tmp = tempfile::tempdir().unwrap();
///     let _guard = CwdGuard::new(tmp.path()).unwrap();
///     // ... test body; cwd is now tmp.path() ...
///     // panic-safe: original cwd is restored when `_guard` drops.
/// }
/// ```
pub struct CwdGuard {
    original: PathBuf,
}

impl CwdGuard {
    /// Capture the current cwd and switch to `target`. Returns the guard;
    /// the original cwd is restored when the guard is dropped.
    pub fn new(target: impl AsRef<Path>) -> std::io::Result<Self> {
        let original = std::env::current_dir()?;
        std::env::set_current_dir(target.as_ref())?; // cwd-ok: blessed CwdGuard swap; users serialise via #[serial(cwd)]
        Ok(Self { original })
    }
}

impl Drop for CwdGuard {
    fn drop(&mut self) {
        // Best-effort: ignore the error during unwind so we never double-panic.
        let _ = std::env::set_current_dir(&self.original); // cwd-ok: RAII restore in the blessed CwdGuard
    }
}

// ---------------------------------------------------------------------------
// TestContextBuilder
// ---------------------------------------------------------------------------

/// Fluent builder for [`Context`] with sensible defaults suitable for tests.
///
/// # Example
///
/// ```rust,ignore
/// use anodizer_core::test_helpers::TestContextBuilder;
///
/// let ctx = TestContextBuilder::new()
///     .project_name("my-app")
///     .tag("v2.0.0")
///     .dry_run(true)
///     .build();
///
/// assert_eq!(ctx.template_vars().get("ProjectName").map(|s| s.as_str()), Some("my-app"));
/// assert_eq!(ctx.template_vars().get("Tag").map(|s| s.as_str()), Some("v2.0.0"));
/// ```
pub struct TestContextBuilder {
    project_name: String,
    tag: String,
    commit: String,
    short_commit: String,
    branch: String,
    dirty: bool,
    semver: SemVer,
    commit_date: String,
    commit_timestamp: String,
    previous_tag: Option<String>,
    snapshot: bool,
    dry_run: bool,
    verbose: bool,
    debug: bool,
    show_skipped: bool,
    publish_only: bool,
    skip_stages: Vec<String>,
    publisher_allowlist: Vec<String>,
    selected_crates: Vec<String>,
    token: Option<String>,
    parallelism: usize,
    single_target: Option<String>,
    crates: Vec<CrateConfig>,
    workspaces: Option<Vec<WorkspaceConfig>>,
    populate_git_vars: bool,
    dist: Option<PathBuf>,
    signs: Vec<SignConfig>,
    binary_signs: Vec<SignConfig>,
    upx: Vec<UpxConfig>,
    defaults: Option<Defaults>,
    source: Option<crate::config::SourceConfig>,
    sboms: Vec<crate::config::SbomConfig>,
    project_root: Option<PathBuf>,
    env_overrides: Vec<(String, String)>,
    seal_env: bool,
    changelog_from: Option<String>,
    changelog_full_history: bool,
    changelog_to: Option<String>,
    changelog_preview: bool,
}

impl Default for TestContextBuilder {
    fn default() -> Self {
        Self {
            project_name: "test-project".to_string(),
            tag: "v1.2.3".to_string(),
            commit: "abc123def456abc123def456abc123def456abc1".to_string(),
            short_commit: "abc123d".to_string(),
            branch: "main".to_string(),
            dirty: false,
            semver: SemVer {
                major: 1,
                minor: 2,
                patch: 3,
                prerelease: None,
                build_metadata: None,
            },
            commit_date: "2026-03-25T10:30:00+00:00".to_string(),
            commit_timestamp: "1774463400".to_string(),
            previous_tag: Some("v1.2.2".to_string()),
            snapshot: false,
            dry_run: false,
            verbose: false,
            debug: false,
            show_skipped: false,
            publish_only: false,
            skip_stages: Vec::new(),
            publisher_allowlist: Vec::new(),
            selected_crates: Vec::new(),
            token: None,
            parallelism: 1,
            single_target: None,
            crates: Vec::new(),
            workspaces: None,
            populate_git_vars: true,
            dist: None,
            signs: Vec::new(),
            binary_signs: Vec::new(),
            upx: Vec::new(),
            defaults: None,
            source: None,
            sboms: Vec::new(),
            project_root: None,
            env_overrides: Vec::new(),
            seal_env: false,
            changelog_from: None,
            changelog_full_history: false,
            changelog_to: None,
            changelog_preview: false,
        }
    }
}

impl TestContextBuilder {
    /// Create a new builder with sensible defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the project name (populates `Config::project_name` and the `ProjectName` template var).
    pub fn project_name(mut self, name: &str) -> Self {
        self.project_name = name.to_string();
        self
    }

    /// Set the git tag. Also parses it to update the semver fields.
    /// If parsing fails, only the tag string is updated.
    pub fn tag(mut self, tag: &str) -> Self {
        self.tag = tag.to_string();
        if let Ok(sv) = crate::git::parse_semver_tag(tag) {
            self.semver = sv;
        }
        self
    }

    /// Set the full commit SHA.
    pub fn commit(mut self, commit: &str) -> Self {
        self.commit = commit.to_string();
        self.short_commit = commit.chars().take(7).collect();
        self
    }

    /// Set the git branch.
    pub fn branch(mut self, branch: &str) -> Self {
        self.branch = branch.to_string();
        self
    }

    /// Set the dirty flag.
    pub fn dirty(mut self, dirty: bool) -> Self {
        self.dirty = dirty;
        self
    }

    /// Set a prerelease suffix (e.g. "rc.1", "beta.2").
    pub fn prerelease(mut self, pre: Option<&str>) -> Self {
        self.semver.prerelease = pre.map(|s| s.to_string());
        self
    }

    /// Set the previous tag.
    pub fn previous_tag(mut self, tag: Option<&str>) -> Self {
        self.previous_tag = tag.map(|s| s.to_string());
        self
    }

    /// Enable or disable snapshot mode.
    pub fn snapshot(mut self, snapshot: bool) -> Self {
        self.snapshot = snapshot;
        self
    }

    /// Enable or disable dry-run mode.
    pub fn dry_run(mut self, dry_run: bool) -> Self {
        self.dry_run = dry_run;
        self
    }

    /// Enable or disable verbose mode.
    pub fn verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    /// Enable or disable debug mode.
    pub fn debug(mut self, debug: bool) -> Self {
        self.debug = debug;
        self
    }

    /// Force per-crate "no `<publisher>` config block" skip lines to surface
    /// at default verbosity (`--show-skipped`). Sets
    /// [`ContextOptions::show_skipped`].
    pub fn show_skipped(mut self, show_skipped: bool) -> Self {
        self.show_skipped = show_skipped;
        self
    }

    /// Enable or disable `--publish-only` mode
    /// ([`ContextOptions::publish_only`]). Drives `Context::is_publish_only`,
    /// which gates the `binary_signs:` loop off (its output has no publish-time
    /// consumer).
    pub fn publish_only(mut self, publish_only: bool) -> Self {
        self.publish_only = publish_only;
        self
    }

    /// Set stages to skip.
    pub fn skip_stages(mut self, stages: Vec<String>) -> Self {
        self.skip_stages = stages;
        self
    }

    /// Set the `--publishers` allowlist (`ContextOptions::publisher_allowlist`).
    /// An empty vec deselects nothing; a non-empty one restricts the publish
    /// stage to exactly the named publishers.
    pub fn publisher_allowlist(mut self, publishers: Vec<String>) -> Self {
        self.publisher_allowlist = publishers;
        self
    }

    /// Set selected crates filter.
    pub fn selected_crates(mut self, crates: Vec<String>) -> Self {
        self.selected_crates = crates;
        self
    }

    /// Set the GitHub token.
    pub fn token(mut self, token: Option<String>) -> Self {
        self.token = token;
        self
    }

    /// Set the parallelism level.
    pub fn parallelism(mut self, p: usize) -> Self {
        self.parallelism = p;
        self
    }

    /// Set a single target triple.
    pub fn single_target(mut self, target: Option<String>) -> Self {
        self.single_target = target;
        self
    }

    /// Add crate configurations to the config.
    pub fn crates(mut self, crates: Vec<CrateConfig>) -> Self {
        self.crates = crates;
        self
    }

    /// Add workspace configurations to the config.
    pub fn workspaces(mut self, workspaces: Vec<WorkspaceConfig>) -> Self {
        self.workspaces = Some(workspaces);
        self
    }

    /// Whether to auto-populate git template vars (default: true).
    /// Set to false if you want to test the context before git vars are populated.
    pub fn populate_git_vars(mut self, populate: bool) -> Self {
        self.populate_git_vars = populate;
        self
    }

    /// Set the dist directory (output directory for artifacts).
    pub fn dist(mut self, dist: PathBuf) -> Self {
        self.dist = Some(dist);
        self
    }

    /// Set sign configurations.
    pub fn signs(mut self, signs: Vec<SignConfig>) -> Self {
        self.signs = signs;
        self
    }

    /// Set binary-specific sign configurations.
    pub fn binary_signs(mut self, binary_signs: Vec<SignConfig>) -> Self {
        self.binary_signs = binary_signs;
        self
    }

    /// Set UPX configurations.
    pub fn upx(mut self, upx: Vec<UpxConfig>) -> Self {
        self.upx = upx;
        self
    }

    /// Set default configuration (e.g. global checksum skip).
    pub fn defaults(mut self, defaults: Defaults) -> Self {
        self.defaults = Some(defaults);
        self
    }

    /// Set source archive configuration.
    pub fn source(mut self, source: crate::config::SourceConfig) -> Self {
        self.source = Some(source);
        self
    }

    /// Add an SBOM configuration to the list.
    pub fn add_sbom(mut self, sbom: crate::config::SbomConfig) -> Self {
        self.sboms.push(sbom);
        self
    }

    /// Set explicit project root directory (avoids process-wide CWD mutation in tests).
    pub fn project_root(mut self, root: PathBuf) -> Self {
        self.project_root = Some(root);
        self
    }

    /// Set the explicit changelog range start (`changelog --from <ref>`).
    pub fn changelog_from(mut self, from: Option<&str>) -> Self {
        self.changelog_from = from.map(str::to_string);
        self
    }

    /// Opt into the full-history changelog range (`changelog ..`): the
    /// changelog stage skips previous-tag auto-discovery.
    pub fn changelog_full_history(mut self, full: bool) -> Self {
        self.changelog_full_history = full;
        self
    }

    /// Set the explicit changelog upper bound (`changelog <from>..<to>`): the
    /// changelog stage walks `<from>..<to>` instead of `<from>..HEAD`.
    pub fn changelog_to(mut self, to: Option<&str>) -> Self {
        self.changelog_to = to.map(str::to_string);
        self
    }

    /// Mark the run as the standalone `changelog --format release-notes`
    /// local preview: the changelog stage bypasses the snapshot-skip gate and
    /// the `github-native` branch, and `resolve_git_context` skips the
    /// tag-at-HEAD / dirty-tree bails.
    pub fn changelog_preview(mut self, preview: bool) -> Self {
        self.changelog_preview = preview;
        self
    }

    /// Add an environment variable override. Calling [`env`](Self::env)
    /// at least once swaps the built context's env source to a
    /// [`MapEnvSource`](crate::MapEnvSource) seeded from the
    /// accumulated overrides — so production code that reads through
    /// [`Context::env_var`](crate::context::Context::env_var) sees the
    /// injected values without `std::env::set_var`. Calls accumulate
    /// (later wins on duplicate key).
    pub fn env<K: Into<String>, V: Into<String>>(mut self, k: K, v: V) -> Self {
        self.env_overrides.push((k.into(), v.into()));
        self
    }

    /// Seal the built context's env source to a *closed*
    /// [`MapEnvSource`](crate::MapEnvSource) holding only the values added
    /// via [`env`](Self::env) (an empty map when none were added) — never the
    /// ambient process environment.
    ///
    /// The default env source is [`ProcessEnvSource`], which reads
    /// `std::env::var`. A test that exercises a credential / runner-detection
    /// path (`NPM_TOKEN`, `ACTIONS_ID_TOKEN_REQUEST_*`, `GITHUB_ACTIONS`,
    /// `RUNNER_ENVIRONMENT`, …) without sealing observes whatever the dev shell
    /// or CI runner happens to export, so the same test passes locally and
    /// fails on a GitHub-hosted runner (which sets those vars). Sealing makes
    /// the env hermetic so the assertion reflects only the injected fixture.
    ///
    /// [`env`](Self::env) already seals (a non-empty override list swaps to a
    /// closed map); call this to seal an *empty* env without seeding a sentinel
    /// override.
    ///
    /// ```ignore
    /// // Closed env: production code sees no OIDC/token vars regardless of host.
    /// let ctx = TestContextBuilder::new().sealed_env().build();
    /// ```
    ///
    /// [`ProcessEnvSource`]: crate::env_source::ProcessEnvSource
    pub fn sealed_env(mut self) -> Self {
        self.seal_env = true;
        self
    }

    /// Build the [`Context`] with the configured values.
    #[allow(clippy::field_reassign_with_default)]
    pub fn build(self) -> Context {
        let mut config = Config::default();
        config.project_name = self.project_name;
        config.crates = self.crates;
        config.workspaces = self.workspaces;
        config.signs = self.signs;
        config.binary_signs = self.binary_signs;
        config.upx = self.upx;
        config.defaults = self.defaults;
        config.source = self.source;
        config.sboms = self.sboms;
        if let Some(dist) = self.dist {
            config.dist = dist;
        }

        let options = ContextOptions {
            snapshot: self.snapshot,
            nightly: false,
            dry_run: self.dry_run,
            quiet: false,
            verbose: self.verbose,
            debug: self.debug,
            skip_stages: self.skip_stages,
            selected_crates: self.selected_crates,
            token: self.token,
            parallelism: self.parallelism,
            single_target: self.single_target,
            release_notes_path: None,
            fail_fast: false,
            partial_target: None,
            merge: false,
            publish_only: self.publish_only,
            preflight_secrets: false,
            project_root: self.project_root,
            strict: false,
            resume_release: false,
            replace_existing_artifacts: false,
            skip_post_publish_poll: false,
            gate_submitter: None,
            rollback_mode: None,
            simulate_failure_publishers: Vec::new(),
            rollback_only: false,
            allow_rerun: false,
            show_skipped: self.show_skipped,
            from_run: None,
            runtime_nondeterministic_allowlist: Vec::new(),
            summary_json_path: None,
            allow_ai_failure: false,
            changelog_from: self.changelog_from,
            changelog_full_history: self.changelog_full_history,
            changelog_to: self.changelog_to,
            changelog_preview: self.changelog_preview,
            notify: false,
            allow_snapshot_publish: false,
            publisher_allowlist: self.publisher_allowlist,
        };

        let mut ctx = Context::new(config, options);

        ctx.git_info = Some(GitInfo {
            tag: self.tag,
            commit: self.commit,
            short_commit: self.short_commit,
            branch: self.branch,
            dirty: self.dirty,
            semver: self.semver,
            commit_date: self.commit_date,
            commit_timestamp: self.commit_timestamp,
            previous_tag: self.previous_tag,
            remote_url: String::new(),
            summary: String::new(),
            tag_subject: String::new(),
            tag_contents: String::new(),
            tag_body: String::new(),
            first_commit: None,
        });

        if self.populate_git_vars {
            ctx.populate_git_vars();
        }

        ctx.populate_metadata_var().unwrap();

        if self.seal_env || !self.env_overrides.is_empty() {
            let mut src = crate::MapEnvSource::new();
            for (k, v) in self.env_overrides {
                src.set(k, v);
            }
            ctx.set_env_source(src);
        }

        ctx
    }
}

// ---------------------------------------------------------------------------
// Context::test_fixture — stable entry point for downstream unit tests
// ---------------------------------------------------------------------------

impl Context {
    /// Return a minimally-populated [`Context`] suitable for unit tests in
    /// downstream crates.
    ///
    /// This is a thin wrapper over [`TestContextBuilder`] with a fixed tag
    /// (`v0.0.0-test`) so the value is stable and obviously synthetic across
    /// every crate that builds a [`Context`] in `#[cfg(test)]` code. Use the
    /// builder directly when a test needs to vary fields.
    ///
    /// Gated by the `test-helpers` feature; enable it in your crate's
    /// `[dev-dependencies]` to access this constructor:
    ///
    /// ```toml
    /// [dev-dependencies]
    /// anodizer-core = { workspace = true, features = ["test-helpers"] }
    /// ```
    pub fn test_fixture() -> Context {
        TestContextBuilder::new().tag("v0.0.0-test").build()
    }
}

// ---------------------------------------------------------------------------
// Filesystem helpers
// ---------------------------------------------------------------------------

/// Create a minimal Cargo project in the given directory.
///
/// Writes a `Cargo.toml` (with project name "test-project") and `src/main.rs`.
pub fn create_test_project(dir: &Path) {
    fs::write(
        dir.join("Cargo.toml"),
        r#"
[package]
name = "test-project"
version = "0.1.0"
edition = "2024"

[[bin]]
name = "test-project"
path = "src/main.rs"
"#,
    )
    .unwrap_or_else(|e| panic!("failed to write Cargo.toml: {e}"));

    fs::create_dir_all(dir.join("src")).unwrap_or_else(|e| panic!("failed to create src/: {e}"));
    fs::write(
        dir.join("src/main.rs"),
        r#"fn main() { println!("hello"); }"#,
    )
    .unwrap_or_else(|e| panic!("failed to write src/main.rs: {e}"));
}

/// Write an `.anodizer.yaml` config file in the given directory.
pub fn create_config(dir: &Path, content: &str) {
    fs::write(dir.join(".anodizer.yaml"), content)
        .unwrap_or_else(|e| panic!("failed to write .anodizer.yaml: {e}"));
}

/// `true` when `name`'s trailing `.sha256` / `.sig` segments form a FORBIDDEN
/// recursive chain — a checksum or signature applied to a derived sidecar.
///
/// GoReleaser-parity legitimate sidecar terminals (return `false`):
///   - `X.sha256`            — checksum of a primary
///   - `X.sig`               — signature of a primary
///   - `X.sha256.sig`        — signature of a checksum (GR signs checksums)
///   - `X.cdx.json.sha256`   — checksum of an SBOM
///
/// Forbidden chains (return `true`) — a derived sidecar fed back as a subject:
///   - `X.sha256.sig.sha256` — checksum OF a signature (the v0.9.0 bug)
///   - `X.sig.sig`           — signature of a signature
///   - `X.sig.sha256`        — checksum of a signature
///   - `X.sha256.sha256`     — checksum of a checksum
///
/// The single allowed adjacency among trailing sidecar segments is `.sig`
/// following `.sha256` (signing a checksum). Any other adjacency — `.sha256`
/// after `.sha256`, `.sha256` after `.sig`, or `.sig` after `.sig` — is a
/// re-derivation of a sidecar and is forbidden.
///
/// Shared by the checksum-stage, sign-stage, and verify-release regression
/// suites, which assert that no produced/demanded asset name matches the chain.
///
/// # Classification basis
///
/// This reasons PURELY on filename suffixes (the trailing `.sha256` / `.sig`
/// adjacency) and assumes a PRIMARY artifact's name never itself ends in a
/// sidecar extension. A hypothetical primary literally named `foo.sig` would
/// be misclassified: its legitimate checksum `foo.sig.sha256` parses as the
/// forbidden `sig → sha256` adjacency even though no recursion occurred.
/// anodize produces no such fixture, so there is no live bug — but a caller
/// that needs kind-aware classification (distinguishing a primary that merely
/// ends in `.sig` from a real signature sidecar) must pass the artifact KIND
/// rather than rely on this name-only heuristic.
pub fn has_recursive_sidecar_chain(name: &str) -> bool {
    // Peel trailing sidecar segments off the end; record them outermost-first.
    let mut rest = name;
    let mut outer_to_inner: Vec<&str> = Vec::new();
    loop {
        if let Some(stripped) = rest.strip_suffix(".sha256") {
            rest = stripped;
            outer_to_inner.push("sha256");
        } else if let Some(stripped) = rest.strip_suffix(".sig") {
            rest = stripped;
            outer_to_inner.push("sig");
        } else {
            break;
        }
    }
    // Re-order inner→outer (the order sidecars were actually applied) and check
    // each adjacency. The ONLY legitimate adjacency is sha256 -> sig.
    let inner_to_outer: Vec<&str> = outer_to_inner.into_iter().rev().collect();
    inner_to_outer
        .windows(2)
        .any(|pair| !(pair[0] == "sha256" && pair[1] == "sig"))
}

/// Create a fake binary file at `dir/<name>` for testing archive/checksum stages.
///
/// The file contains a small amount of recognizable data so tests can
/// verify the file was included in archives or checksum outputs.
pub fn create_fake_binary(dir: &Path, name: &str) -> std::path::PathBuf {
    let path = dir.join(name);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .unwrap_or_else(|e| panic!("failed to create parent dir for fake binary: {e}"));
    }
    // Write a recognizable pattern that is not all zeros
    let data: Vec<u8> = (0..256u16).map(|i| (i % 256) as u8).collect();
    fs::write(&path, &data).unwrap_or_else(|e| panic!("failed to write fake binary: {e}"));
    path
}

// ---------------------------------------------------------------------------
// Transient-spawn retry (Windows nextest flakiness)
// ---------------------------------------------------------------------------

// NTSTATUS process-creation failure codes (as the i32 `ExitStatus::code()`
// surfaces them on Windows). Windows GitHub runners intermittently fail to
// *create* a child process under heavy parallel nextest load — desktop-heap /
// handle pressure makes the loader abort before the program's first
// instruction. The OS reports this as one of these NTSTATUS values, never as a
// program-level error: a real `git`/`node` failure returns 1 or 128. Retrying
// only on these codes therefore masks no genuine failure — the program never
// ran. Treated as i32 to match the sign-extended value `code()` returns.
#[cfg(windows)]
const TRANSIENT_SPAWN_CODES: &[i32] = &[
    0xC000_0142u32 as i32, // STATUS_DLL_INIT_FAILED
    0xC000_0135u32 as i32, // STATUS_DLL_NOT_FOUND
    0xC000_007Bu32 as i32, // STATUS_INVALID_IMAGE_FORMAT
    0xC000_0017u32 as i32, // STATUS_NO_MEMORY
    0xC000_0018u32 as i32, // STATUS_CONFLICTING_ADDRESSES
];

/// True iff `status` is a transient Windows process-creation failure (the OS
/// failing to *start* the child, not the child erroring).
///
/// Always `false` on non-Windows targets — these NTSTATUS codes are
/// Windows-only, and no Unix exit status is a "spawn-init failure".
#[cfg(windows)]
pub fn is_transient_spawn_failure(status: &std::process::ExitStatus) -> bool {
    status
        .code()
        .is_some_and(|c| TRANSIENT_SPAWN_CODES.contains(&c))
}

/// True iff `status` is a transient Windows process-creation failure (the OS
/// failing to *start* the child, not the child erroring).
///
/// Always `false` on non-Windows targets — these NTSTATUS codes are
/// Windows-only, and no Unix exit status is a "spawn-init failure".
#[cfg(not(windows))]
pub fn is_transient_spawn_failure(_status: &std::process::ExitStatus) -> bool {
    false
}

/// Run a freshly-built [`Command`] to completion, retrying on transient Windows
/// process-creation failures.
///
/// `build` MUST construct a brand-new [`Command`] on each call — [`Command`] is
/// consumed by [`Command::output`], so a single instance cannot be reused
/// across attempts. `what` names the spawned tool for panic messages.
///
/// Retries up to 5 attempts only when [`is_transient_spawn_failure`] holds (a
/// Windows loader abort under parallel nextest load — see
/// [`TRANSIENT_SPAWN_CODES`]); a real program error is returned immediately and
/// unretried, so this masks no genuine failure. Backoff between attempts is
/// 50ms, 100ms, 200ms, 400ms.
pub fn output_with_spawn_retry(
    mut build: impl FnMut() -> Command,
    what: &str,
) -> std::process::Output {
    const MAX_ATTEMPTS: u32 = 5;
    for attempt in 0..MAX_ATTEMPTS {
        let out = build()
            .output()
            .unwrap_or_else(|e| panic!("{what} failed to spawn: {e}"));
        let last = attempt + 1 == MAX_ATTEMPTS;
        if is_transient_spawn_failure(&out.status) && !last {
            // 50, 100, 200, 400ms — give the loader time to shed handle pressure.
            let backoff = std::time::Duration::from_millis(50u64 << attempt);
            std::thread::sleep(backoff);
            continue;
        }
        return out;
    }
    unreachable!("loop returns on the final attempt")
}

// ---------------------------------------------------------------------------
// Git helpers
// ---------------------------------------------------------------------------

/// Run a git command in `dir`, panicking on failure.
fn run_git(dir: &Path, args: &[&str]) {
    let owned: Vec<String> = args.iter().map(|a| a.to_string()).collect();
    let output = output_with_spawn_retry(
        || {
            let mut cmd = Command::new("git");
            cmd.args(&owned).current_dir(dir);
            cmd
        },
        "git",
    );
    assert!(
        output.status.success(),
        "git {:?} failed with status {}: {}",
        args,
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );
}

/// Initialize a git repository with a config, initial commit, and `v0.1.0` tag.
///
/// Expects that the directory already has files to commit (e.g. from [`create_test_project`]).
pub fn init_git_repo(dir: &Path) {
    run_git(dir, &["init"]);
    run_git(dir, &["config", "user.email", "test@test.com"]);
    run_git(dir, &["config", "user.name", "Test"]);
    run_git(dir, &["add", "-A"]);
    run_git(dir, &["commit", "-m", "initial"]);
    run_git(dir, &["tag", "v0.1.0"]);
}

/// Initialize a git repository with multiple commits.
///
/// Each entry in `commits` becomes a separate commit. A file `commit_N.txt` is
/// created for each so that there is always something to commit. A `v0.1.0` tag
/// is placed on the first commit.
pub fn init_git_repo_with_commits(dir: &Path, commits: &[&str]) {
    run_git(dir, &["init"]);
    run_git(dir, &["config", "user.email", "test@test.com"]);
    run_git(dir, &["config", "user.name", "Test"]);

    for (i, message) in commits.iter().enumerate() {
        let filename = format!("commit_{}.txt", i);
        fs::write(dir.join(&filename), format!("content for commit {}", i))
            .unwrap_or_else(|e| panic!("failed to write commit file: {e}"));
        run_git(dir, &["add", "-A"]);
        run_git(dir, &["commit", "-m", message]);

        // Tag the first commit
        if i == 0 {
            run_git(dir, &["tag", "v0.1.0"]);
        }
    }
}

// ---------------------------------------------------------------------------
// GitInfo helper
// ---------------------------------------------------------------------------

/// Create a [`GitInfo`] with sensible defaults for testing.
///
/// - tag: `v1.2.3`, semver: 1.2.3
/// - commit: `abc123def456...`
/// - branch: `main`
/// - previous_tag: `v1.2.2`
///
/// The `dirty` and `prerelease` parameters control the most commonly varied fields.
pub fn make_git_info(dirty: bool, prerelease: Option<&str>) -> GitInfo {
    GitInfo {
        tag: "v1.2.3".to_string(),
        commit: "abc123def456abc123def456abc123def456abc1".to_string(),
        short_commit: "abc123d".to_string(),
        branch: "main".to_string(),
        dirty,
        semver: SemVer {
            major: 1,
            minor: 2,
            patch: 3,
            prerelease: prerelease.map(|s| s.to_string()),
            build_metadata: None,
        },
        commit_date: "2026-03-25T10:30:00+00:00".to_string(),
        commit_timestamp: "1774463400".to_string(),
        previous_tag: Some("v1.2.2".to_string()),
        remote_url: String::new(),
        summary: String::new(),
        tag_subject: String::new(),
        tag_contents: String::new(),
        tag_body: String::new(),
        first_commit: None,
    }
}

// ---------------------------------------------------------------------------
// Module-level tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(windows)]
    #[test]
    fn transient_spawn_classifier_windows() {
        use std::os::windows::process::ExitStatusExt;
        let init_fail = std::process::ExitStatus::from_raw(0xC000_0142);
        assert!(
            is_transient_spawn_failure(&init_fail),
            "STATUS_DLL_INIT_FAILED must classify as transient spawn failure"
        );
        let real_error = std::process::ExitStatus::from_raw(1);
        assert!(
            !is_transient_spawn_failure(&real_error),
            "a genuine exit code 1 must NOT classify as a spawn failure"
        );
    }

    #[cfg(not(windows))]
    #[test]
    fn transient_spawn_classifier_non_windows_always_false() {
        // On Unix no exit status is a process-creation init failure; a real
        // program error (here `false`, which exits 1) must classify as false.
        let status = Command::new("false")
            .status()
            .expect("spawn `false` to obtain a known-failing status");
        assert!(!status.success(), "`false` is expected to exit non-zero");
        assert!(
            !is_transient_spawn_failure(&status),
            "non-Windows classifier must always be false"
        );
    }

    #[test]
    fn recursive_sidecar_chain_detector_classification() {
        // Forbidden: a checksum/signature applied to a derived sidecar.
        assert!(
            has_recursive_sidecar_chain("x.sha256.sig.sha256"),
            "checksum of a signature"
        );
        assert!(
            has_recursive_sidecar_chain("x.tar.gz.sig.sig"),
            "signature of a signature"
        );
        assert!(
            has_recursive_sidecar_chain("x.tar.gz.sig.sha256"),
            "checksum of a signature"
        );
        assert!(
            has_recursive_sidecar_chain("x.tar.gz.sha256.sha256"),
            "checksum of a checksum"
        );
        assert!(
            has_recursive_sidecar_chain("x.cdx.json.sha256.sig.sha256"),
            "checksum of a signature-of-a-checksum"
        );
        // Legitimate GoReleaser-parity terminals.
        assert!(
            !has_recursive_sidecar_chain("x.tar.gz.sha256.sig"),
            "signature of a checksum is GR-legit (the second level)"
        );
        assert!(!has_recursive_sidecar_chain("x.tar.gz.sha256"));
        assert!(!has_recursive_sidecar_chain("x.tar.gz.sig"));
        assert!(!has_recursive_sidecar_chain("x.cdx.json.sha256"));
        assert!(!has_recursive_sidecar_chain("x.tar.gz"));
    }

    #[test]
    fn test_builder_default_produces_valid_context() {
        let ctx = TestContextBuilder::new().build();
        assert_eq!(
            ctx.template_vars().get("ProjectName"),
            Some(&"test-project".to_string())
        );
        assert_eq!(ctx.template_vars().get("Tag"), Some(&"v1.2.3".to_string()));
        assert_eq!(
            ctx.template_vars().get("Version"),
            Some(&"1.2.3".to_string())
        );
        assert_eq!(ctx.template_vars().get("Major"), Some(&"1".to_string()));
        assert_eq!(ctx.template_vars().get("Branch"), Some(&"main".to_string()));
        assert!(!ctx.is_dry_run());
        assert!(!ctx.is_snapshot());
    }

    #[test]
    fn test_builder_custom_project_name() {
        let ctx = TestContextBuilder::new().project_name("my-app").build();
        assert_eq!(
            ctx.template_vars().get("ProjectName"),
            Some(&"my-app".to_string())
        );
    }

    #[test]
    fn test_builder_custom_tag_updates_semver() {
        let ctx = TestContextBuilder::new().tag("v3.0.0-rc.1").build();
        assert_eq!(
            ctx.template_vars().get("Tag"),
            Some(&"v3.0.0-rc.1".to_string())
        );
        assert_eq!(ctx.template_vars().get("Major"), Some(&"3".to_string()));
        assert_eq!(
            ctx.template_vars().get("Prerelease"),
            Some(&"rc.1".to_string())
        );
    }

    #[test]
    fn test_builder_dirty_flag() {
        let ctx = TestContextBuilder::new().dirty(true).build();
        assert_eq!(
            ctx.template_vars().get_structured("IsGitDirty"),
            Some(&serde_json::Value::Bool(true))
        );
        assert_eq!(
            ctx.template_vars().get("GitTreeState"),
            Some(&"dirty".to_string())
        );
    }

    #[test]
    fn test_builder_dry_run() {
        let ctx = TestContextBuilder::new().dry_run(true).build();
        assert!(ctx.is_dry_run());
    }

    #[test]
    fn test_builder_snapshot() {
        let ctx = TestContextBuilder::new().snapshot(true).build();
        assert!(ctx.is_snapshot());
        assert_eq!(
            ctx.template_vars().get_structured("IsSnapshot"),
            Some(&serde_json::Value::Bool(true))
        );
    }

    #[test]
    fn test_builder_skip_stages() {
        let ctx = TestContextBuilder::new()
            .skip_stages(vec!["build".to_string(), "publish".to_string()])
            .build();
        assert!(ctx.should_skip("build"));
        assert!(ctx.should_skip("publish"));
        assert!(!ctx.should_skip("release"));
    }

    #[test]
    fn test_builder_no_populate_git_vars() {
        let ctx = TestContextBuilder::new().populate_git_vars(false).build();
        // ProjectName is set in Context::new, not populate_git_vars
        assert_eq!(
            ctx.template_vars().get("ProjectName"),
            Some(&"test-project".to_string())
        );
        // Tag should not be set since we skipped populate_git_vars
        assert_eq!(ctx.template_vars().get("Tag"), None);
    }

    #[test]
    fn test_make_git_info_clean() {
        let info = make_git_info(false, None);
        assert!(!info.dirty);
        assert_eq!(info.semver.prerelease, None);
        assert_eq!(info.tag, "v1.2.3");
    }

    #[test]
    fn test_make_git_info_dirty_with_prerelease() {
        let info = make_git_info(true, Some("beta.1"));
        assert!(info.dirty);
        assert_eq!(info.semver.prerelease, Some("beta.1".to_string()));
    }

    #[test]
    fn test_create_fake_binary() {
        let tmp = tempfile::TempDir::new().unwrap();

        let path = create_fake_binary(tmp.path(), "myapp");
        assert!(path.exists());
        let data = fs::read(&path).unwrap();
        assert_eq!(data.len(), 256);
    }

    #[test]
    fn test_create_fake_binary_nested() {
        let tmp = tempfile::TempDir::new().unwrap();

        let path = create_fake_binary(tmp.path(), "subdir/myapp");
        assert!(path.exists());
    }

    #[test]
    fn test_builder_render_template() {
        let ctx = TestContextBuilder::new()
            .project_name("myapp")
            .tag("v2.0.0")
            .build();
        let result = ctx
            .render_template("{{ .ProjectName }}-{{ .Version }}")
            .unwrap();
        assert_eq!(result, "myapp-2.0.0");
    }

    #[test]
    fn test_builder_with_crates() {
        let crate_cfg = CrateConfig {
            name: "my-crate".to_string(),
            path: ".".to_string(),
            tag_template: "v{{ .Version }}".to_string(),
            ..Default::default()
        };
        let ctx = TestContextBuilder::new().crates(vec![crate_cfg]).build();
        assert_eq!(ctx.config.crates.len(), 1);
        assert_eq!(ctx.config.crates[0].name, "my-crate");
    }

    #[test]
    fn context_test_fixture_builds_without_panic() {
        let ctx = Context::test_fixture();
        // Tag is the documented stable value so downstream crates can
        // assert against it.
        assert_eq!(
            ctx.template_vars().get("Tag"),
            Some(&"v0.0.0-test".to_string())
        );
        let info = ctx
            .git_info
            .as_ref()
            .expect("test_fixture must populate git_info");
        assert_eq!(info.tag, "v0.0.0-test");
        assert_eq!(info.semver.major, 0);
        assert_eq!(info.semver.minor, 0);
        assert_eq!(info.semver.patch, 0);
        assert_eq!(info.semver.prerelease.as_deref(), Some("test"));
    }

    #[test]
    fn test_context_builder_env_injects_map_source() {
        let ctx = TestContextBuilder::new()
            .env("A", "1")
            .env("B", "2")
            .build();
        assert_eq!(ctx.env_var("A"), Some("1".to_string()));
        assert_eq!(ctx.env_var("B"), Some("2".to_string()));
        assert_eq!(ctx.env_var("C"), None);
    }

    #[test]
    fn test_context_builder_without_env_uses_process_source() {
        let ctx = TestContextBuilder::new().build();
        // An unset name returns None regardless of which source is wired.
        // The assertion targets the unset-var branch so the test stays
        // deterministic across CI / dev shells.
        assert_eq!(ctx.env_var("ANODIZER_T3_UNSET_VAR_X"), None);
    }
}