-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go/internal/load: always use DefaultExecName to determine binary …
…name It should produce equivalent results to split on the import path of the package rather than its directory, in GOPATH mode. That means the common code in DefaultExecName can be used. We're in the middle of Go 1.12 cycle, so now is a good time to make it happen for Go 1.13. Modify isVersionElement to accept path elements like "v2", "v3", "v10", rather than "/v2", "/v3", "/v10". Then use it in DefaultExecName instead of the ad-hoc isVersion function. There is no change in behavior. Add tests for DefaultExecName and isVersionElement. Updates #26869 Change-Id: Ic6da2c92587459aa2b327385e994b72a6e183092 Reviewed-on: https://go-review.googlesource.com/c/go/+/168678 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
- Loading branch information
Showing
2 changed files
with
79 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package load | ||
|
||
import ( | ||
"cmd/go/internal/cfg" | ||
"testing" | ||
) | ||
|
||
func TestDefaultExecName(t *testing.T) { | ||
oldModulesEnabled := cfg.ModulesEnabled | ||
defer func() { cfg.ModulesEnabled = oldModulesEnabled }() | ||
for _, tt := range []struct { | ||
in string | ||
wantMod string | ||
wantGopath string | ||
}{ | ||
{"example.com/mycmd", "mycmd", "mycmd"}, | ||
{"example.com/mycmd/v0", "v0", "v0"}, | ||
{"example.com/mycmd/v1", "v1", "v1"}, | ||
{"example.com/mycmd/v2", "mycmd", "v2"}, // Semantic import versioning, use second last element in module mode. | ||
{"example.com/mycmd/v3", "mycmd", "v3"}, // Semantic import versioning, use second last element in module mode. | ||
{"mycmd", "mycmd", "mycmd"}, | ||
{"mycmd/v0", "v0", "v0"}, | ||
{"mycmd/v1", "v1", "v1"}, | ||
{"mycmd/v2", "mycmd", "v2"}, // Semantic import versioning, use second last element in module mode. | ||
{"v0", "v0", "v0"}, | ||
{"v1", "v1", "v1"}, | ||
{"v2", "v2", "v2"}, | ||
} { | ||
{ | ||
cfg.ModulesEnabled = true | ||
gotMod := DefaultExecName(tt.in) | ||
if gotMod != tt.wantMod { | ||
t.Errorf("DefaultExecName(%q) in module mode = %v; want %v", tt.in, gotMod, tt.wantMod) | ||
} | ||
} | ||
{ | ||
cfg.ModulesEnabled = false | ||
gotGopath := DefaultExecName(tt.in) | ||
if gotGopath != tt.wantGopath { | ||
t.Errorf("DefaultExecName(%q) in gopath mode = %v; want %v", tt.in, gotGopath, tt.wantGopath) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestIsVersionElement(t *testing.T) { | ||
t.Parallel() | ||
for _, tt := range []struct { | ||
in string | ||
want bool | ||
}{ | ||
{"v0", false}, | ||
{"v05", false}, | ||
{"v1", false}, | ||
{"v2", true}, | ||
{"v3", true}, | ||
{"v9", true}, | ||
{"v10", true}, | ||
{"v11", true}, | ||
{"v", false}, | ||
{"vx", false}, | ||
} { | ||
got := isVersionElement(tt.in) | ||
if got != tt.want { | ||
t.Errorf("isVersionElement(%q) = %v; want %v", tt.in, got, tt.want) | ||
} | ||
} | ||
} |