Skip to content

Commit b8ab3e1

Browse files
cliedemanchristophwitzko
authored andcommitted
refactor: use testify
1 parent 77a0d57 commit b8ab3e1

File tree

9 files changed

+155
-205
lines changed

9 files changed

+155
-205
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ require (
66
github.com/Masterminds/semver v1.5.0
77
github.com/google/go-github/v30 v30.1.0
88
github.com/shuheiktgw/go-travis v0.2.4
9+
github.com/stretchr/testify v1.5.1
910
github.com/urfave/cli/v2 v2.2.0
1011
github.com/xanzy/go-gitlab v0.31.0
1112
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
13+
gopkg.in/yaml.v2 v2.2.8 // indirect
1214
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF0
55
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
66
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
77
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
89
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
910
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1011
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
@@ -16,6 +17,7 @@ github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASu
1617
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
1718
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
1819
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
20+
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
1921
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
2022
github.com/hashicorp/go-retryablehttp v0.6.4 h1:BbgctKO892xEyOXnGiaAwIoSq1QZ/SS4AhjoAh9DnfY=
2123
github.com/hashicorp/go-retryablehttp v0.6.4/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
@@ -27,9 +29,13 @@ github.com/shuheiktgw/go-travis v0.2.4 h1:IAnh/Dyv7ql87qtJWUcvR5MM8e5iCDGoENl9Vz
2729
github.com/shuheiktgw/go-travis v0.2.4/go.mod h1:RtODX49bvgHTvfzFvGEPFtU0dKVk0D3PyvUQR/63hT0=
2830
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
2931
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
32+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
3033
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3134
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
35+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
3236
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
37+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
38+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
3339
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
3440
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
3541
github.com/xanzy/go-gitlab v0.31.0 h1:+nHztQuCXGSMluKe5Q9IRaPdz6tO8O0gMkQ0vqGpiBk=
@@ -59,3 +65,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
5965
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6066
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
6167
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
68+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
69+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

pkg/condition/github_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package condition
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestGithubValid(t *testing.T) {
810
gha := GitHubActions{}
911
err := gha.RunCondition(CIConfig{"defaultBranch": ""})
10-
if err == nil {
11-
t.Fail()
12-
}
12+
assert.EqualError(t, err, "This test run is not running on a branch build.")
1313
}

pkg/condition/gitlab_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package condition
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestGitlabValid(t *testing.T) {
810
gl := GitLab{}
911
err := gl.RunCondition(CIConfig{"defaultBranch": ""})
10-
if err == nil {
11-
t.Fail()
12-
}
12+
assert.EqualError(t, err, "This test run is not running on a branch build.")
1313
}

pkg/condition/travis_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package condition
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestTravisValid(t *testing.T) {
810
travis := TravisCI{}
911
err := travis.RunCondition(CIConfig{"token": "", "defaultBranch": "", "private": false})
10-
if err == nil {
11-
t.Fail()
12-
}
12+
assert.EqualError(t, err, "semantic-release didn’t run on Travis CI and therefore a new version won’t be published.")
1313
}

pkg/semrel/github_test.go

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ import (
1212

1313
"github.com/Masterminds/semver"
1414
"github.com/google/go-github/v30/github"
15+
"github.com/stretchr/testify/require"
1516
)
1617

1718
func TestNewGithubRepository(t *testing.T) {
19+
require := require.New(t)
20+
1821
repo, err := NewGitHubRepository(context.TODO(), "", "", "")
19-
if repo != nil || err == nil {
20-
t.Fatal("invalid initialization")
21-
}
22+
require.Nil(repo)
23+
require.EqualError(err, "invalid slug")
24+
2225
repo, err = NewGitHubRepository(context.TODO(), "", "owner/test-repo", "token")
23-
if repo == nil || err != nil {
24-
t.Fatal("invalid initialization")
25-
}
26+
require.NotNil(repo)
27+
require.NoError(err)
28+
require.Equal(repo.Owner(), "owner")
29+
require.Equal(repo.Repo(), "test-repo")
30+
2631
repo, err = NewGitHubRepository(context.TODO(), "github.enterprise", "owner/test-repo", "token")
27-
if repo.Client.BaseURL.Host != "github.enterprise" || err != nil {
28-
t.Fatal("invalid enterprise initialization")
29-
}
32+
require.NotNil(repo)
33+
require.NoError(err)
34+
require.Equal("github.enterprise", repo.Client.BaseURL.Host)
3035
}
3136

3237
func createGithubCommit(sha, message string) *github.RepositoryCommit {
@@ -103,10 +108,7 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
103108

104109
func getNewGithubTestRepo(t *testing.T) (*GitHubRepository, *httptest.Server) {
105110
repo, err := NewGitHubRepository(context.TODO(), "", "owner/test-repo", "token")
106-
if err != nil {
107-
t.Fatal(err)
108-
return nil, nil
109-
}
111+
require.NoError(t, err)
110112
ts := httptest.NewServer(http.HandlerFunc(githubHandler))
111113
repo.Client.BaseURL, _ = url.Parse(ts.URL + "/")
112114
return repo, ts
@@ -116,24 +118,17 @@ func TestGithubGetInfo(t *testing.T) {
116118
repo, ts := getNewGithubTestRepo(t)
117119
defer ts.Close()
118120
defaultBranch, isPrivate, err := repo.GetInfo()
119-
if err != nil {
120-
t.Fatal(err)
121-
}
122-
if defaultBranch != GITHUB_DEFAULTBRANCH || !isPrivate {
123-
t.Fatal("invalid response")
124-
}
121+
require.NoError(t, err)
122+
require.Equal(t, defaultBranch, GITLAB_DEFAULTBRANCH)
123+
require.True(t, isPrivate)
125124
}
126125

127126
func TestGithubGetCommits(t *testing.T) {
128127
repo, ts := getNewGithubTestRepo(t)
129128
defer ts.Close()
130129
commits, err := repo.GetCommits("")
131-
if err != nil {
132-
t.Fatal(err)
133-
}
134-
if len(commits) != 4 {
135-
t.Fatal("invalid response")
136-
}
130+
require.NoError(t, err)
131+
require.Len(t, commits, 4)
137132

138133
if !compareCommit(commits[0], "feat", "app", Change{false, true, false}) ||
139134
!compareCommit(commits[1], "fix", "", Change{false, false, true}) ||
@@ -146,54 +141,34 @@ func TestGithubGetCommits(t *testing.T) {
146141
func TestGithubGetLatestRelease(t *testing.T) {
147142
repo, ts := getNewGithubTestRepo(t)
148143
defer ts.Close()
149-
release, err := repo.GetLatestRelease("", nil)
150-
if err != nil {
151-
t.Fatal(err)
152-
}
153-
if release.SHA != "deadbeef" || release.Version.String() != "2020.4.19" {
154-
t.Fatal("invalid tag")
155-
}
156-
157-
re := regexp.MustCompile("^v[0-9]*")
158-
release, err = repo.GetLatestRelease("", re)
159-
if err != nil {
160-
t.Fatal(err)
161-
}
162-
if release.SHA != "deadbeef" || release.Version.String() != "2.0.0" {
163-
t.Fatal("invalid tag")
164-
}
165-
166-
release, err = repo.GetLatestRelease("2-beta", nil)
167-
if err != nil {
168-
t.Fatal(err)
169-
}
170-
if release.SHA != "deadbeef" || release.Version.String() != "2.1.0-beta" {
171-
t.Fatal("invalid tag")
172-
}
173-
174-
release, err = repo.GetLatestRelease("3-beta", nil)
175-
if err != nil {
176-
t.Fatal(err)
177-
}
178-
if release.SHA != "deadbeef" || release.Version.String() != "3.0.0-beta.2" {
179-
t.Fatal("invalid tag")
180-
}
181144

182-
release, err = repo.GetLatestRelease("4-beta", nil)
183-
if err != nil {
184-
t.Fatal(err)
185-
}
186-
if release.SHA != "deadbeef" || release.Version.String() != "4.0.0-beta" {
187-
t.Fatal("invalid tag")
145+
testCases := []struct {
146+
vrange string
147+
re *regexp.Regexp
148+
expectedSHA string
149+
expectedVersion string
150+
}{
151+
{"", nil, "deadbeef", "2020.4.19"},
152+
{"", regexp.MustCompile("^v[0-9]*"), "deadbeef", "2.0.0"},
153+
{"2-beta", nil, "deadbeef", "2.1.0-beta"},
154+
{"3-beta", nil, "deadbeef", "3.0.0-beta.2"},
155+
{"4-beta", nil, "deadbeef", "4.0.0-beta"},
156+
}
157+
158+
for _, tc := range testCases {
159+
t.Run(fmt.Sprintf("VersionRange: %s, RE: %s", tc.vrange, tc.re), func(t *testing.T) {
160+
release, err := repo.GetLatestRelease(tc.vrange, tc.re)
161+
require.NoError(t, err)
162+
require.Equal(t, tc.expectedSHA, release.SHA)
163+
require.Equal(t, tc.expectedVersion, release.Version.String())
164+
})
188165
}
189166
}
190167

191168
func TestGithubCreateRelease(t *testing.T) {
192169
repo, ts := getNewGithubTestRepo(t)
193170
defer ts.Close()
194-
newVersion, _ := semver.NewVersion("2.0.0")
171+
newVersion := semver.MustParse("2.0.0")
195172
err := repo.CreateRelease("", newVersion, false, "", "deadbeef")
196-
if err != nil {
197-
t.Fatal(err)
198-
}
173+
require.NoError(t, err)
199174
}

0 commit comments

Comments
 (0)