-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
154 lines (135 loc) · 3.96 KB
/
main_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
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
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"path"
"testing"
homedir "github.com/mitchellh/go-homedir"
)
var UserHomeDir string
func init() {
// Determine Users home directory
var err error
UserHomeDir, err = homedir.Dir()
if err != nil {
log.Fatalf("Could not determine Users home directory: %v", err)
}
}
func TestParseConfig(t *testing.T) {
difFolderPerm := []struct {
name string
folderPerm os.FileMode
configLocation string
expResp string
}{
{
"no perm, directory exists",
0000,
"/tmp/noPermissionsHere/config.json",
"Could not access config file: open /tmp/noPermissionsHere/config.json: permission denied",
},
{
"execute only, directory doesn't exist yet",
0100,
"/tmp/executeOnly/config.json",
"Could not write default config: open /tmp/executeOnly/config.json: permission denied",
},
{
"write only",
0200,
"/tmp/writeOnly/config.json",
"Could not access config file: open /tmp/writeOnly/config.json: permission denied",
},
{
"write & execute",
0300,
"/tmp/writeAndExecute/config.json",
"no error",
}, {
"read only",
0400,
"/tmp/readOnly/config.json",
"Could not access config file: open /tmp/readOnly/config.json: permission denied",
}, {
"read & execute",
0500,
"/tmp/readAndExecute/config.json",
"Could not write default config: open /tmp/readAndExecute/config.json: permission denied",
}, {
"read & write",
0600,
"/tmp/readAndWrite/config.json",
"Could not access config file: open /tmp/readAndWrite/config.json: permission denied",
}, {
"all permissions",
0700,
"/tmp/allPermissions/config.json",
"no error",
},
}
for _, tc := range difFolderPerm {
t.Run(tc.name, func(t *testing.T) {
confDir := path.Dir(tc.configLocation)
if err := os.Mkdir(confDir, tc.folderPerm); err != nil {
t.Fatalf("Could not create dir: %s: %v", confDir, err)
}
_, err := parseConfig(tc.configLocation)
// Check if the response from parseConfig, is the expected one
if err != nil {
if err.Error() != tc.expResp {
t.Errorf("Expected: '%v' got: '%v'", tc.expResp, err.Error())
}
} else {
if tc.expResp != "no error" {
t.Errorf("Got no error, but expected '%v'", tc.expResp)
}
}
// Make sure in the successful cases the file is actually there
if tc.expResp == "no error" {
cont, err := ioutil.ReadFile(tc.configLocation)
if err != nil {
t.Errorf("Expected a file at %s, but could not read it due to: %v", tc.configLocation, err)
}
defaultConfig := []byte(`{"affectedApps":["Mail","Calendar"]}`)
if bytes.Compare(cont, defaultConfig) != 0 {
t.Errorf("%s should have the following content: '%v', but has '%v'", tc.configLocation, defaultConfig, cont)
}
}
// Clean up
os.Chmod(confDir, 0700)
if err := os.RemoveAll(confDir); err != nil {
t.Fatalf("Could not clean up '%s': %v; Please clean up this directory manually, as it can corrupt further testing", confDir, err)
}
})
}
}
func TestDetermineActions(t *testing.T) {
// Test Default Case
if determineActions("") != nil {
t.Errorf("Calling determineActions with no parameters, should return nil")
}
if determineActions("does-not-exist") != nil {
t.Errorf("Calling determinActions with an invalid parameter, should return nil")
}
// Test Version Case
if determineActions("version") != nil {
t.Errorf("Calling determinActions with the version flag, should return nil")
}
// Set Up some sample apps
curConfig.AffectedApps = []string{
"Calendar",
"Messages",
}
// Test On Case
res := determineActions("on")
if len(res) != len(curConfig.AffectedApps)+1 { //plus 1 for Notification Center
t.Errorf("len res %d, len curConfig %d", len(res), len(curConfig.AffectedApps))
}
// Test Off Case
res = determineActions("off")
if len(res) != len(curConfig.AffectedApps)+1 { //plus 1 for Notification Center
t.Errorf("len res %d, len curConfig %d", len(res), len(curConfig.AffectedApps))
}
}