-
Notifications
You must be signed in to change notification settings - Fork 399
/
markers_suite_test.go
146 lines (111 loc) · 3.85 KB
/
markers_suite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2019 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 markers_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pkgstest "golang.org/x/tools/go/packages/packagestest"
"sigs.k8s.io/controller-tools/pkg/loader"
testloader "sigs.k8s.io/controller-tools/pkg/loader/testutils"
)
func TestMarkers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Markers Suite")
}
// do this once because it creates files
var fakePkg *loader.Package
var exported *pkgstest.Exported
var _ = BeforeSuite(func() {
modules := []pkgstest.Module{
{
Name: "sigs.k8s.io/controller-tools/pkg/markers/testdata",
Files: map[string]interface{}{
"file.go": `
package testdata
import (
// nothing here should be parsed
// +testing:pkglvl="not here import 1"
// +testing:pkglvl="not here import 2"
foo "fmt" // +testing:pkglvl="not here import 3"
// +testing:pkglvl="not here import 4"
)
// +testing:pkglvl="here unattached"
// +testing:pkglvl="here reassociated"
// +testing:typelvl="here before type"
// +testing:eitherlvl="here not reassociated"
// +testing:fieldlvl="not here not near field"
// normal godoc
// +testing:pkglvl="not here godoc"
// +testing:typelvl="here on type"
// normal godoc
type Foo struct {
// +testing:pkglvl="not here in struct"
// +testing:fieldlvl="here before godoc"
// normal godoc
// +testing:fieldlvl="here in godoc"
// +testing:pkglvl="not here godoc"
// normal godoc
WithGodoc string // +testing:fieldlvl="not here after field"
// +testing:fieldlvl="here without godoc"
WithoutGodoc int
} // +testing:pkglvl="not here after type"
// +testing:pkglvl="not here on var"
var (
// +testing:pkglvl="not here in var"
Bar = "foo"
)
/* This type of doc has spaces preserved in go-ast, but we'd like to trim them. */
type HasDocsWithSpaces struct {
}
/*
This type of doc has spaces preserved in go-ast, but we'd like to trim them,
especially when formatted like this.
*/
type HasDocsWithSpaces2 struct {
}
type Baz interface {
// +testing:pkglvl="not here in interface"
}
// +testing:typelvl="not here beyond closest"
// +testing:typelvl="here without godoc"
// +testing:pkglvl="here reassociated no godoc"
type Quux string
// +testing:pkglvl="here at end after last node"
/* +testing:pkglvl="not here in block" */
// +testing:typelvl="here on typedecl with no more"
type Cheese struct { }
// ensure that we're fine if we've got an end-of-line
// comment that's the last comment of the file, but
// we still have a bit more to traverse (field list --> ident).
// THIS MUST CONTAIN THE LAST COMMENT IN THE FILE
// TODO(directxman12): split this off into its own case
type private struct {
bar int // not collected
}
`,
},
},
}
By("setting up the fake packages")
var pkgs []*loader.Package
var err error
pkgs, exported, err = testloader.LoadFakeRoots(pkgstest.Modules, modules, "sigs.k8s.io/controller-tools/pkg/markers/testdata")
Expect(err).NotTo(HaveOccurred())
Expect(pkgs).To(HaveLen(1))
fakePkg = pkgs[0]
})
var _ = AfterSuite(func() {
By("cleaning up the fake packages")
exported.Cleanup()
})