util: add tests (#5989)

This commit is contained in:
ᴜɴᴋɴᴡᴏɴ 2020-03-16 01:54:08 +08:00 committed by GitHub
parent 9e9ca66467
commit a4de85dc80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 103 additions and 3 deletions

View File

@ -0,0 +1,53 @@
// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package errutil
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
type notFoundError struct {
val bool
}
func (notFoundError) Error() string {
return "not found"
}
func (e notFoundError) NotFound() bool {
return e.val
}
func TestIsNotFound(t *testing.T) {
tests := []struct {
name string
err error
expVal bool
}{
{
name: "error does not implement NotFound",
err: errors.New("a simple error"),
expVal: false,
},
{
name: "error implements NotFound but not a not found",
err: notFoundError{val: false},
expVal: false,
},
{
name: "error implements NotFound",
err: notFoundError{val: true},
expVal: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expVal, IsNotFound(test.err))
})
}
}

View File

@ -10,8 +10,26 @@ import (
"github.com/gogs/git-module"
"github.com/stretchr/testify/assert"
"gogs.io/gogs/internal/errutil"
)
func TestError_NotFound(t *testing.T) {
tests := []struct {
err error
expVal bool
}{
{err: git.ErrSubmoduleNotExist, expVal: true},
{err: git.ErrRevisionNotExist, expVal: true},
{err: git.ErrNoMergeBase, expVal: false},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
assert.Equal(t, test.expVal, errutil.IsNotFound(NewError(test.err)))
})
}
}
func TestIsErrRevisionNotExist(t *testing.T) {
assert.True(t, IsErrRevisionNotExist(git.ErrRevisionNotExist))
assert.False(t, IsErrRevisionNotExist(os.ErrNotExist))

View File

@ -0,0 +1,29 @@
// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package osutil
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"gogs.io/gogs/internal/errutil"
)
func TestError_NotFound(t *testing.T) {
tests := []struct {
err error
expVal bool
}{
{err: os.ErrNotExist, expVal: true},
{err: os.ErrClosed, expVal: false},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
assert.Equal(t, test.expVal, errutil.IsNotFound(NewError(test.err)))
})
}
}

View File

@ -20,5 +20,5 @@ func TemplatePreview(c *context.Context) {
c.Data["ResetPwdCodeLives"] = conf.Auth.ResetPasswordCodeLives / 60
c.Data["CurDbValue"] = ""
c.Success( (c.Params("*")))
c.Success(c.Params("*"))
}