Add middleware tests and add additional comments
This commit is contained in:
Родитель
5493c9684f
Коммит
e1b335e09f
|
@ -73,10 +73,6 @@ func generateResponseFile(data *gengokit.Data, prevFile io.Reader, templFP strin
|
|||
if genCode, err = h.Render(templFP, data); err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot render template: %s", templFP)
|
||||
}
|
||||
default:
|
||||
if genCode, err = applyTemplateFromPath(templFP, data); err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot render template: %s", templFP)
|
||||
}
|
||||
case middlewares.EndpointsPath:
|
||||
m := middlewares.New()
|
||||
m.LoadEndpoints(prevFile)
|
||||
|
@ -89,6 +85,10 @@ func generateResponseFile(data *gengokit.Data, prevFile io.Reader, templFP strin
|
|||
if genCode, err = m.Render(templFP, data); err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot render template: %s", templFP)
|
||||
}
|
||||
default:
|
||||
if genCode, err = applyTemplateFromPath(templFP, data); err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot render template: %s", templFP)
|
||||
}
|
||||
}
|
||||
|
||||
codeBytes, err := ioutil.ReadAll(genCode)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Package middlewares renders the service and endpointe middleware files in NAME-service/middlewares/.
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
|
@ -9,28 +10,42 @@ import (
|
|||
"github.com/TuneLab/go-truss/gengokit/middlewares/templates"
|
||||
)
|
||||
|
||||
// EndpointsPath is the path to the Endpoints middleware file that package middlewares renders
|
||||
const EndpointsPath = "NAME-service/middlewares/endpoints.gotemplate"
|
||||
|
||||
// ServicePath is the path to the Service middleware file that package middlewares renders
|
||||
const ServicePath = "NAME-service/middlewares/service.gotemplate"
|
||||
|
||||
// New returns a Middleware which can render EndpointsFile and ServiceFile as
|
||||
// well as read in previous versions of each respective file
|
||||
func New() *Middlewares {
|
||||
var m Middlewares
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
// Middlewares satisfies the gengokit.Renderable interface to render
|
||||
// middlewares, it has methods to load previous versions of the middlewares in
|
||||
// to update them.
|
||||
type Middlewares struct {
|
||||
// contains unexported fields
|
||||
prevEndpoints io.Reader
|
||||
prevService io.Reader
|
||||
}
|
||||
|
||||
// LoadEndpoints loads a previous version of EndpointsFile
|
||||
func (m *Middlewares) LoadEndpoints(prev io.Reader) {
|
||||
m.prevEndpoints = prev
|
||||
}
|
||||
|
||||
// LoadService loads a previous version of ServiceFile
|
||||
func (m *Middlewares) LoadService(prev io.Reader) {
|
||||
m.prevService = prev
|
||||
}
|
||||
|
||||
// Render can render either EndpointsPath or ServicePath. With no previous
|
||||
// version it renders the templates, if there was a previous version loaded in,
|
||||
// it passes that through
|
||||
func (m *Middlewares) Render(alias string, data *gengokit.Data) (io.Reader, error) {
|
||||
switch alias {
|
||||
case EndpointsPath:
|
||||
|
|
|
@ -0,0 +1,236 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/TuneLab/go-truss/gengokit"
|
||||
thelper "github.com/TuneLab/go-truss/gengokit/gentesthelper"
|
||||
"github.com/TuneLab/go-truss/svcdef"
|
||||
)
|
||||
|
||||
var gopath []string
|
||||
|
||||
func init() {
|
||||
gopath = filepath.SplitList(os.Getenv("GOPATH"))
|
||||
}
|
||||
|
||||
func TestNewServiceMiddleware(t *testing.T) {
|
||||
var wantService = `
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
pb "github.com/TuneLab/go-truss/gengokit/general-service"
|
||||
)
|
||||
|
||||
func InjectServiceMiddlewares(in pb.ProtoServiceServer) pb.ProtoServiceServer {
|
||||
return in
|
||||
}
|
||||
`
|
||||
|
||||
_, data, err := generalService()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
middleware := New()
|
||||
service, err := middleware.Render(ServicePath, data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
serviceBytes, err := ioutil.ReadAll(service)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
wantFormatted, serviceFormatted, diff := thelper.DiffGoCode(wantService, string(serviceBytes))
|
||||
if wantFormatted != serviceFormatted {
|
||||
t.Fatalf("Serivce middleware different than expected:\n\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEndpointMiddleware(t *testing.T) {
|
||||
var wantEndpoints = `
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
)
|
||||
|
||||
func InjectEndpointMiddlewares(in endpoint.Endpoint) endpoint.Endpoint {
|
||||
return in
|
||||
}
|
||||
`
|
||||
|
||||
_, data, err := generalService()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
middleware := New()
|
||||
endpoints, err := middleware.Render(EndpointsPath, data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
endpointsBytes, err := ioutil.ReadAll(endpoints)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
wantFormatted, endpointFormatted, diff := thelper.DiffGoCode(wantEndpoints, string(endpointsBytes))
|
||||
if wantFormatted != endpointFormatted {
|
||||
t.Fatalf("Endpoints middleware different than expected:\n\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderPrevService(t *testing.T) {
|
||||
var wantService = `
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
pb "github.com/TuneLab/go-truss/gengokit/general-service"
|
||||
)
|
||||
|
||||
func InjectServiceMiddlewares(in pb.ProtoServiceServer) pb.ProtoServiceServer {
|
||||
return in
|
||||
}
|
||||
|
||||
func FooBar() error {
|
||||
return nil
|
||||
}
|
||||
`
|
||||
_, data, err := generalService()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
middleware := New()
|
||||
|
||||
middleware.LoadService(strings.NewReader(wantService))
|
||||
|
||||
service, err := middleware.Render(ServicePath, data)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
serviceBytes, err := ioutil.ReadAll(service)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
wantFormatted, serviceFormatted, diff := thelper.DiffGoCode(wantService, string(serviceBytes))
|
||||
if wantFormatted != serviceFormatted {
|
||||
t.Fatalf("Sevice middleware modified unexpectedly:\n\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderPrevEndpoints(t *testing.T) {
|
||||
var wantEndpoints = `
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
)
|
||||
|
||||
func InjectEndpointMiddlewares(in endpoint.Endpoint) endpoint.Endpoint {
|
||||
return in
|
||||
}
|
||||
|
||||
func BarFoo(err error) bool {
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
`
|
||||
|
||||
_, data, err := generalService()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
middleware := New()
|
||||
|
||||
middleware.LoadEndpoints(strings.NewReader(wantEndpoints))
|
||||
|
||||
endpoints, err := middleware.Render(EndpointsPath, data)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
endpointsBytes, err := ioutil.ReadAll(endpoints)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
wantFormatted, endpointFormatted, diff := thelper.DiffGoCode(wantEndpoints, string(endpointsBytes))
|
||||
if wantFormatted != endpointFormatted {
|
||||
t.Fatalf("Endpoints middleware modified unexpectedly:\n\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUnknownFile(t *testing.T) {
|
||||
_, data, err := generalService()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
middleware := New()
|
||||
|
||||
_, err = middleware.Render("not/valid/file.go", data)
|
||||
if err == nil {
|
||||
t.Fatalf("Tried to render unknown file")
|
||||
}
|
||||
}
|
||||
|
||||
func generalService() (*svcdef.Svcdef, *gengokit.Data, error) {
|
||||
const def = `
|
||||
syntax = "proto3";
|
||||
|
||||
// General package
|
||||
package general;
|
||||
|
||||
import "google.golang.org/genproto/googleapis/api/serviceconfig/annotations.proto";
|
||||
|
||||
// RequestMessage is so foo
|
||||
message RequestMessage {
|
||||
string input = 1;
|
||||
}
|
||||
|
||||
// ResponseMessage is so bar
|
||||
message ResponseMessage {
|
||||
string output = 1;
|
||||
}
|
||||
|
||||
// ProtoService is a service
|
||||
service ProtoService {
|
||||
// ProtoMethod is simple. Like a gopher.
|
||||
rpc ProtoMethod (RequestMessage) returns (ResponseMessage) {
|
||||
// No {} in path and no body, everything is in the query
|
||||
option (google.api.http) = {
|
||||
get: "/route"
|
||||
};
|
||||
}
|
||||
}
|
||||
`
|
||||
sd, err := svcdef.NewFromString(def, gopath)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
conf := gengokit.Config{
|
||||
GoPackage: "github.com/TuneLab/go-truss/gengokit/general-service",
|
||||
PBPackage: "github.com/TuneLab/go-truss/gengokit/general-service",
|
||||
}
|
||||
|
||||
data, err := gengokit.NewData(sd, conf)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return sd, data, nil
|
||||
}
|
Загрузка…
Ссылка в новой задаче