Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/modload/import_test.go

Documentation: cmd/go/internal/modload

     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  	"context"
     9  	"internal/testenv"
    10  	"regexp"
    11  	"strings"
    12  	"testing"
    13  
    14  	"golang.org/x/mod/module"
    15  )
    16  
    17  var importTests = []struct {
    18  	path string
    19  	m    module.Version
    20  	err  string
    21  }{
    22  	{
    23  		path: "golang.org/x/net/context",
    24  		m: module.Version{
    25  			Path: "golang.org/x/net",
    26  		},
    27  	},
    28  	{
    29  		path: "golang.org/x/net",
    30  		err:  `module golang.org/x/net@.* found \(v0.0.0-.*\), but does not contain package golang.org/x/net`,
    31  	},
    32  	{
    33  		path: "golang.org/x/text",
    34  		m: module.Version{
    35  			Path: "golang.org/x/text",
    36  		},
    37  	},
    38  	{
    39  		path: "github.com/rsc/quote/buggy",
    40  		m: module.Version{
    41  			Path:    "github.com/rsc/quote",
    42  			Version: "v1.5.2",
    43  		},
    44  	},
    45  	{
    46  		path: "github.com/rsc/quote",
    47  		m: module.Version{
    48  			Path:    "github.com/rsc/quote",
    49  			Version: "v1.5.2",
    50  		},
    51  	},
    52  	{
    53  		path: "golang.org/x/foo/bar",
    54  		err:  "cannot find module providing package golang.org/x/foo/bar",
    55  	},
    56  }
    57  
    58  func TestQueryImport(t *testing.T) {
    59  	testenv.MustHaveExternalNetwork(t)
    60  	testenv.MustHaveExecPath(t, "git")
    61  
    62  	oldAllowMissingModuleImports := allowMissingModuleImports
    63  	oldRootMode := RootMode
    64  	defer func() {
    65  		allowMissingModuleImports = oldAllowMissingModuleImports
    66  		RootMode = oldRootMode
    67  	}()
    68  	allowMissingModuleImports = true
    69  	RootMode = NoRoot
    70  
    71  	ctx := context.Background()
    72  
    73  	for _, tt := range importTests {
    74  		t.Run(strings.ReplaceAll(tt.path, "/", "_"), func(t *testing.T) {
    75  			// Note that there is no build list, so Import should always fail.
    76  			m, err := queryImport(ctx, tt.path)
    77  
    78  			if tt.err == "" {
    79  				if err != nil {
    80  					t.Fatalf("queryImport(_, %q): %v", tt.path, err)
    81  				}
    82  			} else {
    83  				if err == nil {
    84  					t.Fatalf("queryImport(_, %q) = %v, nil; expected error", tt.path, m)
    85  				}
    86  				if !regexp.MustCompile(tt.err).MatchString(err.Error()) {
    87  					t.Fatalf("queryImport(_, %q): error %q, want error matching %#q", tt.path, err, tt.err)
    88  				}
    89  			}
    90  
    91  			if m.Path != tt.m.Path || (tt.m.Version != "" && m.Version != tt.m.Version) {
    92  				t.Errorf("queryImport(_, %q) = %v, _; want %v", tt.path, m, tt.m)
    93  			}
    94  		})
    95  	}
    96  }
    97  

View as plain text