github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/modload/import_test.go (about)

     1  // Copyright 2018 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 modload
     6  
     7  import (
     8  	"github.com/gagliardetto/golang-go/not-internal/testenv"
     9  	"regexp"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  var importTests = []struct {
    15  	path string
    16  	err  string
    17  }{
    18  	{
    19  		path: "golang.org/x/net/context",
    20  		err:  "missing module for import: golang.org/x/net@.* provides golang.org/x/net/context",
    21  	},
    22  	{
    23  		path: "golang.org/x/net",
    24  		err:  `module golang.org/x/net@.* found \(v0.0.0-.*\), but does not contain package golang.org/x/net`,
    25  	},
    26  	{
    27  		path: "golang.org/x/text",
    28  		err:  "missing module for import: golang.org/x/text@.* provides golang.org/x/text",
    29  	},
    30  	{
    31  		path: "github.com/rsc/quote/buggy",
    32  		err:  "missing module for import: github.com/rsc/quote@v1.5.2 provides github.com/rsc/quote/buggy",
    33  	},
    34  	{
    35  		path: "github.com/rsc/quote",
    36  		err:  "missing module for import: github.com/rsc/quote@v1.5.2 provides github.com/rsc/quote",
    37  	},
    38  	{
    39  		path: "golang.org/x/foo/bar",
    40  		err:  "cannot find module providing package golang.org/x/foo/bar",
    41  	},
    42  }
    43  
    44  func TestImport(t *testing.T) {
    45  	testenv.MustHaveExternalNetwork(t)
    46  	testenv.MustHaveExecPath(t, "git")
    47  	defer func(old bool) {
    48  		allowMissingModuleImports = old
    49  	}(allowMissingModuleImports)
    50  	AllowMissingModuleImports()
    51  
    52  	for _, tt := range importTests {
    53  		t.Run(strings.ReplaceAll(tt.path, "/", "_"), func(t *testing.T) {
    54  			// Note that there is no build list, so Import should always fail.
    55  			m, dir, err := Import(tt.path)
    56  			if err == nil {
    57  				t.Fatalf("Import(%q) = %v, %v, nil; expected error", tt.path, m, dir)
    58  			}
    59  			if !regexp.MustCompile(tt.err).MatchString(err.Error()) {
    60  				t.Fatalf("Import(%q): error %q, want error matching %#q", tt.path, err, tt.err)
    61  			}
    62  		})
    63  	}
    64  }