diff --git a/Changes.md b/Changes.md index 5f83cd1..5aab3d2 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,7 @@ +## v0.0.6 2021-05-11 + +* Fix bug in remove trailing zeros change when the version is all zeros. + ## v0.0.5 2021-05-10 * Remove trailing zeros from parsed package versions because version comparison diff --git a/README.md b/README.md index 965b6fe..883ad4b 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,16 @@ for details on this tool. [![CircleCI](https://circleci.com/gh/ActiveState/langtools.svg?style=svg)](https://circleci.com/gh/ActiveState/langtools) +## To Create a New Release +* Tag master: `git tag v0.0.6` +* Push the tag: `git push origin v0.0.6` +* Go to the [releases page](https://github.com/ActiveState/langtools/releases) +* Click on the name of the new release, `v0.0.6` in this example +* Click the `Edit Release` button on the top right +* Enter in the release notes, which are the same as you added to `Changes.md` + * Remove any manual line breaks as they don't look good +* Click the `Publish Release` button on the bottom + ## Authors This library was created by: diff --git a/pkg/version/shared_test.go b/pkg/version/shared_test.go index e1996e8..6a1bce2 100644 --- a/pkg/version/shared_test.go +++ b/pkg/version/shared_test.go @@ -1,6 +1,7 @@ package version import ( + "fmt" "testing" "github.com/ericlagergren/decimal" @@ -14,6 +15,7 @@ func TestParseGeneric(t *testing.T) { version string expected []string }{ + {"Numbers", "0", []string{"0"}}, {"Numbers", "1", []string{"1"}}, {"Numbers", "1.0", []string{"1"}}, {"Numbers", "0.92", []string{"0", "92"}}, @@ -309,6 +311,32 @@ func TestString(t *testing.T) { assert.Equal(t, "1.2.3 (SemVer)", v.String()) } +func TestTrimTrailingZeros(t *testing.T) { + tests := []struct { + input, expected []string + }{ + {[]string{"0"}, []string{"0"}}, + {[]string{"1"}, []string{"1"}}, + {[]string{"0", "0"}, []string{"0"}}, + {[]string{"0", "1"}, []string{"0", "1"}}, + {[]string{"1", "0"}, []string{"1"}}, + {[]string{"1", "1"}, []string{"1", "1"}}, + {[]string{"0", "0", "0"}, []string{"0"}}, + {[]string{"1", "0", "0"}, []string{"1"}}, + {[]string{"1", "0", "1"}, []string{"1", "0", "1"}}, + {[]string{"1", "1", "1"}, []string{"1", "1", "1"}}, + } + + for _, tt := range tests { + t.Run(fmt.Sprint(tt.input), func(t *testing.T) { + input := mustStringsToDecimal(t, tt.input) + actual := trimTrailingZeros(input) + expected := mustStringsToDecimal(t, tt.expected) + assert.Equal(t, expected, actual, "expected %v got %v", expected, actual) + }) + } +} + func parseOrFatalGeneric(t *testing.T, v string) *Version { ver, err := ParseGeneric(v) assert.NoError(t, err, "no error parsing %s as a generic version", v) @@ -322,3 +350,10 @@ func parseOrFatalSemVer(t *testing.T, v string) *Version { return ver } + +func mustStringsToDecimal(t *testing.T, s []string) []*decimal.Big { + d, err := stringsToDecimals(s) + assert.NoError(t, err, "no error parsing strings to decimals") + + return d +} diff --git a/pkg/version/version.go b/pkg/version/version.go index 528f9c4..37eb49c 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -111,9 +111,13 @@ func stringsToDecimals(strings []string) ([]*decimal.Big, error) { func trimTrailingZeros(decimals []*decimal.Big) []*decimal.Big { indexOfLastZero := len(decimals) - for indexOfLastZero > 0 && decimals[indexOfLastZero-1].Cmp(bigZero) == 0 { - indexOfLastZero-- + for i := len(decimals) - 1; i > 0; i-- { + if decimals[i].Cmp(bigZero) != 0 { + break + } + indexOfLastZero = i } + return decimals[0:indexOfLastZero] }