Skip to content

Commit

Permalink
Fix bug in trailing zero removal when version is all zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsanterre authored and jalan committed May 11, 2021
1 parent ce02157 commit b32edf9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
35 changes: 35 additions & 0 deletions pkg/version/shared_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package version

import (
"fmt"
"testing"

"github.com/ericlagergren/decimal"
Expand All @@ -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"}},
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
8 changes: 6 additions & 2 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down

0 comments on commit b32edf9

Please sign in to comment.