github.com/aminjam/goflat@v0.4.1-0.20160331105230-ec639fc0d5b3/goflat_test.go (about) 1 package goflat_test 2 3 import ( 4 "bufio" 5 "bytes" 6 "io" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 11 . "github.com/aminjam/goflat" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("GoFlat", func() { 18 var ( 19 tmpDir string 20 examples string 21 ) 22 BeforeEach(func() { 23 tmpDir, _ = ioutil.TempDir(os.TempDir(), "") 24 wd, _ := os.Getwd() 25 examples = filepath.Join(wd, ".examples") 26 }) 27 AfterEach(func() { 28 defer os.RemoveAll(tmpDir) 29 }) 30 31 Context("when invalid builder", func() { 32 var ( 33 templateDir string 34 builder FlatBuilder 35 buffer bytes.Buffer 36 writer io.Writer 37 ) 38 BeforeEach(func() { 39 templateDir, _ = ioutil.TempDir(os.TempDir(), "") 40 template := filepath.Join(templateDir, "test") 41 err := ioutil.WriteFile(template, []byte(`Hello`), 0666) 42 Expect(err).To(BeNil()) 43 44 builder, err = NewFlatBuilder(tmpDir, template) 45 Expect(err).To(BeNil()) 46 47 writer = bufio.NewWriter(&buffer) 48 }) 49 AfterEach(func() { 50 defer os.RemoveAll(templateDir) 51 buffer.Reset() 52 }) 53 It("should catch undefined MainGo and DefaultPipes", func() { 54 flat := builder.Flat() 55 err := flat.GoRun(writer, writer) 56 Expect(err).ToNot(BeNil()) 57 Expect(err.Error()).To(ContainSubstring(ErrMainGoUndefined)) 58 Expect(err.Error()).To(ContainSubstring(ErrDefaultPipesUndefined)) 59 }) 60 It("should catch undefined MainGo", func() { 61 builder.EvalGoPipes("") 62 flat := builder.Flat() 63 err := flat.GoRun(writer, writer) 64 Expect(err).ToNot(BeNil()) 65 Expect(err.Error()).To(ContainSubstring(ErrMainGoUndefined)) 66 Expect(err.Error()).ToNot(ContainSubstring(ErrDefaultPipesUndefined)) 67 }) 68 It("should catch undefined DefaultPipes", func() { 69 builder.EvalMainGo() 70 flat := builder.Flat() 71 err := flat.GoRun(writer, writer) 72 Expect(err).ToNot(BeNil()) 73 Expect(err.Error()).ToNot(ContainSubstring(ErrMainGoUndefined)) 74 Expect(err.Error()).To(ContainSubstring(ErrDefaultPipesUndefined)) 75 }) 76 }) 77 78 Context("when defining custom pipes", func() { 79 It("should override and extend default pipes", func() { 80 templateDir, _ := ioutil.TempDir(os.TempDir(), "") 81 defer os.RemoveAll(templateDir) 82 template := filepath.Join(templateDir, "test") 83 err := ioutil.WriteFile(template, []byte(`Hello {{"oink oink oink" | replace "k" "ky"}}, tell us a {{"SECRET" | sanitize}}.`), 0666) 84 Expect(err).To(BeNil()) 85 customPipes := filepath.Join(examples, "pipes", "pipes.go") 86 87 builder, err := NewFlatBuilder(tmpDir, template) 88 Expect(err).To(BeNil()) 89 90 err = builder.EvalGoPipes(customPipes) 91 Expect(err).To(BeNil()) 92 err = builder.EvalMainGo() 93 Expect(err).To(BeNil()) 94 95 var buffer bytes.Buffer 96 writer := bufio.NewWriter(&buffer) 97 flat := builder.Flat() 98 err = flat.GoRun(writer, writer) 99 Expect(err).To(BeNil()) 100 101 Expect(buffer.String()).To(ContainSubstring("Hello oinky oink oink, tell us a TERCES.")) 102 }) 103 }) 104 105 Context("when current working direction is changed", func() { 106 var wd string 107 BeforeEach(func() { 108 wd, _ = os.Getwd() 109 err := os.Chdir(examples) 110 Expect(err).To(BeNil()) 111 }) 112 AfterEach(func() { 113 err := os.Chdir(wd) 114 Expect(err).To(BeNil()) 115 }) 116 It("should run successfully", func() { 117 118 templateDir, _ := ioutil.TempDir(os.TempDir(), "") 119 defer os.RemoveAll(templateDir) 120 template := filepath.Join(templateDir, "test") 121 err := ioutil.WriteFile(template, []byte(`Hello World.`), 0666) 122 Expect(err).To(BeNil()) 123 124 builder, err := NewFlatBuilder(tmpDir, template) 125 Expect(err).To(BeNil()) 126 127 err = builder.EvalGoPipes("") 128 Expect(err).To(BeNil()) 129 err = builder.EvalMainGo() 130 Expect(err).To(BeNil()) 131 132 var buffer bytes.Buffer 133 writer := bufio.NewWriter(&buffer) 134 flat := builder.Flat() 135 err = flat.GoRun(writer, writer) 136 Expect(err).To(BeNil()) 137 138 Expect(buffer.String()).To(ContainSubstring("Hello World")) 139 }) 140 }) 141 142 Context("when go packages are missing", func() { 143 It("should go get the needed packages", func() { 144 assetsDir, _ := ioutil.TempDir(os.TempDir(), "") 145 defer os.RemoveAll(assetsDir) 146 template := filepath.Join(assetsDir, "template") 147 err := ioutil.WriteFile(template, []byte(`{{.Object.Name}}`), 0666) 148 Expect(err).To(BeNil()) 149 inputFile := filepath.Join(assetsDir, "object.go") 150 err = ioutil.WriteFile(inputFile, []byte(`package main 151 import "gopkg.in/yaml.v2" 152 type Object struct { Name string } 153 func NewObject() Object { 154 o := Object{} 155 yaml.Unmarshal([]byte("name: Jane"), &o) 156 return o 157 }`), 0666) 158 Expect(err).To(BeNil()) 159 160 builder, err := NewFlatBuilder(tmpDir, template) 161 Expect(err).To(BeNil()) 162 163 err = builder.EvalGoInputs([]string{inputFile}) 164 Expect(err).To(BeNil()) 165 err = builder.EvalGoPipes("") 166 Expect(err).To(BeNil()) 167 err = builder.EvalMainGo() 168 Expect(err).To(BeNil()) 169 170 var buffer bytes.Buffer 171 writer := bufio.NewWriter(&buffer) 172 flat := builder.Flat() 173 err = os.Unsetenv("GOPATH") 174 Expect(err).To(BeNil()) 175 err = flat.GoRun(writer, writer) 176 Expect(err).To(BeNil()) 177 178 Expect(buffer.String()).To(Equal("Jane\n")) 179 }) 180 }) 181 182 Context("when running the examples templates", func() { 183 var ( 184 result []byte 185 result_file string 186 buffer bytes.Buffer 187 188 template string 189 ) 190 AfterEach(func() { 191 buffer.Reset() 192 }) 193 JustBeforeEach(func() { 194 builder, err := NewFlatBuilder(tmpDir, template) 195 Expect(err).To(BeNil()) 196 197 inputFiles := []string{ 198 filepath.Join(examples, "inputs", "private.go"), 199 filepath.Join(examples, "inputs", "repos.go"), 200 } 201 err = builder.EvalGoInputs(inputFiles) 202 Expect(err).To(BeNil()) 203 err = builder.EvalGoPipes("") 204 Expect(err).To(BeNil()) 205 err = builder.EvalMainGo() 206 Expect(err).To(BeNil()) 207 208 writer := bufio.NewWriter(&buffer) 209 flat := builder.Flat() 210 err = flat.GoRun(writer, writer) 211 Expect(err).To(BeNil()) 212 result, err = ioutil.ReadFile(result_file) 213 Expect(err).To(BeNil()) 214 }) 215 Describe("parsing YAML template", func() { 216 BeforeEach(func() { 217 template = filepath.Join(examples, "template.yml") 218 result_file = filepath.Join(examples, "output.yml") 219 }) 220 It("should show the parsed output", func() { 221 Expect(result).ToNot(BeNil()) 222 Expect(buffer.String()).To(Equal(string(result))) 223 }) 224 }) 225 Describe("parsing JSON template", func() { 226 BeforeEach(func() { 227 template = filepath.Join(examples, "template.json") 228 result_file = filepath.Join(examples, "output.json") 229 }) 230 It("should show the parsed output", func() { 231 Expect(result).ToNot(BeNil()) 232 Expect(buffer.String()).To(Equal(string(result))) 233 }) 234 }) 235 Describe("parsing XML template", func() { 236 BeforeEach(func() { 237 template = filepath.Join(examples, "template.xml") 238 result_file = filepath.Join(examples, "output.xml") 239 }) 240 It("should show the parsed output", func() { 241 Expect(result).ToNot(BeNil()) 242 Expect(buffer.String()).To(Equal(string(result))) 243 }) 244 }) 245 }) 246 })