Skip to content

Commit

Permalink
add github workflow, meeting the awesome-go quality guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Mar 30, 2024
1 parent 1bc959a commit bdfb508
Show file tree
Hide file tree
Showing 25 changed files with 663 additions and 486 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build
run: go build -v ./cmd/...

- name: Test
run: go run ./script/run-test -v -cover -coverpkg github.com/xhd2015/xgo/runtime/... -coverprofile cover.out

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

- name: Build Release
run: go run ./script/build-release

# - name: Check Version
# run: ~/.xgo/bin/xgo revision

# - name: Check Go Version
# run: ~/.xgo/bin/xgo exec go version
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@

# generated traces
/runtime/test/**/*.json
/test/**/*.json
/test/**/*.json

# coverage
/covers
cover*.out
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)**

Expand Down Expand Up @@ -428,7 +433,7 @@ In conclusion, `xgo` and monkey are compared as the following:
|-|-|-|
|Technique|IR|ASM|
|Function Mock|Y|Y|
|Unexpected Function Mock|Y|N|
|Unexported Function Mock|Y|N|
|Per-Instance Method Mock|Y|N|
|Per-Goroutine Mock|Y|N|
|Per-Generic Type Mock|Y|Y|
Expand Down
2 changes: 1 addition & 1 deletion cmd/trace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"net/http"
"os"
"os/exec"
"runtime/debug"
"runtime"
"runtime/debug"
"strings"
"time"

Expand Down
1 change: 1 addition & 0 deletions cmd/trace/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

.trace-list-root {
height: 100%;
overflow-y: scroll;
border: 1px solid grey;
list-style: none;
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/xgo/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
204 changes: 102 additions & 102 deletions cmd/xgo/log.go
Original file line number Diff line number Diff line change
@@ -1,102 +1,102 @@
package main

import (
"fmt"
"io"
"os"
"runtime"
"strings"
"time"

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

var logDebugFile *os.File

func setupDebugLog(logDebugOption *string) (func(), error) {
var logDebugFileName string
if logDebugOption != nil {
logDebugFileName = *logDebugOption
if logDebugFileName == "" {
logDebugFileName = "debug.log"
}
}
if logDebugFileName == "" {
return nil, nil
}
return initLog(logDebugFileName)
}

func initLog(logDebugFileName string) (func(), error) {
if logDebugFileName == "stdout" {
logDebugFile = os.Stdout
return nil, nil
}
if logDebugFileName == "stderr" {
logDebugFile = os.Stderr
return nil, nil
}
var err error
logDebugFile, err = os.Create(logDebugFileName)
if err != nil {
return nil, fmt.Errorf("create log: %s %w", logDebugFileName, err)
}
return func() {
logDebugFile.Close()
}, nil
}

func logDebug(format string, args ...interface{}) {
if logDebugFile == nil {
return
}
fmt.Fprint(logDebugFile, time.Now().Format("2006-01-02 15:04:05"), " ")
fmt.Fprintf(logDebugFile, format, args...)
if !strings.HasSuffix(format, "\n") {
fmt.Fprintln(logDebugFile)
}
logDebugFile.Sync()
}

func logStartup() {
logDebug("start: %v", os.Args)
logDebug("runtime.GOOS=%s", runtime.GOOS)
logDebug("runtime.GOARCH=%s", runtime.GOARCH)
logDebug("runtime.Version()=%s", runtime.Version())
logDebug("runtime.GOROOT()=%s", runtime.GOROOT())
logDebug("os exe suffix: %s", osinfo.EXE_SUFFIX)
logDebug("os force copy unsym: %v", osinfo.FORCE_COPY_UNSYM)
}

// if [[ $verbose = true ]];then
//
// tail -fn1 "$shdir/compile.log" &
// trap "kill -9 $!" EXIT
// fi
func tailLog(logFile string) {
file, err := os.OpenFile(logFile, os.O_RDONLY|os.O_CREATE, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "open compile log: %v\n", err)
return
}
_, err = file.Seek(0, io.SeekEnd)
if err != nil {
fmt.Fprintf(os.Stderr, "seek tail compile log: %v\n", err)
return
}
buf := make([]byte, 1024)
for {
n, err := file.Read(buf)
if n > 0 {
os.Stdout.Write(buf[:n])
}
if err != nil {
if err == io.EOF {
time.Sleep(50 * time.Millisecond)
continue
}
fmt.Fprintf(os.Stderr, "tail compile log: %v\n", err)
return
}
}
}
package main

import (
"fmt"
"io"
"os"
"runtime"
"strings"
"time"

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

var logDebugFile *os.File

func setupDebugLog(logDebugOption *string) (func(), error) {
var logDebugFileName string
if logDebugOption != nil {
logDebugFileName = *logDebugOption
if logDebugFileName == "" {
logDebugFileName = "debug.log"
}
}
if logDebugFileName == "" {
return nil, nil
}
return initLog(logDebugFileName)
}

func initLog(logDebugFileName string) (func(), error) {
if logDebugFileName == "stdout" {
logDebugFile = os.Stdout
return nil, nil
}
if logDebugFileName == "stderr" {
logDebugFile = os.Stderr
return nil, nil
}
var err error
logDebugFile, err = os.Create(logDebugFileName)
if err != nil {
return nil, fmt.Errorf("create log: %s %w", logDebugFileName, err)
}
return func() {
logDebugFile.Close()
}, nil
}

func logDebug(format string, args ...interface{}) {
if logDebugFile == nil {
return
}
fmt.Fprint(logDebugFile, time.Now().Format("2006-01-02 15:04:05"), " ")
fmt.Fprintf(logDebugFile, format, args...)
if !strings.HasSuffix(format, "\n") {
fmt.Fprintln(logDebugFile)
}
logDebugFile.Sync()
}

func logStartup() {
logDebug("start: %v", os.Args)
logDebug("runtime.GOOS=%s", runtime.GOOS)
logDebug("runtime.GOARCH=%s", runtime.GOARCH)
logDebug("runtime.Version()=%s", runtime.Version())
logDebug("runtime.GOROOT()=%s", runtime.GOROOT())
logDebug("os exe suffix: %s", osinfo.EXE_SUFFIX)
logDebug("os force copy unsym: %v", osinfo.FORCE_COPY_UNSYM)
}

// if [[ $verbose = true ]];then
//
// tail -fn1 "$shdir/compile.log" &
// trap "kill -9 $!" EXIT
// fi
func tailLog(logFile string) {
file, err := os.OpenFile(logFile, os.O_RDONLY|os.O_CREATE, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "open compile log: %v\n", err)
return
}
_, err = file.Seek(0, io.SeekEnd)
if err != nil {
fmt.Fprintf(os.Stderr, "seek tail compile log: %v\n", err)
return
}
buf := make([]byte, 1024)
for {
n, err := file.Read(buf)
if n > 0 {
os.Stdout.Write(buf[:n])
}
if err != nil {
if err == io.EOF {
time.Sleep(50 * time.Millisecond)
continue
}
fmt.Fprintf(os.Stderr, "tail compile log: %v\n", err)
return
}
}
}
22 changes: 10 additions & 12 deletions cmd/xgo/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit bdfb508

Please sign in to comment.