anodizer 0.5.0

A Rust-native release automation tool inspired by GoReleaser
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
//! End-to-end tests for `anodizer bump`.
//!
//! Each test builds a minimal cargo workspace inside a `TempDir`, initializes
//! a git repo, and shells out to the compiled `anodizer` binary via
//! `CARGO_BIN_EXE_anodizer`. Workspaces only contain manifests (no sources) —
//! `bump` never invokes cargo, it only reads/writes `Cargo.toml`.

use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

fn anodizer() -> Command {
    Command::new(env!("CARGO_BIN_EXE_anodizer"))
}

fn run_git(dir: &Path, args: &[&str]) {
    let out = Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .unwrap_or_else(|e| panic!("git {:?} failed to spawn: {e}", args));
    assert!(
        out.status.success(),
        "git {:?} failed: {}",
        args,
        String::from_utf8_lossy(&out.stderr)
    );
}

fn git_init(dir: &Path) {
    run_git(dir, &["init", "-q"]);
    run_git(dir, &["config", "user.email", "test@test.com"]);
    run_git(dir, &["config", "user.name", "Test"]);
    run_git(dir, &["config", "commit.gpgsign", "false"]);
}

fn git_add_commit(dir: &Path, message: &str) {
    run_git(dir, &["add", "-A"]);
    run_git(dir, &["commit", "-q", "-m", message]);
}

fn git_commit_empty_on_path(dir: &Path, relpath: &str, content: &str, message: &str) {
    let full = dir.join(relpath);
    if let Some(parent) = full.parent() {
        fs::create_dir_all(parent).unwrap();
    }
    fs::write(&full, content).unwrap();
    run_git(dir, &["add", "-A"]);
    run_git(dir, &["commit", "-q", "-m", message]);
}

// ---------------------------------------------------------------------------
// Workspace builders
// ---------------------------------------------------------------------------

/// Single-crate workspace at `tmp/Cargo.toml` with literal version "0.1.0".
fn single_crate_workspace(tmp: &Path) {
    fs::write(
        tmp.join("Cargo.toml"),
        r#"[package]
name = "demo"
version = "0.1.0"
edition = "2024"
"#,
    )
    .unwrap();
}

/// Two-member workspace: `core` (0.1.0) and `cli` (0.1.0), cli depends on core.
fn two_crate_workspace(tmp: &Path) {
    fs::write(
        tmp.join("Cargo.toml"),
        r#"[workspace]
members = ["crates/core", "crates/cli"]
resolver = "2"
"#,
    )
    .unwrap();
    fs::create_dir_all(tmp.join("crates/core")).unwrap();
    fs::create_dir_all(tmp.join("crates/cli")).unwrap();
    fs::write(
        tmp.join("crates/core/Cargo.toml"),
        r#"[package]
name = "core"
version = "0.1.0"
edition = "2024"
"#,
    )
    .unwrap();
    fs::write(
        tmp.join("crates/cli/Cargo.toml"),
        r#"[package]
name = "cli"
version = "0.1.0"
edition = "2024"

[dependencies]
core = { path = "../core", version = "0.1.0" }
"#,
    )
    .unwrap();
}

/// Workspace with `version.workspace = true` inheritance on both members.
fn inheriting_workspace(tmp: &Path) {
    fs::write(
        tmp.join("Cargo.toml"),
        r#"[workspace]
members = ["crates/a", "crates/b"]
resolver = "2"

[workspace.package]
version = "0.3.0"
"#,
    )
    .unwrap();
    fs::create_dir_all(tmp.join("crates/a")).unwrap();
    fs::create_dir_all(tmp.join("crates/b")).unwrap();
    fs::write(
        tmp.join("crates/a/Cargo.toml"),
        r#"[package]
name = "a"
version.workspace = true
edition = "2024"
"#,
    )
    .unwrap();
    fs::write(
        tmp.join("crates/b/Cargo.toml"),
        r#"[package]
name = "b"
version.workspace = true
edition = "2024"
"#,
    )
    .unwrap();
}

/// Workspace where one member is `publish = false`.
fn workspace_with_private_member(tmp: &Path) {
    fs::write(
        tmp.join("Cargo.toml"),
        r#"[workspace]
members = ["crates/pub", "crates/priv"]
resolver = "2"
"#,
    )
    .unwrap();
    fs::create_dir_all(tmp.join("crates/pub")).unwrap();
    fs::create_dir_all(tmp.join("crates/priv")).unwrap();
    fs::write(
        tmp.join("crates/pub/Cargo.toml"),
        r#"[package]
name = "pub"
version = "0.1.0"
edition = "2024"
"#,
    )
    .unwrap();
    fs::write(
        tmp.join("crates/priv/Cargo.toml"),
        r#"[package]
name = "priv"
version = "0.1.0"
edition = "2024"
publish = false
"#,
    )
    .unwrap();
}

