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

Commit

Permalink
Added a regex check for a release candidate syntax in the protobuf ve…
Browse files Browse the repository at this point in the history
…rsion. If it exists, remove the hyphen for the version portion of the URL (#515)
  • Loading branch information
Steve Ayers authored Oct 15, 2019
1 parent 28347be commit 0d05c76
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
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

0 comments on commit 0d05c76

Please sign in to comment.