internal/installer/windowsmsi: restore pre-release version splitting

CL 554057 updated the splitVersion implementation to account for major
Go releases now having a trailing ".0" component, so "go1.34" would be
"go1.34.0" instead. However, our pre-release versions still need to be
handled as before, which that CL inadvertently broke, and no test case
caught.

Update the code, test cases, and add error reporting accordingly.

For golang/go#63147.

Change-Id: Iab49ee70f17930cc2e6b871311c5d32cd68704d2
Reviewed-on: https://go-review.googlesource.com/c/build/+/555315
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Dmitri Shuralyov 2024-01-10 17:45:29 -05:00 коммит произвёл Gopher Robot
Родитель 2d3e831a78
Коммит fc458ab4f3
2 изменённых файлов: 26 добавлений и 12 удалений

Просмотреть файл

@ -96,7 +96,10 @@ func ConstructInstaller(_ context.Context, workDir, tgzPath string, opt Installe
}
fmt.Println("\nBuilding package (running wix candle).")
verMajor, verMinor := splitVersion(version)
verMajor, verMinor, err := splitVersion(version)
if err != nil {
return "", fmt.Errorf("failed to split version %q: %v", version, err)
}
var msArch string
switch opt.GOARCH {
case "386":
@ -245,22 +248,28 @@ func run(name string, args ...string) error {
return cmd.Run()
}
var versionRE = regexp.MustCompile(`^go1\.(\d+\.\d+)`)
var versionRE = regexp.MustCompile(`^go1\.(\d+(\.\d+)?)`)
// splitVersion splits a Go version string such as "go1.23.4" (as matched by versionRE)
// into its parts: major and minor.
func splitVersion(v string) (major, minor int) {
// splitVersion splits a Go version string such as "go1.23.4" or "go1.24rc1"
// (as matched by versionRE) into its parts: major and minor.
func splitVersion(v string) (major, minor int, _ error) {
m := versionRE.FindStringSubmatch(v)
if len(m) < 2 {
return 0, 0
return 0, 0, fmt.Errorf("no regexp match")
}
parts := strings.Split(m[1], ".")
if len(parts) < 2 {
return 0, 0
major, err := strconv.Atoi(parts[0])
if err != nil {
return 0, 0, fmt.Errorf("parsing major part: %v", err)
}
major, _ = strconv.Atoi(parts[0])
minor, _ = strconv.Atoi(parts[1])
return major, minor
if len(parts) >= 2 {
var err error
minor, err = strconv.Atoi(parts[1])
if err != nil {
return 0, 0, fmt.Errorf("parsing minor part: %v", err)
}
}
return major, minor, nil
}
const storageBase = "https://storage.googleapis.com/go-builder-data/release/"

Просмотреть файл

@ -42,8 +42,13 @@ func TestSplitVersion(t *testing.T) {
}{
{"go1.34.0", 34, 0},
{"go1.34.7", 34, 7},
{"go1.35rc1", 35, 0},
} {
major, minor := splitVersion(tc.v)
major, minor, err := splitVersion(tc.v)
if err != nil {
t.Errorf("splitVersion(%q) returned error %v; want nil", tc.v, err)
continue
}
if major != tc.major || minor != tc.minor {
t.Errorf("splitVersion(%q) = %v, %v; want %v, %v", tc.v, major, minor, tc.major, tc.minor)
}