github.com/c9s/go@v0.0.0-20180120015821-984e81f64e0c/src/go/importer/importer_test.go (about)

     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  	"internal/testenv"
     9  	"io"
    10  	"os"
    11  	"os/exec"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestFor(t *testing.T) {
    17  	testenv.MustHaveGoBuild(t)
    18  
    19  	const thePackage = "math/big"
    20  	out, err := exec.Command(testenv.GoToolPath(t), "list", "-f={{context.Compiler}}:{{.Target}}", thePackage).CombinedOutput()
    21  	if err != nil {
    22  		t.Fatalf("go list %s: %v\n%s", thePackage, err, out)
    23  	}
    24  	target := strings.TrimSpace(string(out))
    25  	i := strings.Index(target, ":")
    26  	compiler, target := target[:i], target[i+1:]
    27  	if !strings.HasSuffix(target, ".a") {
    28  		t.Fatalf("unexpected package %s target %q (not *.a)", thePackage, target)
    29  	}
    30  
    31  	if compiler == "gccgo" {
    32  		t.Skip("golang.org/issue/22500")
    33  	}
    34  
    35  	t.Run("LookupDefault", func(t *testing.T) {
    36  		imp := For(compiler, nil)
    37  		pkg, err := imp.Import(thePackage)
    38  		if err != nil {
    39  			t.Fatal(err)
    40  		}
    41  		if pkg.Path() != thePackage {
    42  			t.Fatalf("Path() = %q, want %q", pkg.Path(), thePackage)
    43  		}
    44  	})
    45  
    46  	t.Run("LookupCustom", func(t *testing.T) {
    47  		lookup := func(path string) (io.ReadCloser, error) {
    48  			if path != "math/bigger" {
    49  				t.Fatalf("lookup called with unexpected path %q", path)
    50  			}
    51  			f, err := os.Open(target)
    52  			if err != nil {
    53  				t.Fatal(err)
    54  			}
    55  			return f, nil
    56  		}
    57  		imp := For(compiler, lookup)
    58  		pkg, err := imp.Import("math/bigger")
    59  		if err != nil {
    60  			t.Fatal(err)
    61  		}
    62  		// Even though we open math/big.a, the import request was for math/bigger
    63  		// and that should be recorded in pkg.Path(), at least for the gc toolchain.
    64  		if pkg.Path() != "math/bigger" {
    65  			t.Fatalf("Path() = %q, want %q", pkg.Path(), "math/bigger")
    66  		}
    67  	})
    68  }