cmd/land: add test coverage
This commit is contained in:
parent
a9cd43396c
commit
6aa9a6d149
|
@ -1 +1,3 @@
|
|||
.gitignore
|
||||
|
||||
: *_test.go |> go test -v -cover -race -o %o |> ./bin/%d.test
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
include_rules
|
||||
|
||||
: *.go |> go build -o %o |> ../../bin/land
|
||||
|
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Xe/ln"
|
||||
|
@ -29,24 +30,7 @@ func main() {
|
|||
ln.FatalErr(ctx, err, ln.F{"msg": "can't create process", "fname": fname})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
returnCode, err := p.Main()
|
||||
|
||||
if !foundMain {
|
||||
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})
|
||||
ln.Log(ctx, ln.Info("process returned code"), ln.F{"code": returnCode, "type": fmt.Sprintf("%T", returnCode)})
|
||||
}
|
||||
|
|
|
@ -114,3 +114,26 @@ func (p *Process) log(ptr int32, len int32) int32 {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
!*.wasm
|
Binary file not shown.
Loading…
Reference in New Issue