Black Lives Matter. Support the Equal Justice Initiative.

Source file src/go/importer/importer_test.go

Documentation: go/importer

     1  // Copyright 2017 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 importer
     6  
     7  import (
     8  	"go/token"
     9  	"internal/testenv"
    10  	"io"
    11  	"os"
    12  	"os/exec"
    13  	"runtime"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  func TestForCompiler(t *testing.T) {
    19  	testenv.MustHaveGoBuild(t)
    20  
    21  	const thePackage = "math/big"
    22  	out, err := exec.Command(testenv.GoToolPath(t), "list", "-f={{context.Compiler}}:{{.Target}}", thePackage).CombinedOutput()
    23  	if err != nil {
    24  		t.Fatalf("go list %s: %v\n%s", thePackage, err, out)
    25  	}
    26  	target := strings.TrimSpace(string(out))
    27  	i := strings.Index(target, ":")
    28  	compiler, target := target[:i], target[i+1:]
    29  	if !strings.HasSuffix(target, ".a") {
    30  		t.Fatalf("unexpected package %s target %q (not *.a)", thePackage, target)
    31  	}
    32  
    33  	if compiler == "gccgo" {
    34  		t.Skip("golang.org/issue/22500")
    35  	}
    36  
    37  	fset := token.NewFileSet()
    38  
    39  	t.Run("LookupDefault", func(t *testing.T) {
    40  		imp := ForCompiler(fset, compiler, nil)
    41  		pkg, err := imp.Import(thePackage)
    42  		if err != nil {
    43  			t.Fatal(err)
    44  		}
    45  		if pkg.Path() != thePackage {
    46  			t.Fatalf("Path() = %q, want %q", pkg.Path(), thePackage)
    47  		}
    48  
    49  		// Check that the fileset positions are accurate.
    50  		// https://github.com/golang/go#28995
    51  		mathBigInt := pkg.Scope().Lookup("Int")
    52  		posn := fset.Position(mathBigInt.Pos()) // "$GOROOT/src/math/big/int.go:25:1"
    53  		filename := strings.Replace(posn.Filename, "$GOROOT", runtime.GOROOT(), 1)
    54  		data, err := os.ReadFile(filename)
    55  		if err != nil {
    56  			t.Fatalf("can't read file containing declaration of math/big.Int: %v", err)
    57  		}
    58  		lines := strings.Split(string(data), "\n")
    59  		if posn.Line > len(lines) || !strings.HasPrefix(lines[posn.Line-1], "type Int") {
    60  			t.Fatalf("Object %v position %s does not contain its declaration",
    61  				mathBigInt, posn)
    62  		}
    63  	})
    64  
    65  	t.Run("LookupCustom", func(t *testing.T) {
    66  		lookup := func(path string) (io.ReadCloser, error) {
    67  			if path != "math/bigger" {
    68  				t.Fatalf("lookup called with unexpected path %q", path)
    69  			}
    70  			f, err := os.Open(target)
    71  			if err != nil {
    72  				t.Fatal(err)
    73  			}
    74  			return f, nil
    75  		}
    76  		imp := ForCompiler(fset, compiler, lookup)
    77  		pkg, err := imp.Import("math/bigger")
    78  		if err != nil {
    79  			t.Fatal(err)
    80  		}
    81  		// Even though we open math/big.a, the import request was for math/bigger
    82  		// and that should be recorded in pkg.Path(), at least for the gc toolchain.
    83  		if pkg.Path() != "math/bigger" {
    84  			t.Fatalf("Path() = %q, want %q", pkg.Path(), "math/bigger")
    85  		}
    86  	})
    87  }
    88  

View as plain text