diff options
-rw-r--r-- | internal/context/auth.go | 2 | ||||
-rw-r--r-- | internal/errutil/errutil_test.go | 53 | ||||
-rw-r--r-- | internal/gitutil/error_test.go | 18 | ||||
-rw-r--r-- | internal/osutil/error_test.go | 29 | ||||
-rw-r--r-- | internal/route/admin/admin.go | 2 | ||||
-rw-r--r-- | internal/route/dev/template.go | 2 |
6 files changed, 103 insertions, 3 deletions
diff --git a/internal/context/auth.go b/internal/context/auth.go index 2a7a1aef..ad583791 100644 --- a/internal/context/auth.go +++ b/internal/context/auth.go @@ -35,7 +35,7 @@ func Toggle(options *ToggleOptions) macaron.Handler { // Check prohibit login users. if c.IsLogged && c.User.ProhibitLogin { c.Data["Title"] = c.Tr("auth.prohibit_login") - c.Success( "user/auth/prohibit_login") + c.Success("user/auth/prohibit_login") return } diff --git a/internal/errutil/errutil_test.go b/internal/errutil/errutil_test.go new file mode 100644 index 00000000..9fcffbc0 --- /dev/null +++ b/internal/errutil/errutil_test.go @@ -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)) + }) + } +} diff --git a/internal/gitutil/error_test.go b/internal/gitutil/error_test.go index c0b9f77a..331751f1 100644 --- a/internal/gitutil/error_test.go +++ b/internal/gitutil/error_test.go @@ -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)) diff --git a/internal/osutil/error_test.go b/internal/osutil/error_test.go new file mode 100644 index 00000000..26ec3b5f --- /dev/null +++ b/internal/osutil/error_test.go @@ -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))) + }) + } +} diff --git a/internal/route/admin/admin.go b/internal/route/admin/admin.go index 0adfac80..fb734892 100644 --- a/internal/route/admin/admin.go +++ b/internal/route/admin/admin.go @@ -239,5 +239,5 @@ func Monitor(c *context.Context) { c.Data["PageIsAdminMonitor"] = true c.Data["Processes"] = process.Processes c.Data["Entries"] = cron.ListTasks() - c.Success( MONITOR) + c.Success(MONITOR) } diff --git a/internal/route/dev/template.go b/internal/route/dev/template.go index 42b0ccc2..8f59e923 100644 --- a/internal/route/dev/template.go +++ b/internal/route/dev/template.go @@ -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("*")) } |