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

tests/integration/api_packages_nuget_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
326326
assert.Equal(t, nuget_module.PropertySymbolID, pps[0].Name)
327327
assert.Equal(t, symbolID, pps[0].Value)
328328
default:
329-
assert.Fail(t, "unexpected file: %v", pf.Name)
329+
assert.FailNow(t, "unexpected file: %v", pf.Name)
330330
}
331331
}
332332

tests/integration/api_repo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func TestAPIRepoMigrate(t *testing.T) {
368368
case "You can not import from disallowed hosts.":
369369
assert.EqualValues(t, "private-ip", testCase.repoName)
370370
default:
371-
assert.Failf(t, "unexpected error '%v' on url '%s'", respJSON["message"], testCase.cloneURL)
371+
assert.FailNow(t, "unexpected error '%v' on url '%s'", respJSON["message"], testCase.cloneURL)
372372
}
373373
} else {
374374
assert.EqualValues(t, testCase.expectedStatus, resp.Code)

tests/integration/api_token_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@ func runTestCase(t *testing.T, testCase *requiredScopeTestCase, user *user_model
483483
} else if minRequiredLevel == auth_model.Write {
484484
unauthorizedLevel = auth_model.Read
485485
} else {
486-
assert.Failf(t, "Invalid test case", "Unknown access token scope level: %v", minRequiredLevel)
487-
return
486+
assert.FailNow(t, "Invalid test case: Unknown access token scope level: %v", minRequiredLevel)
488487
}
489488
}
490489

tests/integration/gpg_git_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,10 @@ func TestGPGGit(t *testing.T) {
106106
assert.NotNil(t, response.Verification)
107107
if response.Verification == nil {
108108
assert.FailNow(t, "no verification provided with response! %v", response)
109-
return
110109
}
111110
assert.True(t, response.Verification.Verified)
112111
if !response.Verification.Verified {
113112
t.FailNow()
114-
return
115113
}
116114
assert.Equal(t, "[email protected]", response.Verification.Signer.Email)
117115
}))
@@ -120,12 +118,10 @@ func TestGPGGit(t *testing.T) {
120118
assert.NotNil(t, response.Verification)
121119
if response.Verification == nil {
122120
assert.FailNow(t, "no verification provided with response! %v", response)
123-
return
124121
}
125122
assert.True(t, response.Verification.Verified)
126123
if !response.Verification.Verified {
127124
t.FailNow()
128-
return
129125
}
130126
assert.Equal(t, "[email protected]", response.Verification.Signer.Email)
131127
}))
@@ -140,12 +136,10 @@ func TestGPGGit(t *testing.T) {
140136
assert.NotNil(t, response.Verification)
141137
if response.Verification == nil {
142138
assert.FailNow(t, "no verification provided with response! %v", response)
143-
return
144139
}
145140
assert.True(t, response.Verification.Verified)
146141
if !response.Verification.Verified {
147142
t.FailNow()
148-
return
149143
}
150144
assert.Equal(t, "[email protected]", response.Verification.Signer.Email)
151145
}))
@@ -160,17 +154,14 @@ func TestGPGGit(t *testing.T) {
160154
assert.NotNil(t, branch.Commit)
161155
if branch.Commit == nil {
162156
assert.FailNow(t, "no commit provided with branch! %v", branch)
163-
return
164157
}
165158
assert.NotNil(t, branch.Commit.Verification)
166159
if branch.Commit.Verification == nil {
167160
assert.FailNow(t, "no verification provided with branch commit! %v", branch.Commit)
168-
return
169161
}
170162
assert.True(t, branch.Commit.Verification.Verified)
171163
if !branch.Commit.Verification.Verified {
172164
t.FailNow()
173-
return
174165
}
175166
assert.Equal(t, "[email protected]", branch.Commit.Verification.Signer.Email)
176167
}))

0 commit comments

Comments
 (0)