github.com/minio/controller-tools@v0.4.7/pkg/loader/testutils/helpers.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 testutils defines utilities for using loader.Packages
    18  // in tests.
    19  //
    20  // The main utility is LoadFakeRoots, which allows writing of fake
    21  // modules to the local filesystem, then loading those from within
    22  // a Ginkgo test.  If not using Ginkgo, you can reproduce similar
    23  // logic with loader.LoadWithConfig and go/packages/packagestest.
    24  package testutils
    25  
    26  import (
    27  	"testing"
    28  
    29  	"github.com/onsi/ginkgo"
    30  	pkgstest "golang.org/x/tools/go/packages/packagestest"
    31  
    32  	"github.com/minio/controller-tools/pkg/loader"
    33  )
    34  
    35  // fakeT wraps Ginkgo's fake testing.T with a real testing.T
    36  // to get the few missing methods.
    37  type fakeT struct {
    38  	ginkgo.GinkgoTInterface
    39  	*testing.T
    40  }
    41  
    42  // LoadFakeRoots loads the given "root" packages by fake module,
    43  // transitively loading and all imports as well.
    44  //
    45  // Loaded packages will have type size, imports, and exports file information
    46  // populated.  Additional information, like ASTs and type-checking information,
    47  // can be accessed via methods on individual packages.
    48  func LoadFakeRoots(exporter pkgstest.Exporter, modules []pkgstest.Module, roots ...string) ([]*loader.Package, *pkgstest.Exported, error) {
    49  	exported := pkgstest.Export(fakeT{ginkgo.GinkgoT(), &testing.T{}}, exporter, modules)
    50  
    51  	pkgs, err := loader.LoadRootsWithConfig(exported.Config, roots...)
    52  	return pkgs, exported, err
    53  }