Skip to content

Refactor older tests to use testify #33140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions models/asymkey/gpg_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/keybase/go-crypto/openpgp/packet"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCheckArmoredGPGKeyString(t *testing.T) {
Expand Down Expand Up @@ -107,9 +108,8 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
=i9b7
-----END PGP PUBLIC KEY BLOCK-----`
keys, err := checkArmoredGPGKeyString(testGPGArmor)
if !assert.NotEmpty(t, keys) {
return
}
require.NotEmpty(t, keys)

ekey := keys[0]
assert.NoError(t, err, "Could not parse a valid GPG armored key", ekey)

Expand Down
9 changes: 3 additions & 6 deletions models/db/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

beans, err := db.NamesToBean()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

whitelist := map[string]string{
"the_table_name_to_skip_checking": "Write a note here to explain why",
Expand All @@ -79,8 +78,6 @@ func TestPrimaryKeys(t *testing.T) {
t.Logf("ignore %q because %q", table.Name, why)
continue
}
if len(table.PrimaryKeys) == 0 {
t.Errorf("table %q has no primary key", table.Name)
}
assert.NotEmpty(t, table.PrimaryKeys, "table %q has no primary key", table.Name)
}
}
6 changes: 3 additions & 3 deletions models/organization/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUser_IsOwnedBy(t *testing.T) {
Expand Down Expand Up @@ -180,9 +181,8 @@ func TestRestrictedUserOrgMembers(t *testing.T) {
ID: 29,
IsRestricted: true,
})
if !assert.True(t, restrictedUser.IsRestricted) {
return // ensure fixtures return restricted user
}
// ensure fixtures return restricted user
require.True(t, restrictedUser.IsRestricted)

testCases := []struct {
name string
Expand Down
24 changes: 9 additions & 15 deletions models/user/openid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
user_model "code.gitea.io/gitea/models/user"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

err = user_model.ToggleUserOpenIDVisibility(db.DefaultContext, oids[0].ID)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

oids, err = user_model.GetUserOpenIDs(db.DefaultContext, int64(2))
if !assert.NoError(t, err) || !assert.Len(t, oids, 1) {
return
}
require.NoError(t, err)
require.Len(t, oids, 1)

assert.False(t, oids[0].Show)
err = user_model.ToggleUserOpenIDVisibility(db.DefaultContext, oids[0].ID)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

oids, err = user_model.GetUserOpenIDs(db.DefaultContext, int64(2))
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
if assert.Len(t, oids, 1) {
assert.True(t, oids[0].Show)
}
Expand Down
11 changes: 7 additions & 4 deletions modules/analyze/vendor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package analyze

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsVendor(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -33,9 +37,8 @@ func TestIsVendor(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
if got := IsVendor(tt.path); got != tt.want {
t.Errorf("IsVendor() = %v, want %v", got, tt.want)
}
got := IsVendor(tt.path)
assert.Equal(t, tt.want, got)
})
}
}
21 changes: 10 additions & 11 deletions modules/auth/openid/discovery_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package openid
import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

// Make sure we can retrieve them
if di := dc.Get("foo"); di == nil {
t.Errorf("Expected a result, got nil")
} else if di.OpEndpoint() != "opEndpoint" || di.OpLocalID() != "opLocalID" || di.ClaimedID() != "claimedID" {
t.Errorf("Expected opEndpoint opLocalID claimedID, got %v %v %v", di.OpEndpoint(), di.OpLocalID(), di.ClaimedID())
}
di := dc.Get("foo")
require.NotNil(t, di)
assert.Equal(t, "opEndpoint", di.OpEndpoint())
assert.Equal(t, "opLocalID", di.OpLocalID())
assert.Equal(t, "claimedID", di.ClaimedID())

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

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

if di := dc.Get("foo"); di != nil {
t.Errorf("Expected a nil, got a result")
}
assert.Nil(t, dc.Get("foo"))
}
33 changes: 8 additions & 25 deletions modules/emoji/emoji_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package emoji

import (
"reflect"
"testing"

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

if !reflect.DeepEqual(a, b) {
t.Errorf("a and b should equal")
}
if !reflect.DeepEqual(b, c) {
t.Errorf("b and c should equal")
}
if !reflect.DeepEqual(c, d) {
t.Errorf("c and d should equal")
}
if !reflect.DeepEqual(a, d) {
t.Errorf("a and d should equal")
}
assert.Equal(t, a, b)
assert.Equal(t, b, c)
assert.Equal(t, c, d)
assert.Equal(t, a, d)

m := FromCode("\U0001f44d")
n := FromAlias(":thumbsup:")
o := FromAlias("+1")

if !reflect.DeepEqual(m, n) {
t.Errorf("m and n should equal")
}
if !reflect.DeepEqual(n, o) {
t.Errorf("n and o should equal")
}
if !reflect.DeepEqual(m, o) {
t.Errorf("m and o should equal")
}
assert.Equal(t, m, n)
assert.Equal(t, m, o)
assert.Equal(t, n, o)
}

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

for i, x := range tests {
s := x.f(x.v)
if s != x.exp {
t.Errorf("test %d `%s` expected `%s`, got: `%s`", i, x.v, x.exp, s)
}
assert.Equalf(t, x.exp, s, "test %d `%s` expected `%s`, got: `%s`", i, x.v, x.exp, s)
}
}

Expand Down
17 changes: 7 additions & 10 deletions modules/eventsource/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package eventsource
import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_wrapNewlines(t *testing.T) {
Expand Down Expand Up @@ -38,16 +41,10 @@ func Test_wrapNewlines(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
gotSum, err := wrapNewlines(w, []byte(tt.prefix), []byte(tt.value))
if err != nil {
t.Errorf("wrapNewlines() error = %v", err)
return
}
if gotSum != int64(len(tt.output)) {
t.Errorf("wrapNewlines() = %v, want %v", gotSum, int64(len(tt.output)))
}
if gotW := w.String(); gotW != tt.output {
t.Errorf("wrapNewlines() = %v, want %v", gotW, tt.output)
}
require.NoError(t, err)

assert.EqualValues(t, len(tt.output), gotSum)
assert.Equal(t, tt.output, w.String())
})
}
}
4 changes: 1 addition & 3 deletions modules/git/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ func TestBlob_Data(t *testing.T) {
output := "file2\n"
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
if !assert.NoError(t, err) {
t.Fatal()
}
require.NoError(t, err)
defer repo.Close()

testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
Expand Down
5 changes: 2 additions & 3 deletions modules/git/commit_sha256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString))
assert.NoError(t, err)
if !assert.NotNil(t, commitFromReader) {
return
}
require.NotNil(t, commitFromReader)
assert.EqualValues(t, sha, commitFromReader.ID)
assert.EqualValues(t, `-----BEGIN PGP SIGNATURE-----

