Skip to content

Commit

Permalink
add integration test for generate command, closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Aug 31, 2021
1 parent 8d8c25b commit 7ff3e7f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
51 changes: 51 additions & 0 deletions internal/cli/commands/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/mantil-io/mantil/internal/cli/commands/deploy"
"github.com/mantil-io/mantil/internal/cli/generate"
Expand All @@ -32,6 +33,9 @@ func Api(name string, methods []string) error {
if err := generateFunctionGitignore(name, projectPath); err != nil {
return err
}
if err := generateFunctionTest(importPath, projectPath, name, methods); err != nil {
return err
}
return generateApi(projectPath, name, methods)
}

Expand Down Expand Up @@ -131,6 +135,53 @@ func generateApiMethods(projectPath, functionName string, methods []string) erro
return nil
}

func generateFunctionTest(importPath, projectPath, functionName string, methods []string) error {
if err := generateApiTestInit(projectPath); err != nil {
return err
}
if err := generateApiTest(importPath, projectPath, functionName, methods); err != nil {
return err
}
return nil
}

func generateApiTestInit(projectPath string) error {
initTest := filepath.Join(projectPath, "test", "init.go")
if fileExists(initTest) {
log.Info("%s already exists", relativePath(projectPath, initTest))
return nil
}
log.Info("generating %s", relativePath(projectPath, initTest))
if err := generate.GenerateFile(
generate.APIFunctionTestInit,
initTest,
); err != nil {
return err
}
return nil
}

func generateApiTest(importPath, projectPath, functionName string, methods []string) error {
apiTest := filepath.Join(projectPath, "test", fmt.Sprintf("%s_test.go", strings.ToLower(functionName)))
if fileExists(apiTest) {
log.Info("%s already exists", relativePath(projectPath, apiTest))
return nil
}
log.Info("generating %s", relativePath(projectPath, apiTest))
if err := generate.GenerateFromTemplate(
generate.APIFunctionTestTemplate,
&generate.Test{
Name: functionName,
ImportPath: importPath,
Methods: methods,
},
apiTest,
); err != nil {
return err
}
return nil
}

func fileExists(path string) bool {
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
Expand Down
71 changes: 71 additions & 0 deletions internal/cli/generate/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type Method struct {
FunctionName string
}

type Test struct {
Name string
ImportPath string
Methods []string
}

var APIDefaultTemplate = `
package {{ .Name | toLower }}
Expand Down Expand Up @@ -60,3 +66,68 @@ func main() {
mantil.LambdaHandler(api)
}
`

var APIFunctionTestInit = `
package main
import (
"log"
"os"
"os/exec"
)
var apiURL string
func init() {
if val, ok := os.LookupEnv("MANTIL_API_URL"); ok {
apiURL = val
return
}
out, err := exec.Command("mantil", "env", "-u").Output()
if err != nil {
log.Fatalf("can't find api url, execute of mantil env -u failed %v", err)
}
apiURL = string(out)
}
`

var APIFunctionTestTemplate = `
package main
import (
"net/http"
"testing"
"github.com/gavv/httpexpect"
"{{ .ImportPath }}/api/{{ .Name | toLower }}"
)
func Test{{ .Name | toLower | title }}Default(t *testing.T) {
api := httpexpect.New(t, apiURL)
req := {{ .Name | toLower }}.DefaultRequest {
// TODO add attributes
}
api.POST("/{{ .Name | toLower }}").
WithJSON(req).
Expect().
ContentType("application/json").
Status(http.StatusOK).
JSON().Object().
Value("TODO")
{{ range $method := .Methods }}
{{ $method | toLower }}Req := {{ $.Name | toLower }}.{{ $method | toLower | title }}Request {
// TODO add attributes
}
api.POST("/{{ $.Name | toLower }}/{{ $method | toLower }}").
WithJSON({{ $method | toLower }}Req).
Expect().
ContentType("application/json").
Status(http.StatusOK).
JSON().Object().
Value("TODO")
{{ end }}
}
`
14 changes: 11 additions & 3 deletions internal/cli/generate/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ func GenerateFromTemplate(tplDef string, data interface{}, outPath string) error
if err != nil {
return err
}
out, err = format(out)
out, err = format(string(out))
if err != nil {
return err
}
return save(out, outPath)
}

func GenerateFile(content string, outPath string) error {
out, err := format(content)
if err != nil {
return err
}
Expand Down Expand Up @@ -44,9 +52,9 @@ func first(s string) string {
return string(s[0])
}

func format(in []byte) ([]byte, error) {
func format(in string) ([]byte, error) {
cmd := exec.Command("gofmt")
cmd.Stdin = strings.NewReader(string(in))
cmd.Stdin = strings.NewReader(in)
out, err := cmd.Output()
if err != nil {
return nil, err
Expand Down

0 comments on commit 7ff3e7f

Please sign in to comment.