/
goflat_test.go
246 lines (219 loc) · 6.73 KB
/
goflat_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package goflat_test
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
. "github.com/aminjam/goflat"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("GoFlat", func() {
var (
tmpDir string
examples string
)
BeforeEach(func() {
tmpDir, _ = ioutil.TempDir(os.TempDir(), "")
wd, _ := os.Getwd()
examples = filepath.Join(wd, ".examples")
})
AfterEach(func() {
defer os.RemoveAll(tmpDir)
})
Context("when invalid builder", func() {
var (
templateDir string
builder FlatBuilder
buffer bytes.Buffer
writer io.Writer
)
BeforeEach(func() {
templateDir, _ = ioutil.TempDir(os.TempDir(), "")
template := filepath.Join(templateDir, "test")
err := ioutil.WriteFile(template, []byte(`Hello`), 0666)
Expect(err).To(BeNil())
builder, err = NewFlatBuilder(tmpDir, template)
Expect(err).To(BeNil())
writer = bufio.NewWriter(&buffer)
})
AfterEach(func() {
defer os.RemoveAll(templateDir)
buffer.Reset()
})
It("should catch undefined MainGo and DefaultPipes", func() {
flat := builder.Flat()
err := flat.GoRun(writer, writer)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(ErrMainGoUndefined))
Expect(err.Error()).To(ContainSubstring(ErrDefaultPipesUndefined))
})
It("should catch undefined MainGo", func() {
builder.EvalGoPipes("")
flat := builder.Flat()
err := flat.GoRun(writer, writer)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(ErrMainGoUndefined))
Expect(err.Error()).ToNot(ContainSubstring(ErrDefaultPipesUndefined))
})
It("should catch undefined DefaultPipes", func() {
builder.EvalMainGo()
flat := builder.Flat()
err := flat.GoRun(writer, writer)
Expect(err).ToNot(BeNil())
Expect(err.Error()).ToNot(ContainSubstring(ErrMainGoUndefined))
Expect(err.Error()).To(ContainSubstring(ErrDefaultPipesUndefined))
})
})
Context("when defining custom pipes", func() {
It("should override and extend default pipes", func() {
templateDir, _ := ioutil.TempDir(os.TempDir(), "")
defer os.RemoveAll(templateDir)
template := filepath.Join(templateDir, "test")
err := ioutil.WriteFile(template, []byte(`Hello {{"oink oink oink" | replace "k" "ky"}}, tell us a {{"SECRET" | sanitize}}.`), 0666)
Expect(err).To(BeNil())
customPipes := filepath.Join(examples, "pipes", "pipes.go")
builder, err := NewFlatBuilder(tmpDir, template)
Expect(err).To(BeNil())
err = builder.EvalGoPipes(customPipes)
Expect(err).To(BeNil())
err = builder.EvalMainGo()
Expect(err).To(BeNil())
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
flat := builder.Flat()
err = flat.GoRun(writer, writer)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("Hello oinky oink oink, tell us a TERCES."))
})
})
Context("when current working direction is changed", func() {
var wd string
BeforeEach(func() {
wd, _ = os.Getwd()
err := os.Chdir(examples)
Expect(err).To(BeNil())
})
AfterEach(func() {
err := os.Chdir(wd)
Expect(err).To(BeNil())
})
It("should run successfully", func() {
templateDir, _ := ioutil.TempDir(os.TempDir(), "")
defer os.RemoveAll(templateDir)
template := filepath.Join(templateDir, "test")
err := ioutil.WriteFile(template, []byte(`Hello World.`), 0666)
Expect(err).To(BeNil())
builder, err := NewFlatBuilder(tmpDir, template)
Expect(err).To(BeNil())
err = builder.EvalGoPipes("")
Expect(err).To(BeNil())
err = builder.EvalMainGo()
Expect(err).To(BeNil())
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
flat := builder.Flat()
err = flat.GoRun(writer, writer)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("Hello World"))
})
})
Context("when go packages are missing", func() {
It("should go get the needed packages", func() {
assetsDir, _ := ioutil.TempDir(os.TempDir(), "")
defer os.RemoveAll(assetsDir)
template := filepath.Join(assetsDir, "template")
err := ioutil.WriteFile(template, []byte(`{{.Object.Name}}`), 0666)
Expect(err).To(BeNil())
inputFile := filepath.Join(assetsDir, "object.go")
err = ioutil.WriteFile(inputFile, []byte(`package main
import "gopkg.in/yaml.v2"
type Object struct { Name string }
func NewObject() Object {
o := Object{}
yaml.Unmarshal([]byte("name: Jane"), &o)
return o
}`), 0666)
Expect(err).To(BeNil())
builder, err := NewFlatBuilder(tmpDir, template)
Expect(err).To(BeNil())
err = builder.EvalGoInputs([]string{inputFile})
Expect(err).To(BeNil())
err = builder.EvalGoPipes("")
Expect(err).To(BeNil())
err = builder.EvalMainGo()
Expect(err).To(BeNil())
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
flat := builder.Flat()
err = os.Unsetenv("GOPATH")
Expect(err).To(BeNil())
err = flat.GoRun(writer, writer)
Expect(err).To(BeNil())
Expect(buffer.String()).To(Equal("Jane\n"))
})
})
Context("when running the examples templates", func() {
var (
result []byte
result_file string
buffer bytes.Buffer
template string
)
AfterEach(func() {
buffer.Reset()
})
JustBeforeEach(func() {
builder, err := NewFlatBuilder(tmpDir, template)
Expect(err).To(BeNil())
inputFiles := []string{
filepath.Join(examples, "inputs", "private.go"),
filepath.Join(examples, "inputs", "repos.go"),
}
err = builder.EvalGoInputs(inputFiles)
Expect(err).To(BeNil())
err = builder.EvalGoPipes("")
Expect(err).To(BeNil())
err = builder.EvalMainGo()
Expect(err).To(BeNil())
writer := bufio.NewWriter(&buffer)
flat := builder.Flat()
err = flat.GoRun(writer, writer)
Expect(err).To(BeNil())
result, err = ioutil.ReadFile(result_file)
Expect(err).To(BeNil())
})
Describe("parsing YAML template", func() {
BeforeEach(func() {
template = filepath.Join(examples, "template.yml")
result_file = filepath.Join(examples, "output.yml")
})
It("should show the parsed output", func() {
Expect(result).ToNot(BeNil())
Expect(buffer.String()).To(Equal(string(result)))
})
})
Describe("parsing JSON template", func() {
BeforeEach(func() {
template = filepath.Join(examples, "template.json")
result_file = filepath.Join(examples, "output.json")
})
It("should show the parsed output", func() {
Expect(result).ToNot(BeNil())
Expect(buffer.String()).To(Equal(string(result)))
})
})
Describe("parsing XML template", func() {
BeforeEach(func() {
template = filepath.Join(examples, "template.xml")
result_file = filepath.Join(examples, "output.xml")
})
It("should show the parsed output", func() {
Expect(result).ToNot(BeNil())
Expect(buffer.String()).To(Equal(string(result)))
})
})
})
})