verctl-core 0.9.2

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
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
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
//! Rewrite collocated tool pins when a package version changes.

use std::fs;
use std::path::{Component, Path, PathBuf};

use anyhow::{Context, Result, bail};
use regex::{NoExpand, Regex, escape};
use toml_edit::{DocumentMut, Formatted, Item, Value};

use crate::config::{Config, PLACEHOLDER, Pin, PinPattern};

/// A version as verctl writes one: digits and dots, then an optional
/// candidate prerelease, `-<tag>.<count>`, such as `0.8.2-rc.1`.
///
/// A pin names a version verctl wrote, so the pattern contains no other
/// prerelease or build. A wider one would read `tool-1.2.3-linux-x64` against
/// `tool-{version}-x64` as version `1.2.3-linux`, and the rewrite would drop
/// `-linux`; this one matches nothing there, so the pin fails instead.
const VERSION: &str = r"[0-9]+(?:\.[0-9]+)*(?:-[a-z]+\.[0-9]+)?";

/// Current declared versions of every configured package.
///
/// # Errors
///
/// Returns an error when a package's driver cannot be resolved, its manifest
/// cannot be read, or the driver cannot read a version from it.
pub fn current_versions(root: &Path, config: &Config) -> Result<Vec<(String, String)>> {
    let mut versions = Vec::new();
    for spec in &config.packages {
        versions.push((spec.name.clone(), spec.current(config, root)?.version));
    }
    Ok(versions)
}

/// Rewrite every pin to the versions given, and return the files changed.
///
/// # Errors
///
/// Returns an error when:
///
/// - a pin file lies outside `root` or is a symlink;
/// - a pin file cannot be read or written;
/// - a pin file has no `[tools]` entry for the pinned tool;
/// - a pin has a pattern that is malformed or matches a number of times its
///   `occurrences` does not allow.
///
/// Files rewritten before the failing pin stay changed.
pub fn write(root: &Path, pins: &[Pin], versions: &[(String, String)]) -> Result<Vec<PathBuf>> {
    let mut written = Vec::new();
    for pin in pins {
        let Some((_, version)) = versions.iter().find(|(name, _)| name == &pin.package) else {
            continue;
        };
        let (path, _, body) = rewritten(root, pin, version)?;
        fs::write(&path, body).with_context(|| format!("write pin {}", path.display()))?;
        written.push(path);
    }
    Ok(written)
}

/// Same checks as `write`, without touching disk.
///
/// # Errors
///
/// Returns an error under the same conditions as [`write()`], except that no
/// file is written, so a write failure cannot occur.
pub fn plan(root: &Path, pins: &[Pin], versions: &[(String, String)]) -> Result<Vec<PathBuf>> {
    let mut planned = Vec::new();
    for pin in pins {
        let Some((_, version)) = versions.iter().find(|(name, _)| name == &pin.package) else {
            continue;
        };
        let (path, _, _) = rewritten(root, pin, version)?;
        planned.push(path);
    }
    Ok(planned)
}

/// A `pins` entry that does not match the version its package is at.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Stale {
    /// The pinned file, as the entry names it.
    pub file: PathBuf,
    /// What is wrong: a pattern that does not match at its declared count,
    /// a file that cannot be rewritten, or one that names another version.
    pub problem: String,
}

/// Every `pins` entry whose file would change, or could not be rewritten, at
/// `versions`, so one run lists every file to fix. Nothing is written.
#[must_use]
pub fn stale(root: &Path, pins: &[Pin], versions: &[(String, String)]) -> Vec<Stale> {
    pins.iter()
        .filter_map(|pin| {
            let (_, version) = versions.iter().find(|(name, _)| name == &pin.package)?;
            let problem = match rewritten(root, pin, version) {
                Ok((_, raw, body)) if raw == body => return None,
                Ok(_) => format!("names a version other than {} {version}", pin.package),
                Err(error) => format!("{error:#}"),
            };
            Some(Stale {
                file: pin.file.clone(),
                problem,
            })
        })
        .collect()
}

/// The pin's path, its bytes now, and its bytes at `version`.
fn rewritten(root: &Path, pin: &Pin, version: &str) -> Result<(PathBuf, String, String)> {
    ensure_inside(root, &pin.file)?;
    let path = root.join(&pin.file);
    let raw = fs::read_to_string(&path).with_context(|| format!("read pin {}", path.display()))?;
    let mut body = raw.clone();
    if let Some(tool) = &pin.tool {
        body = rewrite_tool(&body, tool, version, &path)?;
    }
    for pattern in &pin.patterns {
        body = rewrite_pattern(&body, pattern, version)
            .with_context(|| format!("pin {}", path.display()))?;
    }
    Ok((path, raw, body))
}

