oakum 0.3.2

A polyglot release tool that derives dependent version bumps from the dependency graph
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
//! Aggregate multiple bump files per package: highest level wins.
//!
//! Parsing the intersection grammar and reading rules live in [`crate::changeset`]
//! (`okm-ep0`, `okm-wnp`). This module takes already-resolved files — package
//! identities, levels, and notes — and folds them into one direct bump per
//! package. Cascade and version math are later passes over that result.
//!
//! Spec rule (`docs/specs/bump-files.md`): files naming the same package
//! accumulate; the highest *release* level decides the bump (`none` never
//! raises), and every note from a releasing contribution appears in the
//! changelog.

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

use super::bump::BumpLevel;
use super::workspace::PackageId;

/// One bump file as the planner sees it after parsing and name resolution.
///
/// The resolver that builds this must give a non-empty `id` unique among the
/// files in one aggregate call; this type does not enforce that.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BumpFile {
    /// Opaque identity (typically the filename). Consumption and `--explain`
    /// use this to name what was folded in.
    pub id: String,
    /// Packages named in the frontmatter, as resolved. Duplicate packages in
    /// one file are allowed here; [`aggregate`] collapses them to the higher
    /// level and attributes the note once.
    pub entries: Vec<(PackageId, BumpLevel)>,
    /// Markdown body after the closing `---`, kept verbatim.
    pub note: String,
}

/// What one bump file asked for a package, before the aggregate max is taken.
///
/// `level` is the file's request, not the eventual aggregate — so `--explain`
/// can say which file raised the ceiling.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Contribution {
    source: String,
    level: BumpLevel,
    note: String,
}

impl Contribution {
    #[must_use]
    pub fn source(&self) -> &str {
        &self.source
    }

    #[must_use]
    pub fn level(&self) -> BumpLevel {
        self.level
    }

    #[must_use]
    pub fn note(&self) -> &str {
        &self.note
    }
}

/// The direct bump for one package after folding every bump file that named it.
///
/// Constructed only by [`aggregate`] (`first` / `absorb`); fields stay private
/// so the max-level invariant cannot drift from the contribution list.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AggregatedBump {
    level: BumpLevel,
    contributions: Vec<Contribution>,
}

impl AggregatedBump {
    fn first(level: BumpLevel, source: String, note: String) -> Self {
        Self {
            level,
            contributions: alloc::vec![Contribution {
                source,
                level,
                note,
            }],
        }
    }

    fn absorb(&mut self, level: BumpLevel, source: &str, note: &str) {
        if level > self.level {
            self.level = level;
        }
        self.contributions.push(Contribution {
            source: String::from(source),
            level,
            note: String::from(note),
        });
        debug_assert_eq!(
            self.level,
            self.contributions
                .iter()
                .map(Contribution::level)
                .max()
                .expect("absorb leaves at least one contribution"),
        );
    }

    #[must_use]
    pub fn level(&self) -> BumpLevel {
        self.level
    }

    #[must_use]
    pub fn contributions(&self) -> &[Contribution] {
        &self.contributions
    }

    /// Notes in bump-file encounter order from contributions that request a
    /// release. Coverage-only [`BumpLevel::None`] notes are omitted (ADR-0028).
    pub fn notes(&self) -> impl Iterator<Item = &str> {
        self.contributions
            .iter()
            .filter(|contribution| contribution.level.is_release())
            .map(Contribution::note)
    }
}

