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