Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/load/pkg_test.go

Documentation: cmd/go/internal/load

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

View as plain text