github.com/waynz0r/controller-tools@v0.4.1-0.20200916220028-16254aeef2d7/pkg/markers/collect_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  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  
    23  	. "sigs.k8s.io/controller-tools/pkg/markers"
    24  )
    25  
    26  type fieldPath struct {
    27  	typ   string
    28  	field string
    29  }
    30  
    31  var _ = Describe("Collecting", func() {
    32  	var col *Collector
    33  	var markersByType map[string]MarkerValues
    34  	var markersByField map[fieldPath]MarkerValues
    35  
    36  	JustBeforeEach(func() {
    37  		By("setting up the registry and collector")
    38  		reg := &Registry{}
    39  
    40  		mustDefine(reg, "testing:pkglvl", DescribesPackage, "")
    41  		mustDefine(reg, "testing:typelvl", DescribesType, "")
    42  		mustDefine(reg, "testing:fieldlvl", DescribesField, "")
    43  		mustDefine(reg, "testing:eitherlvl", DescribesType, "")
    44  		mustDefine(reg, "testing:eitherlvl", DescribesPackage, "")
    45  
    46  		col = &Collector{Registry: reg}
    47  
    48  		By("gathering markers by type name")
    49  		markersByType = make(map[string]MarkerValues)
    50  		markersByField = make(map[fieldPath]MarkerValues)
    51  		err := EachType(col, fakePkg, func(info *TypeInfo) {
    52  			markersByType[info.Name] = info.Markers
    53  			for _, field := range info.Fields {
    54  				markersByField[fieldPath{typ: info.Name, field: field.Name}] = field.Markers
    55  			}
    56  		})
    57  		Expect(err).NotTo(HaveOccurred())
    58  		Expect(fakePkg.Errors).To(HaveLen(0))
    59  	})
    60  
    61  	Context("of package-level markers", func() {
    62  
    63  		It("should consider markers anywhere not obviously type- or field-level as package-level", func() {
    64  			By("grabbing all package-level markers")
    65  			pkgMarkers, err := PackageMarkers(col, fakePkg)
    66  			Expect(err).NotTo(HaveOccurred())
    67  			Expect(fakePkg.Errors).To(HaveLen(0))
    68  
    69  			By("checking that it only contains package-level markers")
    70  			Expect(pkgMarkers).NotTo(HaveKey("testing:typelvl"))
    71  
    72  			By("checking that it contains the right package-level markers")
    73  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here unattached")))
    74  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here at end after last node")))
    75  
    76  			By("checking that it doesn't contain any markers it's not supposed to")
    77  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", Not(ContainElement(ContainSubstring("not here")))))
    78  		})
    79  	})
    80  
    81  	Context("of type-level markers", func() {
    82  		It("should not have package-level markers associated", func() {
    83  			Expect(markersByType).To(HaveKeyWithValue("Foo", Not(HaveKey("testing:pkglvl"))))
    84  		})
    85  
    86  		It("should not contain markers it's not supposed to", func() {
    87  			Expect(markersByType).NotTo(ContainElement(HaveKeyWithValue("testing:typelvl", ContainElement(ContainSubstring("not here")))))
    88  			Expect(markersByType).NotTo(ContainElement(HaveKeyWithValue("testing:eitherlvl", ContainElement(ContainSubstring("not here")))))
    89  		})
    90  
    91  		Context("with godoc", func() {
    92  			It("should associate markers in godoc", func() {
    93  				Expect(markersByType).To(HaveKeyWithValue("Foo",
    94  					HaveKeyWithValue("testing:typelvl", ContainElement("here on type"))))
    95  			})
    96  
    97  			It("should associate markers in the closest non-godoc block", func() {
    98  				Expect(markersByType).To(HaveKeyWithValue("Foo",
    99  					HaveKeyWithValue("testing:typelvl", ContainElement("here before type"))))
   100  			})
   101  
   102  			It("should re-associate package-level markers from the closest non-godoc", func() {
   103  				By("grabbing package markers")
   104  				pkgMarkers, err := PackageMarkers(col, fakePkg)
   105  				Expect(err).NotTo(HaveOccurred())
   106  				Expect(fakePkg.Errors).To(HaveLen(0))
   107  
   108  				By("checking that the marker got reassociated")
   109  				Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here reassociated")))
   110  			})
   111  		})
   112  		Context("without godoc", func() {
   113  			It("should associate markers in the closest block", func() {
   114  				Expect(markersByType).To(HaveKeyWithValue("Quux",
   115  					HaveKeyWithValue("testing:typelvl", ContainElement("here without godoc"))))
   116  			})
   117  
   118  			It("should re-associate package-level markers from the closest block", func() {
   119  				By("grabbing package markers")
   120  				pkgMarkers, err := PackageMarkers(col, fakePkg)
   121  				Expect(err).NotTo(HaveOccurred())
   122  				Expect(fakePkg.Errors).To(HaveLen(0))
   123  
   124  				By("checking that the marker got reassociated")
   125  				Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here reassociated no godoc")))
   126  
   127  			})
   128  		})
   129  
   130  		It("should not re-associate package-level markers in godoc", func() {
   131  			By("grabbing package markers")
   132  			pkgMarkers, err := PackageMarkers(col, fakePkg)
   133  			Expect(err).NotTo(HaveOccurred())
   134  			Expect(fakePkg.Errors).To(HaveLen(0))
   135  
   136  			By("checking that the marker didn't get reassociated")
   137  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", Not(ContainElement("not here godoc"))))
   138  		})
   139  
   140  		It("should not re-associate package-level markers if there's also a type-level marker", func() {
   141  			By("checking that the type has the marker")
   142  			Expect(markersByType).To(HaveKeyWithValue("Foo",
   143  				HaveKeyWithValue("testing:eitherlvl", ContainElement("here not reassociated"))))
   144  
   145  			By("grabbing package markers")
   146  			pkgMarkers, err := PackageMarkers(col, fakePkg)
   147  			Expect(err).NotTo(HaveOccurred())
   148  			Expect(fakePkg.Errors).To(HaveLen(0))
   149  
   150  			By("checking that the marker didn't get reassociated to the package")
   151  			Expect(pkgMarkers).To(SatisfyAny(
   152  				Not(HaveKey("testing:eitherlvl")),
   153  				HaveKeyWithValue("testing:eitherlvl", Not(ContainElement("here not reassociated")))))
   154  
   155  		})
   156  
   157  		It("should consider markers on the gendecl even if there are no more markers in the file", func() {
   158  			Expect(markersByType).To(HaveKeyWithValue("Cheese",
   159  				HaveKeyWithValue("testing:typelvl", ContainElement("here on typedecl with no more"))))
   160  
   161  		})
   162  	})
   163  
   164  	Context("of field-level markers", func() {
   165  		It("should not contain markers it's not supposed to", func() {
   166  			Expect(markersByField).NotTo(ContainElement(HaveKeyWithValue("testing:fieldlvl", ContainElement(ContainSubstring("not here")))))
   167  		})
   168  		Context("with godoc", func() {
   169  			It("should associate markers in godoc", func() {
   170  				Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
   171  					HaveKeyWithValue("testing:fieldlvl", ContainElement("here in godoc"))))
   172  			})
   173  
   174  			It("should associate markers in the closest non-godoc block", func() {
   175  				Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
   176  					HaveKeyWithValue("testing:fieldlvl", ContainElement("here before godoc"))))
   177  			})
   178  		})
   179  		Context("without godoc", func() {
   180  			It("should associate markers in the closest block", func() {
   181  				Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithoutGodoc"},
   182  					HaveKeyWithValue("testing:fieldlvl", ContainElement("here without godoc"))))
   183  			})
   184  		})
   185  
   186  		It("should not consider markers at the end of a line", func() {
   187  			Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
   188  				HaveKeyWithValue("testing:fieldlvl", Not(ContainElement("not here after field")))))
   189  
   190  			Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithoutGodoc"},
   191  				HaveKeyWithValue("testing:fieldlvl", Not(ContainElement("not here after field")))))
   192  		})
   193  	})
   194  })