Skip to content

Commit

Permalink
feat(cli): yomo init with test file (#680)
Browse files Browse the repository at this point in the history
# Description

`yomo init` command now generates `app_test.go` template for testing
`app.go`.
  • Loading branch information
woorui authored Nov 27, 2023
1 parent cada47f commit 6d8b913
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (

// defaultSFNFile is the default serverless file name
const (
defaultSFNSourceFile = "app.go"
defaultSFNCompliedFile = "sfn.wasm"
defaultSFNSourceFile = "app.go"
defaultSFNTestSourceFile = "app_test.go"
defaultSFNCompliedFile = "sfn.wasm"
)

// GetRootPath get root path
Expand Down
7 changes: 7 additions & 0 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ var initCmd = &cobra.Command{
return
}

// create app_test.go
testName := filepath.Join(name, defaultSFNTestSourceFile)
if err := file.PutContents(testName, golang.InitTestTmpl); err != nil {
log.FailureStatusEvent(os.Stdout, "Write unittest tmpl into app_test.go file failure with the error: %v", err)
return
}

// create .env
fname = filepath.Join(name, ".env")
if err := file.PutContents(fname, []byte(fmt.Sprintf("YOMO_SFN_NAME=%s\n", name))); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cli/serverless/golang/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var PartialsTmpl []byte
//go:embed templates/init.tmpl
var InitTmpl []byte

//go:embed templates/init_test.tmpl
var InitTestTmpl []byte

//go:embed templates/init_rx.tmpl
var InitRxTmpl []byte

Expand Down
36 changes: 36 additions & 0 deletions cli/serverless/golang/templates/init_test.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"reflect"
"testing"

"github.com/yomorun/yomo/serverless/mock"
)

func TestHandler(t *testing.T) {
tests := []struct {
name string
ctx *mock.MockContext
// want is the expected data and tag that be written by ctx.Write
want []mock.DataAndTag
}{
{
name: "upper",
ctx: mock.NewMockContext([]byte("hello"), 0x33),
want: []mock.DataAndTag{
{Data: []byte("HELLO"), Tag: 0x34},
},
},
// TODO: add more test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Handler(tt.ctx)
got := tt.ctx.RecordWritten()

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TestHandler got: %v, want: %v", got, tt.want)
}
})
}
}
62 changes: 62 additions & 0 deletions serverless/mock/mock_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package mock

import (
"sync"

"github.com/yomorun/yomo/serverless"
"github.com/yomorun/yomo/serverless/guest"
)

// DataAndTag is a pair of data and tag.
type DataAndTag struct {
Data []byte
Tag uint32
}

// MockContext mock context.
type MockContext struct {
data []byte
tag uint32

mu sync.Mutex
wrSlice []DataAndTag
}

// NewMockContext returns the mock context.
// the data is that returned by ctx.Data(), the tag is that returned by ctx.Tag().
func NewMockContext(data []byte, tag uint32) *MockContext {
return &MockContext{
data: data,
tag: tag,
}
}

func (c *MockContext) Data() []byte {
return c.data
}
func (c *MockContext) Tag() uint32 {
return c.tag
}
func (m *MockContext) HTTP() serverless.HTTP {
return &guest.GuestHTTP{}
}

func (c *MockContext) Write(tag uint32, data []byte) error {
c.mu.Lock()
defer c.mu.Unlock()

c.wrSlice = append(c.wrSlice, DataAndTag{
Data: data,
Tag: tag,
})

return nil
}

// RecordWritten returns the data records be written with `ctx.Write`.
func (c *MockContext) RecordWritten() []DataAndTag {
c.mu.Lock()
defer c.mu.Unlock()

return c.wrSlice
}

1 comment on commit 6d8b913

@vercel
Copy link

@vercel vercel bot commented on 6d8b913 Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

yomo – ./

yomo.run
yomo-git-master-yomorun.vercel.app
yomo.vercel.app
www.yomo.run
yomo-yomorun.vercel.app

Please sign in to comment.