Skip to content

Commit 7049f31

Browse files
committed
always error on iface tests, avoid separate testdata dir
1 parent 2ebad8e commit 7049f31

File tree

8 files changed

+111
-229
lines changed

8 files changed

+111
-229
lines changed

pkg/crd/gen_integration_test.go

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434

3535
var _ = Describe("CRD Generation proper defaulting", func() {
3636
var (
37-
ctx, ctx2 *genall.GenerationContext
38-
out *outputRule
37+
ctx, ctx2, ctx3 *genall.GenerationContext
38+
out *outputRule
3939

4040
genDir = filepath.Join("testdata", "gen")
4141
)
@@ -53,7 +53,10 @@ var _ = Describe("CRD Generation proper defaulting", func() {
5353
Expect(pkgs).To(HaveLen(1))
5454
pkgs2, err := loader.LoadRoots("./...")
5555
Expect(err).NotTo(HaveOccurred())
56-
Expect(pkgs2).To(HaveLen(2))
56+
Expect(pkgs2).To(HaveLen(3))
57+
pkgs3, err := loader.LoadRoots("./iface")
58+
Expect(err).NotTo(HaveOccurred())
59+
Expect(pkgs3).To(HaveLen(1))
5760

5861
By("setup up the context")
5962
reg := &markers.Registry{}
@@ -73,6 +76,12 @@ var _ = Describe("CRD Generation proper defaulting", func() {
7376
Checker: &loader.TypeChecker{},
7477
OutputRule: out,
7578
}
79+
ctx3 = &genall.GenerationContext{
80+
Collector: &markers.Collector{Registry: reg},
81+
Roots: pkgs3,
82+
Checker: &loader.TypeChecker{},
83+
OutputRule: out,
84+
}
7685
})
7786

7887
It("should fail to generate v1beta1 CRDs", func() {
@@ -106,13 +115,15 @@ var _ = Describe("CRD Generation proper defaulting", func() {
106115
Expect(gen.Generate(ctx2)).NotTo(HaveOccurred())
107116

108117
By("loading the desired YAMLs")
118+
expectedFileIfaces, err := os.ReadFile(filepath.Join(genDir, "iface", "iface.example.com_kindwithifaces.yaml"))
119+
Expect(err).NotTo(HaveOccurred())
109120
expectedFileFoos, err := os.ReadFile(filepath.Join(genDir, "bar.example.com_foos.yaml"))
110121
Expect(err).NotTo(HaveOccurred())
111122
expectedFileZoos, err := os.ReadFile(filepath.Join(genDir, "zoo", "bar.example.com_zoos.yaml"))
112123
Expect(err).NotTo(HaveOccurred())
113124

114-
By("comparing the two, output must be deterministic because groupKinds are sorted")
115-
expectedOut := string(expectedFileFoos) + string(expectedFileZoos)
125+
By("comparing the three, output must be deterministic because groupKinds are sorted")
126+
expectedOut := string(expectedFileFoos) + string(expectedFileIfaces) + string(expectedFileZoos)
116127
Expect(out.buf.String()).To(Equal(expectedOut), cmp.Diff(out.buf.String(), expectedOut))
117128
})
118129

@@ -169,43 +180,25 @@ var _ = Describe("CRD Generation proper defaulting", func() {
169180
By("comparing the two")
170181
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile)))
171182
})
172-
})
173183

