github.com/matthchr/controller-tools@v0.3.1-0.20200602225425-d33ced351ff8/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 "github.com/matthchr/controller-tools/pkg/loader" 27 testloader "github.com/matthchr/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: "github.com/matthchr/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 type Baz interface { 92 // +testing:pkglvl="not here in interface" 93 } 94 95 // +testing:typelvl="not here beyond closest" 96 97 // +testing:typelvl="here without godoc" 98 // +testing:pkglvl="here reassociated no godoc" 99 100 type Quux string 101 102 // +testing:pkglvl="here at end after last node" 103 104 /* +testing:pkglvl="not here in block" */ 105 106 // +testing:typelvl="here on typedecl with no more" 107 type Cheese struct { } 108 109 // ensure that we're fine if we've got an end-of-line 110 // comment that's the last comment of the file, but 111 // we still have a bit more to traverse (field list --> ident). 112 // THIS MUST CONTAIN THE LAST COMMENT IN THE FILE 113 // TODO(directxman12): split this off into its own case 114 type private struct { 115 bar int // not collected 116 } 117 `, 118 }, 119 }, 120 } 121 122 By("setting up the fake packages") 123 var pkgs []*loader.Package 124 var err error 125 pkgs, exported, err = testloader.LoadFakeRoots(pkgstest.Modules, modules, "github.com/matthchr/controller-tools/pkg/markers/testdata") 126 Expect(err).NotTo(HaveOccurred()) 127 Expect(pkgs).To(HaveLen(1)) 128 129 fakePkg = pkgs[0] 130 }) 131 132 var _ = AfterSuite(func() { 133 By("cleaning up the fake packages") 134 exported.Cleanup() 135 })