fn ensure_inside(root: &Path, file: &Path) -> Result<()> {
    if file.is_absolute()
        || file
            .components()
            .any(|part| matches!(part, Component::ParentDir))
    {
        bail!("pin file must stay inside the repo: {}", file.display());
    }
    let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
    let path = root.join(file);
    if let Ok(meta) = fs::symlink_metadata(&path)
        && meta.file_type().is_symlink()
    {
        bail!("pin file must not be a symlink: {}", path.display());
    }
    let resolved = path.canonicalize().unwrap_or(path);
    if !resolved.starts_with(&root) {
        bail!("pin file must stay inside the repo: {}", file.display());
    }
    Ok(())
}

/// A mise `[tools]` entry, plus every `?ref=v…` include naming its repo.
///
/// Only the version value changes. A bare string, the `version` of an inline
/// table such as `{ version = "1.2.3", os = ["linux"] }`, and the `version` of
/// a `[tools.<name>]` table all keep their comments, spacing, and options.
fn rewrite_tool(body: &str, tool: &str, version: &str, path: &Path) -> Result<String> {
    let mut doc: DocumentMut = body
        .parse()
        .with_context(|| format!("parse pin {}", path.display()))?;
    let tools = doc
        .get_mut("tools")
        .and_then(Item::as_table_like_mut)
        .with_context(|| format!("{} has no [tools]", path.display()))?;
    let Some(entry) = tools.get_mut(tool) else {
        bail!("{} has no tools.{tool}", path.display());
    };
    set_tool_version(entry, version).with_context(|| format!("{} tools.{tool}", path.display()))?;
    rewrite_own_refs(&doc.to_string(), tool, version)
        .with_context(|| format!("pin {}", path.display()))
}

/// Replace the version a `[tools]` entry specifies, and nothing else of it.
fn set_tool_version(entry: &mut Item, version: &str) -> Result<()> {
    match entry {
        Item::Value(Value::InlineTable(table)) => {
            set_string(table.get_mut("version").context(NO_VERSION)?, version)
        }
        Item::Table(table) => {
            let Some(Item::Value(existing)) = table.get_mut("version") else {
                bail!(NO_VERSION);
            };
            set_string(existing, version)
        }
        Item::Value(existing) => set_string(existing, version),
        Item::None | Item::ArrayOfTables(_) => bail!("is not a version or a table"),
    }
}

/// What an entry that contains no version string reports.
const NO_VERSION: &str = "has no version string to rewrite";

/// Swap the string in `existing` for `version`, keeping the whitespace and the
/// comment around it.
fn set_string(existing: &mut Value, version: &str) -> Result<()> {
    let Value::String(old) = existing else {
        bail!(NO_VERSION);
    };
    let mut new = Formatted::new(version.to_owned());
    *new.decor_mut() = old.decor().clone();
    *old = new;
    Ok(())
}

/// The version in every occurrence of `pattern.match`, where `{version}`
/// marks it. The rest is literal. A bash header, a README sentence, and a
/// YAML value all rewrite the same way, with nothing assumed about the file.
///
/// `Config::load` refuses a pattern with nothing marking where the version
/// starts and ends. So the pattern matches the whole token, a candidate's
/// prerelease included, and never a numeric head with its tail kept.
///
/// The file must contain the version exactly as often as declared. The count is
/// never inferred. A count off in either direction stops the release. Too few
/// means the pin tracks nothing, and too many means a version spelling nobody
/// accounted for. See `Occurrences` for the vocabulary.
fn rewrite_pattern(body: &str, pattern: &PinPattern, version: &str) -> Result<String> {
    let text = &pattern.r#match;
    let (before, after) = text
        .split_once(PLACEHOLDER)
        .with_context(|| format!("pattern {text:?} has no {PLACEHOLDER}"))?;
    if after.contains(PLACEHOLDER) {
        bail!("pattern {text:?} has more than one {PLACEHOLDER}");
    }
    // `R` is carriage return and line feed (CRLF) mode. Without it `$` matches
    // only before `\n`. A file checked out with CRLF endings has a `\r` between
    // the version and the line end. It matches nothing, so a correct file stops
    // a release.
    let (open, close) = if pattern.whole_line {
        ("(?mR)^", "$")
    } else {
        ("", "")
    };
    let finder = Regex::new(&format!(
        "{open}{}{VERSION}{}{close}",
        escape(before),
        escape(after)
    ))
    .with_context(|| format!("pattern {text:?}"))?;
    let found = finder.find_iter(body).count();
    if !pattern.occurrences.allows(found) {
        bail!(
            "pattern {text:?} matches {found} times, expected {}",
            pattern.occurrences
        );
    }
    let pinned = format!("{before}{version}{after}");
    Ok(finder.replace_all(body, NoExpand(&pinned)).into_owned())
}