fn read_version(manifest: &Path) -> String {
    let text = fs::read_to_string(manifest).unwrap();
    for line in text.lines() {
        if let Some(rest) = line.trim().strip_prefix("version")
            && let Some(eq) = rest.find('=')
        {
            let raw = rest[eq + 1..].trim();
            // strip quotes if present
            if let Some(s) = raw
                .trim_start_matches('"')
                .split('"')
                .next()
                .filter(|s| !s.is_empty() && s.chars().next().unwrap().is_ascii_digit())
            {
                return s.to_string();
            }
        }
    }
    panic!("no version in {}", manifest.display());
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[test]
fn patch_bumps_single_crate() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "bump failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(read_version(&tmp.path().join("Cargo.toml")), "0.1.1");
}

#[test]
fn minor_explicit_then_major() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    anodizer()
        .current_dir(tmp.path())
        .args(["bump", "minor", "-y"])
        .status()
        .unwrap();
    assert_eq!(read_version(&tmp.path().join("Cargo.toml")), "0.2.0");
    // Tree is now dirty from the first bump; second bump must pass --allow-dirty.
    anodizer()
        .current_dir(tmp.path())
        .args(["bump", "major", "-y", "--allow-dirty"])
        .status()
        .unwrap();
    assert_eq!(read_version(&tmp.path().join("Cargo.toml")), "1.0.0");
}

#[test]
fn dry_run_writes_nothing() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let before = fs::read_to_string(tmp.path().join("Cargo.toml")).unwrap();
    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "minor", "--dry-run"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    // The plan table should mention the old→new transition.
    assert!(stdout.contains("0.1.0"), "stdout missing 0.1.0: {stdout}");
    assert!(stdout.contains("0.2.0"), "stdout missing 0.2.0: {stdout}");
    let after = fs::read_to_string(tmp.path().join("Cargo.toml")).unwrap();
    assert_eq!(before, after, "--dry-run should not touch the manifest");
}

#[test]
fn dry_run_json_is_parseable() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "minor", "--dry-run", "--output", "json"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "json dry-run failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).expect("stdout must be JSON");
    let arr = v.as_array().expect("json root must be an array");
    assert_eq!(arr.len(), 1);
    let row = &arr[0];
    assert_eq!(row["crate"], "demo");
    assert_eq!(row["current"], "0.1.0");
    assert_eq!(row["next"], "0.2.0");
    assert_eq!(row["level"], "minor");
}

#[test]
fn dirty_tree_refused_without_allow_dirty() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");
    // Introduce a dirty change.
    fs::write(tmp.path().join("dirty.txt"), "hello").unwrap();

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "-y"])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "bump should refuse a dirty tree; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(
        err.contains("uncommitted") || err.contains("dirty"),
        "error should mention uncommitted changes: {err}"
    );
}

#[test]
fn publish_false_skipped_from_workspace() {
    let tmp = TempDir::new().unwrap();
    workspace_with_private_member(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "--workspace", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "bump --workspace failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(
        read_version(&tmp.path().join("crates/pub/Cargo.toml")),
        "0.1.1"
    );
    // publish = false crate should be untouched.
    assert_eq!(
        read_version(&tmp.path().join("crates/priv/Cargo.toml")),
        "0.1.0"
    );
}

#[test]
fn workspace_package_inheritance_bumps_root_only() {
    let tmp = TempDir::new().unwrap();
    inheriting_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "minor", "--workspace", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "bump --workspace failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    // Root [workspace.package].version → 0.4.0.
    let root = fs::read_to_string(tmp.path().join("Cargo.toml")).unwrap();
    assert!(
        root.contains("version = \"0.4.0\""),
        "root should be bumped to 0.4.0: {root}"
    );
    // Members must still say version.workspace — we don't rewrite them.
    let a = fs::read_to_string(tmp.path().join("crates/a/Cargo.toml")).unwrap();
    assert!(a.contains("version.workspace = true"), "member a: {a}");
}

