aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/import_test.go
blob: 47ce89a0849f98465ad3b73963cc39bed2f3c2cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package modload

import (
	"context"
	"internal/testenv"
	"regexp"
	"strings"
	"testing"
)

var importTests = []struct {
	path string
	err  string
}{
	{
		path: "golang.org/x/net/context",
		err:  "missing module for import: golang.org/x/net@.* provides golang.org/x/net/context",
	},
	{
		path: "golang.org/x/net",
		err:  `module golang.org/x/net@.* found \(v0.0.0-.*\), but does not contain package golang.org/x/net`,
	},
	{
		path: "golang.org/x/text",
		err:  "missing module for import: golang.org/x/text@.* provides golang.org/x/text",
	},
	{
		path: "github.com/rsc/quote/buggy",
		err:  "missing module for import: github.com/rsc/quote@v1.5.2 provides github.com/rsc/quote/buggy",
	},
	{
		path: "github.com/rsc/quote",
		err:  "missing module for import: github.com/rsc/quote@v1.5.2 provides github.com/rsc/quote",
	},
	{
		path: "golang.org/x/foo/bar",
		err:  "cannot find module providing package golang.org/x/foo/bar",
	},
}

func TestImport(t *testing.T) {
	testenv.MustHaveExternalNetwork(t)
	testenv.MustHaveExecPath(t, "git")
	defer func(old bool) {
		allowMissingModuleImports = old
	}(allowMissingModuleImports)
	AllowMissingModuleImports()

	ctx := context.Background()

	for _, tt := range importTests {
		t.Run(strings.ReplaceAll(tt.path, "/", "_"), func(t *testing.T) {
			// Note that there is no build list, so Import should always fail.
			m, dir, err := Import(ctx, tt.path)
			if err == nil {
				t.Fatalf("Import(%q) = %v, %v, nil; expected error", tt.path, m, dir)
			}
			if !regexp.MustCompile(tt.err).MatchString(err.Error()) {
				t.Fatalf("Import(%q): error %q, want error matching %#q", tt.path, err, tt.err)
			}
		})
	}
}