github.com/TheSpiritXIII/controller-tools@v0.14.1/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/TheSpiritXIII/controller-tools/pkg/loader"
    27  	testloader "github.com/TheSpiritXIII/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/TheSpiritXIII/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  					// This is a description
   103  					// this is an example as yaml:
   104  					// ---
   105  					// foo:
   106  					//   bar:
   107  					//     dar:
   108  					//     - value1
   109  					//     - value2
   110  					type HasNonAsteriskDocWithYamlEmbeeded struct {
   111  					}
   112  
   113  					type Baz interface {
   114  						// +testing:pkglvl="not here in interface"
   115  					}
   116  
   117  					// +testing:typelvl="not here beyond closest"
   118  
   119  					// +testing:typelvl="here without godoc"
   120  					// +testing:pkglvl="here reassociated no godoc"
   121  
   122  					type Quux string
   123  
   124  					// +testing:pkglvl="here at end after last node"
   125  
   126  					/* +testing:pkglvl="not here in block" */
   127  
   128  					// +testing:typelvl="here on typedecl with no more"
   129  					type Cheese struct { }
   130  
   131  					// ensure that we're fine if we've got an end-of-line
   132  					// comment that's the last comment of the file, but
   133  					// we still have a bit more to traverse (field list --> ident).
   134  					// THIS MUST CONTAIN THE LAST COMMENT IN THE FILE
   135  					// TODO(directxman12): split this off into its own case
   136  					type private struct {
   137  						bar int // not collected
   138  					}
   139  				`,
   140  			},
   141  		},
   142  	}
   143  
   144  	By("setting up the fake packages")
   145  	var pkgs []*loader.Package
   146  	var err error
   147  	pkgs, exported, err = testloader.LoadFakeRoots(pkgstest.Modules, modules, "github.com/TheSpiritXIII/controller-tools/pkg/markers/testdata")
   148  	Expect(err).NotTo(HaveOccurred())
   149  	Expect(pkgs).To(HaveLen(1))
   150  
   151  	fakePkg = pkgs[0]
   152  })
   153  
   154  var _ = AfterSuite(func() {
   155  	By("cleaning up the fake packages")
   156  	exported.Cleanup()
   157  })