diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f43336d8..7005ee85 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,7 +25,10 @@ jobs: run: go build -v ./cmd/... - name: Test - run: go run ./script/run-test -v + run: go run ./script/run-test -v -cover -coverpkg github.com/xhd2015/xgo/runtime/... -coverprofile covers/cover.out + + - name: Print coverage + run: cd runtime && go tool cover --func ../covers/cover-runtime.out - name: Build Release run: go run ./script/build-release diff --git a/.gitignore b/.gitignore index 583e9b51..309e19c8 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,8 @@ # generated traces /runtime/test/**/*.json -/test/**/*.json \ No newline at end of file +/test/**/*.json + +# coverage +/covers +cover*.out \ No newline at end of file diff --git a/README.md b/README.md index 252fe9df..6bc63215 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ + # xgo +[![Go Reference](https://pkg.go.dev/badge/github.com/xhd2015/xgo.svg)](https://pkg.go.dev/github.com/xhd2015/xgo) +[![Go Report Card](https://goreportcard.com/badge/github.com/xhd2015/xgo)](https://goreportcard.com/report/github.com/xhd2015/xgo) +[![Go Coverage](https://img.shields.io/badge/Coverage-71.9%25-brightgreen)](https://github.com/xhd2015/xgo/actions) +[![CI](https://github.com/xhd2015/xgo/workflows/Go/badge.svg)](https://github.com/xhd2015/xgo/actions) **English | [简体中文](./README_zh_cn.md)** diff --git a/cmd/xgo/debug.go b/cmd/xgo/debug.go index ffc91e98..7e55d094 100644 --- a/cmd/xgo/debug.go +++ b/cmd/xgo/debug.go @@ -19,13 +19,12 @@ func getVscodeDebugFile(tmpDir string, vscode string) (vscodeDebugFile string, s } else if vscode == "stdout" || strings.HasPrefix(vscode, stdoutParamPrefix) { vscodeDebugFile = filepath.Join(tmpDir, "vscode_launch.json") } else { - f, err := os.Stat(vscode) - if err != nil { - if !errors.Is(err, os.ErrNotExist) { - return "", "", fmt.Errorf("check vscode debug flag: %w", err) + f, statErr := os.Stat(vscode) + if statErr != nil { + if !errors.Is(statErr, os.ErrNotExist) { + return "", "", fmt.Errorf("check vscode debug flag: %w", statErr) } - err = nil // treat as file } if f != nil && f.IsDir() { diff --git a/cmd/xgo/patch.go b/cmd/xgo/patch.go index 2f8ab7b9..e71505b9 100644 --- a/cmd/xgo/patch.go +++ b/cmd/xgo/patch.go @@ -29,7 +29,7 @@ func patchRuntimeAndCompiler(origGoroot string, goroot string, xgoSrc string, go return fmt.Errorf("requires goroot") } if isDevelopment && xgoSrc == "" { - return fmt.Errorf("requries xgoSrc") + return fmt.Errorf("requires xgoSrc") } if !isDevelopment && !revisionChanged { return nil @@ -892,12 +892,11 @@ func syncGoroot(goroot string, dstDir string, forceCopy bool) error { return fmt.Errorf("bad goroot: %s", goroot) } - dstFile, err := os.Stat(dstGoBin) - if err != nil { - if !os.IsNotExist(err) { - return err + dstFile, statErr := os.Stat(dstGoBin) + if statErr != nil { + if !os.IsNotExist(statErr) { + return statErr } - err = nil } if dstFile != nil && !dstFile.IsDir() && dstFile.Size() == srcFile.Size() { @@ -965,7 +964,7 @@ func buildInstrumentTool(goroot string, xgoSrc string, compilerBin string, compi // but if that is not found, we can fallback to ~/.xgo/bin/exec_tool // because exec_tool changes rarely, so it is safe to use // an older version. -// we may add version to check if exec_tool is compitable +// we may add version to check if exec_tool is compatible func findBuiltExecTool() (string, error) { dirName := filepath.Dir(os.Args[0]) absDirName, err := filepath.Abs(dirName) @@ -1008,12 +1007,11 @@ func buildCompiler(goroot string, output string) error { } func compareAndUpdateCompilerID(compilerFile string, compilerIDFile string) (changed bool, err error) { - prevData, err := ioutil.ReadFile(compilerIDFile) - if err != nil { - if !errors.Is(err, os.ErrNotExist) { - return false, err + prevData, statErr := ioutil.ReadFile(compilerIDFile) + if statErr != nil { + if !errors.Is(statErr, os.ErrNotExist) { + return false, statErr } - err = nil } prevID := string(prevData) curID, err := getBuildID(compilerFile) diff --git a/cmd/xgo/patch/runtime_def_gen.go b/cmd/xgo/patch/runtime_def_gen.go index 05e8f09d..3cf4cc54 100644 --- a/cmd/xgo/patch/runtime_def_gen.go +++ b/cmd/xgo/patch/runtime_def_gen.go @@ -18,4 +18,4 @@ func __xgo_on_test_start(fn interface{}) func __xgo_get_test_starts() []interface{} func __xgo_peek_panic() interface{} func __xgo_mem_equal(a, b unsafe.Pointer, size uintptr) bool -func __xgo_get_pc_name(pc uintptr) string` +func __xgo_get_pc_name(pc uintptr) string` \ No newline at end of file diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 28249b46..ea982900 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.11" -const REVISION = "e0f9e969d9f62daa515c94bbde128f91a029b38e+1" -const NUMBER = 131 +const REVISION = "6d76bd0ec32e7b161f19ac216fc42af3eccecadd+1" +const NUMBER = 132 func getRevision() string { return fmt.Sprintf("%s %s BUILD_%d", VERSION, REVISION, NUMBER) diff --git a/runtime/core/version.go b/runtime/core/version.go index 0a5d1a3f..ea708ade 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.11" -const REVISION = "e0f9e969d9f62daa515c94bbde128f91a029b38e+1" -const NUMBER = 131 +const REVISION = "6d76bd0ec32e7b161f19ac216fc42af3eccecadd+1" +const NUMBER = 132 // these fields will be filled by compiler const XGO_VERSION = "" @@ -40,7 +40,7 @@ func checkVersion() error { return errors.New("newer xgo available, consider run 'xgo upgrade'") } // only one case is feasible: XGO_NUMBER >= runtime NUMBER - // because xgo can be compitable with older sdk + // because xgo can be compatible with older sdk return nil } var updateCmd string @@ -49,5 +49,5 @@ func checkVersion() error { } else { updateCmd = "go get github.com/xhd2015/xgo/runtime@latest" } - return fmt.Errorf("xgo v%s maybe incompitable with xgo/runtime v%s, consider run '%s'", XGO_VERSION, VERSION, updateCmd) + return fmt.Errorf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run '%s'", XGO_VERSION, VERSION, updateCmd) } diff --git a/script/run-test/main.go b/script/run-test/main.go index 5ec2fd62..5473508d 100644 --- a/script/run-test/main.go +++ b/script/run-test/main.go @@ -56,6 +56,10 @@ func main() { var xgoTestOnly bool var xgoRuntimeTestOnly bool var xgoDefaultTestOnly bool + + var cover bool + var coverpkgs []string + var coverprofile string for i := 0; i < n; i++ { arg := args[i] if arg == "--exclude" { @@ -103,6 +107,20 @@ func main() { xgoDefaultTestOnly = true continue } + if arg == "-cover" { + cover = true + continue + } + if arg == "-coverpkg" { + coverpkgs = append(coverpkgs, args[i+1]) + i++ + continue + } + if arg == "-coverprofile" { + coverprofile = args[i+1] + i++ + continue + } if arg == "--" { if i+1 < n { remainArgs = append(remainArgs, args[i+1:]...) @@ -116,6 +134,14 @@ func main() { fmt.Fprintf(os.Stderr, "unknown flag: %s\n", arg) os.Exit(1) } + if coverprofile != "" { + absProfile, err := filepath.Abs(coverprofile) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + coverprofile = absProfile + } goRelease := "go-release" var goroots []string _, err := os.Stat(goRelease) @@ -199,8 +225,28 @@ func main() { runXgoTestFlag = false runDefault = true } + addArgs := func(args []string, variant string) []string { + if cover { + args = append(args, "-cover") + } + for _, coverPkg := range coverpkgs { + args = append(args, "-coverpkg", coverPkg) + } + if coverprofile != "" { + var prefix string + var suffix string + idx := strings.LastIndex(coverprofile, ".") + if idx < 0 { + prefix = coverprofile + } else { + prefix, suffix = coverprofile[:idx], coverprofile[idx:] + } + args = append(args, "-coverprofile", prefix+"-"+variant+suffix) + } + return args + } if runXgoTestFlag { - err := runXgoTest(goroot, remainArgs, remainTests) + err := runXgoTest(goroot, addArgs(remainArgs, "xgo"), remainTests) if err != nil { fmt.Fprintf(os.Stdout, "FAIL %s: %v(%v)\n", goroot, err, time.Since(begin)) os.Exit(1) @@ -208,14 +254,14 @@ func main() { } if runRuntimeTestFlag { - err := runRuntimeTest(goroot, remainArgs, remainTests) + err := runRuntimeTest(goroot, addArgs(remainArgs, "runtime"), remainTests) if err != nil { fmt.Fprintf(os.Stdout, "FAIL %s: %v(%v)\n", goroot, err, time.Since(begin)) os.Exit(1) } } if runDefault { - err := runDefaultTest(goroot, remainArgs, remainTests) + err := runDefaultTest(goroot, addArgs(remainArgs, "default"), remainTests) if err != nil { fmt.Fprintf(os.Stdout, "FAIL %s: %v(%v)\n", goroot, err, time.Since(begin)) os.Exit(1) diff --git a/test/func_list_test.go b/test/func_list_test.go index 1e3c4052..d1e6e38f 100644 --- a/test/func_list_test.go +++ b/test/func_list_test.go @@ -34,7 +34,7 @@ func TestFuncList(t *testing.T) { expectMainLines := []string{ "func:main example", "func:main someInt.value", - // "func:main (*someInt).inc", // this output is replaced with a simplier display name + // "func:main (*someInt).inc", // this output is replaced with a simpler display name "func:main someInt.inc", }