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