Skip to content

Commit

Permalink
Merge pull request #3 from kanopy-platform/bugfix_convert_router_output
Browse files Browse the repository at this point in the history
BUGFIX: Fix default convert router output
  • Loading branch information
colinhoglund authored Jun 22, 2022
2 parents 29b27b0 + 6b87d43 commit 8aa48d7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 65 deletions.
41 changes: 0 additions & 41 deletions internal/plugin/newline.go

This file was deleted.

16 changes: 4 additions & 12 deletions internal/plugin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ func WithConvertPlugins(plugins ...converter.Plugin) RouterOption {
}
}

func NewDefaultRouter() *Router {
return NewRouter(
WithConvertPlugins(
NewAddNewline(),
),
)
}

func NewRouter(opts ...RouterOption) *Router {
router := &Router{}

Expand All @@ -37,16 +29,16 @@ func NewRouter(opts ...RouterOption) *Router {
return router
}

func (r *Router) Convert(ctx context.Context, req *converter.Request) (conf *drone.Config, err error) {
func (r *Router) Convert(ctx context.Context, req *converter.Request) (*drone.Config, error) {
for _, plugin := range r.convertPlugins {
conf, err = plugin.Convert(ctx, req)
out, err := plugin.Convert(ctx, req)
if err != nil {
return nil, err
}

// modify the request object before it gets passed to the next plugin
req.Config = *conf
req.Config = *out
}

return
return &req.Config, nil
}
48 changes: 38 additions & 10 deletions internal/plugin/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,48 @@ import (
"context"
"testing"

"github.com/drone/drone-go/drone"
"github.com/drone/drone-go/plugin/converter"
"github.com/stretchr/testify/assert"
)

func TestRouter(t *testing.T) {
type addNewline struct{}

func newAddNewline() *addNewline {
return &addNewline{}
}

func (p *addNewline) Convert(ctx context.Context, req *converter.Request) (*drone.Config, error) {
return &drone.Config{Data: req.Config.Data + "\n"}, nil
}

func TestConvertRouter(t *testing.T) {
t.Parallel()

r := NewRouter(
WithConvertPlugins(
NewAddNewline(),
NewAddNewline(),
),
)
conf, err := r.Convert(context.Background(), &converter.Request{})
assert.NoError(t, err)
assert.Equal(t, "\n\n", conf.Data)
tests := []struct {
description string
router *Router
input string
want string
}{
{
description: "test without convert plugins",
router: NewRouter(),
input: "name: default",
want: "name: default",
},
{
description: "test with multiple convert plugins",
router: NewRouter(WithConvertPlugins(newAddNewline(), newAddNewline())),
input: "name: default",
want: "name: default\n\n",
},
}

for _, test := range tests {
req := &converter.Request{Config: drone.Config{Data: test.input}}
conf, err := test.router.Convert(context.Background(), req)
assert.NoError(t, err)
assert.Equal(t, test.want, conf.Data, test.description)
}
}
2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func New(secret string, opts ...ServerOptFunc) http.Handler {
s := &Server{
router: http.NewServeMux(),
secret: secret,
pluginRouter: plugin.NewDefaultRouter(),
pluginRouter: plugin.NewRouter(),
}

for _, opt := range opts {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestHandleRoot(t *testing.T) {
req := newSignedRequest(t, converter.V1, httptest.NewRequest("GET", "/", strings.NewReader("{}")))
testHandler.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, `{"data":"\n","kind":""}`, w.Body.String())
assert.Equal(t, `{"data":"","kind":""}`, w.Body.String())
}

func TestHandleHealthz(t *testing.T) {
Expand Down

0 comments on commit 8aa48d7

Please sign in to comment.