-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinygo_test.go
More file actions
133 lines (115 loc) · 3.28 KB
/
tinygo_test.go
File metadata and controls
133 lines (115 loc) · 3.28 KB
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
package tinygo
import (
"net/http"
"path/filepath"
"runtime"
"testing"
)
func TestNewConfig(t *testing.T) {
c := newConfig()
if c.version != DefaultVersion {
t.Errorf("expected version %s, got %s", DefaultVersion, c.version)
}
expectedDir := defaultInstallDir(runtime.GOOS)
if c.installDir != expectedDir {
t.Errorf("expected installDir %s, got %s", expectedDir, c.installDir)
}
if c.lookPath == nil {
t.Error("lookPath should not be nil")
}
if c.httpClient == nil {
t.Error("httpClient should not be nil")
}
}
func TestWithVersion(t *testing.T) {
v := "0.39.0"
c := newConfig(WithVersion(v))
if c.version != v {
t.Errorf("expected version %s, got %s", v, c.version)
}
}
func TestWithInstallDir(t *testing.T) {
dir := "/tmp/tinygo"
c := newConfig(WithInstallDir(dir))
if c.installDir != dir {
t.Errorf("expected installDir %s, got %s", dir, c.installDir)
}
}
func TestBinPath(t *testing.T) {
tests := []struct {
goos string
expected string
}{
{"linux", "tinygo/bin/tinygo"},
{"darwin", "tinygo/bin/tinygo"},
{"windows", "tinygo/bin/tinygo.exe"},
}
for _, tt := range tests {
t.Run(tt.goos, func(t *testing.T) {
c := newConfig(WithInstallDir("/tmp"), withGOOS(tt.goos))
got := c.binPath()
// filepath.Join handles platform-specific path separators.
// However, in our tests, we're forcing GOOS. Let's make sure our comparison is correct.
var expected string
if tt.goos == "windows" {
// Special case for windows if we're on a non-windows host.
// However, filepath.Join will use host's separator.
// Since binPath() uses filepath.Join, it will use host's separator.
expected = filepath.Join("/tmp", "tinygo", "bin", "tinygo.exe")
} else {
expected = filepath.Join("/tmp", "tinygo", "bin", "tinygo")
}
if got != expected {
t.Errorf("expected %s, got %s", expected, got)
}
})
}
}
func TestDownloadURL(t *testing.T) {
tests := []struct {
goos string
goarch string
ext string
}{
{"linux", "amd64", "tar.gz"},
{"linux", "arm64", "tar.gz"},
{"darwin", "amd64", "tar.gz"},
{"darwin", "arm64", "tar.gz"},
{"windows", "amd64", "zip"},
}
version := "0.40.1"
for _, tt := range tests {
t.Run(tt.goos+"-"+tt.goarch, func(t *testing.T) {
c := newConfig(WithVersion(version), withGOOS(tt.goos), withGOARCH(tt.goarch))
got := c.downloadURL()
expected := "https://github.com/tinygo-org/tinygo/releases/download/v" +
version + "/tinygo" + version + "." + tt.goos + "-" + tt.goarch + "." + tt.ext
if got != expected {
t.Errorf("expected %s, got %s", expected, got)
}
})
}
}
func TestInternalOptions(t *testing.T) {
mockLookPath := func(string) (string, error) { return "", nil }
mockClient := &http.Client{}
c := newConfig(withLookPath(mockLookPath), withHTTPClient(mockClient))
if c.lookPath == nil {
t.Error("lookPath should not be nil")
}
// We can't easily compare function pointers, but we can verify it's set.
if c.httpClient != mockClient {
t.Error("httpClient was not set correctly")
}
}
func TestWithLogger(t *testing.T) {
var logged []string
logger := func(s string) {
logged = append(logged, s)
}
c := newConfig(WithLogger(logger))
c.logger("test message")
if len(logged) != 1 || logged[0] != "test message" {
t.Errorf("logger not working as expected")
}
}