File: updater_test.go

package info (click to toggle)
golang-github-rhysd-go-github-selfupdate 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: sh: 21; makefile: 6
file content (106 lines) | stat: -rw-r--r-- 2,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package selfupdate

import (
	"os"
	"strings"
	"testing"
)

func TestGitHubTokenEnv(t *testing.T) {
	token := os.Getenv("GITHUB_TOKEN")
	if token == "" {
		t.Skip("because $GITHUB_TOKEN is not set")
	}
	_ = DefaultUpdater()
	if _, err := NewUpdater(Config{}); err != nil {
		t.Error("Failed to initialize updater with empty config")
	}
	if _, err := NewUpdater(Config{APIToken: token}); err != nil {
		t.Error("Failed to initialize updater with API token config")
	}
}

func TestGitHubTokenIsNotSet(t *testing.T) {
	token := os.Getenv("GITHUB_TOKEN")
	if token != "" {
		defer os.Setenv("GITHUB_TOKEN", token)
	}
	os.Setenv("GITHUB_TOKEN", "")
	_ = DefaultUpdater()
	if _, err := NewUpdater(Config{}); err != nil {
		t.Error("Failed to initialize updater with empty config")
	}
}

func TestGitHubEnterpriseClient(t *testing.T) {
	url := "https://github.company.com/api/v3/"
	up, err := NewUpdater(Config{APIToken: "hogehoge", EnterpriseBaseURL: url})
	if err != nil {
		t.Fatal(err)
	}
	if up.api.BaseURL.String() != url {
		t.Error("Base URL was set to", up.api.BaseURL, ", want", url)
	}
	if up.api.UploadURL.String() != url {
		t.Error("Upload URL was set to", up.api.UploadURL, ", want", url)
	}

	url2 := "https://upload.github.company.com/api/v3/"
	up, err = NewUpdater(Config{
		APIToken:            "hogehoge",
		EnterpriseBaseURL:   url,
		EnterpriseUploadURL: url2,
	})
	if err != nil {
		t.Fatal(err)
	}
	if up.api.BaseURL.String() != url {
		t.Error("Base URL was set to", up.api.BaseURL, ", want", url)
	}
	if up.api.UploadURL.String() != url2 {
		t.Error("Upload URL was set to", up.api.UploadURL, ", want", url2)
	}
}

func TestGitHubEnterpriseClientInvalidURL(t *testing.T) {
	_, err := NewUpdater(Config{APIToken: "hogehoge", EnterpriseBaseURL: ":this is not a URL"})
	if err == nil {
		t.Fatal("Invalid URL should raise an error")
	}
}

func TestCompileRegexForFiltering(t *testing.T) {
	filters := []string{
		"^hello$",
		"^(\\d\\.)+\\d$",
	}
	up, err := NewUpdater(Config{
		Filters: filters,
	})
	if err != nil {
		t.Fatal(err)
	}
	if len(up.filters) != 2 {
		t.Fatalf("Wanted 2 regexes but got %d", len(up.filters))
	}
	for i, r := range up.filters {
		want := filters[i]
		got := r.String()
		if want != got {
			t.Errorf("Compiled regex is %q but specified was %q", got, want)
		}
	}
}

func TestFilterRegexIsBroken(t *testing.T) {
	_, err := NewUpdater(Config{
		Filters: []string{"(foo"},
	})
	if err == nil {
		t.Fatal("Error unexpectedly did not occur")
	}
	msg := err.Error()
	if !strings.Contains(msg, "Could not compile regular expression \"(foo\" for filtering releases") {
		t.Fatalf("Error message is unexpected: %q", msg)
	}
}