cmd/land: add test coverage

This commit is contained in:
Cadey Ratio 2018-06-07 19:02:12 -07:00
parent a9cd43396c
commit 6aa9a6d149
7 changed files with 59 additions and 19 deletions

View File

@ -1 +1,3 @@
.gitignore .gitignore
: *_test.go |> go test -v -cover -race -o %o |> ./bin/%d.test

View File

@ -1 +1,3 @@
include_rules
: *.go |> go build -o %o |> ../../bin/land : *.go |> go build -o %o |> ../../bin/land

View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"flag" "flag"
"fmt"
"os" "os"
"github.com/Xe/ln" "github.com/Xe/ln"
@ -29,24 +30,7 @@ func main() {
ln.FatalErr(ctx, err, ln.F{"msg": "can't create process", "fname": fname}) ln.FatalErr(ctx, err, ln.F{"msg": "can't create process", "fname": fname})
} }
foundMain := false returnCode, err := p.Main()
mainID := uint32(0)
for name, entry := range p.mod.Export.Entries {
if name == "main" && entry.FieldStr == "main" {
mainID = entry.Index
foundMain = true
break
}
}
if !foundMain { ln.Log(ctx, ln.Info("process returned code"), ln.F{"code": returnCode, "type": fmt.Sprintf("%T", returnCode)})
ln.Fatal(ctx, ln.F{"msg": "no main function exported"})
}
returnCode, err := p.vm.ExecCode(int64(mainID))
if err != nil {
ln.FatalErr(ctx, err)
}
ln.Log(ctx, ln.Info("process returned code"), ln.F{"code": returnCode})
} }

View File

@ -114,3 +114,26 @@ func (p *Process) log(ptr int32, len int32) int32 {
return 0 return 0
} }
func (p *Process) Main() (uint32, error) {
foundMain := false
mainID := uint32(0)
for name, entry := range p.mod.Export.Entries {
if name == "main" && entry.FieldStr == "main" {
mainID = entry.Index
foundMain = true
break
}
}
if !foundMain {
return 1, errors.New("no main function found")
}
returnCode, err := p.vm.ExecCode(int64(mainID))
if err != nil {
return 1, err
}
return returnCode.(uint32), nil
}

28
cmd/land/process_test.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"os"
"testing"
)
func TestHelloWorld(t *testing.T) {
fin, err := os.Open("./testdata/hello.wasm")
if err != nil {
t.Fatal(err)
}
defer fin.Close()
p, err := NewProcess(fin)
if err != nil {
t.Fatal(err)
}
ret, err := p.Main()
if err != nil {
t.Fatal(err)
}
if ret != 0 {
t.Fatalf("expected return code to be 0, got: %d", ret)
}
}

1
cmd/land/testdata/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
!*.wasm

BIN
cmd/land/testdata/hello.wasm vendored Normal file

Binary file not shown.