-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrade_test.go
More file actions
99 lines (90 loc) · 2.54 KB
/
Copy pathupgrade_test.go
File metadata and controls
99 lines (90 loc) · 2.54 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
package main
import (
"os"
"path/filepath"
"runtime"
"testing"
)
func TestAssetNameForRuntime(t *testing.T) {
got := assetNameForRuntime()
want := "gander-" + runtime.GOOS + "-" + runtime.GOARCH
if got != want {
t.Errorf("assetNameForRuntime() = %q, want %q", got, want)
}
}
func TestFindAsset(t *testing.T) {
assets := []releaseAsset{
{Name: "gander-darwin-arm64", BrowserDownloadURL: "https://example.com/a"},
{Name: "gander-linux-amd64", BrowserDownloadURL: "https://example.com/b"},
}
got, ok := findAsset(assets, "gander-linux-amd64")
if !ok {
t.Fatal("findAsset: not found")
}
if got.BrowserDownloadURL != "https://example.com/b" {
t.Errorf("got URL %q", got.BrowserDownloadURL)
}
if _, ok := findAsset(assets, "gander-windows-amd64"); ok {
t.Error("findAsset should not match missing platform")
}
}
func TestListAssetNames(t *testing.T) {
got := listAssetNames([]releaseAsset{
{Name: "a"}, {Name: "b"}, {Name: "c"},
})
want := "a, b, c"
if got != want {
t.Errorf("listAssetNames = %q, want %q", got, want)
}
if got := listAssetNames(nil); got != "" {
t.Errorf("listAssetNames(nil) = %q, want empty", got)
}
}
func TestVerifySha256(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "blob")
if err := os.WriteFile(path, []byte("hello"), 0644); err != nil {
t.Fatal(err)
}
if err := verifySha256(path, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"); err != nil {
t.Errorf("verifySha256: %v", err)
}
if err := verifySha256(path, "0000000000000000000000000000000000000000000000000000000000000000"); err == nil {
t.Error("verifySha256 should fail on wrong checksum")
}
}
func TestInstallBinary(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src")
dst := filepath.Join(dir, "subdir", "gander")
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(dst, []byte("old"), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(src, []byte("new"), 0755); err != nil {
t.Fatal(err)
}
if err := installBinary(src, dst); err != nil {
t.Fatalf("installBinary: %v", err)
}
got, err := os.ReadFile(dst)
if err != nil {
t.Fatal(err)
}
if string(got) != "new" {
t.Errorf("installed contents = %q, want %q", got, "new")
}
info, err := os.Stat(dst)
if err != nil {
t.Fatal(err)
}
if info.Mode()&0100 == 0 {
t.Error("installed binary not executable")
}
matches, _ := filepath.Glob(filepath.Join(dir, "subdir", "gander-upgrade-*.bin"))
if len(matches) != 0 {
t.Errorf("temp files left behind: %v", matches)
}
}