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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
package flamegraph
import (
"errors"
"os"
"os/exec"
"strconv"
"testing"
)
func TestBrowserOpenCommandPartsRequiresCommand(t *testing.T) {
_, err := browserOpenCommandParts("", "http://localhost:1234/")
if err == nil {
t.Fatalf("expected error for empty open command")
}
}
func TestBrowserOpenCommandPartsAppendsURLWhenMissing(t *testing.T) {
got, err := browserOpenCommandParts("chromium --new-window", "http://localhost:1234/")
if err != nil {
t.Fatalf("browserOpenCommandParts returned error: %v", err)
}
want := []string{"chromium", "--new-window", "http://localhost:1234/"}
if len(got) != len(want) {
t.Fatalf("len(parts) = %d, want %d", len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("parts[%d] = %q, want %q", i, got[i], want[i])
}
}
}
func TestBrowserOpenCommandPartsReplacesURLPlaceholder(t *testing.T) {
got, err := browserOpenCommandParts("open-browser --target={url}", "http://localhost:1234/")
if err != nil {
t.Fatalf("browserOpenCommandParts returned error: %v", err)
}
want := []string{"open-browser", "--target=http://localhost:1234/"}
if len(got) != len(want) {
t.Fatalf("len(parts) = %d, want %d", len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("parts[%d] = %q, want %q", i, got[i], want[i])
}
}
}
func TestMaybeOpenLiveBrowserWithoutCommandSkipsOpen(t *testing.T) {
called := false
orig := openBrowserURLFn
openBrowserURLFn = func(url, openCommand string) error {
called = true
return nil
}
t.Cleanup(func() { openBrowserURLFn = orig })
err := maybeOpenLiveBrowser("http://localhost:1234/", LiveServerOptions{})
if err != nil {
t.Fatalf("maybeOpenLiveBrowser returned error: %v", err)
}
if called {
t.Fatalf("expected browser opener not to be called without open command")
}
}
func TestMaybeOpenLiveBrowserWithCommandCallsOpen(t *testing.T) {
called := false
orig := openBrowserURLFn
openBrowserURLFn = func(url, openCommand string) error {
called = true
if url != "http://localhost:1234/" {
t.Fatalf("url = %q, want %q", url, "http://localhost:1234/")
}
if openCommand != "chromium" {
t.Fatalf("openCommand = %q, want %q", openCommand, "chromium")
}
return nil
}
t.Cleanup(func() { openBrowserURLFn = orig })
err := maybeOpenLiveBrowser("http://localhost:1234/", LiveServerOptions{
OpenCommand: "chromium",
})
if err != nil {
t.Fatalf("maybeOpenLiveBrowser returned error: %v", err)
}
if !called {
t.Fatalf("expected browser opener to be called")
}
}
func TestMaybeOpenLiveBrowserPropagatesOpenError(t *testing.T) {
orig := openBrowserURLFn
openBrowserURLFn = func(url, openCommand string) error {
return errors.New("launch failed")
}
t.Cleanup(func() { openBrowserURLFn = orig })
err := maybeOpenLiveBrowser("http://localhost:1234/", LiveServerOptions{
OpenCommand: "chromium",
})
if err == nil || err.Error() != "launch failed" {
t.Fatalf("expected launch failed error, got %v", err)
}
}
func TestOpenBrowserURLReturnsErrorWhenCommandExitsNonZero(t *testing.T) {
err := openBrowserURL("http://localhost:1234/", "false")
if err == nil {
t.Fatalf("expected non-nil error")
}
}
func TestOpenBrowserURLReturnsNilWhenCommandExitsZero(t *testing.T) {
err := openBrowserURL("http://localhost:1234/", "true")
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
}
func TestApplySudoInvokerContextWithEnvSetsCredential(t *testing.T) {
cmd := exec.Command("echo")
uid := os.Getuid()
gid := os.Getgid()
env := []string{
"SUDO_UID=" + strconv.Itoa(uid),
"SUDO_GID=" + strconv.Itoa(gid),
"SUDO_USER=tester",
"HOME=/root",
}
applySudoInvokerContextWithEnv(cmd, 0, env)
if cmd.SysProcAttr == nil || cmd.SysProcAttr.Credential == nil {
t.Fatalf("expected process credentials to be configured")
}
if got := cmd.SysProcAttr.Credential.Uid; got != uint32(uid) {
t.Fatalf("credential uid = %d, want %d", got, uint32(uid))
}
if got := cmd.SysProcAttr.Credential.Gid; got != uint32(gid) {
t.Fatalf("credential gid = %d, want %d", got, uint32(gid))
}
if got, ok := lookupEnvValue(cmd.Env, "USER"); !ok || got != "tester" {
t.Fatalf("USER env = %q (ok=%v), want %q", got, ok, "tester")
}
if got, ok := lookupEnvValue(cmd.Env, "LOGNAME"); !ok || got != "tester" {
t.Fatalf("LOGNAME env = %q (ok=%v), want %q", got, ok, "tester")
}
}
func TestApplySudoInvokerContextWithEnvSkipsWhenNotRoot(t *testing.T) {
cmd := exec.Command("echo")
env := []string{
"SUDO_UID=1000",
"SUDO_GID=1000",
"SUDO_USER=tester",
}
applySudoInvokerContextWithEnv(cmd, 1000, env)
if cmd.SysProcAttr != nil {
t.Fatalf("expected credentials to remain nil for non-root euid")
}
if cmd.Env != nil {
t.Fatalf("expected environment to remain nil for non-root euid")
}
}
func TestNotifyLiveWarningUsesCallback(t *testing.T) {
var got string
notifyLiveWarning(func(message string) {
got = message
}, "open failed")
if got != "open failed" {
t.Fatalf("warning callback got %q, want %q", got, "open failed")
}
}
|