github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/refactor/lexical/lexical_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package lexical
     6  
     7  import (
     8  	"go/build"
     9  	"testing"
    10  
    11  	"golang.org/x/tools/go/buildutil"
    12  	"golang.org/x/tools/go/loader"
    13  )
    14  
    15  func TestStdlib(t *testing.T) {
    16  	defer func(saved func(format string, args ...interface{})) {
    17  		logf = saved
    18  	}(logf)
    19  	logf = t.Errorf
    20  
    21  	ctxt := build.Default // copy
    22  
    23  	// Enumerate $GOROOT packages.
    24  	saved := ctxt.GOPATH
    25  	ctxt.GOPATH = "" // disable GOPATH during AllPackages
    26  	pkgs := buildutil.AllPackages(&ctxt)
    27  	ctxt.GOPATH = saved
    28  
    29  	// Throw in a number of go.tools packages too.
    30  	pkgs = append(pkgs,
    31  		"golang.org/x/tools/cmd/godoc",
    32  		"golang.org/x/tools/refactor/lexical")
    33  
    34  	// Load, parse and type-check the program.
    35  	conf := loader.Config{
    36  		Build:         &ctxt,
    37  		SourceImports: true,
    38  	}
    39  	for _, path := range pkgs {
    40  		if err := conf.ImportWithTests(path); err != nil {
    41  			t.Error(err)
    42  		}
    43  	}
    44  
    45  	iprog, err := conf.Load()
    46  	if err != nil {
    47  		t.Fatalf("Load failed: %v", err)
    48  	}
    49  
    50  	// This test ensures that Structure doesn't panic and that
    51  	// its internal sanity-checks against go/types don't fail.
    52  	for pkg, info := range iprog.AllPackages {
    53  		_ = Structure(iprog.Fset, pkg, &info.Info, info.Files)
    54  	}
    55  }