github.com/shogo82148/goa-v1@v1.6.2/goagen/meta/generator_test.go (about) 1 package meta_test 2 3 import ( 4 "os" 5 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 "github.com/shogo82148/goa-v1/goagen/codegen" 9 "github.com/shogo82148/goa-v1/goagen/meta" 10 ) 11 12 var _ = Describe("Run", func() { 13 14 const invalidPkgPath = "foobar" 15 16 var ( 17 compiledFiles []string 18 compileError error 19 outputWorkspace *codegen.Workspace 20 designWorkspace *codegen.Workspace 21 genWorkspace *codegen.Workspace 22 23 outputDir string 24 designPkgPath string 25 genPkgSource string 26 customFlags []string 27 28 m *meta.Generator 29 ) 30 _ = compiledFiles 31 32 BeforeEach(func() { 33 designPkgPath = "design" 34 genPkgSource = "package gen\nfunc Generate() ([]string, error) { return nil, nil }" 35 36 var err error 37 38 outputWorkspace, err = codegen.NewWorkspace("output") 39 Ω(err).ShouldNot(HaveOccurred()) 40 p, err := outputWorkspace.NewPackage("testOutput") 41 Ω(err).ShouldNot(HaveOccurred()) 42 outputDir = p.Abs() 43 44 designWorkspace, err = codegen.NewWorkspace("test") 45 Ω(err).ShouldNot(HaveOccurred()) 46 47 genWorkspace, err = codegen.NewWorkspace("gen") 48 Ω(err).ShouldNot(HaveOccurred()) 49 50 compiledFiles = nil 51 compileError = nil 52 customFlags = []string{"--custom=arg"} 53 }) 54 55 JustBeforeEach(func() { 56 if designPkgPath != "" && designPkgPath != invalidPkgPath { 57 designPackage, err := designWorkspace.NewPackage(designPkgPath) 58 Ω(err).ShouldNot(HaveOccurred()) 59 file, err := designPackage.CreateSourceFile("design.go") 60 Ω(err).ShouldNot(HaveOccurred()) 61 _, err = file.Write([]byte("package design")) 62 Ω(err).ShouldNot(HaveOccurred()) 63 file.Close() 64 } 65 66 genPackage, err := genWorkspace.NewPackage("gen") 67 Ω(err).ShouldNot(HaveOccurred()) 68 file, err := genPackage.CreateSourceFile("gen.go") 69 Ω(err).ShouldNot(HaveOccurred()) 70 _, err = file.Write([]byte(genPkgSource)) 71 Ω(err).ShouldNot(HaveOccurred()) 72 file.Close() 73 74 m = &meta.Generator{ 75 Genfunc: "gen.Generate", 76 Imports: []*codegen.ImportSpec{codegen.SimpleImport("gen")}, 77 OutDir: outputDir, 78 CustomFlags: customFlags, 79 DesignPkgPath: designPkgPath, 80 } 81 compiledFiles, compileError = m.Generate() 82 }) 83 84 AfterEach(func() { 85 designWorkspace.Delete() 86 outputWorkspace.Delete() 87 genWorkspace.Delete() 88 }) 89 90 // FIXME: @shogo82148 91 // Context("with an invalid GOPATH environment variable", func() { 92 // var gopath string 93 // const invalidPath = "DOES NOT EXIST" 94 95 // BeforeEach(func() { 96 // gopath = os.Getenv("GOPATH") 97 // os.Setenv("GOPATH", invalidPath) 98 // }) 99 100 // AfterEach(func() { 101 // os.Setenv("GOPATH", gopath) 102 // }) 103 104 // It("fails with a useful error message", func() { 105 // Ω(compileError).Should(MatchError(HavePrefix(`invalid design package import path: cannot find package "design" in any of:`))) 106 // Ω(compileError).Should(MatchError(HaveSuffix(filepath.Join(invalidPath, "src", "design") + " (from $GOPATH)"))) 107 // }) 108 109 // }) 110 111 // Context("with an invalid design package path", func() { 112 // BeforeEach(func() { 113 // designPkgPath = invalidPkgPath 114 // }) 115 116 // It("fails with a useful error message", func() { 117 // Ω(compileError).Should(MatchError(HavePrefix("invalid design package import path: cannot find package"))) 118 // Ω(compileError).Should(MatchError(ContainSubstring(invalidPkgPath))) 119 // }) 120 // }) 121 122 // Context("with no go compiler in PATH", func() { 123 // var pathEnv string 124 // const invalidPath = "/foobar" 125 126 // BeforeEach(func() { 127 // pathEnv = os.Getenv("PATH") 128 // os.Setenv("PATH", invalidPath) 129 // }) 130 131 // AfterEach(func() { 132 // os.Setenv("PATH", pathEnv) 133 // }) 134 135 // It("fails with a useful error message", func() { 136 // Ω(compileError).Should(MatchError(`failed to find a go compiler, looked in "` + os.Getenv("PATH") + `"`)) 137 // }) 138 // }) 139 140 Context("with no output directory specified", func() { 141 BeforeEach(func() { 142 outputDir = "" 143 }) 144 145 It("fails with a useful error message", func() { 146 Ω(compileError).Should(MatchError("missing output directory flag")) 147 }) 148 }) 149 150 Context("with no design package path specified", func() { 151 BeforeEach(func() { 152 designPkgPath = "" 153 }) 154 155 It("fails with a useful error message", func() { 156 Ω(compileError).Should(MatchError("missing design package flag")) 157 }) 158 }) 159 160 Context("with gen package content", func() { 161 162 BeforeEach(func() { 163 outputDir = os.TempDir() 164 }) 165 166 // Context("that is not valid Go code", func() { 167 // BeforeEach(func() { 168 // genPkgSource = invalidSource 169 // }) 170 171 // It("fails with a useful error message", func() { 172 // Ω(compileError.Error()).Should(ContainSubstring("syntax error")) 173 // }) 174 // }) 175 176 // Context("whose code blows up", func() { 177 // BeforeEach(func() { 178 // genPkgSource = panickySource 179 // }) 180 181 // It("fails with a useful error message", func() { 182 // Ω(compileError.Error()).Should(ContainSubstring("panic: kaboom")) 183 // }) 184 // }) 185 186 // Context("with valid code", func() { 187 // BeforeEach(func() { 188 // genPkgSource = validSource 189 // }) 190 191 // It("successfully runs", func() { 192 // Ω(compileError).ShouldNot(HaveOccurred()) 193 // }) 194 195 // Context("with a comma separated list of path in GOPATH", func() { 196 // var gopath string 197 // BeforeEach(func() { 198 // gopath = os.Getenv("GOPATH") 199 // os.Setenv("GOPATH", fmt.Sprintf("%s%c%s", gopath, os.PathListSeparator, os.TempDir())) 200 // }) 201 202 // AfterEach(func() { 203 // os.Setenv("GOPATH", gopath) 204 // }) 205 206 // It("successful runs", func() { 207 // Ω(compileError).ShouldNot(HaveOccurred()) 208 // }) 209 // }) 210 // }) 211 212 // Context("with code that returns generated file paths", func() { 213 // var filePaths = []string{"foo", "bar"} 214 215 // BeforeEach(func() { 216 // var b bytes.Buffer 217 // tmpl, err := template.New("source").Parse(validSourceTmpl) 218 // Ω(err).ShouldNot(HaveOccurred()) 219 // err = tmpl.Execute(&b, filePaths) 220 // Ω(err).ShouldNot(HaveOccurred()) 221 // genPkgSource = b.String() 222 // }) 223 224 // It("returns the paths", func() { 225 // Ω(compileError).ShouldNot(HaveOccurred()) 226 // Ω(compiledFiles).Should(Equal(filePaths)) 227 // }) 228 // }) 229 230 // Context("with code that uses custom flags", func() { 231 // BeforeEach(func() { 232 // var b bytes.Buffer 233 // tmpl, err := template.New("source").Parse(validSourceTmplWithCustomFlags) 234 // Ω(err).ShouldNot(HaveOccurred()) 235 // err = tmpl.Execute(&b, "--custom=arg") 236 // Ω(err).ShouldNot(HaveOccurred()) 237 // genPkgSource = b.String() 238 239 // }) 240 241 // It("returns no error", func() { 242 // Ω(compileError).ShouldNot(HaveOccurred()) 243 // }) 244 // }) 245 }) 246 }) 247 248 const ( 249 invalidSource = `package gen 250 invalid go code 251 ` 252 253 panickySource = `package gen 254 func Generate() ([]string, error) { 255 return nil, nil 256 } 257 258 func init() { panic("kaboom") } 259 ` 260 261 validSource = `package gen 262 func Generate() ([]string, error) { 263 return nil, nil 264 } 265 ` 266 267 validSourceTmpl = `package gen 268 import "fmt" 269 func Generate() ([]string, error) { 270 {{range .}}fmt.Println("{{.}}") 271 {{end}} 272 return nil, nil 273 } 274 ` 275 276 validSourceTmplWithCustomFlags = `package gen 277 import "fmt" 278 import "os" 279 280 func Generate() ([]string, error) { 281 for _, arg := range os.Args { 282 if arg == "{{.}}" { 283 return nil, nil 284 } 285 } 286 return nil, fmt.Errorf("no flag {{.}} found") 287 } 288 ` 289 )