diff --git a/internal/errors/file.go b/internal/errors/file.go index 267a6a0e5a..e5c6cc84ee 100644 --- a/internal/errors/file.go +++ b/internal/errors/file.go @@ -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") ) diff --git a/internal/errors/file_test.go b/internal/errors/file_test.go new file mode 100644 index 0000000000..80a4880aa6 --- /dev/null +++ b/internal/errors/file_test.go @@ -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) + } + }) + } +}