github.com/v2fly/tools@v0.100.0/go/internal/gccgoimporter/importer_test.go (about)

     1  // Copyright 2013 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  // Except for this comment, this file is a verbatim copy of the file
     6  // with the same name in $GOROOT/src/go/internal/gccgoimporter without
     7  // the import of "testenv".
     8  
     9  package gccgoimporter
    10  
    11  import (
    12  	"go/types"
    13  	"io/ioutil"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  	"regexp"
    18  	"strconv"
    19  	"testing"
    20  )
    21  
    22  type importerTest struct {
    23  	pkgpath, name, want, wantval string
    24  	wantinits                    []string
    25  	gccgoVersion                 int // minimum gccgo version (0 => any)
    26  }
    27  
    28  func runImporterTest(t *testing.T, imp Importer, initmap map[*types.Package]InitData, test *importerTest) {
    29  	pkg, err := imp(make(map[string]*types.Package), test.pkgpath, ".", nil)
    30  	if err != nil {
    31  		t.Error(err)
    32  		return
    33  	}
    34  
    35  	if test.name != "" {
    36  		obj := pkg.Scope().Lookup(test.name)
    37  		if obj == nil {
    38  			t.Errorf("%s: object not found", test.name)
    39  			return
    40  		}
    41  
    42  		got := types.ObjectString(obj, types.RelativeTo(pkg))
    43  		if got != test.want {
    44  			t.Errorf("%s: got %q; want %q", test.name, got, test.want)
    45  		}
    46  
    47  		if test.wantval != "" {
    48  			gotval := obj.(*types.Const).Val().String()
    49  			if gotval != test.wantval {
    50  				t.Errorf("%s: got val %q; want val %q", test.name, gotval, test.wantval)
    51  			}
    52  		}
    53  	}
    54  
    55  	if len(test.wantinits) > 0 {
    56  		initdata := initmap[pkg]
    57  		found := false
    58  		// Check that the package's own init function has the package's priority
    59  		for _, pkginit := range initdata.Inits {
    60  			if pkginit.InitFunc == test.wantinits[0] {
    61  				found = true
    62  				break
    63  			}
    64  		}
    65  
    66  		if !found {
    67  			t.Errorf("%s: could not find expected function %q", test.pkgpath, test.wantinits[0])
    68  		}
    69  
    70  		// FIXME: the original version of this test was written against
    71  		// the v1 export data scheme for capturing init functions, so it
    72  		// verified the priority values. We moved away from the priority
    73  		// scheme some time ago; it is not clear how much work it would be
    74  		// to validate the new init export data.
    75  	}
    76  }
    77  
    78  // When adding tests to this list, be sure to set the 'gccgoVersion'
    79  // field if the testcases uses a "recent" Go addition (ex: aliases).
    80  var importerTests = [...]importerTest{
    81  	{pkgpath: "pointer", name: "Int8Ptr", want: "type Int8Ptr *int8"},
    82  	{pkgpath: "complexnums", name: "NN", want: "const NN untyped complex", wantval: "(-1 + -1i)"},
    83  	{pkgpath: "complexnums", name: "NP", want: "const NP untyped complex", wantval: "(-1 + 1i)"},
    84  	{pkgpath: "complexnums", name: "PN", want: "const PN untyped complex", wantval: "(1 + -1i)"},
    85  	{pkgpath: "complexnums", name: "PP", want: "const PP untyped complex", wantval: "(1 + 1i)"},
    86  	{pkgpath: "conversions", name: "Bits", want: "const Bits Units", wantval: `"bits"`},
    87  	{pkgpath: "time", name: "Duration", want: "type Duration int64"},
    88  	{pkgpath: "time", name: "Nanosecond", want: "const Nanosecond Duration", wantval: "1"},
    89  	{pkgpath: "unicode", name: "IsUpper", want: "func IsUpper(r rune) bool"},
    90  	{pkgpath: "unicode", name: "MaxRune", want: "const MaxRune untyped rune", wantval: "1114111"},
    91  	{pkgpath: "imports", wantinits: []string{"imports..import", "fmt..import"}},
    92  	{pkgpath: "importsar", name: "Hello", want: "var Hello string"},
    93  	{pkgpath: "aliases", name: "A14", gccgoVersion: 7, want: "type A14 = func(int, T0) chan T2"},
    94  	{pkgpath: "aliases", name: "C0", gccgoVersion: 7, want: "type C0 struct{f1 C1; f2 C1}"},
    95  	{pkgpath: "escapeinfo", name: "NewT", want: "func NewT(data []byte) *T"},
    96  	{pkgpath: "issue27856", name: "M", gccgoVersion: 7, want: "type M struct{E F}"},
    97  	{pkgpath: "v1reflect", name: "Type", want: "type Type interface{Align() int; AssignableTo(u Type) bool; Bits() int; ChanDir() ChanDir; Elem() Type; Field(i int) StructField; FieldAlign() int; FieldByIndex(index []int) StructField; FieldByName(name string) (StructField, bool); FieldByNameFunc(match func(string) bool) (StructField, bool); Implements(u Type) bool; In(i int) Type; IsVariadic() bool; Key() Type; Kind() Kind; Len() int; Method(int) Method; MethodByName(string) (Method, bool); Name() string; NumField() int; NumIn() int; NumMethod() int; NumOut() int; Out(i int) Type; PkgPath() string; Size() uintptr; String() string; common() *commonType; rawString() string; runtimeType() *runtimeType; uncommon() *uncommonType}"},
    98  	{pkgpath: "nointerface", name: "I", want: "type I int"},
    99  	{pkgpath: "issue29198", name: "FooServer", gccgoVersion: 7, want: "type FooServer struct{FooServer *FooServer; user string; ctx context.Context}"},
   100  	{pkgpath: "issue30628", name: "Apple", want: "type Apple struct{hey sync.RWMutex; x int; RQ [517]struct{Count uintptr; NumBytes uintptr; Last uintptr}}"},
   101  	{pkgpath: "issue31540", name: "S", gccgoVersion: 7, want: "type S struct{b int; map[Y]Z}"},
   102  	{pkgpath: "issue34182", name: "T1", want: "type T1 struct{f *T2}"},
   103  	{pkgpath: "notinheap", name: "S", want: "type S struct{}"},
   104  }
   105  
   106  func TestGoxImporter(t *testing.T) {
   107  	testenv.MustHaveExec(t) // this is to skip nacl, js
   108  	initmap := make(map[*types.Package]InitData)
   109  	imp := GetImporter([]string{"testdata"}, initmap)
   110  
   111  	for _, test := range importerTests {
   112  		runImporterTest(t, imp, initmap, &test)
   113  	}
   114  }
   115  
   116  // gccgoPath returns a path to gccgo if it is present (either in
   117  // path or specified via GCCGO environment variable), or an
   118  // empty string if no gccgo is available.
   119  func gccgoPath() string {
   120  	gccgoname := os.Getenv("GCCGO")
   121  	if gccgoname == "" {
   122  		gccgoname = "gccgo"
   123  	}
   124  	if gpath, gerr := exec.LookPath(gccgoname); gerr == nil {
   125  		return gpath
   126  	}
   127  	return ""
   128  }
   129  
   130  func TestObjImporter(t *testing.T) {
   131  	// This test relies on gccgo being around.
   132  	gpath := gccgoPath()
   133  	if gpath == "" {
   134  		t.Skip("This test needs gccgo")
   135  	}
   136  
   137  	verout, err := exec.Command(gpath, "--version").CombinedOutput()
   138  	if err != nil {
   139  		t.Logf("%s", verout)
   140  		t.Fatal(err)
   141  	}
   142  	vers := regexp.MustCompile(`([0-9]+)\.([0-9]+)`).FindSubmatch(verout)
   143  	if len(vers) == 0 {
   144  		t.Fatalf("could not find version number in %s", verout)
   145  	}
   146  	major, err := strconv.Atoi(string(vers[1]))
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  	minor, err := strconv.Atoi(string(vers[2]))
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	t.Logf("gccgo version %d.%d", major, minor)
   155  
   156  	tmpdir, err := ioutil.TempDir("", "TestObjImporter")
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	defer os.RemoveAll(tmpdir)
   161  
   162  	initmap := make(map[*types.Package]InitData)
   163  	imp := GetImporter([]string{tmpdir}, initmap)
   164  
   165  	artmpdir, err := ioutil.TempDir("", "TestObjImporter")
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  	defer os.RemoveAll(artmpdir)
   170  
   171  	arinitmap := make(map[*types.Package]InitData)
   172  	arimp := GetImporter([]string{artmpdir}, arinitmap)
   173  
   174  	for _, test := range importerTests {
   175  		if major < test.gccgoVersion {
   176  			// Support for type aliases was added in GCC 7.
   177  			t.Logf("skipping %q: not supported before gccgo version %d", test.pkgpath, test.gccgoVersion)
   178  			continue
   179  		}
   180  
   181  		gofile := filepath.Join("testdata", test.pkgpath+".go")
   182  		if _, err := os.Stat(gofile); os.IsNotExist(err) {
   183  			continue
   184  		}
   185  		ofile := filepath.Join(tmpdir, test.pkgpath+".o")
   186  		afile := filepath.Join(artmpdir, "lib"+test.pkgpath+".a")
   187  
   188  		cmd := exec.Command(gpath, "-fgo-pkgpath="+test.pkgpath, "-c", "-o", ofile, gofile)
   189  		out, err := cmd.CombinedOutput()
   190  		if err != nil {
   191  			t.Logf("%s", out)
   192  			t.Fatalf("gccgo %s failed: %s", gofile, err)
   193  		}
   194  
   195  		runImporterTest(t, imp, initmap, &test)
   196  
   197  		cmd = exec.Command("ar", "cr", afile, ofile)
   198  		out, err = cmd.CombinedOutput()
   199  		if err != nil {
   200  			t.Logf("%s", out)
   201  			t.Fatalf("ar cr %s %s failed: %s", afile, ofile, err)
   202  		}
   203  
   204  		runImporterTest(t, arimp, arinitmap, &test)
   205  
   206  		if err = os.Remove(ofile); err != nil {
   207  			t.Fatal(err)
   208  		}
   209  		if err = os.Remove(afile); err != nil {
   210  			t.Fatal(err)
   211  		}
   212  	}
   213  }