-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_test.go
More file actions
86 lines (78 loc) · 1.82 KB
/
install_test.go
File metadata and controls
86 lines (78 loc) · 1.82 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
package tinygo
import (
"archive/tar"
"compress/gzip"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestInstall(t *testing.T) {
// 1. Create a dummy tar.gz archive
archiveFile, err := os.CreateTemp("", "tinygo-*.tar.gz")
if err != nil {
t.Fatal(err)
}
defer os.Remove(archiveFile.Name())
defer archiveFile.Close()
gw := gzip.NewWriter(archiveFile)
tw := tar.NewWriter(gw)
mockBody := "#!/bin/bash\necho \"tinygo version 0.40.1 linux/amd64\"\n"
tw.WriteHeader(&tar.Header{
Name: "tinygo/bin/tinygo",
Mode: 0755,
Size: int64(len(mockBody)),
})
tw.Write([]byte(mockBody))
tw.Close()
gw.Close()
// 2. Mock HTTP server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, archiveFile.Name())
}))
defer ts.Close()
// 3. Configure and run Install
tmpInstallDir, err := os.MkdirTemp("", "tinygo-install-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpInstallDir)
err = Install(
WithInstallDir(tmpInstallDir),
withHTTPClient(ts.Client()),
withDownloadURLFunc(func() string { return ts.URL }),
withGOOS("linux"),
)
if err != nil {
t.Fatalf("Install failed: %v", err)
}
// 4. Verify binary exists
binPath := filepath.Join(tmpInstallDir, "tinygo/bin/tinygo")
if _, err := os.Stat(binPath); os.IsNotExist(err) {
t.Errorf("binary not found at %s", binPath)
}
// 5. Test idempotency
var logged []string
logger := func(s string) {
logged = append(logged, s)
}
err = Install(
WithInstallDir(tmpInstallDir),
WithLogger(logger),
withGOOS("linux"),
)
if err != nil {
t.Fatalf("Second Install failed: %v", err)
}
found := false
for _, l := range logged {
if l == "TinyGo already installed at "+binPath {
found = true
break
}
}
if !found {
t.Errorf("Expected idempotency log message")
}
}