/// Every `?ref=v…` include that names this tool's own repository. A sibling
/// repo's include in the same file is not ours to move. The URL path must
/// carry the tool's own name.
///
/// The ref runs to the end of the URL or to the next query separator (`&`,
/// `#`, `;`). A ref that is a whole version, prerelease included, moves. A
/// ref that starts with a digit and is not a whole version, such as
/// `v1.2.3_local`, fails, so its numeric head is never moved with its tail
/// kept. A ref that does not start with a digit, such as a branch, is not a pin
/// and stays put.
fn rewrite_own_refs(body: &str, tool: &str, version: &str) -> Result<String> {
    let name = escape(tool.rsplit(['/', ':']).next().unwrap_or(tool));
    let finder = Regex::new(&format!(
        r#"(?<include>/{name}(?:\.git)?//[^\s"',\]]*\?ref=v)(?<reference>[0-9][^\s"',\]&#;]*)"#
    ))
    .with_context(|| format!("tool {tool:?}"))?;
    let whole = Regex::new(&format!("^{VERSION}$")).context("the version pattern")?;
    if let Some(found) = finder
        .captures_iter(body)
        .find(|found| !whole.is_match(&found["reference"]))
    {
        bail!(
            "{:?} pins v{}, which is not a version this rewrites",
            &found[0],
            &found["reference"]
        );
    }
    Ok(finder
        .replace_all(body, format!("${{include}}{version}"))
        .into_owned())
}

#[cfg(test)]
mod tests {
    use std::os::unix::fs::symlink;

    use indoc::indoc;
    use tempfile::TempDir;

    use super::*;
    use crate::config::Occurrences;

    const VERCTL: &str = "github:victor-software-house/verctl";
    const DOC: &str = "README.md";

