Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Better parsing for release candidates with protoc URL #515

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/protoc/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -310,9 +311,14 @@ func (d *downloader) getProtocURL(goos string, goarch string) (string, error) {
if err != nil {
return "", err
}

// Protoc download URLs for release candidates don't use the hyphen in the version part of the URL
re := regexp.MustCompile(`(rc)-(\d+$)`)
version := re.ReplaceAllString(d.config.Compile.ProtobufVersion, "$1$2")

return fmt.Sprintf(
"https://github.com/protocolbuffers/protobuf/releases/download/v%s/protoc-%s-%s-%s.zip",
d.config.Compile.ProtobufVersion,
version,
d.config.Compile.ProtobufVersion,
protocS,
unameM,
Expand Down
71 changes: 71 additions & 0 deletions internal/protoc/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,77 @@ func TestNewDownloaderProtocValidation(t *testing.T) {
}
}

func TestNewDownloaderProtocURL(t *testing.T) {
tests := []struct {
desc string
expectedURL string
expectError bool
goarch string
goos string
protobufVersion string
}{
{
protobufVersion: "3.10.0",
goos: "linux",
goarch: "amd64",
expectedURL: "https://github.com/protocolbuffers/protobuf/releases/download/v3.10.0/protoc-3.10.0-linux-x86_64.zip",
desc: "linux amd64 official version",
},
{
protobufVersion: "3.10.0-rc-1",
goos: "linux",
goarch: "amd64",
expectedURL: "https://github.com/protocolbuffers/protobuf/releases/download/v3.10.0-rc1/protoc-3.10.0-rc-1-linux-x86_64.zip",
desc: "linux amd64 release candidate",
},
{
protobufVersion: "3.10.0",
goos: "darwin",
goarch: "amd64",
expectedURL: "https://github.com/protocolbuffers/protobuf/releases/download/v3.10.0/protoc-3.10.0-osx-x86_64.zip",
desc: "darwin amd64 official version",
},
{
protobufVersion: "3.10.0-rc-1",
goos: "darwin",
goarch: "amd64",
expectedURL: "https://github.com/protocolbuffers/protobuf/releases/download/v3.10.0-rc1/protoc-3.10.0-rc-1-osx-x86_64.zip",
desc: "darwin amd64 release candidate",
},
{
goos: "foo",
goarch: "amd64",
expectError: true,
desc: "invalid goos",
},
{
goos: "linux",
goarch: "foo",
expectError: true,
desc: "invalid goarch",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
dl, err := newDownloader(
settings.Config{
Compile: settings.CompileConfig{
ProtobufVersion: tt.protobufVersion,
},
},
)

url, err := dl.getProtocURL(tt.goos, tt.goarch)
if tt.expectError {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, url, tt.expectedURL)
}
})
}
}

func newTestGetenvFunc(xdgCacheHome string, home string) func(string) string {
m := make(map[string]string)
if xdgCacheHome != "" {
Expand Down