|
|
|
@ -3,7 +3,6 @@ package gogen
|
|
|
|
|
import (
|
|
|
|
|
_ "embed"
|
|
|
|
|
goformat "go/format"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
@ -52,7 +51,7 @@ var (
|
|
|
|
|
|
|
|
|
|
func TestParser(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -73,7 +72,7 @@ func TestParser(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestMultiService(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(testMultiServiceTemplate), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(testMultiServiceTemplate), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -88,7 +87,7 @@ func TestMultiService(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestApiNoInfo(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(apiNoInfo), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(apiNoInfo), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -100,7 +99,7 @@ func TestApiNoInfo(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestInvalidApiFile(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(invalidApiFile), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(invalidApiFile), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -110,7 +109,7 @@ func TestInvalidApiFile(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestAnonymousAnnotation(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(anonymousAnnotation), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(anonymousAnnotation), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -125,7 +124,7 @@ func TestAnonymousAnnotation(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestApiHasMiddleware(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(apiHasMiddleware), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(apiHasMiddleware), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -137,7 +136,7 @@ func TestApiHasMiddleware(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestApiHasJwt(t *testing.T) {
|
|
|
|
|
filename := "jwt.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(apiJwt), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(apiJwt), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -149,7 +148,7 @@ func TestApiHasJwt(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestApiHasJwtAndMiddleware(t *testing.T) {
|
|
|
|
|
filename := "jwt.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(apiJwtWithMiddleware), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(apiJwtWithMiddleware), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -161,7 +160,7 @@ func TestApiHasJwtAndMiddleware(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestApiHasNoRequestBody(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(apiHasNoRequest), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(apiHasNoRequest), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -171,7 +170,7 @@ func TestApiHasNoRequestBody(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestApiRoutes(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(apiRouteTest), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(apiRouteTest), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -183,7 +182,7 @@ func TestApiRoutes(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestHasCommentRoutes(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(hasCommentApiTest), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(hasCommentApiTest), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -195,7 +194,7 @@ func TestHasCommentRoutes(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestInlineTypeNotExist(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(hasInlineNoExistTest), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(hasInlineNoExistTest), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -205,12 +204,12 @@ func TestInlineTypeNotExist(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestHasImportApi(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(hasImportApi), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(hasImportApi), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
|
importApiName := "importApi.api"
|
|
|
|
|
err = ioutil.WriteFile(importApiName, []byte(importApi), os.ModePerm)
|
|
|
|
|
err = os.WriteFile(importApiName, []byte(importApi), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(importApiName)
|
|
|
|
|
|
|
|
|
@ -231,7 +230,7 @@ func TestHasImportApi(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestNoStructApi(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(noStructTagApi), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(noStructTagApi), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -244,7 +243,7 @@ func TestNoStructApi(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestNestTypeApi(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(nestTypeApi), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(nestTypeApi), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -254,7 +253,7 @@ func TestNestTypeApi(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestCamelStyle(t *testing.T) {
|
|
|
|
|
filename := "greet.api"
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
|
|
|
|
err := os.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
|
@ -281,7 +280,7 @@ func validateWithCamel(t *testing.T, api, camel string) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if strings.HasSuffix(path, ".go") {
|
|
|
|
|
code, err := ioutil.ReadFile(path)
|
|
|
|
|
code, err := os.ReadFile(path)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Nil(t, validateCode(string(code)))
|
|
|
|
|
}
|
|
|
|
|