From df7fdaccd9c907bd70be807f79acada02528cc32 Mon Sep 17 00:00:00 2001 From: Mithrandie Date: Sun, 19 May 2019 19:59:36 +0900 Subject: [PATCH] Fix the following bug. - Versions are not compared correctly in check-update subcommand. --- lib/action/update.go | 22 +++++++++------ lib/action/update_test.go | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/lib/action/update.go b/lib/action/update.go index 31212d0b..e2a1eca2 100644 --- a/lib/action/update.go +++ b/lib/action/update.go @@ -48,17 +48,23 @@ func (v *Version) IsLaterThan(v2 *Version) bool { return false } - if v.Major > v2.Major { - return true + if v.Major != v2.Major { + return v.Major > v2.Major } - if v.Minor > v2.Minor { - return true + if v.Minor != v2.Minor { + return v.Minor > v2.Minor } - if v.Patch > v2.Patch { - return true + if v.Patch != v2.Patch { + return v.Patch > v2.Patch } - if 0 < v2.PreRelease && v.PreRelease > v2.PreRelease { - return true + if v.PreRelease != v2.PreRelease { + if v.PreRelease == 0 { + return true + } + if v2.PreRelease == 0 { + return false + } + return v.PreRelease > v2.PreRelease } return false } diff --git a/lib/action/update_test.go b/lib/action/update_test.go index 84dcb656..505b690a 100644 --- a/lib/action/update_test.go +++ b/lib/action/update_test.go @@ -38,6 +38,19 @@ var versionIsLaterThanTests = []struct { }, Result: false, }, + { + Version1: &Version{ + Major: 1, + Minor: 2, + Patch: 1, + }, + Version2: &Version{ + Major: 1, + Minor: 3, + Patch: 0, + }, + Result: false, + }, { Version1: &Version{ Major: 2, @@ -77,6 +90,51 @@ var versionIsLaterThanTests = []struct { }, Result: true, }, + { + Version1: &Version{ + Major: 1, + Minor: 2, + Patch: 3, + PreRelease: 0, + }, + Version2: &Version{ + Major: 1, + Minor: 2, + Patch: 3, + PreRelease: 4, + }, + Result: true, + }, + { + Version1: &Version{ + Major: 1, + Minor: 2, + Patch: 3, + PreRelease: 4, + }, + Version2: &Version{ + Major: 1, + Minor: 2, + Patch: 3, + PreRelease: 0, + }, + Result: false, + }, + { + Version1: &Version{ + Major: 1, + Minor: 2, + Patch: 3, + PreRelease: 4, + }, + Version2: &Version{ + Major: 1, + Minor: 2, + Patch: 3, + PreRelease: 5, + }, + Result: false, + }, } func TestVersion_IsLaterThan(t *testing.T) {