Skip to content
Merged
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
87 changes: 87 additions & 0 deletions pkg/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (

const testBaseURL = "https://example.com/file.tgz"

const (
testGithubOrg = "devsy-org"
testGithubRepo = "devsy"
testGithubFile = "devsy-linux-amd64"
testGithubTag = "v1.2.3"
)

func TestSanitizeURL(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -38,3 +45,83 @@ func TestSanitizeURL(t *testing.T) {
})
}
}

func TestParseGithubURLValid(t *testing.T) {
tests := []struct {
name string
path string
want githubReleaseRef
}{
{
name: "latest release",
path: "/" + testGithubOrg + "/" + testGithubRepo + "/releases/latest/download/" + testGithubFile,
want: githubReleaseRef{
org: testGithubOrg,
repo: testGithubRepo,
file: testGithubFile,
},
},
{
name: "tagged release",
path: "/" + testGithubOrg + "/" + testGithubRepo + "/releases/download/" + testGithubTag + "/" + testGithubFile,
want: githubReleaseRef{
org: testGithubOrg,
repo: testGithubRepo,
release: testGithubTag,
file: testGithubFile,
},
},
{
name: "latest without leading slash",
path: testGithubOrg + "/" + testGithubRepo + "/releases/latest/download/" + testGithubFile,
want: githubReleaseRef{
org: testGithubOrg,
repo: testGithubRepo,
file: testGithubFile,
},
},
{
name: "tagged without leading slash",
path: testGithubOrg + "/" + testGithubRepo + "/releases/download/" + testGithubTag + "/" + testGithubFile,
want: githubReleaseRef{
org: testGithubOrg,
repo: testGithubRepo,
release: testGithubTag,
file: testGithubFile,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := parseGithubURL(tt.path)
if !ok {
t.Fatalf("parseGithubURL(%q) ok = false, want true", tt.path)
}
if got != tt.want {
t.Errorf("parseGithubURL(%q) = %+v, want %+v", tt.path, got, tt.want)
}
})
}
}

func TestParseGithubURLInvalid(t *testing.T) {
tests := []struct {
name string
path string
}{
{name: "too few segments", path: "/devsy-org/devsy/releases/latest/download"},
{name: "too many segments", path: "/devsy-org/devsy/releases/latest/download/dir/file"},
{name: "missing releases segment", path: "/devsy-org/devsy/archive/v1.2.3/file"},
{name: "unknown middle segment", path: "/devsy-org/devsy/releases/foo/download/file"},
{name: "latest without download", path: "/devsy-org/devsy/releases/latest/file"},
{name: "empty path", path: ""},
{name: "root slash", path: "/"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, ok := parseGithubURL(tt.path); ok {
t.Errorf("parseGithubURL(%q) ok = true, want false", tt.path)
}
})
}
}
Loading