    /// One hand-authored file with every version spelling a repo really
    /// carries:
    ///
    /// - a tagged include
    /// - a tarball invocation
    /// - a bare mention
    /// - a sibling repo's pin that is not ours
    /// - a non-version that must not match
    const BEFORE: &str = indoc! {r#"
        # verctl

        ```toml
        [task_config]
        includes = [
          "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.0.1",
        ]
        ```

        Install: mise x github:victor-software-house/verctl@0.0.1 -- verctl --version
        Pin verctl@0.0.1 in CI.
        Unrelated: qctl@0.0.1 and forkctl 0.0.19 stay put.
        Floating: verctl@latest.
    "#};

    /// What a release does to `BEFORE`: the exact bytes, or the failure.
    enum Outcome {
        Serves(&'static str),
        Fails(&'static str),
    }

    fn versions(to: &str) -> Vec<(String, String)> {
        vec![("verctl".into(), to.into())]
    }

    /// A pin on this repo's own tool entry in `file`.
    fn tool_pin(file: &str, package: &str) -> Pin {
        Pin {
            file: PathBuf::from(file),
            tool: Some(VERCTL.into()),
            pattern_ids: Vec::new(),
            patterns: Vec::new(),
            package: package.into(),
        }
    }

    /// A pin that knows nothing about `file` but the text around the version.
    fn pattern_pin(file: &str, patterns: &[(&str, Occurrences)]) -> Pin {
        Pin {
            file: PathBuf::from(file),
            tool: None,
            pattern_ids: Vec::new(),
            patterns: patterns
                .iter()
                .map(|(text, occurrences)| PinPattern {
                    r#match: (*text).to_owned(),
                    occurrences: *occurrences,
                    whole_line: false,
                })
                .collect(),
            package: "verctl".into(),
        }
    }

    fn write_file(root: &TempDir, file: &str, body: &str) -> PathBuf {
        let path = root.path().join(file);
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(&path, body).unwrap();
        path
    }

    /// `check` lists each pin left behind: one that names another version,
    /// and one whose pattern does not match at its declared count.
    #[test]
    fn stale_names_each_pin_left_behind() {
        let root = TempDir::new().unwrap();
        write_file(&root, DOC, BEFORE);
        let current = [
            pattern_pin(DOC, &[("verctl@{version} in CI", Occurrences::Once)]),
            pattern_pin(DOC, &[("Floating: verctl@{version}.", Occurrences::Once)]),
        ];
        assert_eq!(
            stale(root.path(), &current[..1], &versions("0.0.1")),
            Vec::<Stale>::new()
        );
        let found = stale(root.path(), &current, &versions("0.0.2"));
        assert_eq!(found.len(), 2, "{found:?}");
        assert!(
            found.iter().all(|pin| pin.file == Path::new(DOC)),
            "{found:?}"
        );
        assert!(
            found[0].problem.contains("other than verctl 0.0.2"),
            "{found:?}"
        );
        assert!(found[1].problem.contains("matches 0 times"), "{found:?}");
        assert_eq!(fs::read_to_string(root.path().join(DOC)).unwrap(), BEFORE);
    }

    /// One before, many pattern sets and versions, each asserted whole.
    #[test]
    #[expect(
        clippy::too_many_lines,
        reason = "one case per scenario; a table, not logic"
    )]
    fn one_document_pins_every_release_exactly() {
        let tarball = "github:victor-software-house/verctl@{version}";
        let include = "verctl.git//tasks/ver?ref=v{version}";
        let bare = "verctl@{version}";
        let cases = [
            (
                "every declared spelling moves together",
                &[
                    (tarball, Occurrences::Once),
                    (include, Occurrences::Once),
                    ("verctl@{version} in CI", Occurrences::Once),
                ][..],
                "0.0.2",
                Outcome::Serves(indoc! {r#"
                    # verctl

                    ```toml
                    [task_config]
                    includes = [
                      "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.0.2",
                    ]
                    ```

                    Install: mise x github:victor-software-house/verctl@0.0.2 -- verctl --version
                    Pin verctl@0.0.2 in CI.
                    Unrelated: qctl@0.0.1 and forkctl 0.0.19 stay put.
                    Floating: verctl@latest.
                "#}),
            ),
            (
                "a version with a two-digit part replaces the whole number",
                &[(tarball, Occurrences::Once)][..],
                "0.0.10",
                Outcome::Serves(indoc! {r#"
                    # verctl

                    ```toml
                    [task_config]
                    includes = [
                      "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.0.1",
                    ]
                    ```

                    Install: mise x github:victor-software-house/verctl@0.0.10 -- verctl --version
                    Pin verctl@0.0.1 in CI.
                    Unrelated: qctl@0.0.1 and forkctl 0.0.19 stay put.
                    Floating: verctl@latest.
                "#}),
            ),
            (
                "a release to the version already served changes nothing",
                &[(tarball, Occurrences::Once), (include, Occurrences::Once)][..],
                "0.0.1",
                Outcome::Serves(BEFORE),
            ),
            (
                "a spelling the file says twice, declared twice, moves twice",
                &[(bare, Occurrences::Exactly(2))][..],
                "1.2.3",
                Outcome::Serves(indoc! {r#"
                    # verctl

                    ```toml
                    [task_config]
                    includes = [
                      "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.0.1",
                    ]
                    ```

                    Install: mise x github:victor-software-house/verctl@1.2.3 -- verctl --version
                    Pin verctl@1.2.3 in CI.
                    Unrelated: qctl@0.0.1 and forkctl 0.0.19 stay put.
                    Floating: verctl@latest.
                "#}),
            ),
            (
                "a spelling whose mentions come and go, declared as many",
                &[(bare, Occurrences::Many)][..],
                "1.2.3",
                Outcome::Serves(indoc! {r#"
                    # verctl

                    ```toml
                    [task_config]
                    includes = [
                      "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.0.1",
                    ]
                    ```

                    Install: mise x github:victor-software-house/verctl@1.2.3 -- verctl --version
                    Pin verctl@1.2.3 in CI.
                    Unrelated: qctl@0.0.1 and forkctl 0.0.19 stay put.
                    Floating: verctl@latest.
                "#}),
            ),
            (
                "two mentions clear a floor of two",
                &[(bare, Occurrences::AtLeast(2))][..],
                "0.0.2",
                Outcome::Serves(indoc! {r#"
                    # verctl

                    ```toml
                    [task_config]
                    includes = [
                      "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.0.1",
                    ]
                    ```

                    Install: mise x github:victor-software-house/verctl@0.0.2 -- verctl --version
                    Pin verctl@0.0.2 in CI.
                    Unrelated: qctl@0.0.1 and forkctl 0.0.19 stay put.
                    Floating: verctl@latest.
                "#}),
            ),
            (
                "the same spelling left at the default arity fails",
                &[(bare, Occurrences::Once)][..],
                "1.2.3",
                Outcome::Fails("matches 2 times, expected exactly once"),
            ),
            (
                "a spelling declared more often than the file says it fails",
                &[(tarball, Occurrences::Exactly(2))][..],
                "0.0.2",
                Outcome::Fails("matches 1 times, expected exactly 2 times"),
            ),
            (
                "one mention does not clear a floor of two",
                &[(tarball, Occurrences::AtLeast(2))][..],
                "0.0.2",
                Outcome::Fails("matches 1 times, expected 2 or more times"),
            ),
            (
                "a spelling the file lost fails",
                &[("verctl@{version} in production", Occurrences::Once)][..],
                "0.0.2",
                Outcome::Fails("matches 0 times, expected exactly once"),
            ),
            (
                "a spelling declared as many, with none left, fails",
                &[("verctl@{version} in production", Occurrences::Many)][..],
                "0.0.2",
                Outcome::Fails("matches 0 times, expected one or more"),
            ),
            (
                "a retired spelling that came back fails",
                &[(tarball, Occurrences::Never)][..],
                "0.0.2",
                Outcome::Fails("matches 1 times, expected never"),
            ),
            (
                "a retired spelling that stayed gone is satisfied",
                &[("verctl@{version} in production", Occurrences::Never)][..],
                "0.0.2",
                Outcome::Serves(BEFORE),
            ),
            (
                "a pattern with no placeholder fails",
                &[("verctl@0.0.1", Occurrences::Once)][..],
                "0.0.2",
                Outcome::Fails("has no {version}"),
            ),
            (
                "a pattern with two placeholders fails",
                &[("verctl@{version}-{version}", Occurrences::Once)][..],
                "0.0.2",
                Outcome::Fails("has more than one {version}"),
            ),
            (
                "a floating pin is not a version and cannot be tracked",
                &[("verctl@{version}.\n", Occurrences::Once)][..],
                "0.0.2",
                Outcome::Fails("matches 0 times, expected exactly once"),
            ),
        ];
        for (label, patterns, version, outcome) in cases {
            let root = TempDir::new().unwrap();
            let path = write_file(&root, DOC, BEFORE);
            let result = write(
                root.path(),
                &[pattern_pin(DOC, patterns)],
                &versions(version),
            );
            match outcome {
                Outcome::Serves(after) => {
                    assert_eq!(result.unwrap(), [path.clone()][..], "{label}");
                    assert_eq!(fs::read_to_string(&path).unwrap(), after, "{label}");
                }
                Outcome::Fails(complaint) => {
                    let report = format!("{:#}", result.unwrap_err());
                    assert!(report.contains(complaint), "{label}: {report}");
                    assert!(report.contains(DOC), "{label}: {report}");
                    assert_eq!(fs::read_to_string(&path).unwrap(), BEFORE, "{label}");
                }
            }
        }
    }

    /// A version verctl never writes, with a `-linux` part or build metadata,
    /// matches nothing, so the pin fails before it half-rewrites one.
    #[test]
    fn a_version_verctl_never_writes_fails_the_pin() {
        for (before, pattern) in [
            (
                "Get tool-1.2.3-linux-x64 today.\n",
                "tool-{version}-x64 today",
            ),
            (
                "Install verctl@1.0.0+build.5 today.\n",
                "verctl@{version} today",
            ),
        ] {
            let root = TempDir::new().unwrap();
            let path = write_file(&root, DOC, before);
            let pins = [pattern_pin(DOC, &[(pattern, Occurrences::Once)])];
            let error = write(root.path(), &pins, &versions("1.3.0")).unwrap_err();
            assert!(
                format!("{error:#}").contains("matches 0 times"),
                "{error:#}"
            );
            assert_eq!(fs::read_to_string(&path).unwrap(), before);
        }
    }

    /// A candidate moves whole to the next version, prerelease included, and
    /// a stable one drops the tail.
    #[test]
    fn a_prerelease_pin_moves_whole_to_the_next_version() {
        for (before, next, after) in [
            (
                "Install verctl@1.0.0-rc.1 today.\n",
                "1.0.0-rc.2",
                "Install verctl@1.0.0-rc.2 today.\n",
            ),
            (
                "Install verctl@1.0.0-rc.2 today.\n",
                "1.0.0",
                "Install verctl@1.0.0 today.\n",
            ),
        ] {
            let root = TempDir::new().unwrap();
            let path = write_file(&root, DOC, before);
            let pins = [pattern_pin(
                DOC,
                &[("verctl@{version} today", Occurrences::Once)],
            )];
            write(root.path(), &pins, &versions(next)).unwrap();
            assert_eq!(fs::read_to_string(&path).unwrap(), after, "{before}");
        }
    }

    /// Two pins in the same file, back to back, with nothing between them.
    #[test]
    fn adjacent_matches_both_move() {
        let root = TempDir::new().unwrap();
        let path = write_file(&root, DOC, "verctl@0.0.1verctl@0.0.1\n");
        let pins = [pattern_pin(
            DOC,
            &[("verctl@{version}", Occurrences::Exactly(2))],
        )];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            "verctl@0.0.2verctl@0.0.2\n"
        );
    }

    /// `$1` is a valid capture reference, so an expanding replacement would
    /// swallow it. A served task file passes `"$1"` right beside its pin.
    #[test]
    fn a_dollar_sign_in_the_pattern_stays_a_dollar_sign() {
        let root = TempDir::new().unwrap();
        let before = "exec verctl@0.0.1 \"$1\"\n";
        let path = write_file(&root, "tasks/ver/ver", before);
        let pins = [pattern_pin(
            "tasks/ver/ver",
            &[("verctl@{version} \"$1\"", Occurrences::Once)],
        )];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            "exec verctl@0.0.2 \"$1\"\n"
        );
    }

    /// The text around the version is literal: `.`, `?`, and `//` mean
    /// themselves, so a line spelled with other characters is not a match.
    #[test]
    fn the_text_around_the_version_is_literal_not_a_regex() {
        let root = TempDir::new().unwrap();
        let before = indoc! {"
            verctl.git//tasks/ver?ref=v0.0.1
            verctlxgitZZtasksZverYrefZv0.0.1
        "};
        let path = write_file(&root, DOC, before);
        let pins = [pattern_pin(
            DOC,
            &[("verctl.git//tasks/ver?ref=v{version}", Occurrences::Once)],
        )];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {"
                verctl.git//tasks/ver?ref=v0.0.2
                verctlxgitZZtasksZverYrefZv0.0.1
            "}
        );
    }

    /// A file that parses as nothing: the served task is a bash script.
    #[test]
    fn rewrites_a_pin_in_a_file_that_is_not_toml() {
        let root = TempDir::new().unwrap();
        let before = indoc! {r#"
            #!/usr/bin/env bash
            #MISE tools = { "github:victor-software-house/verctl" = "0.0.1" }

            exec verctl "$@"
        "#};
        let path = write_file(&root, "tasks/ver/ver", before);
        let pins = [pattern_pin(
            "tasks/ver/ver",
            &[(
                r#""github:victor-software-house/verctl" = "{version}""#,
                Occurrences::Once,
            )],
        )];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                #!/usr/bin/env bash
                #MISE tools = { "github:victor-software-house/verctl" = "0.0.2" }

                exec verctl "$@"
            "#}
        );
    }

    #[test]
    fn a_pattern_pin_on_a_package_no_fragment_bumped_stays_put() {
        let root = TempDir::new().unwrap();
        let path = write_file(&root, DOC, BEFORE);
        let mut pin = pattern_pin(DOC, &[("verctl@{version} in CI", Occurrences::Once)]);
        pin.package = "other".into();
        assert_eq!(
            write(root.path(), &[pin], &versions("0.0.2")).unwrap(),
            Vec::<PathBuf>::new()
        );
        assert_eq!(fs::read_to_string(&path).unwrap(), BEFORE);
    }

    #[test]
    fn a_dry_run_reports_the_file_without_writing_it() {
        let root = TempDir::new().unwrap();
        let path = write_file(&root, DOC, BEFORE);
        let pins = [pattern_pin(
            DOC,
            &[("verctl@{version} in CI", Occurrences::Once)],
        )];
        assert_eq!(
            plan(root.path(), &pins, &versions("0.0.2")).unwrap(),
            [path.clone()][..]
        );
        assert_eq!(fs::read_to_string(&path).unwrap(), BEFORE);
    }

    #[test]
    fn updates_the_named_tool_and_keeps_siblings() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.release.toml",
            indoc! {r#"
                [tools]
                leftover = "1"
                "github:victor-software-house/verctl" = "0.0.1"
            "#},
        );
        let pins = [tool_pin("mise.release.toml", "verctl")];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools]
                leftover = "1"
                "github:victor-software-house/verctl" = "0.0.2"
            "#}
        );
    }

    /// A tool entry with options and comments keeps every byte but its
    /// version.
    #[test]
    fn a_tool_entry_with_options_and_comments_keeps_them() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.release.toml",
            indoc! {r#"
                [tools]
                # the release tool
                "github:victor-software-house/verctl" = { version = "0.0.1", os = ["linux"] } # pinned
                node = "22" # untouched
            "#},
        );
        let pins = [tool_pin("mise.release.toml", "verctl")];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools]
                # the release tool
                "github:victor-software-house/verctl" = { version = "0.0.2", os = ["linux"] } # pinned
                node = "22" # untouched
            "#}
        );
    }

    #[test]
    fn a_tool_table_keeps_its_options() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.release.toml",
            indoc! {r#"
                [tools."github:victor-software-house/verctl"]
                version = "0.0.1" # pinned
                os = ["linux"]
            "#},
        );
        let pins = [tool_pin("mise.release.toml", "verctl")];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools."github:victor-software-house/verctl"]
                version = "0.0.2" # pinned
                os = ["linux"]
            "#}
        );
    }

    #[test]
    fn skips_when_the_package_is_not_in_the_plan() {
        let root = TempDir::new().unwrap();
        let before = indoc! {r#"
            [tools]
            "github:victor-software-house/verctl" = "0.0.1"
        "#};
        let path = write_file(&root, "mise.release.toml", before);
        let pins = [tool_pin("mise.release.toml", "other")];
        assert_eq!(
            write(root.path(), &pins, &versions("0.0.2")).unwrap(),
            Vec::<PathBuf>::new()
        );
        assert_eq!(fs::read_to_string(&path).unwrap(), before);
    }

    /// A tool entry and the includes that fetch it, in one file, at once.
    /// A sibling repo's include in that file is not ours to move.
    #[test]
    fn a_tool_entry_and_its_own_includes_move_together() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.toml",
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.1"

                [task_config]
                includes = [
                  "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v1.2.3",
                  "git::https://github.com/victor-software-house/qctl.git//tasks/q?ref=v0.0.1",
                ]
            "#},
        );
        let pins = [tool_pin("mise.toml", "verctl")];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.2"

                [task_config]
                includes = [
                  "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.0.2",
                  "git::https://github.com/victor-software-house/qctl.git//tasks/q?ref=v0.0.1",
                ]
            "#}
        );
    }

    /// An include whose ref is a prerelease is a whole version, so the pin
    /// moves it to the new version and no tail of the old one stays behind.
    #[test]
    fn an_include_with_a_prerelease_ref_moves_whole_to_the_next_version() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.toml",
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.8.0-rc.1"

                [task_config]
                includes = [
                  "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.8.0-rc.1",
                ]
            "#},
        );
        write(
            root.path(),
            &[tool_pin("mise.toml", "verctl")],
            &versions("0.8.0"),
        )
        .unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.8.0"

                [task_config]
                includes = [
                  "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v0.8.0",
                ]
            "#}
        );
    }

    /// A ref that starts with a digit and is not a whole version is not ours
    /// to move. Moving its numeric head would leave the tail on a tag of
    /// another release. So the pin fails and the file stays as it was.
    #[test]
    fn an_include_with_a_ref_that_is_not_a_version_fails_instead_of_half_rewriting() {
        let root = TempDir::new().unwrap();
        let before = indoc! {r#"
            [tools]
            "github:victor-software-house/verctl" = "0.0.1"

            [task_config]
            includes = [
              "git::https://github.com/victor-software-house/verctl.git//tasks/ver?ref=v1.2.3_local",
            ]
        "#};
        let path = write_file(&root, "mise.toml", before);
        let err = write(
            root.path(),
            &[tool_pin("mise.toml", "verctl")],
            &versions("0.0.2"),
        )
        .unwrap_err();
        let report = format!("{err:#}");
        assert!(report.contains("v1.2.3_local"), "{report}");
        assert!(report.contains("mise.toml"), "{report}");
        assert_eq!(fs::read_to_string(&path).unwrap(), before);
    }

    /// The ref ends at the next query separator: a parameter after it is
    /// not part of the version and stays as it was.
    #[test]
    fn a_ref_ends_at_the_next_query_separator() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.toml",
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.1"

                [task_config]
                includes = [
                  "git::https://github.com/victor-software-house/verctl.git//a?ref=v0.0.1&depth=1",
                  "git::https://github.com/victor-software-house/verctl.git//b?ref=v0.0.1#x",
                  "git::https://github.com/victor-software-house/verctl.git//c?ref=v0.0.1;y",
                ]
            "#},
        );
        write(
            root.path(),
            &[tool_pin("mise.toml", "verctl")],
            &versions("0.0.2"),
        )
        .unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.2"

                [task_config]
                includes = [
                  "git::https://github.com/victor-software-house/verctl.git//a?ref=v0.0.2&depth=1",
                  "git::https://github.com/victor-software-house/verctl.git//b?ref=v0.0.2#x",
                  "git::https://github.com/victor-software-house/verctl.git//c?ref=v0.0.2;y",
                ]
            "#}
        );
    }

    /// A TOML array with no space after the comma: only the quote and comma
    /// separate our include from the next repo's.
    #[test]
    fn one_include_ends_where_the_next_begins() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.toml",
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.1"
                [task_config]
                includes = ["git::https://example.com/verctl.git//a?ref=v0.0.1","git::https://example.com/qctl.git//b?ref=v0.0.1"]
            "#},
        );
        let pins = [tool_pin("mise.toml", "verctl")];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.2"
                [task_config]
                includes = ["git::https://example.com/verctl.git//a?ref=v0.0.2","git::https://example.com/qctl.git//b?ref=v0.0.1"]
            "#}
        );
    }

    /// A line-owning spelling anchors on the line and ignores the text next to
    /// it. Every case here is the same pattern against a different document.
    /// Only the neighbourhood changes.
    #[test]
    fn a_whole_line_pattern_owns_its_line_and_nothing_else() {
        let cases = [
            (
                "the line moves, and the key after it is irrelevant",
                indoc! {"
                    ---
                    name: demo
                    version: 0.0.1
                    files: [a.md]
                    ---
                "},
                Ok(indoc! {"
                    ---
                    name: demo
                    version: 0.0.2
                    files: [a.md]
                    ---
                "}),
            ),
            (
                "the same words inside a sentence are not the line",
                indoc! {"
                    ---
                    version: 0.0.1
                    ---

                    Write version: 9.9.9 when you mean the tool's own.
                "},
                Ok(indoc! {"
                    ---
                    version: 0.0.2
                    ---

                    Write version: 9.9.9 when you mean the tool's own.
                "}),
            ),
            (
                "a tail the version does not cover leaves the line unmatched",
                indoc! {"
                    ---
                    version: 0.0.1_local
                    ---
                "},
                Err("matches 0 times"),
            ),
            (
                "a line ends where the file says it does, CRLF included",
                "---\r\nversion: 0.0.1\r\n---\r\n",
                Ok("---\r\nversion: 0.0.2\r\n---\r\n"),
            ),
            (
                "an indented key is a line that starts with something else",
                indoc! {"
                    ---
                    tool:
                      version: 0.0.1
                    ---
                "},
                Err("matches 0 times"),
            ),
        ];
        for (scenario, before, expected) in cases {
            let root = TempDir::new().unwrap();
            let path = write_file(&root, "SKILL.md", before);
            let pins = [Pin {
                file: PathBuf::from("SKILL.md"),
                tool: None,
                pattern_ids: Vec::new(),
                patterns: vec![PinPattern {
                    r#match: "version: {version}".into(),
                    occurrences: Occurrences::Once,
                    whole_line: true,
                }],
                package: "verctl".into(),
            }];
            match (write(root.path(), &pins, &versions("0.0.2")), expected) {
                (Ok(_), Ok(after)) => {
                    assert_eq!(fs::read_to_string(&path).unwrap(), after, "{scenario}");
                }
                (Err(error), Err(needle)) => {
                    let text = format!("{error:#}");
                    assert!(text.contains(needle), "{scenario}: {text}");
                    assert_eq!(
                        fs::read_to_string(&path).unwrap(),
                        before,
                        "{scenario}: nothing is written when the pin cannot be rewritten"
                    );
                }
                (outcome, _) => panic!("{scenario}: {outcome:?}"),
            }
        }
    }

    /// Owning a line does not set how many lines match. The arity does.
    #[test]
    fn a_whole_line_pattern_counts_lines_the_way_its_arity_says() {
        let root = TempDir::new().unwrap();
        let before = indoc! {"
            version: 0.0.1
            not the line: version: 0.0.1
            version: 0.0.1
        "};
        let path = write_file(&root, "pinned.md", before);
        let two = |occurrences| {
            [Pin {
                file: PathBuf::from("pinned.md"),
                tool: None,
                pattern_ids: Vec::new(),
                patterns: vec![PinPattern {
                    r#match: "version: {version}".into(),
                    occurrences,
                    whole_line: true,
                }],
                package: "verctl".into(),
            }]
        };
        let err = write(root.path(), &two(Occurrences::Once), &versions("0.0.2")).unwrap_err();
        assert!(format!("{err:#}").contains("matches 2 times"), "{err:#}");
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            before,
            "an arity the file breaks writes nothing"
        );

        write(
            root.path(),
            &two(Occurrences::Exactly(2)),
            &versions("0.0.2"),
        )
        .unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {"
                version: 0.0.2
                not the line: version: 0.0.1
                version: 0.0.2
            "},
            "both lines move and the one that only contains the words does not"
        );
    }

    /// A tool and a pattern in one pin: the structural rewrite for the table
    /// mise owns, the pattern for the prose beside it.
    #[test]
    fn a_tool_and_a_pattern_in_one_pin_both_apply() {
        let root = TempDir::new().unwrap();
        let path = write_file(
            &root,
            "mise.toml",
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.1"

                # Consumers run verctl@0.0.1.
            "#},
        );
        let pins = [Pin {
            file: PathBuf::from("mise.toml"),
            tool: Some(VERCTL.into()),
            pattern_ids: Vec::new(),
            patterns: vec![PinPattern {
                r#match: "verctl@{version}".into(),
                occurrences: Occurrences::Once,
                whole_line: false,
            }],
            package: "verctl".into(),
        }];
        write(root.path(), &pins, &versions("0.0.2")).unwrap();
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            indoc! {r#"
                [tools]
                "github:victor-software-house/verctl" = "0.0.2"

                # Consumers run verctl@0.0.2.
            "#}
        );
    }

    #[test]
    fn a_tool_pin_needs_the_table_it_names() {
        let root = TempDir::new().unwrap();
        // ast-grep-ignore: no-escaped-newline (TODO(`VER-171`))
        let before = "[tools]\nleftover = \"1\"\n";
        let path = write_file(&root, "mise.release.toml", before);
        let err = write(
            root.path(),
            &[tool_pin("mise.release.toml", "verctl")],
            &versions("0.0.2"),
        )
        .unwrap_err();
        assert!(format!("{err:#}").contains("has no tools."), "{err:#}");
        assert_eq!(fs::read_to_string(&path).unwrap(), before);
    }

    #[test]
    fn rejects_parent_dir_pins() {
        let err = ensure_inside(Path::new("/tmp"), Path::new("../secret")).unwrap_err();
        assert!(format!("{err:#}").contains("inside the repo"), "{err:#}");
    }

    #[test]
    fn rejects_symlink_pins() {
        let root = TempDir::new().unwrap();
        let target = root.path().join("outside.txt");
        fs::write(&target, "x\n").unwrap();
        let link = root.path().join("mise.release.toml");
        symlink(&target, &link).unwrap();
        let err = ensure_inside(root.path(), Path::new("mise.release.toml")).unwrap_err();
        assert!(format!("{err:#}").contains("symlink"), "{err:#}");
    }
}