|
| 1 | +#!/usr/bin/env rust-script |
| 2 | +//! Test that version bumping logic correctly skips past already-published versions. |
| 3 | +//! |
| 4 | +//! This experiment verifies the core logic from issue #22: |
| 5 | +//! Given a current version and a set of "published" versions, the bump must produce |
| 6 | +//! a version strictly greater than the max published version. |
| 7 | +//! |
| 8 | +//! ```cargo |
| 9 | +//! [dependencies] |
| 10 | +//! ``` |
| 11 | +
|
| 12 | +fn bump(major: u32, minor: u32, patch: u32, bump_type: &str) -> (u32, u32, u32) { |
| 13 | + match bump_type { |
| 14 | + "major" => (major + 1, 0, 0), |
| 15 | + "minor" => (major, minor + 1, 0), |
| 16 | + _ => (major, minor, patch + 1), |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +fn ensure_version_exceeds( |
| 21 | + version: (u32, u32, u32), |
| 22 | + max_published: Option<(u32, u32, u32)>, |
| 23 | +) -> (u32, u32, u32) { |
| 24 | + let (major, minor, patch) = version; |
| 25 | + |
| 26 | + if let Some((pub_major, pub_minor, pub_patch)) = max_published { |
| 27 | + if (major, minor, patch) <= (pub_major, pub_minor, pub_patch) { |
| 28 | + // Set to max_published + patch 1 to exceed it |
| 29 | + println!( |
| 30 | + " {}.{}.{} <= {}.{}.{}, adjusting to {}.{}.{}", |
| 31 | + major, minor, patch, |
| 32 | + pub_major, pub_minor, pub_patch, |
| 33 | + pub_major, pub_minor, pub_patch + 1 |
| 34 | + ); |
| 35 | + return (pub_major, pub_minor, pub_patch + 1); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + (major, minor, patch) |
| 40 | +} |
| 41 | + |
| 42 | +fn test_case( |
| 43 | + name: &str, |
| 44 | + current: (u32, u32, u32), |
| 45 | + bump_type: &str, |
| 46 | + max_published: Option<(u32, u32, u32)>, |
| 47 | + expected: (u32, u32, u32), |
| 48 | +) { |
| 49 | + let bumped = bump(current.0, current.1, current.2, bump_type); |
| 50 | + let final_ver = ensure_version_exceeds(bumped, max_published); |
| 51 | + |
| 52 | + let passed = final_ver == expected; |
| 53 | + let status = if passed { "PASS" } else { "FAIL" }; |
| 54 | + |
| 55 | + println!( |
| 56 | + "[{}] {}: {}.{}.{} --{}-> {}.{}.{} (max_pub={}) => {}.{}.{} (expected {}.{}.{})", |
| 57 | + status, |
| 58 | + name, |
| 59 | + current.0, current.1, current.2, |
| 60 | + bump_type, |
| 61 | + bumped.0, bumped.1, bumped.2, |
| 62 | + max_published.map_or("none".to_string(), |(a,b,c)| format!("{}.{}.{}", a, b, c)), |
| 63 | + final_ver.0, final_ver.1, final_ver.2, |
| 64 | + expected.0, expected.1, expected.2, |
| 65 | + ); |
| 66 | + |
| 67 | + if !passed { |
| 68 | + std::process::exit(1); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn main() { |
| 73 | + println!("=== Testing version bumping past published versions ===\n"); |
| 74 | + |
| 75 | + // Case 1: Normal bump, version exceeds published -> no adjustment needed |
| 76 | + test_case( |
| 77 | + "normal minor bump exceeds published", |
| 78 | + (0, 2, 0), "minor", Some((0, 2, 0)), |
| 79 | + (0, 3, 0), |
| 80 | + ); |
| 81 | + |
| 82 | + // Case 2: Patch bump produces 0.2.1 which exceeds 0.2.0 -> OK |
| 83 | + test_case( |
| 84 | + "patch bump past published", |
| 85 | + (0, 2, 0), "patch", Some((0, 2, 0)), |
| 86 | + (0, 2, 1), |
| 87 | + ); |
| 88 | + |
| 89 | + // Case 3: Minor bump from 0.1.0 gives 0.2.0 which equals published 0.2.0 -> must exceed |
| 90 | + test_case( |
| 91 | + "minor bump equal to published", |
| 92 | + (0, 1, 0), "minor", Some((0, 2, 0)), |
| 93 | + (0, 2, 1), |
| 94 | + ); |
| 95 | + |
| 96 | + // Case 4: Major bump, no conflict |
| 97 | + test_case( |
| 98 | + "major bump no conflict", |
| 99 | + (0, 5, 3), "major", Some((0, 5, 3)), |
| 100 | + (1, 0, 0), |
| 101 | + ); |
| 102 | + |
| 103 | + // Case 5: No published versions at all |
| 104 | + test_case( |
| 105 | + "first release", |
| 106 | + (0, 1, 0), "patch", None, |
| 107 | + (0, 1, 1), |
| 108 | + ); |
| 109 | + |
| 110 | + // Case 6: Published version much higher - should jump to published+1 |
| 111 | + test_case( |
| 112 | + "published ahead - jumps to max_published+1", |
| 113 | + (0, 1, 0), "patch", Some((0, 5, 0)), |
| 114 | + (0, 5, 1), |
| 115 | + ); |
| 116 | + |
| 117 | + // Case 7: THE ACTUAL BUG SCENARIO |
| 118 | + // Cargo.toml has 0.2.0, published is 0.2.0, minor bump gives 0.3.0 |
| 119 | + // 0.3.0 > 0.2.0 so no adjustment needed |
| 120 | + test_case( |
| 121 | + "issue-22 scenario: Cargo.toml at published version", |
| 122 | + (0, 2, 0), "minor", Some((0, 2, 0)), |
| 123 | + (0, 3, 0), |
| 124 | + ); |
| 125 | + |
| 126 | + // Case 8: Edge case - what if version-and-commit somehow produces the same version |
| 127 | + // (this was the actual bug: current=0.2.0, bump produced 0.2.0 because of stale state) |
| 128 | + test_case( |
| 129 | + "issue-22 actual bug: bump produces same as published", |
| 130 | + (0, 1, 5), "patch", Some((0, 2, 0)), |
| 131 | + (0, 2, 1), |
| 132 | + ); |
| 133 | + |
| 134 | + println!("\n=== All tests passed! ==="); |
| 135 | +} |
0 commit comments