sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/machinery/errors_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package machinery 18 19 import ( 20 "errors" 21 "path/filepath" 22 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 ) 26 27 var _ = Describe("Errors", func() { 28 var ( 29 path = filepath.Join("path", "to", "file") 30 testErr = errors.New("test error") 31 ) 32 33 DescribeTable("should contain the wrapped error", 34 func(err error) { 35 Expect(errors.Is(err, testErr)).To(BeTrue()) 36 }, 37 Entry("for validate errors", ValidateError{testErr}), 38 Entry("for set template defaults errors", SetTemplateDefaultsError{testErr}), 39 Entry("for file existence errors", ExistsFileError{testErr}), 40 Entry("for file opening errors", OpenFileError{testErr}), 41 Entry("for directory creation errors", CreateDirectoryError{testErr}), 42 Entry("for file creation errors", CreateFileError{testErr}), 43 Entry("for file reading errors", ReadFileError{testErr}), 44 Entry("for file writing errors", WriteFileError{testErr}), 45 Entry("for file closing errors", CloseFileError{testErr}), 46 ) 47 48 // NOTE: the following test increases coverage 49 It("should print a descriptive error message", func() { 50 Expect(ModelAlreadyExistsError{path}.Error()).To(ContainSubstring("model already exists")) 51 Expect(UnknownIfExistsActionError{path, -1}.Error()).To(ContainSubstring("unknown behavior if file exists")) 52 Expect(FileAlreadyExistsError{path}.Error()).To(ContainSubstring("file already exists")) 53 }) 54 })