github.com/shogo82148/goa-v1@v1.6.2/goagen/gen_main/generator_test.go (about) 1 package genmain_test 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 "github.com/shogo82148/goa-v1/design" 11 genmain "github.com/shogo82148/goa-v1/goagen/gen_main" 12 "github.com/shogo82148/goa-v1/version" 13 ) 14 15 var _ = Describe("Generate", func() { 16 const testgenPackagePath = "github.com/shogo82148/goa-v1/goagen/gen_main/goatest" 17 18 var outDir string 19 var files []string 20 var genErr error 21 22 BeforeEach(func() { 23 gopath := filepath.SplitList(os.Getenv("GOPATH"))[0] 24 outDir = filepath.Join(gopath, "src", testgenPackagePath) 25 err := os.MkdirAll(outDir, 0777) 26 Ω(err).ShouldNot(HaveOccurred()) 27 os.Args = []string{"goagen", "--out=" + outDir, "--design=foo", "--version=" + version.String()} 28 }) 29 30 JustBeforeEach(func() { 31 files, genErr = genmain.Generate() 32 }) 33 34 AfterEach(func() { 35 os.RemoveAll(outDir) 36 }) 37 38 // FIXME: @shogo82148 https://github.com/shogo82148/goa-v1/pull/1/checks?check_run_id=382586488#step:6:80 39 // Context("with a dummy API", func() { 40 // BeforeEach(func() { 41 // design.Design = &design.APIDefinition{ 42 // Name: "test api", 43 // Title: "dummy API with no resource", 44 // Description: "I told you it's dummy", 45 // } 46 // }) 47 48 // It("generates a dummy app", func() { 49 // Ω(genErr).Should(BeNil()) 50 // Ω(files).Should(HaveLen(1)) 51 // content, err := ioutil.ReadFile(filepath.Join(outDir, "main.go")) 52 // Ω(err).ShouldNot(HaveOccurred()) 53 // Ω(len(strings.Split(string(content), "\n"))).Should(BeNumerically(">=", 16)) 54 // _, err = gexec.Build(testgenPackagePath) 55 // Ω(err).ShouldNot(HaveOccurred()) 56 // }) 57 // }) 58 59 Context("with resources", func() { 60 var resource *design.ResourceDefinition 61 62 BeforeEach(func() { 63 resource = &design.ResourceDefinition{ 64 Name: "first", 65 Description: "first stuff", 66 Actions: map[string]*design.ActionDefinition{}, 67 } 68 alpha := &design.ActionDefinition{ 69 Parent: resource, 70 Name: "alpha", 71 Schemes: []string{"http"}, 72 Description: "Alpha-like things", 73 } 74 resource.Actions[alpha.Name] = alpha 75 design.Design = &design.APIDefinition{ 76 Name: "whatever", 77 Title: "test API", 78 Description: "Ain't matter none", 79 Resources: map[string]*design.ResourceDefinition{ 80 "first": resource, 81 }, 82 } 83 }) 84 85 It("generates controllers ready for regeneration", func() { 86 Ω(genErr).Should(BeNil()) 87 Ω(files).Should(HaveLen(2)) 88 content, err := os.ReadFile(filepath.Join(outDir, "first.go")) 89 Ω(err).ShouldNot(HaveOccurred()) 90 Ω(content).Should(MatchRegexp("FirstController_Alpha: start_implement")) 91 Ω(content).Should(MatchRegexp(`// FirstController_Alpha: start_implement\s*// Put your logic here\s*return nil\s*// FirstController_Alpha: end_implement`)) 92 }) 93 94 Context("regenerated with a new resource", func() { 95 BeforeEach(func() { 96 // Perform a first generation 97 files, genErr = genmain.Generate() 98 99 // Put some impl in the existing controller 100 existing, err := os.ReadFile(filepath.Join(outDir, "first.go")) 101 Ω(err).ShouldNot(HaveOccurred()) 102 103 // First add an import for fmt, to make sure it remains 104 existing = bytes.Replace(existing, []byte("import ("), []byte("import (\n\t\"fmt\")"), 1) 105 106 // Next add some body that uses fmt 107 existing = bytes.Replace(existing, []byte("// Put your logic here"), []byte("fmt.Println(\"I did it first\")"), 1) 108 109 err = os.WriteFile(filepath.Join(outDir, "first.go"), existing, os.ModePerm) 110 Ω(err).ShouldNot(HaveOccurred()) 111 112 // Add an action to the existing resource 113 beta := &design.ActionDefinition{ 114 Parent: resource, 115 Name: "beta", 116 Schemes: []string{"http"}, 117 Description: "Beta-like things", 118 } 119 resource.Actions[beta.Name] = beta 120 121 // Add a new resource 122 resource2 := &design.ResourceDefinition{ 123 Name: "second", 124 Description: "second stuff", 125 Actions: map[string]*design.ActionDefinition{}, 126 } 127 gamma := &design.ActionDefinition{ 128 Parent: resource2, 129 Name: "gamma", 130 Schemes: []string{"http"}, 131 Description: "Gamma-like things", 132 } 133 resource2.Actions[gamma.Name] = gamma 134 135 design.Design.Resources[resource2.Name] = resource2 136 137 // Set up the regeneration for the JustBeforeEach 138 os.Args = append(os.Args, "--regen") 139 }) 140 141 It("generates scaffolding for new and existing resources", func() { 142 Ω(genErr).Should(BeNil()) 143 Ω(files).Should(HaveLen(2)) 144 Ω(files).Should(ConsistOf(filepath.Join(outDir, "first.go"), filepath.Join(outDir, "second.go"))) 145 146 content, err := os.ReadFile(filepath.Join(outDir, "second.go")) 147 Ω(err).ShouldNot(HaveOccurred()) 148 Ω(content).Should(ContainSubstring("SecondController_Gamma: start_implement")) 149 150 }) 151 152 It("regenerates controllers without modifying existing impls", func() { 153 content, err := os.ReadFile(filepath.Join(outDir, "first.go")) 154 Ω(err).ShouldNot(HaveOccurred()) 155 156 // First make sure the new controller is in place 157 Ω(content).Should(ContainSubstring("FirstController_Beta: start_implement")) 158 159 // Check the fmt import 160 Ω(string(content)).Should(MatchRegexp(`import \(\s*[^)]*\"fmt\"`)) 161 162 // Check the body is in place 163 Ω(content).Should(MatchRegexp(`// FirstController_Alpha: start_implement\s*fmt.Println\("I did it first"\)\s*return nil\s*// FirstController_Alpha: end_implement`)) 164 }) 165 }) 166 167 }) 168 }) 169 170 var _ = Describe("NewGenerator", func() { 171 var generator *genmain.Generator 172 173 var args = struct { 174 api *design.APIDefinition 175 outDir string 176 designPkg string 177 target string 178 force bool 179 regen bool 180 noExample bool 181 }{ 182 api: &design.APIDefinition{ 183 Name: "test api", 184 }, 185 outDir: "out_dir", 186 designPkg: "design", 187 target: "app", 188 force: false, 189 regen: false, 190 } 191 192 Context("with options all options set", func() { 193 BeforeEach(func() { 194 195 generator = genmain.NewGenerator( 196 genmain.API(args.api), 197 genmain.OutDir(args.outDir), 198 genmain.DesignPkg(args.designPkg), 199 genmain.Target(args.target), 200 genmain.Force(args.force), 201 genmain.Regen(args.regen), 202 ) 203 }) 204 205 It("has all public properties set with expected value", func() { 206 Ω(generator).ShouldNot(BeNil()) 207 Ω(generator.API.Name).Should(Equal(args.api.Name)) 208 Ω(generator.OutDir).Should(Equal(args.outDir)) 209 Ω(generator.DesignPkg).Should(Equal(args.designPkg)) 210 Ω(generator.Target).Should(Equal(args.target)) 211 Ω(generator.Force).Should(Equal(args.force)) 212 Ω(generator.Regen).Should(Equal(args.regen)) 213 }) 214 215 }) 216 })