Skip to content

Commit 6ca3450

Browse files
authored
Merge pull request #2082 from Adirio/export-scaffold-machinery
⚠️ Export scaffolding machinery
2 parents fdb87df + 61dcae6 commit 6ca3450

25 files changed

+1013
-2298
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ test-unit: ## Run the unit tests
9797
.PHONY: test-coverage
9898
test-coverage: ## Run unit tests creating the output to report coverage
9999
- rm -rf *.out # Remove all coverage files if exists
100-
go test -race -failfast -tags=integration -coverprofile=coverage-all.out -coverpkg="./pkg/cli/...,./pkg/config/...,./pkg/internal/...,./pkg/model/...,./pkg/plugin/...,./pkg/plugins/golang,./pkg/plugins/internal/..." ./pkg/...
100+
go test -race -failfast -tags=integration -coverprofile=coverage-all.out -coverpkg="./pkg/cli/...,./pkg/config/...,./pkg/internal/...,./pkg/machinery/...,./pkg/model/...,./pkg/plugin/...,./pkg/plugins/golang" ./pkg/...
101101

102102
.PHONY: test-integration
103103
test-integration: ## Run the integration tests

pkg/machinery/errors.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
"fmt"
21+
22+
"sigs.k8s.io/kubebuilder/v3/pkg/model/file"
23+
)
24+
25+
// This file contains the errors returned by the scaffolding machinery
26+
// They are exported to be able to check which kind of error was returned
27+
28+
// ValidateError is a wrapper error that will be used for errors returned by RequiresValidation.Validate
29+
type ValidateError struct {
30+
error
31+
}
32+
33+
// Unwrap implements Wrapper interface
34+
func (e ValidateError) Unwrap() error {
35+
return e.error
36+
}
37+
38+
// SetTemplateDefaultsError is a wrapper error that will be used for errors returned by Template.SetTemplateDefaults
39+
type SetTemplateDefaultsError struct {
40+
error
41+
}
42+
43+
// Unwrap implements Wrapper interface
44+
func (e SetTemplateDefaultsError) Unwrap() error {
45+
return e.error
46+
}
47+
48+
// PluginError is a wrapper error that will be used for errors returned by model.Plugin.Pipe
49+
type PluginError struct {
50+
error
51+
}
52+
53+
// Unwrap implements Wrapper interface
54+
func (e PluginError) Unwrap() error {
55+
return e.error
56+
}
57+
58+
// ExistsFileError is a wrapper error that will be used for errors when checking for a file existence
59+
type ExistsFileError struct {
60+
error
61+
}
62+
63+
// Unwrap implements Wrapper interface
64+
func (e ExistsFileError) Unwrap() error {
65+
return e.error
66+
}
67+
68+
// OpenFileError is a wrapper error that will be used for errors when opening a file
69+
type OpenFileError struct {
70+
error
71+
}
72+
73+
// Unwrap implements Wrapper interface
74+
func (e OpenFileError) Unwrap() error {
75+
return e.error
76+
}
77+
78+
// CreateDirectoryError is a wrapper error that will be used for errors when creating a directory
79+
type CreateDirectoryError struct {
80+
error
81+
}
82+
83+
// Unwrap implements Wrapper interface
84+
func (e CreateDirectoryError) Unwrap() error {
85+
return e.error
86+
}
87+
88+
// CreateFileError is a wrapper error that will be used for errors when creating a file
89+
type CreateFileError struct {
90+
error
91+
}
92+
93+
// Unwrap implements Wrapper interface
94+
func (e CreateFileError) Unwrap() error {
95+
return e.error
96+
}
97+
98+
// ReadFileError is a wrapper error that will be used for errors when reading a file
99+
type ReadFileError struct {
100+
error
101+
}
102+
103+
// Unwrap implements Wrapper interface
104+
func (e ReadFileError) Unwrap() error {
105+
return e.error
106+
}
107+
108+
// WriteFileError is a wrapper error that will be used for errors when writing a file
109+
type WriteFileError struct {
110+
error
111+
}
112+
113+
// Unwrap implements Wrapper interface
114+
func (e WriteFileError) Unwrap() error {
115+
return e.error
116+
}
117+
118+
// CloseFileError is a wrapper error that will be used for errors when closing a file
119+
type CloseFileError struct {
120+
error
121+
}
122+
123+
// Unwrap implements Wrapper interface
124+
func (e CloseFileError) Unwrap() error {
125+
return e.error
126+
}
127+
128+
// ModelAlreadyExistsError is returned if the file is expected not to exist but a previous model does
129+
type ModelAlreadyExistsError struct {
130+
path string
131+
}
132+
133+
// Error implements error interface
134+
func (e ModelAlreadyExistsError) Error() string {
135+
return fmt.Sprintf("failed to create %s: model already exists", e.path)
136+
}
137+
138+
// UnknownIfExistsActionError is returned if the if-exists-action is unknown
139+
type UnknownIfExistsActionError struct {
140+
path string
141+
ifExistsAction file.IfExistsAction
142+
}
143+
144+
// Error implements error interface
145+
func (e UnknownIfExistsActionError) Error() string {
146+
return fmt.Sprintf("unknown behavior if file exists (%d) for %s", e.ifExistsAction, e.path)
147+
}
148+
149+
// FileAlreadyExistsError is returned if the file is expected not to exist but it does
150+
type FileAlreadyExistsError struct {
151+
path string
152+
}
153+
154+
// Error implements error interface
155+
func (e FileAlreadyExistsError) Error() string {
156+
return fmt.Sprintf("failed to create %s: file already exists", e.path)
157+
}

pkg/machinery/errors_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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"
24+
. "github.com/onsi/ginkgo/extensions/table"
25+
. "github.com/onsi/gomega"
26+
)
27+
28+
var _ = Describe("Errors", func() {
29+
var (
30+
path = filepath.Join("path", "to", "file")
31+
testErr = errors.New("test error")
32+
)
33+
34+
DescribeTable("should contain the wrapped error",
35+
func(err error) {
36+
Expect(errors.Is(err, testErr)).To(BeTrue())
37+
},
38+
Entry("for validate errors", ValidateError{testErr}),
39+
Entry("for set template defaults errors", SetTemplateDefaultsError{testErr}),
40+
Entry("for plugin errors", PluginError{testErr}),
41+
Entry("for file existence errors", ExistsFileError{testErr}),
42+
Entry("for file opening errors", OpenFileError{testErr}),
43+
Entry("for directory creation errors", CreateDirectoryError{testErr}),
44+
Entry("for file creation errors", CreateFileError{testErr}),
45+
Entry("for file reading errors", ReadFileError{testErr}),
46+
Entry("for file writing errors", WriteFileError{testErr}),
47+
Entry("for file closing errors", CloseFileError{testErr}),
48+
)
49+
50+
// NOTE: the following test increases coverage
51+
It("should print a descriptive error message", func() {
52+
Expect(ModelAlreadyExistsError{path}.Error()).To(ContainSubstring("model already exists"))
53+
Expect(UnknownIfExistsActionError{path, -1}.Error()).To(ContainSubstring("unknown behavior if file exists"))
54+
Expect(FileAlreadyExistsError{path}.Error()).To(ContainSubstring("file already exists"))
55+
})
56+
})
File renamed without changes.

0 commit comments

Comments
 (0)