Skip to content

Commit

Permalink
add script/cover to merge coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 3, 2024
1 parent 7784829 commit 9b0f6cd
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ jobs:
- name: Test
run: go run ./script/run-test --reset-instrument --debug -v -cover -coverpkg github.com/xhd2015/xgo/runtime/... -coverprofile cover.out

- name: Merge Coverages
run: go run ./script/cover merge ./cover-runtime.out ./cover-runtime-sub.out -o cover-runtime-merged.out

- name: Print coverage
run: cd runtime && go tool cover --func ../cover-runtime.out
run: cd runtime && go tool cover --func ../cover-runtime-merged.out

- name: Build Release
run: go run ./script/build-release --include-install-src --include-local
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.17"
const REVISION = "d56f35be1d57233e6b58145879ebe0fedf976361+1"
const NUMBER = 155
const REVISION = "77848295e3d73a3eba8ce2bbd1b95d8c988929d5+1"
const NUMBER = 156

func getRevision() string {
return fmt.Sprintf("%s %s BUILD_%d", VERSION, REVISION, NUMBER)
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.17"
const REVISION = "d56f35be1d57233e6b58145879ebe0fedf976361+1"
const NUMBER = 155
const REVISION = "77848295e3d73a3eba8ce2bbd1b95d8c988929d5+1"
const NUMBER = 156

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
1 change: 0 additions & 1 deletion runtime/go.sum

This file was deleted.

9 changes: 9 additions & 0 deletions runtime/test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/xhd2015/xgo/runtime/test

go 1.18

require (
github.com/xhd2015/xgo/runtime v1.0.16
)

replace github.com/xhd2015/xgo/runtime => ../
176 changes: 176 additions & 0 deletions script/cover/cover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package main

import (
"fmt"
"io"
"os"
"strconv"
"strings"
)

func main() {
args := os.Args[1:]

if len(args) == 0 {
fmt.Fprintf(os.Stderr, "requires cmd\n")
os.Exit(1)
}
cmd := args[0]
args = args[1:]

var remainArgs []string
var outFile string
n := len(args)
for i := 0; i < n; i++ {
arg := args[i]
if arg == "-o" {
outFile = args[i+1]
i++
continue
}
if !strings.HasPrefix(arg, "-") {
remainArgs = append(remainArgs, arg)
continue
}
fmt.Fprintf(os.Stderr, "unrecognized flag: %s\n", arg)
os.Exit(1)
}

switch cmd {
case "merge":
if len(remainArgs) == 0 {
fmt.Fprintf(os.Stderr, "requires files\n")
os.Exit(1)
}
err := mergeCover(remainArgs, outFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
default:
fmt.Fprintf(os.Stderr, "unrecognized cmd: %s\n", cmd)
os.Exit(1)
}
}

func mergeCover(files []string, outFile string) error {
covs := make([][]*covLine, 0, len(files))
for _, file := range files {
content, err := os.ReadFile(file)
if err != nil {
return err
}
covs = append(covs, parseCover(string(content)))
}
res := merge(covs)
res = filter(res, func(line *covLine) bool {
if strings.HasPrefix(line.prefix, "github.com/xhd2015/xgo/runtime/test") {
return false
}
return true
})

mergedCov := formatCoverage("set", res)

var out io.Writer = os.Stdout
if outFile != "" {
file, err := os.Create(outFile)
if err != nil {
return err
}
defer file.Close()
out = file
}
_, err := io.WriteString(out, mergedCov)
return err
}

func filter(covs []*covLine, check func(line *covLine) bool) []*covLine {
n := len(covs)
j := 0
for i := 0; i < n; i++ {
if check(covs[i]) {
covs[j] = covs[i]
j++
}
}
return covs[:j]
}

func formatCoverage(mode string, lines []*covLine) string {
strs := make([]string, 0, len(lines)+1)
strs = append(strs, "mode: "+mode)
for _, line := range lines {
strs = append(strs, line.prefix+" "+strconv.FormatInt(line.count, 10))
}
return strings.Join(strs, "\n")
}

func merge(covs [][]*covLine) []*covLine {
if len(covs) == 0 {
return nil
}
if len(covs) == 1 {
return covs[0]
}
result := covs[0]
for i := 1; i < len(covs); i++ {
result = mergeCov(result, covs[i])
}
return result
}

func mergeCov(a []*covLine, b []*covLine) []*covLine {
for _, line := range b {
idx := -1
for i := 0; i < len(a); i++ {
if a[i].prefix == line.prefix {
idx = i
break
}
}
if idx < 0 {
a = append(a, line)
} else {
// fmt.Printf("add %s %d %d\n", a[idx].prefix, a[idx].count, line.count)
a[idx].count += line.count
}
}
return a
}

type covLine struct {
prefix string
count int64
}

func parseCover(content string) []*covLine {
lines := strings.Split(content, "\n")
if len(lines) > 0 && strings.HasPrefix(lines[0], "mode:") {
lines = lines[1:]
}
covLines := make([]*covLine, 0, len(lines))
for _, line := range lines {
covLine := parseCovLine(line)
if covLine == nil {
continue
}
covLines = append(covLines, covLine)
}
return covLines
}

func parseCovLine(line string) *covLine {
idx := strings.LastIndex(line, " ")
if idx < 0 {
return nil
}
cnt, err := strconv.ParseInt(line[idx+1:], 10, 64)
if err != nil {
cnt = 0
}
return &covLine{
prefix: line[:idx],
count: cnt,
}
}
38 changes: 30 additions & 8 deletions script/run-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// go run ./script/run-test/ --include go1.17.13 --include go1.18.10 --include go1.19.13 --include go1.20.14 --include go1.21.8 --include go1.22.1 -count=1 --xgo-default-test-only -run TestFuncNameSliceShouldWorkWithDebug -v

// TODO: remove duplicate test between xgo test and runtime test
var runtimeTests = []string{
var runtimeSubTests = []string{
"trace_panic_peek",
"func_list",
"trap_inspect_func",
Expand Down Expand Up @@ -231,16 +231,20 @@ func main() {
runDefault := true
runXgoTestFlag := true
runRuntimeTestFlag := true
runRuntimeSubTestFlag := true
if xgoTestOnly {
runRuntimeTestFlag = false
runRuntimeSubTestFlag = false
runXgoTestFlag = true
runDefault = false
} else if xgoRuntimeTestOnly {
runRuntimeTestFlag = true
runRuntimeSubTestFlag = true
runXgoTestFlag = false
runDefault = false
} else if xgoDefaultTestOnly {
runRuntimeTestFlag = false
runRuntimeSubTestFlag = false
runXgoTestFlag = false
runDefault = true
}
Expand All @@ -262,7 +266,7 @@ func main() {
}
args = append(args, "-coverprofile", prefix+"-"+variant+suffix)
}
if debug && (variant == "xgo" || variant == "runtime") {
if debug && (variant == "xgo" || variant == "runtime" || variant == "runtime-sub") {
args = append(args, "--log-debug=stdout")
}
return args
Expand All @@ -282,6 +286,13 @@ func main() {
os.Exit(1)
}
}
if runRuntimeSubTestFlag {
err := runRuntimeSubTest(goroot, addArgs(remainArgs, "runtime-sub"), 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, addArgs(remainArgs, "default"), remainTests)
if err != nil {
Expand Down Expand Up @@ -331,13 +342,17 @@ func runXgoTest(goroot string, args []string, tests []string) error {
func runRuntimeTest(goroot string, args []string, tests []string) error {
return doRunTest(goroot, testKind_runtimeTest, args, tests)
}
func runRuntimeSubTest(goroot string, args []string, tests []string) error {
return doRunTest(goroot, testKind_runtimeSubTest, args, tests)
}

type testKind string

const (
testKind_default testKind = ""
testKind_xgoTest testKind = "xgo-test"
testKind_runtimeTest testKind = "runtime-test"
testKind_default testKind = ""
testKind_xgoTest testKind = "xgo-test"
testKind_runtimeTest testKind = "runtime-test"
testKind_runtimeSubTest testKind = "runtime-sub-test"
)

func doRunTest(goroot string, kind testKind, args []string, tests []string) error {
Expand Down Expand Up @@ -371,9 +386,6 @@ func doRunTest(goroot string, kind testKind, args []string, tests []string) erro
if len(tests) > 0 {
testArgs = append(testArgs, tests...)
} else {
for _, runtimeTest := range runtimeTests {
testArgs = append(testArgs, "./test/"+runtimeTest)
}
testArgs = append(testArgs,
"./core/...",
"./functab/...",
Expand All @@ -382,6 +394,16 @@ func doRunTest(goroot string, kind testKind, args []string, tests []string) erro
"./mock/...",
)
}
case testKind_runtimeSubTest:
testArgs = []string{"run", "./cmd/xgo", "test", "--project-dir", "runtime/test"}
testArgs = append(testArgs, args...)
if len(tests) > 0 {
testArgs = append(testArgs, tests...)
} else {
for _, runtimeTest := range runtimeSubTests {
testArgs = append(testArgs, "./"+runtimeTest+"/...")
}
}
}

// debug
Expand Down
26 changes: 15 additions & 11 deletions test/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"testing"

"github.com/xhd2015/xgo/support/cmd"
"github.com/xhd2015/xgo/support/osinfo"

"github.com/xhd2015/xgo/support/filecopy"
Expand Down Expand Up @@ -38,23 +39,26 @@ func linkRuntimeAndTest(testDir string, goModOnly bool) (rootDir string, subDir
return "", "", err
}

// Windows no link
if runtime.GOOS != "windows" {
// copy runtime to a tmp directory, and
// test under there
if goModOnly {
err = filecopy.LinkFile(filepath.Join("..", "runtime", "go.mod"), filepath.Join(tmpDir, "go.mod"))
} else {
err = filecopy.CopyFile(filepath.Join("..", "runtime", "go.mod"), filepath.Join(tmpDir, "go.mod"))
if err != nil {
return "", "", err
}
// copy runtime to a tmp directory, and
// test under there
if !goModOnly {
if osinfo.FORCE_COPY_UNSYM {
err = filecopy.LinkFiles(filepath.Join("..", "runtime"), tmpDir)
}
} else {
if goModOnly {
err = filecopy.CopyFile(filepath.Join("..", "runtime", "go.mod"), filepath.Join(tmpDir, "go.mod"))
} else {
// Windows no link
err = filecopy.CopyReplaceDir(filepath.Join("..", "runtime"), tmpDir, false)
}
if err != nil {
return "", "", err
}
}

// set go.mod to go1.18
err = cmd.Dir(tmpDir).Run("go", "mod", "edit", "-go=1.18")
if err != nil {
return "", "", err
}
Expand Down

0 comments on commit 9b0f6cd

Please sign in to comment.