This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirectory_source_test.go
107 lines (88 loc) · 2.61 KB
/
directory_source_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
package configo
import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestDirectorySourceFixture(t *testing.T) {
gunit.Run(new(DirectorySourceFixture), t)
}
type DirectorySourceFixture struct {
*gunit.Fixture
dirPath string
files map[string]string
}
// //////////////////////////////////////////////////////////////
// test c'tor
func (this *DirectorySourceFixture) Setup() {
if directory, err := ioutil.TempDir("", "dirSrc"); err == nil {
this.dirPath = directory
this.files = map[string]string{
"File1": "My file contents",
"A-name_withMixed&Casing": "ContEnT$ _with_\n{{mixed}} Casing\n",
}
for filename, content := range this.files {
full := path.Join(directory, filename)
if err := ioutil.WriteFile(full, []byte(content), 0600); err != nil {
this.Error("Error writing test files:", err)
}
}
} else {
panic(err)
}
}
// test d'tor
func (this *DirectorySourceFixture) Teardown() {
if err := os.RemoveAll(this.dirPath); err != nil {
this.Error("Error removing test files:", err)
}
}
// //////////////////////////////////////////////////////////////
func (this *DirectorySourceFixture) TestBadDirectoryPanic() {
src := FromDirectory("&path/@should/!not/*exist")
this.So(func() { src.Initialize() }, should.Panic)
}
func (this *DirectorySourceFixture) TestBadDirectoryNotPanic() {
src := FromOptionalDirectory("&path/@should/!not/*exist")
src.Initialize()
this.So(len(src.files), should.Equal, 0)
}
func (this *DirectorySourceFixture) TestInitalize() {
src := FromDirectory(this.dirPath)
src.Initialize()
this.So(src.path, should.Equal, this.dirPath)
this.So(src.mustExist, should.Equal, true)
this.So(len(src.files), should.Equal, 2)
}
func (this *DirectorySourceFixture) TestStrings() {
src := FromDirectory(this.dirPath)
src.Initialize()
for key, val := range this.files {
data, err := src.Strings(key)
this.So(err, should.BeEmpty)
this.So(len(data), should.NotBeZeroValue)
this.So(data[0], should.Equal, val)
}
}
func (this *DirectorySourceFixture) TestStringsCase() {
src := FromDirectory(this.dirPath)
src.Initialize()
for key, val := range this.files {
key = strings.ToUpper(key)
data, err := src.Strings(key)
this.So(err, should.BeEmpty)
this.So(len(data), should.NotBeZeroValue)
this.So(data[0], should.Equal, val)
}
}
func (this *DirectorySourceFixture) TestStringsMissing() {
src := FromDirectory(this.dirPath)
src.Initialize()
data, err := src.Strings("key/does/not-exist")
this.So(data, should.BeEmpty)
this.So(err, should.Equal, ErrKeyNotFound)
}