174-
var _ = Describe("CRD Generation with any", func() {
175-
It("Works", func() {
176-
oldWorkingDir, err := os.Getwd()
184+
It("should gracefully error on interface types", func() {
185+
gen := &crd.Generator{}
186+
err := gen.Generate(ctx3)
177187
Expect(err).NotTo(HaveOccurred())
178-
const testModuleDir = "testdata2"
179-
Expect(os.Chdir(testModuleDir)).To(Succeed()) // go modules are directory-sensitive
180-
defer func() {
181-
Expect(os.Chdir(oldWorkingDir)).To(Succeed())
182-
}()
183188

184-
pkgs, err := loader.LoadRoots("./v1/...")
189+
wd, err := os.Getwd()
185190
Expect(err).NotTo(HaveOccurred())
186-
187-
reg := &markers.Registry{}
188-
Expect(crdmarkers.Register(reg)).To(Succeed())
189-
gen := &crd.Generator{}
190-
out := &outputRule{
191-
buf: &bytes.Buffer{},
191+
matches := 0
192+
for _, pkg := range ctx3.Roots {
193+
for _, pkgError := range pkg.Errors {
194+
posRel, err := filepath.Rel(filepath.Join(wd, genDir), pkgError.Pos)
195+
Expect(err).NotTo(HaveOccurred())
196+
Expect(posRel).To(Equal("iface/iface_types.go:32:6"))
197+
Expect(pkgError.Msg).To(Equal("cannot generate schema for interface type any"))
198+
matches++
199+
}
192200
}
193-
ctx := &genall.GenerationContext{
194-
Collector: &markers.Collector{
195-
Registry: reg,
196-
},
197-
Roots: pkgs,
198-
Checker: &loader.TypeChecker{
199-
NodeFilters: []loader.NodeFilter{gen.CheckFilter()},
200-
},
201-
OutputRule: out,
202-
}
203-
err = gen.Generate(ctx)
204-
Expect(err).NotTo(HaveOccurred())
205-
wantCRDYAML, err := os.ReadFile("v1/example.com_foos.yaml")
206-
Expect(err).NotTo(HaveOccurred())
207-
gotCRDYAML := out.buf.String()
208-
Expect(gotCRDYAML).To(Equal(string(wantCRDYAML)), cmp.Diff(gotCRDYAML, string(wantCRDYAML)))
201+
Expect(matches).To(Equal(1))
209202
})
210203
})
211204

pkg/crd/schema.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,8 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
294294
Format: fmt,
295295
}
296296
}
297-
if ifaceInfo, isInterface := typeInfo.(*types.Interface); isInterface {
298-
// The "any" type introduced in Go1.18 shows up as an identifier / empty interface.
299-
if !ifaceInfo.Empty() {
300-
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("cannot generate schema for non-empty interface type %s", ident.Name), ident))
301-
}
297+
if _, isInterface := typeInfo.(*types.Interface); isInterface {
298+
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("cannot generate schema for interface type %s", ident.Name), ident))
302299
return &apiext.JSONSchemaProps{}
303300
}
304301
// NB(directxman12): if there are dot imports, this might be an external reference,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: (devel)
7+
name: kindwithifaces.iface.example.com
8+
spec:
9+
group: iface.example.com
10+
names:
11+
kind: KindWithIFace
12+
listKind: KindWithIFaceList
13+
plural: kindwithifaces
14+
singular: kindwithiface
15+
scope: Namespaced
16+
versions:
17+
- name: iface
18+
schema:
19+
openAPIV3Schema:
20+
properties:
21+
apiVersion:
22+
description: |-
23+
APIVersion defines the versioned schema of this representation of an object.
24+
Servers should convert recognized schemas to the latest internal value, and
25+
may reject unrecognized values.
26+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
27+
type: string
28+
kind:
29+
description: |-
30+
Kind is a string value representing the REST resource this object represents.
31+
Servers may infer this from the endpoint the client submits requests to.
32+
Cannot be updated.
33+
In CamelCase.
34+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
35+
type: string
36+
metadata:
37+
type: object
38+
spec:
39+
properties:
40+
bar: {}
41+
type: object
42+
required:
43+
- metadata
44+
type: object
45+
served: true
46+
storage: true
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
//go:generate ../../../../../.run-controller-gen.sh crd:crdVersions=v1 paths=. output:dir=.
17+
18+
// +groupName=iface.example.com
19+
package iface
20+
21+
import (
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
)
24+
25+
type KindWithIFace struct {
26+
metav1.TypeMeta `json:",inline"`
27+
metav1.ObjectMeta `json:"metadata"`
28+
Spec KindWithIFaceSpec `json:"spec,omitempty"`
29+
}
30+
31+
type KindWithIFaceSpec struct {
32+
Bar any `json:"bar,omitempty"`
33+
}

pkg/crd/testdata2/go.mod

Lines changed: 0 additions & 26 deletions
This file was deleted.

pkg/crd/testdata2/go.sum

Lines changed: 0 additions & 97 deletions
This file was deleted.

pkg/crd/testdata2/v1/example.com_foos.yaml

Lines changed: 0 additions & 45 deletions
This file was deleted.

pkg/crd/testdata2/v1/foo_types.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)