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