Skip to content

Commit 893fec4

Browse files
authored
Merge pull request #5 from upfluence/tg-fix-parser
tag: Fix the parser to handle version > 9
2 parents 10cb14a + aba6cfa commit 893fec4

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

cmd/gh-downloader/release_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"sort"
5+
"testing"
6+
7+
"github.com/google/go-github/github"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestReleasesSort(t *testing.T) {
12+
for _, tt := range []struct {
13+
name string
14+
with releases
15+
want []string
16+
}{
17+
{
18+
name: "path",
19+
with: buildReleases("foo-1.2.100", "foo-1.2.2", "foo-1.2.30"),
20+
want: []string{"foo-1.2.2", "foo-1.2.30", "foo-1.2.100"},
21+
},
22+
{
23+
name: "minor",
24+
with: buildReleases("foo-1.200.2", "foo-1.1.2", "foo-1.30.2"),
25+
want: []string{"foo-1.1.2", "foo-1.30.2", "foo-1.200.2"},
26+
},
27+
{
28+
name: "major",
29+
with: buildReleases("foo-300.2.1", "foo-2.2.1", "foo-50.2.1"),
30+
want: []string{"foo-2.2.1", "foo-50.2.1", "foo-300.2.1"},
31+
},
32+
{
33+
name: "with suffix",
34+
with: buildReleases("foo-1.2.2-rc19", "foo-1.2.2-rc10", "foo-1.2.2-rc1"),
35+
want: []string{"foo-1.2.2-rc1", "foo-1.2.2-rc10", "foo-1.2.2-rc19"},
36+
},
37+
} {
38+
t.Run(tt.name, func(t *testing.T) {
39+
sort.Sort(tt.with)
40+
41+
var out = make([]string, len(tt.with))
42+
43+
for i, rr := range tt.with {
44+
out[i] = *rr.TagName
45+
}
46+
47+
assert.Equal(t, tt.want, out)
48+
})
49+
}
50+
}
51+
52+
func newRelease(tag string) github.RepositoryRelease {
53+
return github.RepositoryRelease{
54+
TagName: &tag,
55+
}
56+
}
57+
58+
func buildReleases(tags ...string) releases {
59+
var out releases
60+
61+
for _, t := range tags {
62+
r := newRelease(t)
63+
out = append(out, &r)
64+
}
65+
66+
return out
67+
}

cmd/gh-downloader/tag.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77
)
88

9-
var versionRe = regexp.MustCompile(`((\w+)-)?v?(\d|x).(\d|x).(\d|x)(-(\w+))?`)
9+
var versionRe = regexp.MustCompile(`((\w+)-)?v?(\d+|x).(\d+|x).(\d+|x)(-(\w+))?`)
1010

1111
type tag struct {
1212
Project string

cmd/gh-downloader/tag_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestNewTag(t *testing.T) {
10+
for _, tt := range []struct {
11+
with string
12+
want tag
13+
}{
14+
{
15+
with: "foo-x.x.x",
16+
want: tag{Project: "foo", Major: -1, Minor: -1, Patch: -1, Suffix: ""},
17+
},
18+
{
19+
with: "foo-vx.x.x",
20+
want: tag{Project: "foo", Major: -1, Minor: -1, Patch: -1, Suffix: ""},
21+
},
22+
{
23+
with: "foo-1.2.3",
24+
want: tag{Project: "foo", Major: 1, Minor: 2, Patch: 3, Suffix: ""},
25+
},
26+
{
27+
with: "foo-v1.2.3",
28+
want: tag{Project: "foo", Major: 1, Minor: 2, Patch: 3, Suffix: ""},
29+
},
30+
{
31+
with: "foo-10.20.30",
32+
want: tag{Project: "foo", Major: 10, Minor: 20, Patch: 30, Suffix: ""},
33+
},
34+
{
35+
with: "foo-10.20.30-bar",
36+
want: tag{Project: "foo", Major: 10, Minor: 20, Patch: 30, Suffix: "bar"},
37+
},
38+
} {
39+
t.Run(tt.with, func(t *testing.T) {
40+
tag := newTag(tt.with)
41+
42+
assert.Equal(t, tt.want, *tag)
43+
})
44+
}
45+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
require (
66
github.com/google/go-github v13.0.1-0.20171030212440-724ae38c5030+incompatible
77
github.com/google/go-querystring v1.0.0 // indirect
8+
github.com/stretchr/testify v1.6.1
89
github.com/upfluence/cfg v0.2.3
910
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
1011
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
7171
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
7272
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
7373
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
74+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
7475
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
7576
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
77+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
7678
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
7779
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
7880
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -202,6 +204,7 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
202204
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
203205
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
204206
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
207+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
205208
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
206209
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
207210
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)