Skip to content

Commit

Permalink
✅ Add test case for internal/errors/file.go (#893)
Browse files Browse the repository at this point in the history
* feat: add test case and comment

Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>

* fix: test case name

Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>

* Update internal/errors/file_test.go

Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>

* Update internal/errors/file_test.go

Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>

* Update internal/errors/file_test.go

Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>

* fix: apply suggestion

Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>

* 🤖 Update license headers / Format go codes and yaml files

Signed-off-by: vdaas-ci <ci@vdaas.org>

Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>
Co-authored-by: vdaas-ci <ci@vdaas.org>
  • Loading branch information
3 people authored Dec 28, 2020
1 parent 8adf43c commit ab1ec89
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/errors/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package errors

var (
// ErrWatchDirNotFound represents an error that the watch directory is not found.
ErrWatchDirNotFound = New("fs watcher watch dir not found")

// ErrFileAlreadyExists represents a function to generate an error that the file already exists.
ErrFileAlreadyExists = func(path string) error {
return Errorf("file already exists: %s", path)
}

// ErrPathNotSpecified represents an error that the path is not specified.
ErrPathNotSpecified = New("the path is not specified")
)
174 changes: 174 additions & 0 deletions internal/errors/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package errors

import (
"testing"
)

func TestErrWatchDirNotFound(t *testing.T) {
type want struct {
want error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "return ErrWatchDirNotFound error",
want: want{
want: New("fs watcher watch dir not found"),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := ErrWatchDirNotFound
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrFileAlreadyExists(t *testing.T) {
type args struct {
path string
}
type want struct {
want error
}
type test struct {
name string
args args
want want
checkFunc func(want, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "return ErrFileAlreadyExists error with the file path is 'metadata.json'",
args: args{
path: "metadata.json",
},
want: want{
want: New("file already exists: metadata.json"),
},
},
{
name: "return ErrFileAlreadyExists error with the file path is empty",
args: args{
path: "",
},
want: want{
want: New("file already exists: "),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := ErrFileAlreadyExists(test.args.path)
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrPathNotSpecified(t *testing.T) {
type want struct {
want error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "return ErrPathNotSpecified error",
want: want{
want: New("the path is not specified"),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := ErrPathNotSpecified
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

0 comments on commit ab1ec89

Please sign in to comment.