github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/goagen/gen_main/generator_test.go (about)

     1  package genmain_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/goadesign/goa/design"
    10  	"github.com/goadesign/goa/goagen/gen_main"
    11  	"github.com/goadesign/goa/version"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("Generate", func() {
    18  	const testgenPackagePath = "github.com/goadesign/goa/goagen/gen_main/goatest"
    19  
    20  	var outDir string
    21  	var files []string
    22  	var genErr error
    23  
    24  	BeforeEach(func() {
    25  		gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
    26  		outDir = filepath.Join(gopath, "src", testgenPackagePath)
    27  		err := os.MkdirAll(outDir, 0777)
    28  		Ω(err).ShouldNot(HaveOccurred())
    29  		os.Args = []string{"goagen", "--out=" + outDir, "--design=foo", "--version=" + version.String()}
    30  	})
    31  
    32  	JustBeforeEach(func() {
    33  		files, genErr = genmain.Generate()
    34  	})
    35  
    36  	AfterEach(func() {
    37  		os.RemoveAll(outDir)
    38  	})
    39  
    40  	Context("with a dummy API", func() {
    41  		BeforeEach(func() {
    42  			design.Design = &design.APIDefinition{
    43  				Name:        "test api",
    44  				Title:       "dummy API with no resource",
    45  				Description: "I told you it's dummy",
    46  			}
    47  		})
    48  
    49  		It("generates a dummy app", func() {
    50  			Ω(genErr).Should(BeNil())
    51  			Ω(files).Should(HaveLen(1))
    52  			content, err := ioutil.ReadFile(filepath.Join(outDir, "main.go"))
    53  			Ω(err).ShouldNot(HaveOccurred())
    54  			Ω(len(strings.Split(string(content), "\n"))).Should(BeNumerically(">=", 16))
    55  			_, err = gexec.Build(testgenPackagePath)
    56  			Ω(err).ShouldNot(HaveOccurred())
    57  		})
    58  	})
    59  })
    60  
    61  var _ = Describe("NewGenerator", func() {
    62  	var generator *genmain.Generator
    63  
    64  	var args = struct {
    65  		api       *design.APIDefinition
    66  		outDir    string
    67  		designPkg string
    68  		target    string
    69  		force     bool
    70  		noExample bool
    71  	}{
    72  		api: &design.APIDefinition{
    73  			Name: "test api",
    74  		},
    75  		outDir:    "out_dir",
    76  		designPkg: "design",
    77  		target:    "app",
    78  		force:     false,
    79  	}
    80  
    81  	Context("with options all options set", func() {
    82  		BeforeEach(func() {
    83  
    84  			generator = genmain.NewGenerator(
    85  				genmain.API(args.api),
    86  				genmain.OutDir(args.outDir),
    87  				genmain.DesignPkg(args.designPkg),
    88  				genmain.Target(args.target),
    89  				genmain.Force(args.force),
    90  			)
    91  		})
    92  
    93  		It("has all public properties set with expected value", func() {
    94  			Ω(generator).ShouldNot(BeNil())
    95  			Ω(generator.API.Name).Should(Equal(args.api.Name))
    96  			Ω(generator.OutDir).Should(Equal(args.outDir))
    97  			Ω(generator.DesignPkg).Should(Equal(args.designPkg))
    98  			Ω(generator.Target).Should(Equal(args.target))
    99  			Ω(generator.Force).Should(Equal(args.force))
   100  		})
   101  
   102  	})
   103  })