#[test]
fn exact_skips_dep_propagation() {
    let tmp = TempDir::new().unwrap();
    two_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "-p", "core", "--exact", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "{:?}",
        String::from_utf8_lossy(&out.stderr)
    );
    // core bumped.
    assert_eq!(
        read_version(&tmp.path().join("crates/core/Cargo.toml")),
        "0.1.1"
    );
    // cli's [dependencies] core = "0.1.0" left untouched.
    let cli = fs::read_to_string(tmp.path().join("crates/cli/Cargo.toml")).unwrap();
    assert!(
        cli.contains("version = \"0.1.0\""),
        "cli dep on core should NOT be rewritten under --exact: {cli}"
    );
}

#[test]
fn propagation_rewrites_sibling_dep() {
    let tmp = TempDir::new().unwrap();
    two_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "-p", "core", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    // core bumped.
    assert_eq!(
        read_version(&tmp.path().join("crates/core/Cargo.toml")),
        "0.1.1"
    );
    // cli's own version is untouched.
    assert_eq!(
        read_version(&tmp.path().join("crates/cli/Cargo.toml")),
        "0.1.0"
    );
    // cli's [dependencies] core version is rewritten.
    let cli = fs::read_to_string(tmp.path().join("crates/cli/Cargo.toml")).unwrap();
    assert!(
        cli.contains("version = \"0.1.1\""),
        "cli dep on core should be rewritten: {cli}"
    );
}

#[test]
fn commit_flag_creates_single_commit() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let before = Command::new("git")
        .current_dir(tmp.path())
        .args(["rev-list", "--count", "HEAD"])
        .output()
        .unwrap();
    let before_n: u32 = String::from_utf8_lossy(&before.stdout)
        .trim()
        .parse()
        .unwrap();

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "--commit", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "bump --commit failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let after = Command::new("git")
        .current_dir(tmp.path())
        .args(["rev-list", "--count", "HEAD"])
        .output()
        .unwrap();
    let after_n: u32 = String::from_utf8_lossy(&after.stdout)
        .trim()
        .parse()
        .unwrap();
    assert_eq!(after_n, before_n + 1, "exactly one new commit expected");

    // Commit message should include the new version.
    let msg = Command::new("git")
        .current_dir(tmp.path())
        .args(["log", "-1", "--pretty=%B"])
        .output()
        .unwrap();
    let msg = String::from_utf8_lossy(&msg.stdout);
    assert!(
        msg.contains("0.1.1"),
        "commit message missing version: {msg}"
    );
    assert!(
        msg.contains("demo"),
        "commit message missing crate name: {msg}"
    );
}

#[test]
fn infer_picks_per_crate_level_from_commits() {
    let tmp = TempDir::new().unwrap();
    two_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");
    // Seed a tag for each crate so the inference range starts there.
    run_git(tmp.path(), &["tag", "core-v0.1.0"]);
    run_git(tmp.path(), &["tag", "cli-v0.1.0"]);

    // Commit that only touches core (a feat) → minor bump for core.
    git_commit_empty_on_path(
        tmp.path(),
        "crates/core/new_feature.rs",
        "pub fn f() {}",
        "feat(core): add new feature",
    );
    // Commit that only touches cli (a fix) → patch bump for cli.
    git_commit_empty_on_path(
        tmp.path(),
        "crates/cli/bugfix.rs",
        "pub fn g() {}",
        "fix(cli): correct bug",
    );

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "--workspace", "--dry-run", "--output", "json"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "infer dry-run failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).expect("json");
    let rows = v.as_array().unwrap();
    let by_name: std::collections::HashMap<&str, &serde_json::Value> = rows
        .iter()
        .map(|r| (r["crate"].as_str().unwrap(), r))
        .collect();
    assert_eq!(by_name["core"]["level"], "minor");
    assert_eq!(by_name["core"]["next"], "0.2.0");
    assert_eq!(by_name["cli"]["level"], "patch");
    assert_eq!(by_name["cli"]["next"], "0.1.1");
}

#[test]
fn multi_crate_without_selection_errors() {
    let tmp = TempDir::new().unwrap();
    two_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "-y"])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "multi-crate bump without selection should error"
    );
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(
        err.contains("-p") || err.contains("--workspace"),
        "error should suggest -p or --workspace: {err}"
    );
}

#[test]
fn release_strips_prerelease() {
    let tmp = TempDir::new().unwrap();
    fs::write(
        tmp.path().join("Cargo.toml"),
        r#"[package]
name = "demo"
version = "1.0.0-rc.1"
edition = "2024"
"#,
    )
    .unwrap();
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "release", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "release bump failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(read_version(&tmp.path().join("Cargo.toml")), "1.0.0");
}

