-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathstartup_test.go
More file actions
89 lines (81 loc) · 3.06 KB
/
Copy pathstartup_test.go
File metadata and controls
89 lines (81 loc) · 3.06 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
package menuet
import (
"strings"
"testing"
)
func TestRenderLaunchdPlistUsesLabelForLaunchdLabelKey(t *testing.T) {
// Regression for the bug where the plist template used {{.Name}} for the
// launchd <key>Label</key> value. Apps that left Name empty would emit a
// plist with an empty Label and launchd would silently ignore the agent.
rendered, err := renderLaunchdPlist(launchdPlistData{
Name: "Human Readable Name",
Label: "com.example.app",
Executable: "/Applications/App.app/Contents/MacOS/app",
})
if err != nil {
t.Fatalf("renderLaunchdPlist: %v", err)
}
if !strings.Contains(rendered, "<key>Label</key><string>com.example.app</string>") {
t.Errorf("launchd Label should be the reverse-DNS identifier; rendered:\n%s", rendered)
}
if strings.Contains(rendered, "<string>Human Readable Name</string>") {
t.Errorf("Name field should not leak into the plist; rendered:\n%s", rendered)
}
}
func TestRenderLaunchdPlistWithEmptyNameStillProducesValidLabel(t *testing.T) {
// cmd/weather and many menuet consumers only set Label, never Name.
// Before the fix this case produced an empty <key>Label</key><string></string>.
rendered, err := renderLaunchdPlist(launchdPlistData{
Name: "",
Label: "com.example.app",
Executable: "/Applications/App.app/Contents/MacOS/app",
})
if err != nil {
t.Fatalf("renderLaunchdPlist: %v", err)
}
if !strings.Contains(rendered, "<key>Label</key><string>com.example.app</string>") {
t.Errorf("empty Name should not blank out the launchd Label; rendered:\n%s", rendered)
}
if strings.Contains(rendered, "<string></string>") {
t.Errorf("plist should not contain an empty <string></string>; rendered:\n%s", rendered)
}
}
func TestRenderLaunchdPlistContainsExecutable(t *testing.T) {
exe := "/Applications/App.app/Contents/MacOS/app"
rendered, err := renderLaunchdPlist(launchdPlistData{
Label: "com.example.app",
Executable: exe,
})
if err != nil {
t.Fatalf("renderLaunchdPlist: %v", err)
}
if !strings.Contains(rendered, "<key>Program</key><string>"+exe+"</string>") {
t.Errorf("plist should embed the executable path; rendered:\n%s", rendered)
}
}
func TestGetStartupPathWithoutLabelReturnsEmpty(t *testing.T) {
// Regression for issue #7: previously this called log.Fatal and crashed
// the host menubar app. The fix is to warn and return empty so callers
// can no-op.
a := &Application{}
if got := a.getStartupPath(); got != "" {
t.Errorf("getStartupPath() with no Label = %q, want empty string", got)
}
}
func TestRunningAtStartupWithoutLabelDoesNotPanic(t *testing.T) {
a := &Application{}
if a.runningAtStartup() {
t.Errorf("runningAtStartup() with no Label = true, want false")
}
}
func TestRemoveStartupItemWithoutLabelIsNoOp(t *testing.T) {
// Should not panic, should not attempt os.Remove("") which produces
// confusing "remove : no such file or directory" log lines.
a := &Application{}
a.removeStartupItem()
}
func TestAddStartupItemWithoutLabelIsNoOp(t *testing.T) {
// Previously this path crashed via log.Fatal in getStartupPath.
a := &Application{}
a.addStartupItem()
}