github.com/aminjam/goflat@v0.4.1-0.20160331105230-ec639fc0d5b3/goflatbuilder_test.go (about) 1 package goflat_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 . "github.com/aminjam/goflat" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("GoFlatBuilder", func() { 16 var ( 17 tmpDir string 18 examples string 19 ) 20 BeforeEach(func() { 21 tmpDir, _ = ioutil.TempDir(os.TempDir(), "") 22 wd, _ := os.Getwd() 23 examples = filepath.Join(wd, ".examples") 24 }) 25 AfterEach(func() { 26 defer os.RemoveAll(tmpDir) 27 }) 28 29 Context("with invalid params", func() { 30 It("should catch invalid baseDir", func() { 31 builder, err := NewFlatBuilder("INVALID", "INVALID") 32 Expect(builder).To(BeNil()) 33 Expect(err).ToNot(BeNil()) 34 Expect(err.Error()).To(ContainSubstring(ErrMissingOnDisk)) 35 }) 36 It("should catch invalid input files", func() { 37 template := filepath.Join(examples, "template.yml") 38 invalid_input_files := []string{"/WRONG/FILE", "WRONG/ANOTHER/FILES"} 39 40 builder, err := NewFlatBuilder(tmpDir, template) 41 Expect(err).To(BeNil()) 42 err = builder.EvalGoInputs(invalid_input_files) 43 Expect(err).ToNot(BeNil()) 44 Expect(err.Error()).To(ContainSubstring(ErrMissingOnDisk)) 45 Expect(err.Error()).To(ContainSubstring("/WRONG/FILE")) 46 }) 47 48 It("should catch invalid template", func() { 49 invalid_template := "/WRONG/FILE.yml" 50 51 builder, err := NewFlatBuilder(tmpDir, invalid_template) 52 Expect(builder).To(BeNil()) 53 Expect(err).ToNot(BeNil()) 54 Expect(err.Error()).To(ContainSubstring(ErrMissingOnDisk)) 55 Expect(err.Error()).To(ContainSubstring(invalid_template)) 56 }) 57 }) 58 Context("#EvalGoInputs", func() { 59 var builder FlatBuilder 60 BeforeEach(func() { 61 var err error 62 template := filepath.Join(examples, "template.yml") 63 builder, err = NewFlatBuilder(tmpDir, template) 64 Expect(err).To(BeNil()) 65 }) 66 It("should create destination input file", func() { 67 inputFiles := []string{ 68 filepath.Join(examples, "inputs", "private.go"), 69 } 70 err := builder.EvalGoInputs(inputFiles) 71 Expect(err).To(BeNil()) 72 flat := builder.Flat() 73 Expect(len(flat.GoInputs)).To(Equal(1)) 74 newFileInfo, err := os.Stat(flat.GoInputs[0].Path) 75 Expect(err).To(BeNil()) 76 orgFileInfo, _ := os.Stat(inputFiles[0]) 77 Expect(orgFileInfo.Size()).To(Equal(newFileInfo.Size())) 78 }) 79 It("should evaluate files with custom struct", func() { 80 orgFile := filepath.Join(examples, "inputs", "a-private-note") 81 err := builder.EvalGoInputs([]string{ 82 orgFile + ":Private", 83 }) 84 Expect(err).To(BeNil()) 85 86 flat := builder.Flat() 87 Expect(len(flat.GoInputs)).To(Equal(1)) 88 newFileInfo, err := os.Stat(flat.GoInputs[0].Path) 89 Expect(err).To(BeNil()) 90 orgFileInfo, _ := os.Stat(orgFile) 91 Expect(orgFileInfo.Size()).To(Equal(newFileInfo.Size())) 92 }) 93 }) 94 Context("#EvalGoPipes", func() { 95 var builder FlatBuilder 96 BeforeEach(func() { 97 var err error 98 template := filepath.Join(examples, "template.yml") 99 builder, err = NewFlatBuilder(tmpDir, template) 100 Expect(err).To(BeNil()) 101 }) 102 It("should create destination input file", func() { 103 pipesFile := filepath.Join(examples, "pipes", "pipes.go") 104 err := builder.EvalGoPipes(pipesFile) 105 Expect(err).To(BeNil()) 106 flat := builder.Flat() 107 Expect(flat.CustomPipes).ToNot(BeEmpty()) 108 109 newFileInfo, err := os.Stat(flat.CustomPipes) 110 Expect(err).To(BeNil()) 111 orgFileInfo, _ := os.Stat(pipesFile) 112 Expect(orgFileInfo.Size()).To(Equal(newFileInfo.Size())) 113 }) 114 }) 115 Context("#EvalMainGo", func() { 116 It("should have created main.go", func() { 117 var ( 118 templateDir, template, infoGo string 119 err error 120 ) 121 templateDir, _ = ioutil.TempDir(os.TempDir(), "") 122 defer os.RemoveAll(templateDir) 123 template = filepath.Join(templateDir, "test.txt") 124 err = ioutil.WriteFile(template, []byte("Hello {{.Info.Name}}"), 0666) 125 Expect(err).To(BeNil()) 126 infoGo = filepath.Join(templateDir, "info.go") 127 err = ioutil.WriteFile(infoGo, []byte(`package main 128 type Info struct { Name string } 129 func NewInfo() Info { return Info { Name: "Jane" } }`), 0666) 130 Expect(err).To(BeNil()) 131 132 builder, err := NewFlatBuilder(tmpDir, template) 133 Expect(err).To(BeNil()) 134 err = builder.EvalGoInputs([]string{infoGo}) 135 Expect(err).To(BeNil()) 136 err = builder.EvalMainGo() 137 Expect(err).To(BeNil()) 138 flat := builder.Flat() 139 140 newFileInfo, err := os.Stat(flat.MainGo) 141 Expect(err).To(BeNil()) 142 Expect(newFileInfo).ToNot(BeNil()) 143 144 data, err := ioutil.ReadFile(flat.MainGo) 145 Expect(err).To(BeNil()) 146 Expect(data).To(ContainSubstring(fmt.Sprintf("data, err := ioutil.ReadFile(\"%s\")", flat.GoTemplate))) 147 Expect(data).To(ContainSubstring(fmt.Sprintf( 148 "result.%s = New%s()", flat.GoInputs[0].StructName, flat.GoInputs[0].StructName))) 149 }) 150 }) 151 })