From 80c9151916c7d4febb1e6a3d30f84c1dfd7cea51 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Fri, 9 Aug 2024 21:25:04 -0700 Subject: [PATCH] =?UTF-8?q?Fix=20crash=20when=20displaying=20prerelease=20?= =?UTF-8?q?=E2=86=92=20final=20version=20change=20(#34)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `update-interactive` tried to format a prerelease → final version number change like this: > 7.2.0.rc1 → 7.2.0 It would crash with a `KeyError` exception: ``` bundle_update_interactive/cli/row.rb:54:in 'Hash#fetch': key not found: nil (KeyError) from bundle_update_interactive/cli/row.rb:54:in 'BundleUpdateInteractive::CLI::Row#apply_semver_highlight' ``` This PR fixes the underlying bug and adds tests for this scenario. --- lib/bundle_update_interactive/semver_change.rb | 4 +++- .../{semver_test.rb => semver_change_test.rb} | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) rename test/bundle_update_interactive/{semver_test.rb => semver_change_test.rb} (81%) diff --git a/lib/bundle_update_interactive/semver_change.rb b/lib/bundle_update_interactive/semver_change.rb index f51ecfc..359c2c2 100644 --- a/lib/bundle_update_interactive/semver_change.rb +++ b/lib/bundle_update_interactive/semver_change.rb @@ -10,10 +10,12 @@ def initialize(old_version, new_version) @same_segments = new_segments.take_while.with_index { |seg, i| seg == old_segments[i] } @diff_segments = new_segments[same_segments.length..] + + @changed = diff_segments.any? || old_segments.length != new_segments.length end def severity - return nil if diff_segments.empty? + return nil unless @changed SEVERITIES[same_segments.length] || :patch end diff --git a/test/bundle_update_interactive/semver_test.rb b/test/bundle_update_interactive/semver_change_test.rb similarity index 81% rename from test/bundle_update_interactive/semver_test.rb rename to test/bundle_update_interactive/semver_change_test.rb index 311305c..07ef910 100644 --- a/test/bundle_update_interactive/semver_test.rb +++ b/test/bundle_update_interactive/semver_change_test.rb @@ -3,7 +3,7 @@ require "test_helper" module BundleUpdateInteractive - class SemverTest < Minitest::Test + class SemverChangeTest < Minitest::Test def test_prerelease_is_considered_patch change = SemverChange.new("7.2.0.beta2", "7.2.0.beta3") @@ -14,6 +14,16 @@ def test_prerelease_is_considered_patch refute_predicate change, :major? end + def test_prerelease_to_final_is_considered_patch + change = SemverChange.new("7.2.0.rc1", "7.2.0") + + assert_equal :patch, change.severity + assert_predicate change, :patch? + + refute_predicate change, :minor? + refute_predicate change, :major? + end + def test_change_in_fourth_segment_is_considered_patch change = SemverChange.new("7.1.3.3", "7.1.3.4") @@ -63,6 +73,12 @@ def test_format_applies_to_all_segments_starting_with_changed_one assert_equal "2.1.6.<1>", SemverChange.new("2.1.6", "2.1.6.1").format(&formatter) end + def test_format_doesnt_apply_to_final_release + formatter = ->(str) { "<#{str}>" } + + assert_equal "7.2.0", SemverChange.new("7.2.0.rc1", "7.2.0").format(&formatter) + end + def test_none_is_true_when_versions_are_identical change = SemverChange.new("1.0.3", "1.0.3")