-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconf_test.go
More file actions
123 lines (107 loc) · 3.05 KB
/
Copy pathconf_test.go
File metadata and controls
123 lines (107 loc) · 3.05 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
// conf_test.go: test suite for conf.go
//
// Copyright (c) 2013 CloudFlare, Inc.
package conf
import (
"io/ioutil"
"os"
"strings"
"testing"
)
func assert(t *testing.T, b bool) {
if !b {
t.Fail()
}
}
// makeFile creates a file with the given contents, returns the file
// name. Returns an empty string on error.
func makeFile(c string) (fn string) {
if f, err := ioutil.TempFile(os.TempDir(), "conf"); err == nil {
f.WriteString(c)
f.Close()
return f.Name()
} else {
return ""
}
}
func TestEmptyConfig(t *testing.T) {
f := makeFile("")
c, err := ReadConfigFile(f)
assert(t, c != nil)
assert(t, err == nil)
assert(t, c.CheckUnread() == "")
os.Remove(f)
}
func TestSimpleConfig(t *testing.T) {
// Note: specific things being tested here:
//
// foo=1 (a normal line)
// \n\n completely empty lines
// \n \n lines with just whitespace
// # comment lines that have comments in them
// bar = baz lines with embedded whitespace and not trailing \n
f := makeFile("foo=1\n \n \n# comment\n\n\n bar = baz ")
c, err := ReadConfigFile(f)
assert(t, c != nil)
assert(t, err == nil)
assert(t, c.CheckUnread() != "")
assert(t, c.GetString("foo", "FOO") == "1")
assert(t, c.GetUint("foo", 2) == 1)
assert(t, c.GetString("bar", "BAR") == "baz")
assert(t, c.GetUint("bar", 2) == 2)
assert(t, c.CheckUnread() == "")
assert(t, c.GetString("baz", "BAZ") == "BAZ")
assert(t, c.GetUint("baz", 3) == 3)
assert(t, c.GetString("bam", "") == "")
assert(t, c.GetUint("bam", 0) == 0)
os.Remove(f)
// Check handling of equals sign in values
f = makeFile("foo==FOO\nbar=BAR=\nbaz=Baz=Baz")
c, err = ReadConfigFile(f)
assert(t, c != nil)
assert(t, err == nil)
assert(t, c.GetString("foo", "") == "=FOO")
assert(t, c.GetString("bar", "") == "BAR=")
assert(t, c.GetString("baz", "") == "Baz=Baz")
os.Remove(f)
// Check handling of empty value
f = makeFile("foo=1\nbar=")
c, err = ReadConfigFile(f)
assert(t, c != nil)
assert(t, err == nil)
assert(t, c.GetString("foo", "FOO") == "1")
assert(t, c.GetUint("foo", 2) == 1)
assert(t, c.GetString("bar", "BAR") == "")
assert(t, c.GetUint("bar", 2) == 2)
os.Remove(f)
}
func TestCheckUnread(t *testing.T) {
f := makeFile("foo=1\nbar=baz")
c, err := ReadConfigFile(f)
assert(t, c != nil)
assert(t, err == nil)
assert(t, c.CheckUnread() != "")
assert(t, strings.Contains(c.CheckUnread(), "foo (1)"))
assert(t, strings.Contains(c.CheckUnread(), "bar (2)"))
assert(t, c.GetString("bar", "BAR") == "baz")
assert(t, strings.Contains(c.CheckUnread(), "foo (1)"))
assert(t, !strings.Contains(c.CheckUnread(), "bar (2)"))
assert(t, c.GetString("foo", "FOO") == "1")
assert(t, !strings.Contains(c.CheckUnread(), "foo (1)"))
assert(t, !strings.Contains(c.CheckUnread(), "bar (2)"))
os.Remove(f)
}
func TestCheckErrors(t *testing.T) {
f := makeFile("foo=1\nbaz")
_, err := ReadConfigFile(f)
assert(t, err != nil)
os.Remove(f)
f = makeFile("foo=1\n=baz")
_, err = ReadConfigFile(f)
assert(t, err != nil)
os.Remove(f)
f = makeFile("foo=1\nfoo=2")
_, err = ReadConfigFile(f)
assert(t, err != nil)
os.Remove(f)
}