github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/internal/gccgoimporter/gccgoinstallation_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.
     7  
     8  package gccgoimporter
     9  
    10  import (
    11  	"go/types"
    12  	"runtime"
    13  	"testing"
    14  )
    15  
    16  // importablePackages is a list of packages that we verify that we can
    17  // import. This should be all standard library packages in all relevant
    18  // versions of gccgo. Note that since gccgo follows a different release
    19  // cycle, and since different systems have different versions installed,
    20  // we can't use the last-two-versions rule of the gc toolchain.
    21  var importablePackages = [...]string{
    22  	"archive/tar",
    23  	"archive/zip",
    24  	"bufio",
    25  	"bytes",
    26  	"compress/bzip2",
    27  	"compress/flate",
    28  	"compress/gzip",
    29  	"compress/lzw",
    30  	"compress/zlib",
    31  	"container/heap",
    32  	"container/list",
    33  	"container/ring",
    34  	"crypto/aes",
    35  	"crypto/cipher",
    36  	"crypto/des",
    37  	"crypto/dsa",
    38  	"crypto/ecdsa",
    39  	"crypto/elliptic",
    40  	"crypto",
    41  	"crypto/hmac",
    42  	"crypto/md5",
    43  	"crypto/rand",
    44  	"crypto/rc4",
    45  	"crypto/rsa",
    46  	"crypto/sha1",
    47  	"crypto/sha256",
    48  	"crypto/sha512",
    49  	"crypto/subtle",
    50  	"crypto/tls",
    51  	"crypto/x509",
    52  	"crypto/x509/pkix",
    53  	"database/sql/driver",
    54  	"database/sql",
    55  	"debug/dwarf",
    56  	"debug/elf",
    57  	"debug/gosym",
    58  	"debug/macho",
    59  	"debug/pe",
    60  	"encoding/ascii85",
    61  	"encoding/asn1",
    62  	"encoding/base32",
    63  	"encoding/base64",
    64  	"encoding/binary",
    65  	"encoding/csv",
    66  	"encoding/gob",
    67  	// "encoding", // Added in GCC 4.9.
    68  	"encoding/hex",
    69  	"encoding/json",
    70  	"encoding/pem",
    71  	"encoding/xml",
    72  	"errors",
    73  	"expvar",
    74  	"flag",
    75  	"fmt",
    76  	"go/ast",
    77  	"go/build",
    78  	"go/doc",
    79  	// "go/format", // Added in GCC 4.8.
    80  	"go/parser",
    81  	"go/printer",
    82  	"go/scanner",
    83  	"go/token",
    84  	"hash/adler32",
    85  	"hash/crc32",
    86  	"hash/crc64",
    87  	"hash/fnv",
    88  	"hash",
    89  	"html",
    90  	"html/template",
    91  	"image/color",
    92  	// "image/color/palette", // Added in GCC 4.9.
    93  	"image/draw",
    94  	"image/gif",
    95  	"image",
    96  	"image/jpeg",
    97  	"image/png",
    98  	"index/suffixarray",
    99  	"io",
   100  	"io/ioutil",
   101  	"log",
   102  	"log/syslog",
   103  	"math/big",
   104  	"math/cmplx",
   105  	"math",
   106  	"math/rand",
   107  	"mime",
   108  	"mime/multipart",
   109  	"net",
   110  	"net/http/cgi",
   111  	// "net/http/cookiejar", // Added in GCC 4.8.
   112  	"net/http/fcgi",
   113  	"net/http",
   114  	"net/http/httptest",
   115  	"net/http/httputil",
   116  	"net/http/pprof",
   117  	"net/mail",
   118  	"net/rpc",
   119  	"net/rpc/jsonrpc",
   120  	"net/smtp",
   121  	"net/textproto",
   122  	"net/url",
   123  	"os/exec",
   124  	"os",
   125  	"os/signal",
   126  	"os/user",
   127  	"path/filepath",
   128  	"path",
   129  	"reflect",
   130  	"regexp",
   131  	"regexp/syntax",
   132  	"runtime/debug",
   133  	"runtime",
   134  	"runtime/pprof",
   135  	"sort",
   136  	"strconv",
   137  	"strings",
   138  	"sync/atomic",
   139  	"sync",
   140  	"syscall",
   141  	"testing",
   142  	"testing/iotest",
   143  	"testing/quick",
   144  	"text/scanner",
   145  	"text/tabwriter",
   146  	"text/template",
   147  	"text/template/parse",
   148  	"time",
   149  	"unicode",
   150  	"unicode/utf16",
   151  	"unicode/utf8",
   152  }
   153  
   154  func TestInstallationImporter(t *testing.T) {
   155  	// This test relies on gccgo being around.
   156  	gpath := gccgoPath()
   157  	if gpath == "" {
   158  		t.Skip("This test needs gccgo")
   159  	}
   160  	if runtime.GOOS == "aix" {
   161  		// We don't yet have a debug/xcoff package for reading
   162  		// object files on AIX. Remove this skip if/when issue #29038
   163  		// is implemented (see also issue #49445).
   164  		t.Skip("no support yet for debug/xcoff")
   165  	}
   166  
   167  	var inst GccgoInstallation
   168  	err := inst.InitFromDriver(gpath)
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  	imp := inst.GetImporter(nil, nil)
   173  
   174  	// Ensure we don't regress the number of packages we can parse. First import
   175  	// all packages into the same map and then each individually.
   176  	pkgMap := make(map[string]*types.Package)
   177  	for _, pkg := range importablePackages {
   178  		_, err = imp(pkgMap, pkg, ".", nil)
   179  		if err != nil {
   180  			t.Error(err)
   181  		}
   182  	}
   183  
   184  	for _, pkg := range importablePackages {
   185  		_, err = imp(make(map[string]*types.Package), pkg, ".", nil)
   186  		if err != nil {
   187  			t.Error(err)
   188  		}
   189  	}
   190  
   191  	// Test for certain specific entities in the imported data.
   192  	for _, test := range [...]importerTest{
   193  		{pkgpath: "io", name: "Reader", want: "type Reader interface{Read(p []byte) (n int, err error)}"},
   194  		{pkgpath: "io", name: "ReadWriter", want: "type ReadWriter interface{Reader; Writer}"},
   195  		{pkgpath: "math", name: "Pi", want: "const Pi untyped float"},
   196  		{pkgpath: "math", name: "Sin", want: "func Sin(x float64) float64"},
   197  		{pkgpath: "sort", name: "Search", want: "func Search(n int, f func(int) bool) int"},
   198  		{pkgpath: "unsafe", name: "Pointer", want: "type Pointer"},
   199  	} {
   200  		runImporterTest(t, imp, nil, &test)
   201  	}
   202  }