-
Notifications
You must be signed in to change notification settings - Fork 26
/
gobrew_test.go
167 lines (140 loc) · 4.14 KB
/
gobrew_test.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gobrew
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func setupGobrew[T testing.TB](t T, ts *httptest.Server) GoBrew {
tags, _ := url.JoinPath(ts.URL, "golang-tags.json")
versionURL, _ := url.JoinPath(ts.URL, "latest")
config := Config{
RootDir: t.TempDir(),
RegistryPathURL: ts.URL,
GobrewDownloadURL: ts.URL,
GobrewTags: tags,
GobrewVersionsURL: versionURL,
}
gb := NewGoBrew(config)
return gb
}
func BenchmarkInstallGo(t *testing.B) {
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
gb := setupGobrew(t, ts)
defer ts.Close()
for i := 0; i < t.N; i++ {
gb.Install("1.9")
exists := gb.existsVersion("1.9")
assert.Equal(t, true, exists)
}
}
func TestInstallAndExistVersion(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.9")
exists := gb.existsVersion("1.9")
assert.Equal(t, true, exists)
t.Log("test finished")
}
func TestUnInstallThenNotExistVersion(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.9")
exists := gb.existsVersion("1.9")
assert.Equal(t, true, exists)
gb.Uninstall("1.9")
exists = gb.existsVersion("1.9")
assert.Equal(t, false, exists)
t.Log("test finished")
}
func TestUpgrade(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)
baseName := ProgramName + fileExt
binaryFile := filepath.Join(binaryDir, baseName)
if oldFile, err := os.Create(binaryFile); err == nil {
// on tests, we have to close the file to avoid an error on os.Rename
_ = oldFile.Close()
}
gb.Upgrade("0.0.0")
if _, err := os.Stat(binaryFile); err != nil {
t.Errorf("updated executable does not exist")
}
t.Log("test finished")
}
func TestDoNotUpgradeLatestVersion(t *testing.T) {
t.Skip("skipping test...needs to rewrite")
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)
baseName := ProgramName + fileExt
binaryFile := filepath.Join(binaryDir, baseName)
currentVersion := gb.getGobrewVersion()
if currentVersion == "" {
t.Skip("could not determine the current version")
}
gb.Upgrade(currentVersion[1:])
if _, err := os.Stat(binaryFile); err == nil {
t.Errorf("unexpected upgrade of latest version")
}
t.Log("test finished")
}
func TestInteractive(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
currentVersion := gb.CurrentVersion()
latestVersion := gb.getLatestVersion()
assert.Equal(t, NoneVersion, currentVersion)
assert.NotEqual(t, currentVersion, latestVersion)
gb.Interactive(false)
currentVersion = gb.CurrentVersion()
assert.Equal(t, currentVersion, latestVersion)
gb.Install("1.16.5") // we know, it is not latest
gb.Use("1.16.5")
currentVersion = gb.CurrentVersion()
assert.Equal(t, "1.16.5", currentVersion)
assert.NotEqual(t, currentVersion, latestVersion)
gb.Interactive(false)
currentVersion = gb.CurrentVersion()
assert.Equal(t, currentVersion, latestVersion)
t.Log("test finished")
}
func TestPrune(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.20")
gb.Install("1.19")
gb.Use("1.19")
gb.Prune()
assert.Equal(t, false, gb.existsVersion("1.20"))
assert.Equal(t, true, gb.existsVersion("1.19"))
t.Log("test finished")
}
func TestGoBrew_CurrentVersion(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
assert.Equal(t, true, gb.CurrentVersion() == NoneVersion)
gb.Install("1.19")
gb.Use("1.19")
assert.Equal(t, true, gb.CurrentVersion() == "1.19")
t.Log("test finished")
}