-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensure_test.go
More file actions
66 lines (58 loc) · 1.65 KB
/
ensure_test.go
File metadata and controls
66 lines (58 loc) · 1.65 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
package tinygo
import (
"archive/tar"
"compress/gzip"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestEnsureInstalled(t *testing.T) {
// Setup mock archive
archiveFile, _ := os.CreateTemp("", "tinygo-*.tar.gz")
defer os.Remove(archiveFile.Name())
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()
archiveFile.Close()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, archiveFile.Name())
}))
defer ts.Close()
tmpDir, _ := os.MkdirTemp("", "tinygo-ensure-*")
defer os.RemoveAll(tmpDir)
lookPathFails := func(string) (string, error) { return "", fmt.Errorf("not found") }
// Test 1: Install when missing
binPath, err := EnsureInstalled(
WithInstallDir(tmpDir),
withHTTPClient(ts.Client()),
withDownloadURLFunc(func() string { return ts.URL }),
withLookPath(lookPathFails),
withGOOS("linux"),
)
if err != nil {
t.Fatalf("EnsureInstalled failed: %v", err)
}
expectedBinPath := filepath.Join(tmpDir, "tinygo/bin/tinygo")
if binPath != expectedBinPath {
t.Errorf("expected %s, got %s", expectedBinPath, binPath)
}
// Test 2: Return existing when present
binPath2, err := EnsureInstalled(
WithInstallDir(tmpDir),
withLookPath(lookPathFails),
withGOOS("linux"),
)
if err != nil {
t.Fatalf("Second EnsureInstalled failed: %v", err)
}
if binPath2 != binPath {
t.Errorf("expected same path, got %s", binPath2)
}
}