github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/load/pkg_test.go (about)

     1  // Copyright 2019 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 load
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/go-asm/go/cmd/go/cfg"
    11  )
    12  
    13  func TestPkgDefaultExecName(t *testing.T) {
    14  	oldModulesEnabled := cfg.ModulesEnabled
    15  	defer func() { cfg.ModulesEnabled = oldModulesEnabled }()
    16  	for _, tt := range []struct {
    17  		in         string
    18  		files      []string
    19  		wantMod    string
    20  		wantGopath string
    21  	}{
    22  		{"example.com/mycmd", []string{}, "mycmd", "mycmd"},
    23  		{"example.com/mycmd/v0", []string{}, "v0", "v0"},
    24  		{"example.com/mycmd/v1", []string{}, "v1", "v1"},
    25  		{"example.com/mycmd/v2", []string{}, "mycmd", "v2"}, // Semantic import versioning, use second last element in module mode.
    26  		{"example.com/mycmd/v3", []string{}, "mycmd", "v3"}, // Semantic import versioning, use second last element in module mode.
    27  		{"mycmd", []string{}, "mycmd", "mycmd"},
    28  		{"mycmd/v0", []string{}, "v0", "v0"},
    29  		{"mycmd/v1", []string{}, "v1", "v1"},
    30  		{"mycmd/v2", []string{}, "mycmd", "v2"}, // Semantic import versioning, use second last element in module mode.
    31  		{"v0", []string{}, "v0", "v0"},
    32  		{"v1", []string{}, "v1", "v1"},
    33  		{"v2", []string{}, "v2", "v2"},
    34  		{"command-line-arguments", []string{"output.go", "foo.go"}, "output", "output"},
    35  	} {
    36  		{
    37  			cfg.ModulesEnabled = true
    38  			pkg := new(Package)
    39  			pkg.ImportPath = tt.in
    40  			pkg.GoFiles = tt.files
    41  			pkg.Internal.CmdlineFiles = len(tt.files) > 0
    42  			gotMod := pkg.DefaultExecName()
    43  			if gotMod != tt.wantMod {
    44  				t.Errorf("pkg.DefaultExecName with ImportPath = %q in module mode = %v; want %v", tt.in, gotMod, tt.wantMod)
    45  			}
    46  		}
    47  		{
    48  			cfg.ModulesEnabled = false
    49  			pkg := new(Package)
    50  			pkg.ImportPath = tt.in
    51  			pkg.GoFiles = tt.files
    52  			pkg.Internal.CmdlineFiles = len(tt.files) > 0
    53  			gotGopath := pkg.DefaultExecName()
    54  			if gotGopath != tt.wantGopath {
    55  				t.Errorf("pkg.DefaultExecName with ImportPath = %q in gopath mode = %v; want %v", tt.in, gotGopath, tt.wantGopath)
    56  			}
    57  		}
    58  	}
    59  }
    60  
    61  func TestIsVersionElement(t *testing.T) {
    62  	t.Parallel()
    63  	for _, tt := range []struct {
    64  		in   string
    65  		want bool
    66  	}{
    67  		{"v0", false},
    68  		{"v05", false},
    69  		{"v1", false},
    70  		{"v2", true},
    71  		{"v3", true},
    72  		{"v9", true},
    73  		{"v10", true},
    74  		{"v11", true},
    75  		{"v", false},
    76  		{"vx", false},
    77  	} {
    78  		got := isVersionElement(tt.in)
    79  		if got != tt.want {
    80  			t.Errorf("isVersionElement(%q) = %v; want %v", tt.in, got, tt.want)
    81  		}
    82  	}
    83  }