Skip to content

Commit dc04044

Browse files
authored
Replace assert.Fail with assert.FailNow (#27578)
assert.Fail() will continue to execute the code while assert.FailNow() not. I thought those uses of assert.Fail() should exit immediately. PS: perhaps it's a good idea to use [require](https://pkg.go.dev/github.com/stretchr/testify/require) somewhere because the assert package's default behavior does not exit when an error occurs, which makes it difficult to find the root error reason.
1 parent dca195e commit dc04044

File tree

14 files changed

+20
-38
lines changed

14 files changed

+20
-38
lines changed

models/asymkey/ssh_key_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Test_SSHParsePublicKey(t *testing.T) {
5151
if err != nil {
5252
// Some servers do not support ecdsa format.
5353
if !strings.Contains(err.Error(), "line 1 too long:") {
54-
assert.Fail(t, "%v", err)
54+
assert.FailNow(t, "%v", err)
5555
}
5656
}
5757
assert.Equal(t, tc.keyType, keyTypeK)
@@ -60,7 +60,7 @@ func Test_SSHParsePublicKey(t *testing.T) {
6060
t.Run("SSHParseKeyNative", func(t *testing.T) {
6161
keyTypeK, lengthK, err := SSHNativeParsePublicKey(tc.content)
6262
if err != nil {
63-
assert.Fail(t, "%v", err)
63+
assert.FailNow(t, "%v", err)
6464
}
6565
assert.Equal(t, tc.keyType, keyTypeK)
6666
assert.EqualValues(t, tc.length, lengthK)

models/unittest/consistency.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ func checkForConsistency(t assert.TestingT, bean any) {
4747
assert.NoError(t, err)
4848
f := consistencyCheckMap[tb.Name]
4949
if f == nil {
50-
assert.Fail(t, "unknown bean type: %#v", bean)
51-
return
50+
assert.FailNow(t, "unknown bean type: %#v", bean)
5251
}
5352
f(t, bean)
5453
}

modules/contexttest/context_tests.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ func LoadRepo(t *testing.T, ctx gocontext.Context, repoID int64) {
8383
ctx.Repo = repo
8484
doer = ctx.Doer
8585
default:
86-
assert.Fail(t, "context is not *context.Context or *context.APIContext")
87-
return
86+
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
8887
}
8988

9089
repo.Repository = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
@@ -105,8 +104,7 @@ func LoadRepoCommit(t *testing.T, ctx gocontext.Context) {
105104
case *context.APIContext:
106105
repo = ctx.Repo
107106
default:
108-
assert.Fail(t, "context is not *context.Context or *context.APIContext")
109-
return
107+
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
110108
}
111109

112110
gitRepo, err := git.OpenRepository(ctx, repo.Repository.RepoPath())
@@ -130,8 +128,7 @@ func LoadUser(t *testing.T, ctx gocontext.Context, userID int64) {
130128
case *context.APIContext:
131129
ctx.Doer = doer
132130
default:
133-
assert.Fail(t, "context is not *context.Context or *context.APIContext")
134-
return
131+
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
135132
}
136133
}
137134

modules/git/repo_attribute_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_nulSeparatedAttributeWriter_ReadAttribute(t *testing.T) {
2727
assert.Equal(t, "linguist-vendored", attr.Attribute)
2828
assert.Equal(t, "unspecified", attr.Value)
2929
case <-time.After(100 * time.Millisecond):
30-
assert.Fail(t, "took too long to read an attribute from the list")
30+
assert.FailNow(t, "took too long to read an attribute from the list")
3131
}
3232
// Write a second attribute again
3333
n, err = wr.Write([]byte(testStr))
@@ -41,7 +41,7 @@ func Test_nulSeparatedAttributeWriter_ReadAttribute(t *testing.T) {
4141
assert.Equal(t, "linguist-vendored", attr.Attribute)
4242
assert.Equal(t, "unspecified", attr.Value)
4343
case <-time.After(100 * time.Millisecond):
44-
assert.Fail(t, "took too long to read an attribute from the list")
44+
assert.FailNow(t, "took too long to read an attribute from the list")
4545
}
4646

4747
// Write a partial attribute
@@ -52,14 +52,14 @@ func Test_nulSeparatedAttributeWriter_ReadAttribute(t *testing.T) {
5252

5353
select {
5454
case <-wr.ReadAttribute():
55-
assert.Fail(t, "There should not be an attribute ready to read")
55+
assert.FailNow(t, "There should not be an attribute ready to read")
5656
case <-time.After(100 * time.Millisecond):
5757
}
5858
_, err = wr.Write([]byte("attribute\x00"))
5959
assert.NoError(t, err)
6060
select {
6161
case <-wr.ReadAttribute():
62-
assert.Fail(t, "There should not be an attribute ready to read")
62+
assert.FailNow(t, "There should not be an attribute ready to read")
6363
case <-time.After(100 * time.Millisecond):
6464
}
6565

modules/git/repo_tag_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func TestRepository_GetTag(t *testing.T) {
7171
if lTag == nil {
7272
assert.NotNil(t, lTag)
7373
assert.FailNow(t, "nil lTag: %s", lTagName)
74-
return
7574
}
7675
assert.EqualValues(t, lTagName, lTag.Name)
7776
assert.EqualValues(t, lTagCommitID, lTag.ID.String())
@@ -105,7 +104,6 @@ func TestRepository_GetTag(t *testing.T) {
105104
if aTag == nil {
106105
assert.NotNil(t, aTag)
107106
assert.FailNow(t, "nil aTag: %s", aTagName)
108-
return
109107
}
110108
assert.EqualValues(t, aTagName, aTag.Name)
111109
assert.EqualValues(t, aTagID, aTag.ID.String())

modules/indexer/code/indexer_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ func TestBleveIndexAndSearch(t *testing.T) {
9696
idx := bleve.NewIndexer(dir)
9797
_, err := idx.Init(context.Background())
9898
if err != nil {
99-
assert.Fail(t, "Unable to create bleve indexer Error: %v", err)
10099
if idx != nil {
101100
idx.Close()
102101
}
103-
return
102+
assert.FailNow(t, "Unable to create bleve indexer Error: %v", err)
104103
}
105104
defer idx.Close()
106105

@@ -118,11 +117,10 @@ func TestESIndexAndSearch(t *testing.T) {
118117

119118
indexer := elasticsearch.NewIndexer(u, "gitea_codes")
120119
if _, err := indexer.Init(context.Background()); err != nil {
121-
assert.Fail(t, "Unable to init ES indexer Error: %v", err)
122120
if indexer != nil {
123121
indexer.Close()
124122
}
125-
return
123+
assert.FailNow(t, "Unable to init ES indexer Error: %v", err)
126124
}
127125

128126
defer indexer.Close()

modules/process/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestManager_Cancel(t *testing.T) {
5050
select {
5151
case <-ctx.Done():
5252
default:
53-
assert.Fail(t, "Cancel should cancel the provided context")
53+
assert.FailNow(t, "Cancel should cancel the provided context")
5454
}
5555
finished()
5656

@@ -62,7 +62,7 @@ func TestManager_Cancel(t *testing.T) {
6262
select {
6363
case <-ctx.Done():
6464
default:
65-
assert.Fail(t, "Cancel should cancel the provided context")
65+
assert.FailNow(t, "Cancel should cancel the provided context")
6666
}
6767
finished()
6868
}

services/pull/check_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) {
5454
case id := <-idChan:
5555
assert.EqualValues(t, pr.ID, id)
5656
case <-time.After(time.Second):
57-
assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
57+
assert.FailNow(t, "Timeout: nothing was added to pullRequestQueue")
5858
}
5959

6060
has, err = prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))

tests/integration/api_packages_conan_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func TestPackageConan(t *testing.T) {
296296

297297
assert.Equal(t, int64(len(contentConaninfo)), pb.Size)
298298
} else {
299-
assert.Fail(t, "unknown file: %s", pf.Name)
299+
assert.FailNow(t, "unknown file: %s", pf.Name)
300300
}
301301
}
302302
})

tests/integration/api_packages_container_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func TestPackageContainer(t *testing.T) {
349349
assert.Equal(t, "application/vnd.docker.image.rootfs.diff.tar.gzip", pfd.Properties.GetByName(container_module.PropertyMediaType))
350350
assert.Equal(t, blobDigest, pfd.Properties.GetByName(container_module.PropertyDigest))
351351
default:
352-
assert.Fail(t, "unknown file: %s", pfd.File.Name)
352+
assert.FailNow(t, "unknown file: %s", pfd.File.Name)
353353
}
354354
}
355355

0 commit comments

Comments
 (0)