Skip to content

Commit 2a02734

Browse files
authored
Refactor older tests to use testify (#33140)
Refactor checks to use assert/require Use require.Eventually for waiting in elastic and meilisearch tests Use require to exit early instead of assert
1 parent fa9191b commit 2a02734

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+218
-348
lines changed

models/asymkey/gpg_key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/keybase/go-crypto/openpgp/packet"
1717
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1819
)
1920

2021
func TestCheckArmoredGPGKeyString(t *testing.T) {
@@ -107,9 +108,8 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
107108
=i9b7
108109
-----END PGP PUBLIC KEY BLOCK-----`
109110
keys, err := checkArmoredGPGKeyString(testGPGArmor)
110-
if !assert.NotEmpty(t, keys) {
111-
return
112-
}
111+
require.NotEmpty(t, keys)
112+
113113
ekey := keys[0]
114114
assert.NoError(t, err, "Could not parse a valid GPG armored key", ekey)
115115

models/db/engine_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys
1616

1717
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1819
)
1920

2021
func TestDumpDatabase(t *testing.T) {
@@ -62,9 +63,7 @@ func TestPrimaryKeys(t *testing.T) {
6263
// Import "code.gitea.io/gitea/cmd" to make sure each db.RegisterModel in init functions has been called.
6364

6465
beans, err := db.NamesToBean()
65-
if err != nil {
66-
t.Fatal(err)
67-
}
66+
require.NoError(t, err)
6867

6968
whitelist := map[string]string{
7069
"the_table_name_to_skip_checking": "Write a note here to explain why",
@@ -79,8 +78,6 @@ func TestPrimaryKeys(t *testing.T) {
7978
t.Logf("ignore %q because %q", table.Name, why)
8079
continue
8180
}
82-
if len(table.PrimaryKeys) == 0 {
83-
t.Errorf("table %q has no primary key", table.Name)
84-
}
81+
assert.NotEmpty(t, table.PrimaryKeys, "table %q has no primary key", table.Name)
8582
}
8683
}

models/organization/org_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/structs"
1717

1818
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1920
)
2021

2122
func TestUser_IsOwnedBy(t *testing.T) {
@@ -180,9 +181,8 @@ func TestRestrictedUserOrgMembers(t *testing.T) {
180181
ID: 29,
181182
IsRestricted: true,
182183
})
183-
if !assert.True(t, restrictedUser.IsRestricted) {
184-
return // ensure fixtures return restricted user
185-
}
184+
// ensure fixtures return restricted user
185+
require.True(t, restrictedUser.IsRestricted)
186186

187187
testCases := []struct {
188188
name string

models/user/openid_test.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
user_model "code.gitea.io/gitea/models/user"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestGetUserOpenIDs(t *testing.T) {
@@ -34,30 +35,23 @@ func TestGetUserOpenIDs(t *testing.T) {
3435
func TestToggleUserOpenIDVisibility(t *testing.T) {
3536
assert.NoError(t, unittest.PrepareTestDatabase())
3637
oids, err := user_model.GetUserOpenIDs(db.DefaultContext, int64(2))
37-
if !assert.NoError(t, err) || !assert.Len(t, oids, 1) {
38-
return
39-
}
38+
require.NoError(t, err)
39+
require.Len(t, oids, 1)
4040
assert.True(t, oids[0].Show)
4141

4242
err = user_model.ToggleUserOpenIDVisibility(db.DefaultContext, oids[0].ID)
43-
if !assert.NoError(t, err) {
44-
return
45-
}
43+
require.NoError(t, err)
4644

4745
oids, err = user_model.GetUserOpenIDs(db.DefaultContext, int64(2))
48-
if !assert.NoError(t, err) || !assert.Len(t, oids, 1) {
49-
return
50-
}
46+
require.NoError(t, err)
47+
require.Len(t, oids, 1)
48+
5149
assert.False(t, oids[0].Show)
5250
err = user_model.ToggleUserOpenIDVisibility(db.DefaultContext, oids[0].ID)
53-
if !assert.NoError(t, err) {
54-
return
55-
}
51+
require.NoError(t, err)
5652

5753
oids, err = user_model.GetUserOpenIDs(db.DefaultContext, int64(2))
58-
if !assert.NoError(t, err) {
59-
return
60-
}
54+
require.NoError(t, err)
6155
if assert.Len(t, oids, 1) {
6256
assert.True(t, oids[0].Show)
6357
}

modules/analyze/vendor_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
package analyze
55

6-
import "testing"
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
711

812
func TestIsVendor(t *testing.T) {
913
tests := []struct {
@@ -33,9 +37,8 @@ func TestIsVendor(t *testing.T) {
3337
}
3438
for _, tt := range tests {
3539
t.Run(tt.path, func(t *testing.T) {
36-
if got := IsVendor(tt.path); got != tt.want {
37-
t.Errorf("IsVendor() = %v, want %v", got, tt.want)
38-
}
40+
got := IsVendor(tt.path)
41+
assert.Equal(t, tt.want, got)
3942
})
4043
}
4144
}

modules/auth/openid/discovery_cache_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package openid
66
import (
77
"testing"
88
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
912
)
1013

1114
type testDiscoveredInfo struct{}
@@ -29,21 +32,17 @@ func TestTimedDiscoveryCache(t *testing.T) {
2932
dc.Put("foo", &testDiscoveredInfo{}) // openid.opEndpoint: "a", openid.opLocalID: "b", openid.claimedID: "c"})
3033

3134
// Make sure we can retrieve them
32-
if di := dc.Get("foo"); di == nil {
33-
t.Errorf("Expected a result, got nil")
34-
} else if di.OpEndpoint() != "opEndpoint" || di.OpLocalID() != "opLocalID" || di.ClaimedID() != "claimedID" {
35-
t.Errorf("Expected opEndpoint opLocalID claimedID, got %v %v %v", di.OpEndpoint(), di.OpLocalID(), di.ClaimedID())
36-
}
35+
di := dc.Get("foo")
36+
require.NotNil(t, di)
37+
assert.Equal(t, "opEndpoint", di.OpEndpoint())
38+
assert.Equal(t, "opLocalID", di.OpLocalID())
39+
assert.Equal(t, "claimedID", di.ClaimedID())
3740

3841
// Attempt to get a non-existent value
39-
if di := dc.Get("bar"); di != nil {
40-
t.Errorf("Expected nil, got %v", di)
41-
}
42+
assert.Nil(t, dc.Get("bar"))
4243

4344
// Sleep one second and try retrieve again
4445
time.Sleep(1 * time.Second)
4546

46-
if di := dc.Get("foo"); di != nil {
47-
t.Errorf("Expected a nil, got a result")
48-
}
47+
assert.Nil(t, dc.Get("foo"))
4948
}

modules/emoji/emoji_test.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package emoji
66

77
import (
8-
"reflect"
98
"testing"
109

1110
"github.com/stretchr/testify/assert"
@@ -22,32 +21,18 @@ func TestLookup(t *testing.T) {
2221
c := FromAlias(":beer:")
2322
d := FromAlias("beer")
2423

25-
if !reflect.DeepEqual(a, b) {
26-
t.Errorf("a and b should equal")
27-
}
28-
if !reflect.DeepEqual(b, c) {
29-
t.Errorf("b and c should equal")
30-
}
31-
if !reflect.DeepEqual(c, d) {
32-
t.Errorf("c and d should equal")
33-
}
34-
if !reflect.DeepEqual(a, d) {
35-
t.Errorf("a and d should equal")
36-
}
24+
assert.Equal(t, a, b)
25+
assert.Equal(t, b, c)
26+
assert.Equal(t, c, d)
27+
assert.Equal(t, a, d)
3728

3829
m := FromCode("\U0001f44d")
3930
n := FromAlias(":thumbsup:")
4031
o := FromAlias("+1")
4132

42-
if !reflect.DeepEqual(m, n) {
43-
t.Errorf("m and n should equal")
44-
}
45-
if !reflect.DeepEqual(n, o) {
46-
t.Errorf("n and o should equal")
47-
}
48-
if !reflect.DeepEqual(m, o) {
49-
t.Errorf("m and o should equal")
50-
}
33+
assert.Equal(t, m, n)
34+
assert.Equal(t, m, o)
35+
assert.Equal(t, n, o)
5136
}
5237

5338
func TestReplacers(t *testing.T) {
@@ -61,9 +46,7 @@ func TestReplacers(t *testing.T) {
6146

6247
for i, x := range tests {
6348
s := x.f(x.v)
64-
if s != x.exp {
65-
t.Errorf("test %d `%s` expected `%s`, got: `%s`", i, x.v, x.exp, s)
66-
}
49+
assert.Equalf(t, x.exp, s, "test %d `%s` expected `%s`, got: `%s`", i, x.v, x.exp, s)
6750
}
6851
}
6952

modules/eventsource/event_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package eventsource
66
import (
77
"bytes"
88
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
912
)
1013

1114
func Test_wrapNewlines(t *testing.T) {
@@ -38,16 +41,10 @@ func Test_wrapNewlines(t *testing.T) {
3841
t.Run(tt.name, func(t *testing.T) {
3942
w := &bytes.Buffer{}
4043
gotSum, err := wrapNewlines(w, []byte(tt.prefix), []byte(tt.value))
41-
if err != nil {
42-
t.Errorf("wrapNewlines() error = %v", err)
43-
return
44-
}
45-
if gotSum != int64(len(tt.output)) {
46-
t.Errorf("wrapNewlines() = %v, want %v", gotSum, int64(len(tt.output)))
47-
}
48-
if gotW := w.String(); gotW != tt.output {
49-
t.Errorf("wrapNewlines() = %v, want %v", gotW, tt.output)
50-
}
44+
require.NoError(t, err)
45+
46+
assert.EqualValues(t, len(tt.output), gotSum)
47+
assert.Equal(t, tt.output, w.String())
5148
})
5249
}
5350
}

modules/git/blob_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ func TestBlob_Data(t *testing.T) {
1717
output := "file2\n"
1818
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
1919
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
20-
if !assert.NoError(t, err) {
21-
t.Fatal()
22-
}
20+
require.NoError(t, err)
2321
defer repo.Close()
2422

2523
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")

modules/git/commit_sha256_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestCommitsCountSha256(t *testing.T) {
@@ -94,9 +95,7 @@ signed commit`
9495

9596
commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString))
9697
assert.NoError(t, err)
97-
if !assert.NotNil(t, commitFromReader) {
98-
return
99-
}
98+
require.NotNil(t, commitFromReader)
10099
assert.EqualValues(t, sha, commitFromReader.ID)
101100
assert.EqualValues(t, `-----BEGIN PGP SIGNATURE-----
102101

modules/git/commit_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestCommitsCount(t *testing.T) {
@@ -91,9 +92,7 @@ empty commit`
9192

9293
commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString))
9394
assert.NoError(t, err)
94-
if !assert.NotNil(t, commitFromReader) {
95-
return
96-
}
95+
require.NotNil(t, commitFromReader)
9796
assert.EqualValues(t, sha, commitFromReader.ID)
9897
assert.EqualValues(t, `-----BEGIN PGP SIGNATURE-----
9998
@@ -159,9 +158,7 @@ ISO-8859-1`
159158

160159
commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString))
161160
assert.NoError(t, err)
162-
if !assert.NotNil(t, commitFromReader) {
163-
return
164-
}
161+
require.NotNil(t, commitFromReader)
165162
assert.EqualValues(t, sha, commitFromReader.ID)
166163
assert.EqualValues(t, `-----BEGIN PGP SIGNATURE-----
167164

modules/git/repo_language_stats_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@ import (
1010
"testing"
1111

1212
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1314
)
1415

1516
func TestRepository_GetLanguageStats(t *testing.T) {
1617
repoPath := filepath.Join(testReposDir, "language_stats_repo")
1718
gitRepo, err := openRepositoryWithDefaultContext(repoPath)
18-
if !assert.NoError(t, err) {
19-
t.Fatal()
20-
}
19+
require.NoError(t, err)
20+
2121
defer gitRepo.Close()
2222

2323
stats, err := gitRepo.GetLanguageStats("8fee858da5796dfb37704761701bb8e800ad9ef3")
24-
if !assert.NoError(t, err) {
25-
t.Fatal()
26-
}
24+
require.NoError(t, err)
2725

2826
assert.EqualValues(t, map[string]int64{
2927
"Python": 134,

modules/git/repo_tag_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ func TestRepository_GetAnnotatedTag(t *testing.T) {
182182

183183
// Annotated tag's name should fail
184184
tag3, err := bareRepo1.GetAnnotatedTag(aTagName)
185-
assert.Error(t, err)
186185
assert.Errorf(t, err, "Length must be 40: %d", len(aTagName))
187186
assert.Nil(t, tag3)
188187

modules/gitgraph/graph_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"testing"
1111

1212
"code.gitea.io/gitea/modules/git"
13+
14+
"github.com/stretchr/testify/assert"
1315
)
1416

1517
func BenchmarkGetCommitGraph(b *testing.B) {
@@ -235,9 +237,7 @@ func TestParseGlyphs(t *testing.T) {
235237
}
236238
row++
237239
}
238-
if len(parser.availableColors) != 9 {
239-
t.Errorf("Expected 9 colors but have %d", len(parser.availableColors))
240-
}
240+
assert.Len(t, parser.availableColors, 9)
241241
}
242242

243243
func TestCommitStringParsing(t *testing.T) {
@@ -262,9 +262,7 @@ func TestCommitStringParsing(t *testing.T) {
262262
return
263263
}
264264

265-
if test.commitMessage != commit.Subject {
266-
t.Errorf("%s does not match %s", test.commitMessage, commit.Subject)
267-
}
265+
assert.Equal(t, test.commitMessage, commit.Subject)
268266
})
269267
}
270268
}

0 commit comments

Comments
 (0)