@@ -17,34 +17,52 @@ limitations under the License.
17
17
package deepcopy_test
18
18
19
19
import (
20
+ "io"
20
21
"io/ioutil"
21
22
"os"
23
+ "sort"
22
24
23
25
"github.com/google/go-cmp/cmp"
24
26
. "github.com/onsi/ginkgo"
25
27
. "github.com/onsi/gomega"
26
- "golang.org/x/tools/go/packages"
27
28
29
+ "sigs.k8s.io/controller-tools/pkg/crd"
28
30
"sigs.k8s.io/controller-tools/pkg/deepcopy"
31
+ "sigs.k8s.io/controller-tools/pkg/genall"
29
32
"sigs.k8s.io/controller-tools/pkg/loader"
30
33
"sigs.k8s.io/controller-tools/pkg/markers"
31
34
)
32
35
33
- func packageErrors (pkg * loader.Package , filterKinds ... packages.ErrorKind ) error {
34
- toSkip := make (map [packages.ErrorKind ]struct {})
35
- for _ , errKind := range filterKinds {
36
- toSkip [errKind ] = struct {}{}
36
+ type outputToMap map [string ]* outputFile
37
+
38
+ // Open implements genall.OutputRule.
39
+ func (m outputToMap ) Open (_ * loader.Package , path string ) (io.WriteCloser , error ) {
40
+ if _ , ok := m [path ]; ! ok {
41
+ m [path ] = & outputFile {}
37
42
}
38
- var outErr error
39
- packages .Visit ([]* packages.Package {pkg .Package }, nil , func (pkgRaw * packages.Package ) {
40
- for _ , err := range pkgRaw .Errors {
41
- if _ , skip := toSkip [err .Kind ]; skip {
42
- continue
43
- }
44
- outErr = err
45
- }
46
- })
47
- return outErr
43
+ return m [path ], nil
44
+ }
45
+
46
+ func (m outputToMap ) fileList () []string {
47
+ ret := make ([]string , 0 , len (m ))
48
+ for path := range m {
49
+ ret = append (ret , path )
50
+ }
51
+ sort .Strings (ret )
52
+ return ret
53
+ }
54
+
55
+ type outputFile struct {
56
+ contents []byte
57
+ }
58
+
59
+ func (o * outputFile ) Write (p []byte ) (int , error ) {
60
+ o .contents = append (o .contents , p ... )
61
+ return len (p ), nil
62
+ }
63
+
64
+ func (o * outputFile ) Close () error {
65
+ return nil
48
66
}
49
67
50
68
var _ = Describe ("CRD Generation From Parsing to CustomResourceDefinition" , func () {
@@ -55,35 +73,37 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
55
73
Expect (os .Chdir ("./testdata" )).To (Succeed ()) // go modules are directory-sensitive
56
74
defer func () { Expect (os .Chdir (cwd )).To (Succeed ()) }()
57
75
58
- By ("loading the roots" )
59
- pkgs , err := loader .LoadRoots ("." )
60
- Expect (err ).NotTo (HaveOccurred ())
61
- Expect (pkgs ).To (HaveLen (1 ))
62
- cronJobPkg := pkgs [0 ]
76
+ output := make (outputToMap )
63
77
64
- By ("setting up the generation context" )
65
- collector := & markers.Collector {Registry : & markers.Registry {}}
66
- Expect (deepcopy.Generator {}.RegisterMarkers (collector .Registry )).To (Succeed ())
67
- checker := & loader.TypeChecker {}
68
- ctx := & deepcopy.ObjectGenCtx {
69
- Collector : collector ,
70
- Checker : checker ,
71
- }
72
-
73
- By ("requesting that types be generated" )
74
- outContents := ctx .GenerateForPackage (cronJobPkg )
78
+ By ("initializing the runtime" )
79
+ optionsRegistry := & markers.Registry {}
80
+ Expect (optionsRegistry .Register (markers .Must (markers .MakeDefinition ("crd" , markers .DescribesPackage , crd.Generator {})))).To (Succeed ())
81
+ Expect (optionsRegistry .Register (markers .Must (markers .MakeDefinition ("object" , markers .DescribesPackage , deepcopy.Generator {})))).To (Succeed ())
82
+ rt , err := genall .FromOptions (optionsRegistry , []string {
83
+ "crd" , // Run another generator first to make sure they don't interfere; see also: the comment on cronjob_types.go:UntypedBlob
84
+ "object" ,
85
+ })
86
+ Expect (err ).NotTo (HaveOccurred ())
87
+ rt .OutputRules = genall.OutputRules {Default : output }
75
88
76
- By ("checking that no errors occurred along the way (expect for type errors) " )
77
- Expect ( packageErrors ( cronJobPkg , packages . TypeError )). NotTo ( HaveOccurred () )
89
+ By ("running the generator and checking for errors" )
90
+ hadErrs := rt . Run ( )
78
91
79
92
By ("checking that we got output contents" )
93
+ Expect (output .fileList ()).To (ContainElement ("zz_generated.deepcopy.go" )) // Don't use HaveKey--the output is too verbose to be usable
94
+ outFile := output ["zz_generated.deepcopy.go" ]
95
+ Expect (outFile ).NotTo (BeNil ())
96
+ outContents := outFile .contents
80
97
Expect (outContents ).NotTo (BeNil ())
81
98
82
99
By ("loading the desired code" )
83
100
expectedFile , err := ioutil .ReadFile ("zz_generated.deepcopy.go" )
84
101
Expect (err ).NotTo (HaveOccurred ())
85
102
86
103
By ("comparing the two" )
87
- Expect (outContents ).To (Equal (expectedFile ), "generated code not as expected, check pkg/deepcopy/testdata/README.md for more details.\n \n Diff:\n \n %s" , cmp .Diff (outContents , expectedFile ))
104
+ Expect (string (outContents )).To (Equal (string (expectedFile )), "generated code not as expected, check pkg/deepcopy/testdata/README.md for more details.\n \n Diff:\n \n %s" , cmp .Diff (outContents , expectedFile ))
105
+
106
+ By ("checking for errors" )
107
+ Expect (hadErrs ).To (BeFalse ())
88
108
})
89
109
})
0 commit comments