-
Notifications
You must be signed in to change notification settings - Fork 31
/
yaml_test.go
91 lines (77 loc) · 2.13 KB
/
yaml_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
package libbuildpack_test
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/cloudfoundry/libbuildpack"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("YAML", func() {
var (
yaml *libbuildpack.YAML
tmpDir string
err error
)
BeforeEach(func() {
tmpDir, err = ioutil.TempDir("", "yaml")
Expect(err).To(BeNil())
yaml = &libbuildpack.YAML{}
})
AfterEach(func() {
err = os.RemoveAll(tmpDir)
Expect(err).To(BeNil())
})
Describe("Load", func() {
Context("file is valid yaml", func() {
BeforeEach(func() {
ioutil.WriteFile(filepath.Join(tmpDir, "valid.yml"), []byte("key: value"), 0666)
})
It("returns an error", func() {
obj := make(map[string]string)
err = yaml.Load(filepath.Join(tmpDir, "valid.yml"), &obj)
Expect(err).To(BeNil())
Expect(obj["key"]).To(Equal("value"))
})
})
Context("file is NOT valid yaml", func() {
BeforeEach(func() {
ioutil.WriteFile(filepath.Join(tmpDir, "invalid.yml"), []byte("not valid yml"), 0666)
})
It("returns an error", func() {
obj := make(map[string]string)
err = yaml.Load(filepath.Join(tmpDir, "invalid.yml"), &obj)
Expect(err).ToNot(BeNil())
})
})
Context("file does not exist", func() {
It("returns an error", func() {
obj := make(map[string]string)
err = yaml.Load(filepath.Join(tmpDir, "does_not_exist.yml"), &obj)
Expect(err).ToNot(BeNil())
})
})
})
Describe("Write", func() {
Context("directory exists", func() {
It("writes the yaml to a file ", func() {
obj := map[string]string{
"key": "val",
}
err = yaml.Write(filepath.Join(tmpDir, "file.yml"), obj)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(filepath.Join(tmpDir, "file.yml"))).To(Equal([]byte("key: val\n")))
})
})
Context("directory does not exist", func() {
It("creates the directory ", func() {
obj := map[string]string{
"key": "val",
}
err = yaml.Write(filepath.Join(tmpDir, "extradir", "file.yml"), obj)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(filepath.Join(tmpDir, "extradir", "file.yml"))).To(Equal([]byte("key: val\n")))
})
})
})
})