diff --git a/go.mod b/go.mod index ee7927c8664..b6fc46b1590 100644 --- a/go.mod +++ b/go.mod @@ -4,19 +4,21 @@ go 1.13 require ( github.com/gobuffalo/flect v0.2.0 - github.com/golang/protobuf v1.3.2 // indirect - github.com/kr/pretty v0.1.0 // indirect - github.com/onsi/ginkgo v1.10.1 - github.com/onsi/gomega v1.7.0 + github.com/golang/protobuf v1.3.4 // indirect + github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/onsi/ginkgo v1.12.0 + github.com/onsi/gomega v1.9.0 github.com/spf13/afero v1.2.2 github.com/spf13/cobra v0.0.5 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.4.0 // indirect - golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect - golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456 // indirect + golang.org/x/mod v0.2.0 // indirect + golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect + golang.org/x/sys v0.0.0-20200302083256-062a44052db1 // indirect golang.org/x/text v0.3.2 // indirect - golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72 - gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect - gopkg.in/yaml.v2 v2.2.4 // indirect + golang.org/x/tools v0.0.0-20200301222351-066e0c02454c + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect + gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect + gopkg.in/yaml.v2 v2.2.8 // indirect sigs.k8s.io/yaml v1.1.0 ) diff --git a/go.sum b/go.sum index b57fdfcb6d6..95d51fd70ef 100644 --- a/go.sum +++ b/go.sum @@ -13,27 +13,28 @@ github.com/gobuffalo/flect v0.2.0 h1:EWCvMGGxOjsgwlWaP+f4+Hh6yrrte7JeFL2S6b+0hdM github.com/gobuffalo/flect v0.2.0/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk= +github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= -github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME= -github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU= +github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.9.0 h1:R1uwffexN6Pr340GtYRIdZmAiN4J+iw6WG4wog1DUXg= +github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -61,39 +62,50 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1: golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI= -golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456 h1:ng0gs1AKnRRuEMZoTLLlbOd+C17zUDepwGQBb/n+JVg= -golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302083256-062a44052db1 h1:trYYa2hBaTeei9Bq2uAXwsfNYW4r+xD/tztngRsT0cQ= +golang.org/x/sys v0.0.0-20200302083256-062a44052db1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72 h1:bw9doJza/SFBEweII/rHQh338oozWyiFsBRHtrflcws= -golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200301222351-066e0c02454c h1:FD7jysxM+EJqg5UYYy3XYDsAiUickFsn4UiaanJkf8c= +golang.org/x/tools v0.0.0-20200301222351-066e0c02454c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= diff --git a/pkg/model/file/errors.go b/pkg/model/file/errors.go new file mode 100644 index 00000000000..20349f55844 --- /dev/null +++ b/pkg/model/file/errors.go @@ -0,0 +1,61 @@ +/* +Copyright 2020 The Kubernetes Authors. + +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 + + http://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 file + +import ( + "errors" +) + +// validateError is a wrapper error that will be used for errors returned by RequiresValidation.Validate +type validateError struct { + error +} + +// NewValidateError wraps an error to specify it was returned by RequiresValidation.Validate +func NewValidateError(err error) error { + return validateError{err} +} + +// Unwrap implements Wrapper interface +func (e validateError) Unwrap() error { + return e.error +} + +// IsValidateError checks if the error was returned by RequiresValidation.Validate +func IsValidateError(err error) bool { + return errors.As(err, &validateError{}) +} + +// setTemplateDefaultsError is a wrapper error that will be used for errors returned by Template.SetTemplateDefaults +type setTemplateDefaultsError struct { + error +} + +// NewSetTemplateDefaultsError wraps an error to specify it was returned by Template.SetTemplateDefaults +func NewSetTemplateDefaultsError(err error) error { + return setTemplateDefaultsError{err} +} + +// Unwrap implements Wrapper interface +func (e setTemplateDefaultsError) Unwrap() error { + return e.error +} + +// IsSetTemplateDefaultsError checks if the error was returned by Template.SetTemplateDefaults +func IsSetTemplateDefaultsError(err error) bool { + return errors.As(err, &setTemplateDefaultsError{}) +} diff --git a/pkg/model/plugin.go b/pkg/model/plugin.go index 06f8854933e..57d326fa378 100644 --- a/pkg/model/plugin.go +++ b/pkg/model/plugin.go @@ -16,9 +16,33 @@ limitations under the License. package model +import ( + "errors" +) + // Plugin is the interface that a plugin must implement // We will (later) have an ExecPlugin that implements this by exec-ing a binary type Plugin interface { // Pipe is the core plugin interface, that transforms a UniverseModel Pipe(*Universe) error } + +// pluginError is a wrapper error that will be used for errors returned by Plugin.Pipe +type pluginError struct { + error +} + +// NewPluginError wraps an error to specify it was returned by Plugin.Pipe +func NewPluginError(err error) error { + return pluginError{err} +} + +// Unwrap implements Wrapper interface +func (e pluginError) Unwrap() error { + return e.error +} + +// IsPluginError checks if the error was returned by Plugin.Pipe +func IsPluginError(err error) bool { + return errors.As(err, &pluginError{}) +} diff --git a/pkg/scaffold/internal/filesystem/errors.go b/pkg/scaffold/internal/filesystem/errors.go index c09a1e01d2e..7f605d3241a 100644 --- a/pkg/scaffold/internal/filesystem/errors.go +++ b/pkg/scaffold/internal/filesystem/errors.go @@ -17,6 +17,7 @@ limitations under the License. package filesystem import ( + "errors" "fmt" ) @@ -30,14 +31,19 @@ type fileExistsError struct { err error } +// Error implements error interface func (e fileExistsError) Error() string { return fmt.Sprintf("failed to check if %s exists: %v", e.path, e.err) } +// Unwrap implements Wrapper interface +func (e fileExistsError) Unwrap() error { + return e.err +} + // IsFileExistsError checks if the returned error is because the file could not be checked for existence -func IsFileExistsError(e error) bool { - _, ok := e.(fileExistsError) - return ok +func IsFileExistsError(err error) bool { + return errors.As(err, &fileExistsError{}) } // openFileError is returned if the file could not be opened @@ -46,14 +52,19 @@ type openFileError struct { err error } +// Error implements error interface func (e openFileError) Error() string { return fmt.Sprintf("failed to open %s: %v", e.path, e.err) } +// Unwrap implements Wrapper interface +func (e openFileError) Unwrap() error { + return e.err +} + // IsOpenFileError checks if the returned error is because the file could not be opened -func IsOpenFileError(e error) bool { - _, ok := e.(openFileError) - return ok +func IsOpenFileError(err error) bool { + return errors.As(err, &openFileError{}) } // createDirectoryError is returned if the directory could not be created @@ -62,14 +73,19 @@ type createDirectoryError struct { err error } +// Error implements error interface func (e createDirectoryError) Error() string { return fmt.Sprintf("failed to create directory for %s: %v", e.path, e.err) } +// Unwrap implements Wrapper interface +func (e createDirectoryError) Unwrap() error { + return e.err +} + // IsCreateDirectoryError checks if the returned error is because the directory could not be created -func IsCreateDirectoryError(e error) bool { - _, ok := e.(createDirectoryError) - return ok +func IsCreateDirectoryError(err error) bool { + return errors.As(err, &createDirectoryError{}) } // createFileError is returned if the file could not be created @@ -78,14 +94,19 @@ type createFileError struct { err error } +// Error implements error interface func (e createFileError) Error() string { return fmt.Sprintf("failed to create %s: %v", e.path, e.err) } +// Unwrap implements Wrapper interface +func (e createFileError) Unwrap() error { + return e.err +} + // IsCreateFileError checks if the returned error is because the file could not be created -func IsCreateFileError(e error) bool { - _, ok := e.(createFileError) - return ok +func IsCreateFileError(err error) bool { + return errors.As(err, &createFileError{}) } // readFileError is returned if the file could not be read @@ -94,14 +115,19 @@ type readFileError struct { err error } +// Error implements error interface func (e readFileError) Error() string { return fmt.Sprintf("failed to read from %s: %v", e.path, e.err) } +// Unwrap implements Wrapper interface +func (e readFileError) Unwrap() error { + return e.err +} + // IsReadFileError checks if the returned error is because the file could not be read -func IsReadFileError(e error) bool { - _, ok := e.(readFileError) - return ok +func IsReadFileError(err error) bool { + return errors.As(err, &readFileError{}) } // writeFileError is returned if the file could not be written @@ -110,14 +136,19 @@ type writeFileError struct { err error } +// Error implements error interface func (e writeFileError) Error() string { return fmt.Sprintf("failed to write to %s: %v", e.path, e.err) } -// IsWriteFileError checks if the returned error is because the file could not be written -func IsWriteFileError(e error) bool { - _, ok := e.(writeFileError) - return ok +// Unwrap implements Wrapper interface +func (e writeFileError) Unwrap() error { + return e.err +} + +// IsWriteFileError checks if the returned error is because the file could not be written to +func IsWriteFileError(err error) bool { + return errors.As(err, &writeFileError{}) } // closeFileError is returned if the file could not be created @@ -126,12 +157,17 @@ type closeFileError struct { err error } +// Error implements error interface func (e closeFileError) Error() string { return fmt.Sprintf("failed to close %s: %v", e.path, e.err) } +// Unwrap implements Wrapper interface +func (e closeFileError) Unwrap() error { + return e.err +} + // IsCloseFileError checks if the returned error is because the file could not be closed -func IsCloseFileError(e error) bool { - _, ok := e.(closeFileError) - return ok +func IsCloseFileError(err error) bool { + return errors.As(err, &closeFileError{}) } diff --git a/pkg/scaffold/internal/filesystem/errors_test.go b/pkg/scaffold/internal/filesystem/errors_test.go new file mode 100644 index 00000000000..cc3651a1017 --- /dev/null +++ b/pkg/scaffold/internal/filesystem/errors_test.go @@ -0,0 +1,83 @@ +/* +Copyright 2020 The Kubernetes Authors. + +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 + + http://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 filesystem + +import ( + "errors" + "path/filepath" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/extensions/table" + . "github.com/onsi/gomega" +) + +func TestErrors(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Error suite") +} + +var _ = Describe("Errors", func() { + var ( + path = filepath.Join("path", "to", "file") + err = errors.New("test error") + fileExistsErr = fileExistsError{path, err} + openFileErr = openFileError{path, err} + createDirectoryErr = createDirectoryError{path, err} + createFileErr = createFileError{path, err} + readFileErr = readFileError{path, err} + writeFileErr = writeFileError{path, err} + closeFileErr = closeFileError{path, err} + ) + + DescribeTable("IsXxxxError should return true for themselves and false for the rest", + func(f func(error) bool, itself error, rest ...error) { + Expect(f(itself)).To(BeTrue()) + for _, err := range rest { + Expect(f(err)).To(BeFalse()) + } + }, + Entry("file exists", IsFileExistsError, fileExistsErr, + openFileErr, createDirectoryErr, createFileErr, readFileErr, writeFileErr, closeFileErr), + Entry("open file", IsOpenFileError, openFileErr, + fileExistsErr, createDirectoryErr, createFileErr, readFileErr, writeFileErr, closeFileErr), + Entry("create directory", IsCreateDirectoryError, createDirectoryErr, + fileExistsErr, openFileErr, createFileErr, readFileErr, writeFileErr, closeFileErr), + Entry("create file", IsCreateFileError, createFileErr, + fileExistsErr, openFileErr, createDirectoryErr, readFileErr, writeFileErr, closeFileErr), + Entry("read file", IsReadFileError, readFileErr, + fileExistsErr, openFileErr, createDirectoryErr, createFileErr, writeFileErr, closeFileErr), + Entry("write file", IsWriteFileError, writeFileErr, + fileExistsErr, openFileErr, createDirectoryErr, createFileErr, readFileErr, closeFileErr), + Entry("close file", IsCloseFileError, closeFileErr, + fileExistsErr, openFileErr, createDirectoryErr, createFileErr, readFileErr, writeFileErr), + ) + + DescribeTable("should contain the wrapped error and error message", + func(err error) { + Expect(err).To(MatchError(err)) + Expect(err.Error()).To(ContainSubstring(err.Error())) + }, + Entry("file exists", fileExistsErr), + Entry("open file", openFileErr), + Entry("create directory", createDirectoryErr), + Entry("create file", createFileErr), + Entry("read file", readFileErr), + Entry("write file", writeFileErr), + Entry("close file", closeFileErr), + ) +}) diff --git a/pkg/scaffold/internal/filesystem/filesystem.go b/pkg/scaffold/internal/filesystem/filesystem.go index 04a32883c3f..e7e362c5c44 100644 --- a/pkg/scaffold/internal/filesystem/filesystem.go +++ b/pkg/scaffold/internal/filesystem/filesystem.go @@ -92,7 +92,7 @@ func FilePermissions(filePerm os.FileMode) Options { func (fs fileSystem) Exists(path string) (bool, error) { exists, err := afero.Exists(fs.fs, path) if err != nil { - return false, fileExistsError{path, err} + return exists, fileExistsError{path, err} } return exists, nil diff --git a/pkg/scaffold/internal/filesystem/filesystem_test.go b/pkg/scaffold/internal/filesystem/filesystem_test.go index 3cebc6da6d2..9f98ce400ac 100644 --- a/pkg/scaffold/internal/filesystem/filesystem_test.go +++ b/pkg/scaffold/internal/filesystem/filesystem_test.go @@ -17,9 +17,7 @@ limitations under the License. package filesystem import ( - "errors" "os" - "path/filepath" "testing" . "github.com/onsi/ginkgo" @@ -153,144 +151,6 @@ var _ = Describe("FileSystem", func() { }) }) - Describe("Errors", func() { - var ( - err = errors.New("test error") - path = filepath.Join("path", "to", "file") - fileExistsErr = fileExistsError{path, err} - openFileErr = openFileError{path, err} - createDirectoryErr = createDirectoryError{path, err} - createFileErr = createFileError{path, err} - readFileErr = readFileError{path, err} - writeFileErr = writeFileError{path, err} - closeFileErr = closeFileError{path, err} - ) - - Context("IsFileExistsError", func() { - It("should return true for file exists errors", func() { - Expect(IsFileExistsError(fileExistsErr)).To(BeTrue()) - }) - - It("should return false for any other error", func() { - Expect(IsFileExistsError(err)).To(BeFalse()) - Expect(IsFileExistsError(openFileErr)).To(BeFalse()) - Expect(IsFileExistsError(createDirectoryErr)).To(BeFalse()) - Expect(IsFileExistsError(createFileErr)).To(BeFalse()) - Expect(IsFileExistsError(readFileErr)).To(BeFalse()) - Expect(IsFileExistsError(writeFileErr)).To(BeFalse()) - Expect(IsFileExistsError(closeFileErr)).To(BeFalse()) - }) - }) - - Context("IsOpenFileError", func() { - It("should return true for open file errors", func() { - Expect(IsOpenFileError(openFileErr)).To(BeTrue()) - }) - - It("should return false for any other error", func() { - Expect(IsOpenFileError(err)).To(BeFalse()) - Expect(IsOpenFileError(fileExistsErr)).To(BeFalse()) - Expect(IsOpenFileError(createDirectoryErr)).To(BeFalse()) - Expect(IsOpenFileError(createFileErr)).To(BeFalse()) - Expect(IsOpenFileError(readFileErr)).To(BeFalse()) - Expect(IsOpenFileError(writeFileErr)).To(BeFalse()) - Expect(IsOpenFileError(closeFileErr)).To(BeFalse()) - }) - }) - - Context("IsCreateDirectoryError", func() { - It("should return true for create directory errors", func() { - Expect(IsCreateDirectoryError(createDirectoryErr)).To(BeTrue()) - }) - - It("should return false for any other error", func() { - Expect(IsCreateDirectoryError(err)).To(BeFalse()) - Expect(IsCreateDirectoryError(fileExistsErr)).To(BeFalse()) - Expect(IsCreateDirectoryError(openFileErr)).To(BeFalse()) - Expect(IsCreateDirectoryError(createFileErr)).To(BeFalse()) - Expect(IsCreateDirectoryError(readFileErr)).To(BeFalse()) - Expect(IsCreateDirectoryError(writeFileErr)).To(BeFalse()) - Expect(IsCreateDirectoryError(closeFileErr)).To(BeFalse()) - }) - }) - - Context("IsCreateFileError", func() { - It("should return true for create file errors", func() { - Expect(IsCreateFileError(createFileErr)).To(BeTrue()) - }) - - It("should return false for any other error", func() { - Expect(IsCreateFileError(err)).To(BeFalse()) - Expect(IsCreateFileError(fileExistsErr)).To(BeFalse()) - Expect(IsCreateFileError(openFileErr)).To(BeFalse()) - Expect(IsCreateFileError(createDirectoryErr)).To(BeFalse()) - Expect(IsCreateFileError(readFileErr)).To(BeFalse()) - Expect(IsCreateFileError(writeFileErr)).To(BeFalse()) - Expect(IsCreateFileError(closeFileErr)).To(BeFalse()) - }) - }) - - Context("IsReadFileError", func() { - It("should return true for read file errors", func() { - Expect(IsReadFileError(readFileErr)).To(BeTrue()) - }) - - It("should return false for any other error", func() { - Expect(IsReadFileError(err)).To(BeFalse()) - Expect(IsReadFileError(fileExistsErr)).To(BeFalse()) - Expect(IsReadFileError(openFileErr)).To(BeFalse()) - Expect(IsReadFileError(createDirectoryErr)).To(BeFalse()) - Expect(IsReadFileError(createFileErr)).To(BeFalse()) - Expect(IsReadFileError(writeFileErr)).To(BeFalse()) - Expect(IsReadFileError(closeFileErr)).To(BeFalse()) - }) - }) - - Context("IsWriteFileError", func() { - It("should return true for write file errors", func() { - Expect(IsWriteFileError(writeFileErr)).To(BeTrue()) - }) - - It("should return false for any other error", func() { - Expect(IsWriteFileError(err)).To(BeFalse()) - Expect(IsWriteFileError(fileExistsErr)).To(BeFalse()) - Expect(IsWriteFileError(openFileErr)).To(BeFalse()) - Expect(IsWriteFileError(createDirectoryErr)).To(BeFalse()) - Expect(IsWriteFileError(createFileErr)).To(BeFalse()) - Expect(IsWriteFileError(readFileErr)).To(BeFalse()) - Expect(IsWriteFileError(closeFileErr)).To(BeFalse()) - }) - }) - - Context("IsCloseFileError", func() { - It("should return true for close file errors", func() { - Expect(IsCloseFileError(closeFileErr)).To(BeTrue()) - }) - - It("should return false for any other error", func() { - Expect(IsCloseFileError(err)).To(BeFalse()) - Expect(IsCloseFileError(fileExistsErr)).To(BeFalse()) - Expect(IsCloseFileError(openFileErr)).To(BeFalse()) - Expect(IsCloseFileError(createDirectoryErr)).To(BeFalse()) - Expect(IsCloseFileError(createFileErr)).To(BeFalse()) - Expect(IsCloseFileError(readFileErr)).To(BeFalse()) - Expect(IsCloseFileError(writeFileErr)).To(BeFalse()) - }) - }) - - Describe("error messages", func() { - It("should contain the wrapped err", func() { - Expect(fileExistsErr.Error()).To(ContainSubstring(err.Error())) - Expect(openFileErr.Error()).To(ContainSubstring(err.Error())) - Expect(createDirectoryErr.Error()).To(ContainSubstring(err.Error())) - Expect(createFileErr.Error()).To(ContainSubstring(err.Error())) - Expect(readFileErr.Error()).To(ContainSubstring(err.Error())) - Expect(writeFileErr.Error()).To(ContainSubstring(err.Error())) - Expect(closeFileErr.Error()).To(ContainSubstring(err.Error())) - }) - }) - }) - // NOTE: FileSystem.Exists, FileSystem.Open, FileSystem.Open().Read, FileSystem.Create and FileSystem.Create().Write // are hard to test in unitary tests as they deal with actual files }) diff --git a/pkg/scaffold/internal/filesystem/mock.go b/pkg/scaffold/internal/filesystem/mock.go index 87cd4c14ac2..b7d213c1fc3 100644 --- a/pkg/scaffold/internal/filesystem/mock.go +++ b/pkg/scaffold/internal/filesystem/mock.go @@ -205,12 +205,12 @@ type mockWriteFile struct { func (f *mockWriteFile) Write(content []byte) (n int, err error) { defer func() { if err == nil && f.closeFileError != nil { - err = closeFileError{path: f.path, err: f.closeFileError} + err = closeFileError{f.path, f.closeFileError} } }() if f.writeFileError != nil { - return 0, writeFileError{path: f.path, err: f.writeFileError} + return 0, writeFileError{f.path, f.writeFileError} } return f.content.Write(content) diff --git a/pkg/scaffold/internal/filesystem/mock_test.go b/pkg/scaffold/internal/filesystem/mock_test.go index a01f943e72d..37f9e827cce 100644 --- a/pkg/scaffold/internal/filesystem/mock_test.go +++ b/pkg/scaffold/internal/filesystem/mock_test.go @@ -159,7 +159,7 @@ var _ = Describe("MockFileSystem", func() { It("should error when calling Exists", func() { _, err := fsi.Exists("") - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsFileExistsError(err)).To(BeTrue()) }) @@ -197,7 +197,7 @@ var _ = Describe("MockFileSystem", func() { It("should error when calling Open", func() { _, err := fsi.Open("") - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsOpenFileError(err)).To(BeTrue()) }) @@ -235,7 +235,7 @@ var _ = Describe("MockFileSystem", func() { It("should error when calling Create", func() { _, err := fsi.Create("") - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsCreateDirectoryError(err)).To(BeTrue()) }) }) @@ -265,7 +265,7 @@ var _ = Describe("MockFileSystem", func() { It("should error when calling Create", func() { _, err := fsi.Create("") - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsCreateFileError(err)).To(BeTrue()) }) }) @@ -332,7 +332,7 @@ var _ = Describe("MockFileSystem", func() { output := make([]byte, 0) _, err = f.Read(output) - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsReadFileError(err)).To(BeTrue()) }) @@ -413,7 +413,7 @@ var _ = Describe("MockFileSystem", func() { Expect(err).NotTo(HaveOccurred()) _, err = f.Write([]byte("")) - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsWriteFileError(err)).To(BeTrue()) }) }) @@ -438,7 +438,7 @@ var _ = Describe("MockFileSystem", func() { Expect(err).NotTo(HaveOccurred()) err = f.Close() - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsCloseFileError(err)).To(BeTrue()) }) @@ -447,7 +447,7 @@ var _ = Describe("MockFileSystem", func() { Expect(err).NotTo(HaveOccurred()) _, err = f.Write([]byte("")) - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(testErr)) Expect(IsCloseFileError(err)).To(BeTrue()) }) }) diff --git a/pkg/scaffold/internal/machinery/errors.go b/pkg/scaffold/internal/machinery/errors.go new file mode 100644 index 00000000000..aef82aeb569 --- /dev/null +++ b/pkg/scaffold/internal/machinery/errors.go @@ -0,0 +1,74 @@ +/* +Copyright 2020 The Kubernetes Authors. + +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 + + http://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 machinery + +import ( + "errors" + "fmt" + + "sigs.k8s.io/kubebuilder/pkg/model/file" +) + +// This file contains the errors returned by the scaffolding machinery +// They are not exported as they should not be created outside of this package +// Exported functions are provided to check which kind of error was returned + +// fileAlreadyExistsError is returned if the file is expected not to exist but it does +type fileAlreadyExistsError struct { + path string +} + +// Error implements error interface +func (e fileAlreadyExistsError) Error() string { + return fmt.Sprintf("failed to create %s: file already exists", e.path) +} + +// IsFileAlreadyExistsError checks if the returned error is because the file already existed when expected not to +func IsFileAlreadyExistsError(err error) bool { + return errors.As(err, &fileAlreadyExistsError{}) +} + +// modelAlreadyExistsError is returned if the file is expected not to exist but a previous model does +type modelAlreadyExistsError struct { + path string +} + +// Error implements error interface +func (e modelAlreadyExistsError) Error() string { + return fmt.Sprintf("failed to create %s: model already exists", e.path) +} + +// IsModelAlreadyExistsError checks if the returned error is because the model already existed when expected not to +func IsModelAlreadyExistsError(err error) bool { + return errors.As(err, &modelAlreadyExistsError{}) +} + +// unknownIfExistsActionError is returned if the if-exists-action is unknown +type unknownIfExistsActionError struct { + path string + ifExistsAction file.IfExistsAction +} + +// Error implements error interface +func (e unknownIfExistsActionError) Error() string { + return fmt.Sprintf("unknown behavior if file exists (%d) for %s", e.ifExistsAction, e.path) +} + +// IsUnknownIfExistsActionError checks if the returned error is because the if-exists-action is unknown +func IsUnknownIfExistsActionError(err error) bool { + return errors.As(err, &unknownIfExistsActionError{}) +} diff --git a/pkg/scaffold/internal/machinery/errors_test.go b/pkg/scaffold/internal/machinery/errors_test.go new file mode 100644 index 00000000000..adcf32b4f70 --- /dev/null +++ b/pkg/scaffold/internal/machinery/errors_test.go @@ -0,0 +1,71 @@ +/* +Copyright 2020 The Kubernetes Authors. + +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 + + http://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 machinery + +import ( + "errors" + "path/filepath" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/extensions/table" + . "github.com/onsi/gomega" +) + +func TestErrors(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Error suite") +} + +var _ = Describe("Errors", func() { + var ( + path = filepath.Join("path", "to", "file") + err = errors.New("test error") + fileAlreadyExistsErr = fileAlreadyExistsError{path} + modelAlreadyExistsErr = modelAlreadyExistsError{path} + unknownIfExistsActionErr = unknownIfExistsActionError{path, -1} + ) + + DescribeTable("IsXxxxError should return true for themselves and false for the rest", + func(f func(error) bool, itself error, rest ...error) { + Expect(f(itself)).To(BeTrue()) + for _, err := range rest { + Expect(f(err)).To(BeFalse()) + } + }, + Entry("file exists", IsFileAlreadyExistsError, fileAlreadyExistsErr, + err, modelAlreadyExistsErr, unknownIfExistsActionErr), + Entry("model exists", IsModelAlreadyExistsError, modelAlreadyExistsErr, + err, fileAlreadyExistsErr, unknownIfExistsActionErr), + Entry("unknown if exists action", IsUnknownIfExistsActionError, unknownIfExistsActionErr, + err, fileAlreadyExistsErr, modelAlreadyExistsErr), + ) + + DescribeTable("should contain the wrapped error and error message", + func(err error) { + Expect(err).To(MatchError(err)) + Expect(err.Error()).To(ContainSubstring(err.Error())) + }, + ) + + // NOTE: the following test increases coverage + It("should print a descriptive error message", func() { + Expect(fileAlreadyExistsErr.Error()).To(ContainSubstring("file already exists")) + Expect(modelAlreadyExistsErr.Error()).To(ContainSubstring("model already exists")) + Expect(unknownIfExistsActionErr.Error()).To(ContainSubstring("unknown behavior if file exists")) + }) +}) diff --git a/pkg/scaffold/internal/machinery/scaffold.go b/pkg/scaffold/internal/machinery/scaffold.go index bb75b9e153f..477ab733973 100644 --- a/pkg/scaffold/internal/machinery/scaffold.go +++ b/pkg/scaffold/internal/machinery/scaffold.go @@ -79,7 +79,7 @@ func (s *scaffold) Execute(universe *model.Universe, files ...file.Builder) erro // Validate file builders if reqValFile, requiresValidation := f.(file.RequiresValidation); requiresValidation { if err := reqValFile.Validate(); err != nil { - return err + return file.NewValidateError(err) } } @@ -101,7 +101,7 @@ func (s *scaffold) Execute(universe *model.Universe, files ...file.Builder) erro // Execute plugins for _, plugin := range s.plugins { if err := plugin.Pipe(universe); err != nil { - return err + return model.NewPluginError(err) } } @@ -120,7 +120,7 @@ func (scaffold) buildFileModel(t file.Template, models map[string]*file.File) er // Set the template default values err := t.SetTemplateDefaults() if err != nil { - return err + return file.NewSetTemplateDefaultsError(err) } // Handle already existing models @@ -129,10 +129,10 @@ func (scaffold) buildFileModel(t file.Template, models map[string]*file.File) er case file.Skip: return nil case file.Error: - return fmt.Errorf("failed to create %s: model already exists", t.GetPath()) + return modelAlreadyExistsError{t.GetPath()} case file.Overwrite: default: - return fmt.Errorf("unknown behavior if file exists (%d) for %s", t.GetIfExistsAction(), t.GetPath()) + return unknownIfExistsActionError{t.GetPath(), t.GetIfExistsAction()} } } @@ -254,12 +254,12 @@ func (s scaffold) loadPreviousModel(i file.Inserter, models map[string]*file.Fil return fromFile, nil case file.Error: // Writing will result in an error, so we can return error now - return nil, fmt.Errorf("failed to create %s: file already exists", i.GetPath()) + return nil, fileAlreadyExistsError{i.GetPath()} case file.Overwrite: // Model has preference return m, nil default: - return nil, fmt.Errorf("unknown behavior if file exists (%d) for %s", m.IfExistsAction, i.GetPath()) + return nil, unknownIfExistsActionError{i.GetPath(), m.IfExistsAction} } } @@ -374,7 +374,7 @@ func (s scaffold) writeFile(f *file.File) error { return nil case file.Error: // By returning an error, the file is not written and the process will fail - return fmt.Errorf("failed to create %s: file already exists", f.Path) + return fileAlreadyExistsError{f.Path} } } diff --git a/pkg/scaffold/internal/machinery/scaffold_test.go b/pkg/scaffold/internal/machinery/scaffold_test.go index 066986cc038..7927b42c041 100644 --- a/pkg/scaffold/internal/machinery/scaffold_test.go +++ b/pkg/scaffold/internal/machinery/scaffold_test.go @@ -143,31 +143,40 @@ var _ = Describe("Scaffold", func() { ) DescribeTable("file builders related errors", - func(errMsg string, files ...file.Builder) { + func(f func(error) bool, files ...file.Builder) { s := &scaffold{fs: filesystem.NewMock()} - err := s.Execute(model.NewUniverse(), files...) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring(errMsg)) + Expect(f(s.Execute(model.NewUniverse(), files...))).To(BeTrue()) }, Entry("should fail if unable to validate a file builder", - testErr.Error(), + file.IsValidateError, fakeRequiresValidation{validateErr: testErr}, ), Entry("should fail if unable to set default values for a template", - testErr.Error(), + file.IsSetTemplateDefaultsError, fakeTemplate{err: testErr}, ), Entry("should fail if an unexpected previous model is found", - "failed to create filename: model already exists", + IsModelAlreadyExistsError, fakeTemplate{fakeBuilder: fakeBuilder{path: "filename"}}, fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: file.Error}}, ), Entry("should fail if behavior if file exists is not defined", - "unknown behavior if file exists (-1) for filename", + IsUnknownIfExistsActionError, fakeTemplate{fakeBuilder: fakeBuilder{path: "filename"}}, fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: -1}}, ), + ) + + // Following errors are unwrapped, so we need to check for substrings + DescribeTable("template related errors", + func(errMsg string, files ...file.Builder) { + s := &scaffold{fs: filesystem.NewMock()} + + err := s.Execute(model.NewUniverse(), files...) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring(errMsg)) + }, Entry("should fail if a template is broken", "template: ", fakeTemplate{body: "{{ .Field }"}, @@ -324,7 +333,7 @@ var _ = Describe("Scaffold", func() { ) DescribeTable("insert strings related errors", - func(errMsg string, files ...file.Builder) { + func(f func(error) bool, files ...file.Builder) { s := &scaffold{ fs: filesystem.NewMock( filesystem.MockExists(func(_ string) bool { return true }), @@ -333,15 +342,15 @@ var _ = Describe("Scaffold", func() { err := s.Execute(model.NewUniverse(), files...) Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring(errMsg)) + Expect(f(err)).To(BeTrue()) }, Entry("should fail if inserting into a model that fails when a file exists and it does exist", - "failed to create filename: file already exists", + IsFileAlreadyExistsError, fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: file.Error}}, fakeInserter{fakeBuilder: fakeBuilder{path: "filename"}}, ), Entry("should fail if inserting into a model with unknown behavior if the file exists and it does exist", - "unknown behavior if file exists (-1) for filename", + IsUnknownIfExistsActionError, fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: -1}}, fakeInserter{fakeBuilder: fakeBuilder{path: "filename"}}, ), @@ -357,8 +366,8 @@ var _ = Describe("Scaffold", func() { model.NewUniverse(), fakeTemplate{}, ) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring(testErr.Error())) + Expect(err).To(MatchError(testErr)) + Expect(model.IsPluginError(err)).To(BeTrue()) }) Context("write when the file already exists", func() { @@ -395,7 +404,7 @@ var _ = Describe("Scaffold", func() { fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: file.Error}, body: fileContent}, ) Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("failed to create filename: file already exists")) + Expect(IsFileAlreadyExistsError(err)).To(BeTrue()) Expect(output.String()).To(BeEmpty()) }) })