-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathembeddedcli_test.go
More file actions
136 lines (119 loc) · 2.99 KB
/
Copy pathembeddedcli_test.go
File metadata and controls
136 lines (119 loc) · 2.99 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
134
135
136
package embeddedcli
import (
"bytes"
"crypto/sha256"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func resetGlobals() {
setupMu.Lock()
defer setupMu.Unlock()
config = Config{}
setupDone = false
pathInitialized = false
}
func mustPanic(t *testing.T, fn func()) {
t.Helper()
defer func() {
if r := recover(); r == nil {
t.Fatalf("expected panic")
}
}()
fn()
}
func binaryNameForOS() string {
name := "copilot"
if runtime.GOOS == "windows" {
name += ".exe"
}
return name
}
func TestSetupPanicsOnNilCli(t *testing.T) {
resetGlobals()
mustPanic(t, func() { Setup(Config{}) })
}
func TestSetupPanicsOnSecondCall(t *testing.T) {
resetGlobals()
hash := sha256.Sum256([]byte("ok"))
Setup(Config{Cli: bytes.NewReader([]byte("ok")), CliHash: hash[:]})
hash2 := sha256.Sum256([]byte("ok"))
mustPanic(t, func() { Setup(Config{Cli: bytes.NewReader([]byte("ok")), CliHash: hash2[:]}) })
resetGlobals()
}
func TestInstallAtWritesBinaryAndLicense(t *testing.T) {
resetGlobals()
tempDir := t.TempDir()
content := []byte("hello")
hash := sha256.Sum256(content)
Setup(Config{
Cli: bytes.NewReader(content),
CliHash: hash[:],
License: []byte("license"),
Version: "1.2.3",
Dir: tempDir,
})
path := Path()
expectedPath := versionedBinaryPath(tempDir, binaryNameForOS(), "1.2.3")
if path != expectedPath {
t.Fatalf("unexpected path: got %q want %q", path, expectedPath)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read binary: %v", err)
}
if !bytes.Equal(got, content) {
t.Fatalf("binary content mismatch")
}
licensePath := path + ".license"
license, err := os.ReadFile(licensePath)
if err != nil {
t.Fatalf("read license: %v", err)
}
if string(license) != "license" {
t.Fatalf("license content mismatch")
}
gotHash, err := hashFile(path)
if err != nil {
t.Fatalf("hash file: %v", err)
}
if !bytes.Equal(gotHash, hash[:]) {
t.Fatalf("hash mismatch")
}
}
func TestInstallAtExistingBinaryHashMismatch(t *testing.T) {
resetGlobals()
tempDir := t.TempDir()
binaryPath := versionedBinaryPath(tempDir, binaryNameForOS(), "")
if err := os.MkdirAll(filepath.Dir(binaryPath), 0755); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(binaryPath, []byte("bad"), 0755); err != nil {
t.Fatalf("write binary: %v", err)
}
goodHash := sha256.Sum256([]byte("good"))
config = Config{
Cli: bytes.NewReader([]byte("good")),
CliHash: goodHash[:],
}
_, err := installAt(tempDir)
if err == nil || !strings.Contains(err.Error(), "hash mismatch") {
t.Fatalf("expected hash mismatch error, got %v", err)
}
}
func TestSanitizeVersion(t *testing.T) {
got := sanitizeVersion("v1.2.3+build/abc")
want := "v1.2.3_build_abc"
if got != want {
t.Fatalf("sanitizeVersion() = %q want %q", got, want)
}
}
func TestVersionedBinaryPath(t *testing.T) {
got := versionedBinaryPath("/tmp", "copilot.exe", "1.0.0")
want := filepath.Join("/tmp", "copilot_1.0.0.exe")
if got != want {
t.Fatalf("versionedBinaryPath() = %q want %q", got, want)
}
}