#[test]
fn pre_appends_prerelease() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "minor", "--pre", "rc.1", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "pre bump failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let text = fs::read_to_string(tmp.path().join("Cargo.toml")).unwrap();
    assert!(
        text.contains("version = \"0.2.0-rc.1\""),
        "expected 0.2.0-rc.1: {text}"
    );
}

// ---------------------------------------------------------------------------
// Bundled changelog (via stage-changelog::render_crate_section)
// ---------------------------------------------------------------------------

#[test]
fn commit_bundles_changelog_when_configured() {
    let tmp = TempDir::new().unwrap();
    single_crate_workspace(tmp.path());
    fs::write(
        tmp.path().join(".anodizer.yaml"),
        r#"version: 2
project_name: demo
crates:
  - name: demo
    path: .
    tag_template: "v{{ Version }}"
changelog:
  sort: asc
  groups:
    - title: Features
      regexp: "^feat"
      order: 0
    - title: Bug Fixes
      regexp: "^fix"
      order: 1
"#,
    )
    .unwrap();
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");
    // Tag the initial state so the changelog inference range starts there.
    run_git(tmp.path(), &["tag", "demo-v0.1.0"]);
    // A feat commit since the tag — this is what the rendered section must
    // include.
    git_commit_empty_on_path(
        tmp.path(),
        "src/feature.rs",
        "pub fn f() {}",
        "feat: add a sparkly new feature",
    );

    let before = Command::new("git")
        .current_dir(tmp.path())
        .args(["rev-list", "--count", "HEAD"])
        .output()
        .unwrap();
    let before_n: u32 = String::from_utf8_lossy(&before.stdout)
        .trim()
        .parse()
        .unwrap();

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "patch", "--commit", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "bump --commit failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let after = Command::new("git")
        .current_dir(tmp.path())
        .args(["rev-list", "--count", "HEAD"])
        .output()
        .unwrap();
    let after_n: u32 = String::from_utf8_lossy(&after.stdout)
        .trim()
        .parse()
        .unwrap();
    assert_eq!(
        after_n,
        before_n + 1,
        "exactly one new commit must include the bundled changelog"
    );

    // Diff of the new commit must touch BOTH Cargo.toml and CHANGELOG.md.
    let diff = Command::new("git")
        .current_dir(tmp.path())
        .args(["diff-tree", "--no-commit-id", "--name-only", "-r", "HEAD"])
        .output()
        .unwrap();
    let names = String::from_utf8_lossy(&diff.stdout);
    assert!(
        names.lines().any(|l| l == "Cargo.toml"),
        "commit must touch Cargo.toml: {names}"
    );
    assert!(
        names.lines().any(|l| l == "CHANGELOG.md"),
        "commit must touch CHANGELOG.md: {names}"
    );

    // Sanity: the new section is present on disk.
    let cl = fs::read_to_string(tmp.path().join("CHANGELOG.md")).unwrap();
    assert!(cl.contains("[0.1.1]"), "changelog missing version: {cl}");
    assert!(
        cl.contains("sparkly new feature"),
        "changelog missing feat description: {cl}"
    );
}

// ---------------------------------------------------------------------------
// Inference must honor each crate's `tag_template` (cfgd-style monorepos
// have crates whose tag is bare `v{{ Version }}` — not `<name>-v…`).
// ---------------------------------------------------------------------------

