github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/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 docsByType map[string]string
    35  
    36  	var markersByField map[fieldPath]MarkerValues
    37  	var docsByField map[fieldPath]string
    38  
    39  	JustBeforeEach(func() {
    40  		By("setting up the registry and collector")
    41  		reg := &Registry{}
    42  
    43  		mustDefine(reg, "testing:pkglvl", DescribesPackage, "")
    44  		mustDefine(reg, "testing:typelvl", DescribesType, "")
    45  		mustDefine(reg, "testing:fieldlvl", DescribesField, "")
    46  		mustDefine(reg, "testing:eitherlvl", DescribesType, "")
    47  		mustDefine(reg, "testing:eitherlvl", DescribesPackage, "")
    48  
    49  		col = &Collector{Registry: reg}
    50  
    51  		By("gathering markers/docs by type/field name")
    52  		markersByType = make(map[string]MarkerValues)
    53  		docsByType = make(map[string]string)
    54  		markersByField = make(map[fieldPath]MarkerValues)
    55  		docsByField = make(map[fieldPath]string)
    56  
    57  		err := EachType(col, fakePkg, func(info *TypeInfo) {
    58  			markersByType[info.Name] = info.Markers
    59  			docsByType[info.Name] = info.Doc
    60  
    61  			for _, field := range info.Fields {
    62  				fieldPath := fieldPath{typ: info.Name, field: field.Name}
    63  				markersByField[fieldPath] = field.Markers
    64  				docsByField[fieldPath] = field.Doc
    65  			}
    66  		})
    67  
    68  		Expect(err).NotTo(HaveOccurred())
    69  		Expect(fakePkg.Errors).To(HaveLen(0))
    70  	})
    71  
    72  	Context("of package-level markers", func() {
    73  
    74  		It("should consider markers anywhere not obviously type- or field-level as package-level", func() {
    75  			By("grabbing all package-level markers")
    76  			pkgMarkers, err := PackageMarkers(col, fakePkg)
    77  			Expect(err).NotTo(HaveOccurred())
    78  			Expect(fakePkg.Errors).To(HaveLen(0))
    79  
    80  			By("checking that it only contains package-level markers")
    81  			Expect(pkgMarkers).NotTo(HaveKey("testing:typelvl"))
    82  
    83  			By("checking that it contains the right package-level markers")
    84  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here unattached")))
    85  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here at end after last node")))
    86  
    87  			By("checking that it doesn't contain any markers it's not supposed to")
    88  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", Not(ContainElement(ContainSubstring("not here")))))
    89  		})
    90  	})
    91  
    92  	Context("of type-level markers", func() {
    93  		It("should not have package-level markers associated", func() {
    94  			Expect(markersByType).To(HaveKeyWithValue("Foo", Not(HaveKey("testing:pkglvl"))))
    95  		})
    96  
    97  		It("should not contain markers it's not supposed to", func() {
    98  			Expect(markersByType).NotTo(ContainElement(HaveKeyWithValue("testing:typelvl", ContainElement(ContainSubstring("not here")))))
    99  			Expect(markersByType).NotTo(ContainElement(HaveKeyWithValue("testing:eitherlvl", ContainElement(ContainSubstring("not here")))))
   100  		})
   101  
   102  		Context("with godoc", func() {
   103  			It("should associate markers in godoc", func() {
   104  				Expect(markersByType).To(HaveKeyWithValue("Foo",
   105  					HaveKeyWithValue("testing:typelvl", ContainElement("here on type"))))
   106  			})
   107  
   108  			It("should have docs without markers", func() {
   109  				Expect(docsByType).To(HaveKeyWithValue("Foo", "normal godoc normal godoc"))
   110  			})
   111  
   112  			It("should associate markers in the closest non-godoc block", func() {
   113  				Expect(markersByType).To(HaveKeyWithValue("Foo",
   114  					HaveKeyWithValue("testing:typelvl", ContainElement("here before type"))))
   115  			})
   116  
   117  			It("should re-associate package-level markers from the closest non-godoc", func() {
   118  				By("grabbing package markers")
   119  				pkgMarkers, err := PackageMarkers(col, fakePkg)
   120  				Expect(err).NotTo(HaveOccurred())
   121  				Expect(fakePkg.Errors).To(HaveLen(0))
   122  
   123  				By("checking that the marker got reassociated")
   124  				Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here reassociated")))
   125  			})
   126  		})
   127  
   128  		Context("types with /*…*/-style comments", func() {
   129  			It("should have doc without extraneous spaces", func() {
   130  				Expect(docsByType).To(HaveKeyWithValue("HasDocsWithSpaces",
   131  					"This type of doc has spaces preserved in go-ast, but we'd like to trim them."))
   132  			})
   133  
   134  			It("should have doc without extraneous spaces, even over multiple lines", func() {
   135  				Expect(docsByType).To(HaveKeyWithValue("HasDocsWithSpaces2",
   136  					"This type of doc has spaces preserved in go-ast, but we'd like to trim them, especially when formatted like this."))
   137  			})
   138  		})
   139  
   140  		Context("without godoc", func() {
   141  			It("should associate markers in the closest block", func() {
   142  				Expect(markersByType).To(HaveKeyWithValue("Quux",
   143  					HaveKeyWithValue("testing:typelvl", ContainElement("here without godoc"))))
   144  			})
   145  
   146  			It("should re-associate package-level markers from the closest block", func() {
   147  				By("grabbing package markers")
   148  				pkgMarkers, err := PackageMarkers(col, fakePkg)
   149  				Expect(err).NotTo(HaveOccurred())
   150  				Expect(fakePkg.Errors).To(HaveLen(0))
   151  
   152  				By("checking that the marker got reassociated")
   153  				Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here reassociated no godoc")))
   154  
   155  			})
   156  		})
   157  
   158  		It("should not re-associate package-level markers in godoc", func() {
   159  			By("grabbing package markers")
   160  			pkgMarkers, err := PackageMarkers(col, fakePkg)
   161  			Expect(err).NotTo(HaveOccurred())
   162  			Expect(fakePkg.Errors).To(HaveLen(0))
   163  
   164  			By("checking that the marker didn't get reassociated")
   165  			Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", Not(ContainElement("not here godoc"))))
   166  		})
   167  
   168  		It("should not re-associate package-level markers if there's also a type-level marker", func() {
   169  			By("checking that the type has the marker")
   170  			Expect(markersByType).To(HaveKeyWithValue("Foo",
   171  				HaveKeyWithValue("testing:eitherlvl", ContainElement("here not reassociated"))))
   172  
   173  			By("grabbing package markers")
   174  			pkgMarkers, err := PackageMarkers(col, fakePkg)
   175  			Expect(err).NotTo(HaveOccurred())
   176  			Expect(fakePkg.Errors).To(HaveLen(0))
   177  
   178  			By("checking that the marker didn't get reassociated to the package")
   179  			Expect(pkgMarkers).To(SatisfyAny(
   180  				Not(HaveKey("testing:eitherlvl")),
   181  				HaveKeyWithValue("testing:eitherlvl", Not(ContainElement("here not reassociated")))))
   182  
   183  		})
   184  
   185  		It("should consider markers on the gendecl even if there are no more markers in the file", func() {
   186  			Expect(markersByType).To(HaveKeyWithValue("Cheese",
   187  				HaveKeyWithValue("testing:typelvl", ContainElement("here on typedecl with no more"))))
   188  
   189  		})
   190  	})
   191  
   192  	Context("of field-level markers", func() {
   193  		It("should not contain markers it's not supposed to", func() {
   194  			Expect(markersByField).NotTo(ContainElement(HaveKeyWithValue("testing:fieldlvl", ContainElement(ContainSubstring("not here")))))
   195  		})
   196  		Context("with godoc", func() {
   197  			It("should associate markers in godoc", func() {
   198  				Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
   199  					HaveKeyWithValue("testing:fieldlvl", ContainElement("here in godoc"))))
   200  			})
   201  
   202  			It("should associate markers in the closest non-godoc block", func() {
   203  				Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
   204  					HaveKeyWithValue("testing:fieldlvl", ContainElement("here before godoc"))))
   205  			})
   206  		})
   207  		Context("without godoc", func() {
   208  			It("should associate markers in the closest block", func() {
   209  				Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithoutGodoc"},
   210  					HaveKeyWithValue("testing:fieldlvl", ContainElement("here without godoc"))))
   211  			})
   212  		})
   213  
   214  		It("should not consider markers at the end of a line", func() {
   215  			Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
   216  				HaveKeyWithValue("testing:fieldlvl", Not(ContainElement("not here after field")))))
   217  
   218  			Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithoutGodoc"},
   219  				HaveKeyWithValue("testing:fieldlvl", Not(ContainElement("not here after field")))))
   220  		})
   221  	})
   222  })