Ver Fonte

util: add tests (#5989)

ᴜɴᴋɴᴡᴏɴ há 4 anos atrás
pai
commit
a4de85dc80

+ 1 - 1
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
 		}
 

+ 53 - 0
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))
+		})
+	}
+}

+ 18 - 0
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))

+ 29 - 0
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)))
+		})
+	}
+}

+ 1 - 1
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)
 }

+ 1 - 1
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("*"))
 }