-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmanifest_test.go
129 lines (104 loc) · 3.2 KB
/
manifest_test.go
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
// manifest_test.go - Test for manifest related functionality on Linux.
// Copyright (c) 2018 - 2020 Richard Huang <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// +build !darwin,!windows
package host
import (
"encoding/json"
"errors"
"github.com/google/go-cmp/cmp"
"io/ioutil"
"log"
"os"
"testing"
)
func TestManifestTargetName(t *testing.T) {
t.Parallel()
got := (&Host{AppName: "app"}).getTargetName()
homeDir, _ := os.UserHomeDir()
want := homeDir + "/.config/google-chrome/NativeMessagingHosts/app.json"
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestManifestInstall(t *testing.T) {
t.Parallel()
log.SetOutput(ioutil.Discard)
compare := func(wantErr int, uninstall bool) func(t *testing.T) {
return func(t *testing.T) {
got := &Host{}
want := &Host{AppName: "install"}
targetName := want.getTargetName()
switch wantErr {
case 0:
if uninstall {
os.Remove(targetName)
}
case 1:
oldOsMkdirAll := osMkdirAll
defer func() { osMkdirAll = oldOsMkdirAll }()
osMkdirAll = func(string, os.FileMode) error {
return errors.New("MkdirAll error")
}
case 2:
oldWriteFile := ioutilWriteFile
defer func() { ioutilWriteFile = oldWriteFile }()
ioutilWriteFile = func(string, []byte, os.FileMode) error {
return errors.New("WriteFile error")
}
}
if err := want.Install(); wantErr == 0 && err != nil {
t.Errorf("install error %s: %v", targetName, err)
} else if wantErr > 0 && err == nil {
t.Fatalf("want error: %s", targetName)
}
if wantErr == 0 {
if _, err := os.Stat(targetName); err != nil {
t.Errorf("missing file %s: %v", targetName, err)
}
manifest, err := ioutil.ReadFile(targetName)
if err != nil {
t.Errorf("read manifest error %s: %v", targetName, err)
}
if err := json.Unmarshal(manifest, got); err != nil {
t.Errorf("unmarshal manifest error %s: %v", targetName, err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
}
}
t.Run("with nothing installed", compare(0, false))
t.Run("with existing installed", compare(0, true))
t.Run("with MkdirAll error", compare(1, false))
t.Run("with WriteFile error", compare(2, false))
}
func TestManifestUninstall(t *testing.T) {
log.SetOutput(ioutil.Discard)
compare := func(h *Host) func(t *testing.T) {
return func(t *testing.T) {
exited := false
oldRuntimeGoexit := runtimeGoexit
defer func() { runtimeGoexit = oldRuntimeGoexit }()
runtimeGoexit = func() { exited = true }
targetName := h.getTargetName()
h.Uninstall()
if _, err := os.Stat(targetName); err == nil {
t.Errorf("uninstall failed %s", targetName)
}
if !exited {
t.Errorf("uninstall did not exit")
}
}
}
h := &Host{AppName: "uninstall"}
t.Run("with nothing installed", compare(h))
if err := h.Install(); err != nil {
t.Errorf("install error %s: %v", h.getTargetName(), err)
}
t.Run("with installed", compare(h))
}