Source file src/go/importer/importer_test.go

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

View as plain text