go-github-selfupdate/selfupdate/detect_test.go

75 lines
1.5 KiB
Go
Raw Normal View History

2017-12-26 02:45:08 +00:00
package selfupdate
import (
"github.com/blang/semver"
"os"
"strings"
2017-12-26 02:45:08 +00:00
"testing"
)
func TestGitHubTokenEnv(t *testing.T) {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
t.Skip("because $GITHUB_TOKEN is not set")
}
_ = NewDetector()
2017-12-26 02:45:08 +00:00
}
func TestDetectReleaseWithVersionPrefix(t *testing.T) {
r, ok, err := DetectLatest("rhysd/github-clone-all")
2017-12-26 02:45:08 +00:00
if err != nil {
t.Fatal("Fetch failed:", err)
}
if !ok {
t.Fatal("Failed to detect latest")
}
if r == nil {
t.Fatal("Release detected but nil returned for it")
}
if r.Version.LE(semver.MustParse("2.0.0")) {
t.Fatal("Incorrect version:", r.Version)
}
if !strings.HasSuffix(r.AssetURL, ".zip") && !strings.HasSuffix(r.AssetURL, ".tar.gz") {
t.Fatal("Incorrect URL for asset:", r.AssetURL)
2017-12-26 02:45:08 +00:00
}
}
func TestInvalidSlug(t *testing.T) {
d := NewDetector()
2017-12-26 02:45:08 +00:00
for _, slug := range []string{
"foo",
"/",
"foo/",
"/bar",
"foo/bar/piyo",
} {
_, _, err := d.DetectLatest(slug)
if err == nil {
t.Error(slug, "should be invalid slug")
}
}
}
func TestNonExistingRepo(t *testing.T) {
d := NewDetector()
2017-12-26 02:45:08 +00:00
v, ok, err := d.DetectLatest("rhysd/non-existing-repo")
if err != nil {
t.Fatal("Non-existing repo should not cause an error:", v)
}
if ok {
t.Fatal("Release for non-existing repo should not be found")
}
}
func TestNoReleaseFound(t *testing.T) {
d := NewDetector()
2017-12-26 02:45:08 +00:00
_, ok, err := d.DetectLatest("rhysd/misc")
if err != nil {
t.Fatal("Repo having no release should not cause an error:", err)
}
if ok {
t.Fatal("Repo having no release should not be found")
}
}