mirror of
https://github.com/gogs/gogs.git
synced 2025-05-23 16:00:56 +00:00
util: add tests (#5989)
This commit is contained in:
parent
9e9ca66467
commit
a4de85dc80
53
internal/errutil/errutil_test.go
Normal file
53
internal/errutil/errutil_test.go
Normal 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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -10,8 +10,26 @@ import (
|
|||||||
|
|
||||||
"github.com/gogs/git-module"
|
"github.com/gogs/git-module"
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestIsErrRevisionNotExist(t *testing.T) {
|
||||||
assert.True(t, IsErrRevisionNotExist(git.ErrRevisionNotExist))
|
assert.True(t, IsErrRevisionNotExist(git.ErrRevisionNotExist))
|
||||||
assert.False(t, IsErrRevisionNotExist(os.ErrNotExist))
|
assert.False(t, IsErrRevisionNotExist(os.ErrNotExist))
|
||||||
|
29
internal/osutil/error_test.go
Normal file
29
internal/osutil/error_test.go
Normal 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)))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -20,5 +20,5 @@ func TemplatePreview(c *context.Context) {
|
|||||||
c.Data["ResetPwdCodeLives"] = conf.Auth.ResetPasswordCodeLives / 60
|
c.Data["ResetPwdCodeLives"] = conf.Auth.ResetPasswordCodeLives / 60
|
||||||
c.Data["CurDbValue"] = ""
|
c.Data["CurDbValue"] = ""
|
||||||
|
|
||||||
c.Success( (c.Params("*")))
|
c.Success(c.Params("*"))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user