#[test]
fn inference_respects_tag_template_from_anodizer_yaml() {
    let tmp = TempDir::new().unwrap();
    two_crate_workspace(tmp.path());
    // .anodizer.yaml gives `core` a custom `core-v` prefix and `cli` the bare
    // `v` prefix (the cfgd top-crate convention). Without the tag-template
    // wiring, bump's inference would fall back to `core-v` and `cli-v` and
    // miss the actual tags below.
    fs::write(
        tmp.path().join(".anodizer.yaml"),
        r#"version: 2
project_name: tag-template-fixture
crates:
  - name: core
    path: crates/core
    tag_template: "core-v{{ Version }}"
  - name: cli
    path: crates/cli
    tag_template: "v{{ Version }}"
"#,
    )
    .unwrap();
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");
    // Tag with the templates' actual prefixes — `core-v` and bare `v`.
    run_git(tmp.path(), &["tag", "core-v0.1.0"]);
    run_git(tmp.path(), &["tag", "v0.1.0"]);
    // After-tag commits: core gets a feat (→ minor), cli gets a chore (→ skip).
    git_commit_empty_on_path(
        tmp.path(),
        "crates/core/feature.rs",
        "pub fn f() {}",
        "feat(core): add feature",
    );
    git_commit_empty_on_path(
        tmp.path(),
        "crates/cli/notes.rs",
        "// notes",
        "chore(cli): housekeeping",
    );

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "--workspace", "--dry-run", "--output", "json"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "infer dry-run failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).expect("json");
    let by_name: std::collections::HashMap<&str, &serde_json::Value> = v
        .as_array()
        .unwrap()
        .iter()
        .map(|r| (r["crate"].as_str().unwrap(), r))
        .collect();
    // `cli`'s tag prefix is bare `v` per .anodizer.yaml; the v0.1.0 tag must
    // be discovered and the only post-tag commit is a chore → skip. With
    // the fallback `<name>-v` prefix this row would scan all-of-history,
    // see the chore, classify it as skip ANYWAY, but the reason string would
    // mention "no commits since cli-v" instead of `since v0.1.0`. Pin to
    // the reason text so the regression cannot hide.
    assert_eq!(
        by_name["cli"]["level"], "skip",
        "cli should skip — no feat/fix since v0.1.0"
    );
    let cli_reason = by_name["cli"]["reason"].as_str().unwrap();
    assert!(
        cli_reason.contains("since v0.1.0"),
        "cli reason must reference the actual v0.1.0 tag, not <name>-v fallback: {cli_reason}"
    );
    // core uses `core-v` template; the post-tag feat → minor, reason cites
    // core-v0.1.0 specifically.
    assert_eq!(by_name["core"]["level"], "minor");
    assert_eq!(by_name["core"]["next"], "0.2.0");
    let core_reason = by_name["core"]["reason"].as_str().unwrap();
    assert!(
        core_reason.contains("since core-v0.1.0"),
        "core reason must reference core-v0.1.0: {core_reason}"
    );
}

// ---------------------------------------------------------------------------
// Strict-mode version-pin enforcement
// ---------------------------------------------------------------------------

/// Two-crate workspace with `core` pinned to `0.1.0` in `.anodizer.yaml`. The
/// `cli` crate is unpinned.
fn pinned_two_crate_workspace(tmp: &Path, pin_core: bool) {
    two_crate_workspace(tmp);
    let core_pin = if pin_core {
        "    version: \"0.1.0\"\n"
    } else {
        ""
    };
    let yaml = format!(
        r#"version: 2
project_name: pinned
crates:
  - name: core
    path: crates/core
    tag_template: "v{{{{ Version }}}}"
{core_pin}  - name: cli
    path: crates/cli
    tag_template: "v{{{{ Version }}}}"
"#,
        core_pin = core_pin,
    );
    fs::write(tmp.join(".anodizer.yaml"), yaml).unwrap();
}

#[test]
fn strict_refuses_bump_when_version_pinned() {
    let tmp = TempDir::new().unwrap();
    pinned_two_crate_workspace(tmp.path(), true);
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["--strict", "bump", "minor", "-p", "core", "-y"])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "strict bump should fail when version is pinned; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("core"), "error must mention crate name: {err}");
    assert!(
        err.contains("0.1.0"),
        "error must mention pinned version: {err}"
    );
    assert!(
        err.contains("0.2.0"),
        "error must mention proposed version: {err}"
    );
    // Manifest must NOT have been touched.
    assert_eq!(
        read_version(&tmp.path().join("crates/core/Cargo.toml")),
        "0.1.0"
    );
}

#[test]
fn strict_allows_bump_when_no_pin() {
    let tmp = TempDir::new().unwrap();
    pinned_two_crate_workspace(tmp.path(), false);
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["--strict", "bump", "minor", "-p", "core", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "strict bump should succeed without a pin; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(
        read_version(&tmp.path().join("crates/core/Cargo.toml")),
        "0.2.0"
    );
}

#[test]
fn non_strict_warns_but_proceeds() {
    let tmp = TempDir::new().unwrap();
    pinned_two_crate_workspace(tmp.path(), true);
    git_init(tmp.path());
    git_add_commit(tmp.path(), "initial");

    let out = anodizer()
        .current_dir(tmp.path())
        .args(["bump", "minor", "-p", "core", "-y"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "non-strict bump should proceed despite pin; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(
        err.to_lowercase().contains("warn"),
        "stderr must include a warning when bumping a pinned crate: {err}"
    );
    assert_eq!(
        read_version(&tmp.path().join("crates/core/Cargo.toml")),
        "0.2.0"
    );
}