Skip to content

Commit b20938c

Browse files
committed
refactor(tests): use testify where possible
1 parent 1a95d9d commit b20938c

File tree

20 files changed

+108
-163
lines changed

20 files changed

+108
-163
lines changed

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
}

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/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
}

modules/indexer/issues/elasticsearch/elasticsearch_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"time"
1212

1313
"code.gitea.io/gitea/modules/indexer/issues/internal/tests"
14+
15+
"github.com/stretchr/testify/require"
1416
)
1517

1618
func TestElasticsearchIndexer(t *testing.T) {
@@ -26,20 +28,10 @@ func TestElasticsearchIndexer(t *testing.T) {
2628
}
2729
}
2830

29-
ok := false
30-
for i := 0; i < 60; i++ {
31+
require.Eventually(t, func() bool {
3132
resp, err := http.Get(url)
32-
if err == nil && resp.StatusCode == http.StatusOK {
33-
ok = true
34-
break
35-
}
36-
t.Logf("Waiting for elasticsearch to be up: %v", err)
37-
time.Sleep(time.Second)
38-
}
39-
if !ok {
40-
t.Fatalf("Failed to wait for elasticsearch to be up")
41-
return
42-
}
33+
return err == nil && resp.StatusCode == http.StatusOK
34+
}, time.Minute, time.Second, "Expected elasticsearch to be up")
4335

4436
indexer := NewIndexer(url, fmt.Sprintf("test_elasticsearch_indexer_%d", time.Now().Unix()))
4537
defer indexer.Close()

modules/indexer/issues/meilisearch/meilisearch_test.go

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

1616
"github.com/meilisearch/meilisearch-go"
1717
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1819
)
1920

2021
func TestMeilisearchIndexer(t *testing.T) {
@@ -32,20 +33,10 @@ func TestMeilisearchIndexer(t *testing.T) {
3233
key = os.Getenv("TEST_MEILISEARCH_KEY")
3334
}
3435

35-
ok := false
36-
for i := 0; i < 60; i++ {
36+
require.Eventually(t, func() bool {
3737
resp, err := http.Get(url)
38-
if err == nil && resp.StatusCode == http.StatusOK {
39-
ok = true
40-
break
41-
}
42-
t.Logf("Waiting for meilisearch to be up: %v", err)
43-
time.Sleep(time.Second)
44-
}
45-
if !ok {
46-
t.Fatalf("Failed to wait for meilisearch to be up")
47-
return
48-
}
38+
return err == nil && resp.StatusCode == http.StatusOK
39+
}, time.Minute, time.Second, "Expected meilisearch to be up")
4940

5041
indexer := NewIndexer(url, key, fmt.Sprintf("test_meilisearch_indexer_%d", time.Now().Unix()))
5142
defer indexer.Close()

modules/issue/template/template_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,8 @@ func Test_minQuotes(t *testing.T) {
957957
}
958958
for _, tt := range tests {
959959
t.Run(tt.name, func(t *testing.T) {
960-
if got := minQuotes(tt.args.value); got != tt.want {
961-
t.Errorf("minQuotes() = %v, want %v", got, tt.want)
962-
}
960+
got := minQuotes(tt.args.value)
961+
assert.Equal(t, tt.want, got)
963962
})
964963
}
965964
}

modules/markup/markdown/renderconfig_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1012
"gopkg.in/yaml.v3"
1113
)
1214

@@ -140,23 +142,13 @@ func TestRenderConfig_UnmarshalYAML(t *testing.T) {
140142
Icon: "table",
141143
Lang: "",
142144
}
143-
if err := yaml.Unmarshal([]byte(strings.ReplaceAll(tt.args, "\t", " ")), got); err != nil {
144-
t.Errorf("RenderConfig.UnmarshalYAML() error = %v\n%q", err, tt.args)
145-
return
146-
}
145+
err := yaml.Unmarshal([]byte(strings.ReplaceAll(tt.args, "\t", " ")), got)
146+
require.NoError(t, err)
147147

148-
if got.Meta != tt.expected.Meta {
149-
t.Errorf("Meta Expected %s Got %s", tt.expected.Meta, got.Meta)
150-
}
151-
if got.Icon != tt.expected.Icon {
152-
t.Errorf("Icon Expected %s Got %s", tt.expected.Icon, got.Icon)
153-
}
154-
if got.Lang != tt.expected.Lang {
155-
t.Errorf("Lang Expected %s Got %s", tt.expected.Lang, got.Lang)
156-
}
157-
if got.TOC != tt.expected.TOC {
158-
t.Errorf("TOC Expected %q Got %q", tt.expected.TOC, got.TOC)
159-
}
148+
assert.Equal(t, tt.expected.Meta, got.Meta)
149+
assert.Equal(t, tt.expected.Icon, got.Icon)
150+
assert.Equal(t, tt.expected.Lang, got.Lang)
151+
assert.Equal(t, tt.expected.TOC, got.TOC)
160152
})
161153
}
162154
}

modules/nosql/redis_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package nosql
55

66
import (
77
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
811
)
912

1013
func TestToRedisURI(t *testing.T) {
@@ -26,9 +29,9 @@ func TestToRedisURI(t *testing.T) {
2629
}
2730
for _, tt := range tests {
2831
t.Run(tt.name, func(t *testing.T) {
29-
if got := ToRedisURI(tt.connection); got == nil || got.String() != tt.want {
30-
t.Errorf(`ToRedisURI(%q) = %s, want %s`, tt.connection, got.String(), tt.want)
31-
}
32+
got := ToRedisURI(tt.connection)
33+
require.NotNil(t, got)
34+
assert.Equal(t, tt.want, got.String())
3235
})
3336
}
3437
}

modules/structs/commit_status_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package structs
55

66
import (
77
"testing"
8+
9+
"github.com/stretchr/testify/assert"
810
)
911

1012
func TestNoBetterThan(t *testing.T) {
@@ -166,9 +168,7 @@ func TestNoBetterThan(t *testing.T) {
166168
for _, tt := range tests {
167169
t.Run(tt.name, func(t *testing.T) {
168170
result := tt.args.css.NoBetterThan(tt.args.css2)
169-
if result != tt.want {
170-
t.Errorf("NoBetterThan() = %v, want %v", result, tt.want)
171-
}
171+
assert.Equal(t, tt.want, result)
172172
})
173173
}
174174
}

0 commit comments

Comments
 (0)