/// Fold bump files into one [`AggregatedBump`] per package.
///
/// Output keys follow [`PackageId`]'s `Ord` so snapshot fixtures compare stably.
/// Contribution order follows the input file order. Within one file, a package
/// appears at most once (duplicate entries collapse to the higher level first).
#[must_use]
pub fn aggregate(files: impl IntoIterator<Item = BumpFile>) -> BTreeMap<PackageId, AggregatedBump> {
    let mut out: BTreeMap<PackageId, AggregatedBump> = BTreeMap::new();
    for file in files {
        let mut per_file: BTreeMap<PackageId, BumpLevel> = BTreeMap::new();
        for (package, level) in file.entries {
            per_file
                .entry(package)
                .and_modify(|existing| {
                    if level > *existing {
                        *existing = level;
                    }
                })
                .or_insert(level);
        }
        for (package, level) in per_file {
            match out.get_mut(&package) {
                Some(aggregated) => aggregated.absorb(level, &file.id, &file.note),
                None => {
                    out.insert(
                        package,
                        AggregatedBump::first(level, file.id.clone(), file.note.clone()),
                    );
                }
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plan::workspace::Ecosystem;
    use alloc::string::ToString;
    use alloc::vec;

    fn cargo(name: &str) -> PackageId {
        PackageId::new(Ecosystem::Cargo, name)
    }

    fn npm(name: &str) -> PackageId {
        PackageId::new(Ecosystem::Npm, name)
    }

    fn file(id: &str, entries: Vec<(PackageId, BumpLevel)>, note: &str) -> BumpFile {
        BumpFile {
            id: id.to_string(),
            entries,
            note: note.to_string(),
        }
    }

    /// Guide example: three patches and one minor → one minor with four notes.
    #[test]
    fn highest_level_wins_and_every_note_is_kept() {
        let core = cargo("core");
        let plan = aggregate([
            file("a.md", vec![(core.clone(), BumpLevel::Patch)], "fix one"),
            file("b.md", vec![(core.clone(), BumpLevel::Patch)], "fix two"),
            file("c.md", vec![(core.clone(), BumpLevel::Patch)], "fix three"),
            file("d.md", vec![(core.clone(), BumpLevel::Minor)], "feature"),
        ]);
        let aggregated = plan.get(&core).expect("core");
        assert_eq!(aggregated.level(), BumpLevel::Minor);
        assert_eq!(
            aggregated.notes().collect::<Vec<_>>(),
            ["fix one", "fix two", "fix three", "feature"]
        );
        assert_eq!(
            aggregated
                .contributions()
                .iter()
                .map(Contribution::level)
                .collect::<Vec<_>>(),
            [
                BumpLevel::Patch,
                BumpLevel::Patch,
                BumpLevel::Patch,
                BumpLevel::Minor
            ]
        );
        assert_eq!(
            aggregated
                .contributions()
                .iter()
                .map(Contribution::source)
                .collect::<Vec<_>>(),
            ["a.md", "b.md", "c.md", "d.md"]
        );
    }

    #[test]
    fn major_outranks_minor_and_patch() {
        let pkg = cargo("oakum");
        let plan = aggregate([
            file("patch.md", vec![(pkg.clone(), BumpLevel::Patch)], "p"),
            file("major.md", vec![(pkg.clone(), BumpLevel::Major)], "m"),
            file("minor.md", vec![(pkg.clone(), BumpLevel::Minor)], "n"),
        ]);
        assert_eq!(plan[&pkg].level(), BumpLevel::Major);
        assert_eq!(plan[&pkg].notes().count(), 3);
    }

    #[test]
    fn later_lower_level_does_not_lower_the_ceiling() {
        let pkg = cargo("oakum");
        let plan = aggregate([
            file(
                "major.md",
                vec![(pkg.clone(), BumpLevel::Major)],
                "breaking",
            ),
            file("patch.md", vec![(pkg.clone(), BumpLevel::Patch)], "typo"),
        ]);
        assert_eq!(plan[&pkg].level(), BumpLevel::Major);
        assert_eq!(plan[&pkg].notes().collect::<Vec<_>>(), ["breaking", "typo"]);
    }

    #[test]
    fn one_file_note_is_attributed_to_each_named_package() {
        let core = cargo("core");
        let utils = cargo("utils");
        let plan = aggregate([file(
            "shared.md",
            vec![
                (core.clone(), BumpLevel::Minor),
                (utils.clone(), BumpLevel::Patch),
            ],
            "shared note",
        )]);
        assert_eq!(plan.len(), 2);
        assert_eq!(plan[&core].level(), BumpLevel::Minor);
        assert_eq!(plan[&utils].level(), BumpLevel::Patch);
        assert_eq!(plan[&core].notes().collect::<Vec<_>>(), ["shared note"]);
        assert_eq!(plan[&utils].notes().collect::<Vec<_>>(), ["shared note"]);
        assert_eq!(plan[&core].contributions()[0].source(), "shared.md");
    }

    #[test]
    fn packages_accumulate_independently() {
        let core = cargo("core");
        let utils = cargo("utils");
        let plan = aggregate([
            file(
                "core-only.md",
                vec![(core.clone(), BumpLevel::Major)],
                "core",
            ),
            file(
                "utils-only.md",
                vec![(utils.clone(), BumpLevel::Patch)],
                "utils",
            ),
            file(
                "both.md",
                vec![
                    (core.clone(), BumpLevel::Patch),
                    (utils.clone(), BumpLevel::Minor),
                ],
                "both",
            ),
        ]);
        assert_eq!(plan[&core].level(), BumpLevel::Major);
        assert_eq!(plan[&utils].level(), BumpLevel::Minor);
        assert_eq!(plan[&core].notes().collect::<Vec<_>>(), ["core", "both"]);
        assert_eq!(plan[&utils].notes().collect::<Vec<_>>(), ["utils", "both"]);
    }

    #[test]
    fn empty_input_yields_empty_plan() {
        assert!(aggregate([]).is_empty());
    }

    #[test]
    fn file_with_no_entries_contributes_nothing() {
        let plan = aggregate([file("empty.md", vec![], "orphan note")]);
        assert!(plan.is_empty());
    }

    #[test]
    fn empty_note_is_still_retained() {
        let pkg = cargo("core");
        let plan = aggregate([file("blank.md", vec![(pkg.clone(), BumpLevel::Patch)], "")]);
        assert_eq!(plan[&pkg].notes().collect::<Vec<_>>(), [""]);
    }

    #[test]
    fn duplicate_entries_in_one_file_take_the_higher_level_once() {
        let pkg = cargo("core");
        let plan = aggregate([file(
            "dup.md",
            vec![
                (pkg.clone(), BumpLevel::Patch),
                (pkg.clone(), BumpLevel::Minor),
                (pkg.clone(), BumpLevel::Patch),
            ],
            "once",
        )]);
        let aggregated = &plan[&pkg];
        assert_eq!(aggregated.level(), BumpLevel::Minor);
        assert_eq!(aggregated.contributions().len(), 1);
        assert_eq!(aggregated.contributions()[0].level(), BumpLevel::Minor);
        assert_eq!(aggregated.notes().collect::<Vec<_>>(), ["once"]);
    }

    #[test]
    fn within_file_earlier_higher_level_beats_later_lower() {
        let pkg = cargo("core");
        let plan = aggregate([file(
            "dup.md",
            vec![
                (pkg.clone(), BumpLevel::Minor),
                (pkg.clone(), BumpLevel::Patch),
            ],
            "once",
        )]);
        let aggregated = &plan[&pkg];
        assert_eq!(aggregated.level(), BumpLevel::Minor);
        assert_eq!(aggregated.contributions().len(), 1);
        assert_eq!(aggregated.contributions()[0].level(), BumpLevel::Minor);
    }

    #[test]
    fn equal_levels_across_files_keep_every_note() {
        let pkg = cargo("core");
        let plan = aggregate([
            file("a.md", vec![(pkg.clone(), BumpLevel::Minor)], "one"),
            file("b.md", vec![(pkg.clone(), BumpLevel::Minor)], "two"),
        ]);
        let aggregated = &plan[&pkg];
        assert_eq!(aggregated.level(), BumpLevel::Minor);
        assert_eq!(aggregated.notes().collect::<Vec<_>>(), ["one", "two"]);
        assert_eq!(
            aggregated
                .contributions()
                .iter()
                .map(Contribution::level)
                .collect::<Vec<_>>(),
            [BumpLevel::Minor, BumpLevel::Minor]
        );
    }

    #[test]
    fn same_name_different_ecosystem_does_not_merge() {
        let crate_pkg = cargo("shared");
        let npm_pkg = npm("shared");
        let plan = aggregate([
            file(
                "crate.md",
                vec![(crate_pkg.clone(), BumpLevel::Major)],
                "cargo",
            ),
            file("js.md", vec![(npm_pkg.clone(), BumpLevel::Patch)], "npm"),
        ]);
        assert_eq!(plan.len(), 2);
        assert_eq!(plan[&crate_pkg].level(), BumpLevel::Major);
        assert_eq!(plan[&npm_pkg].level(), BumpLevel::Patch);
    }

    #[test]
    fn output_packages_are_ordered_by_package_id() {
        let plan = aggregate([
            file("z.md", vec![(cargo("z-last"), BumpLevel::Patch)], "z"),
            file("a.md", vec![(cargo("a-first"), BumpLevel::Patch)], "a"),
        ]);
        let names: Vec<_> = plan.keys().map(|id| id.name.as_str()).collect();
        assert_eq!(names, ["a-first", "z-last"]);
    }

    #[test]
    fn none_never_raises_and_notes_skip_coverage_only() {
        let pkg = cargo("core");
        let plan = aggregate([
            file("cover.md", vec![(pkg.clone(), BumpLevel::None)], "covered"),
            file("fix.md", vec![(pkg.clone(), BumpLevel::Patch)], "fixed"),
        ]);
        assert_eq!(plan[&pkg].level(), BumpLevel::Patch);
        assert_eq!(plan[&pkg].notes().collect::<Vec<_>>(), ["fixed"]);

        let none_only = aggregate([file(
            "only.md",
            vec![(pkg.clone(), BumpLevel::None)],
            "no release",
        )]);
        assert_eq!(none_only[&pkg].level(), BumpLevel::None);
        assert_eq!(none_only[&pkg].notes().count(), 0);
    }
}