Expand Down
9 changes: 3 additions & 6 deletions modules/git/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString))
assert.NoError(t, err)
if !assert.NotNil(t, commitFromReader) {
return
}
require.NotNil(t, commitFromReader)
assert.EqualValues(t, sha, commitFromReader.ID)
assert.EqualValues(t, `-----BEGIN PGP SIGNATURE-----

Expand Down Expand Up @@ -159,9 +158,7 @@ ISO-8859-1`

commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString))
assert.NoError(t, err)
if !assert.NotNil(t, commitFromReader) {
return
}
require.NotNil(t, commitFromReader)
assert.EqualValues(t, sha, commitFromReader.ID)
assert.EqualValues(t, `-----BEGIN PGP SIGNATURE-----

Expand Down
10 changes: 4 additions & 6 deletions modules/git/repo_language_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRepository_GetLanguageStats(t *testing.T) {
repoPath := filepath.Join(testReposDir, "language_stats_repo")
gitRepo, err := openRepositoryWithDefaultContext(repoPath)
if !assert.NoError(t, err) {
t.Fatal()
}
require.NoError(t, err)

defer gitRepo.Close()

stats, err := gitRepo.GetLanguageStats("8fee858da5796dfb37704761701bb8e800ad9ef3")
if !assert.NoError(t, err) {
t.Fatal()
}
require.NoError(t, err)

assert.EqualValues(t, map[string]int64{
"Python": 134,
Expand Down
1 change: 0 additions & 1 deletion modules/git/repo_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func TestRepository_GetAnnotatedTag(t *testing.T) {

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

Expand Down
10 changes: 4 additions & 6 deletions modules/gitgraph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"testing"

"code.gitea.io/gitea/modules/git"

"github.com/stretchr/testify/assert"
)

func BenchmarkGetCommitGraph(b *testing.B) {
Expand Down Expand Up @@ -235,9 +237,7 @@ func TestParseGlyphs(t *testing.T) {
}
row++
}
if len(parser.availableColors) != 9 {
t.Errorf("Expected 9 colors but have %d", len(parser.availableColors))
}
assert.Len(t, parser.availableColors, 9)
}

func TestCommitStringParsing(t *testing.T) {
Expand All @@ -262,9 +262,7 @@ func TestCommitStringParsing(t *testing.T) {
return
}

if test.commitMessage != commit.Subject {
t.Errorf("%s does not match %s", test.commitMessage, commit.Subject)
}
assert.Equal(t, test.commitMessage, commit.Subject)
})
}
}
Expand Down
Loading
Loading