From 2b8ae28d0552ca0fb9af81171b3ff82329ca9107 Mon Sep 17 00:00:00 2001 From: JustAnotherID Date: Mon, 30 Oct 2023 13:22:10 -0500 Subject: [PATCH 1/2] =?UTF-8?q?ci:=20=E5=A2=9E=E5=8A=A0=20pr=20=E6=97=B6?= =?UTF-8?q?=E7=9A=84=20test=20=E5=92=8C=20lint=20ci=20(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ci: 增加 pr test ci * chore: make lint happy --- .github/workflows/test_and_lint.yml | 65 +++++++++++++++++++++++++++++ cmd/main.go | 6 +-- rollvm.go | 18 ++++---- rollvm_test.go | 26 ++++++------ types.go | 20 ++++----- types_serialization_test.go | 6 +-- 6 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/test_and_lint.yml diff --git a/.github/workflows/test_and_lint.yml b/.github/workflows/test_and_lint.yml new file mode 100644 index 00000000..fcbd56a3 --- /dev/null +++ b/.github/workflows/test_and_lint.yml @@ -0,0 +1,65 @@ +on: + push: + paths: + - '**.go' + - 'go.mod' + - '**.peg' + pull_request: + paths: + - '**.go' + - 'go.mod' + - '**.peg' + +name: Test & Lint + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Code + uses: actions/checkout@v3 + - name: Install Go + uses: actions/setup-go@v4 + with: + go-version: stable + - run: go install github.com/pointlander/peg@v1.0.1 + - run: peg -switch -inline roll.peg + - run: go mod tidy + - run: go get + - run: go generate ./... + - run: go test -v -race ./... + + lint: + runs-on: ubuntu-latest + steps: + - name: Code + uses: actions/checkout@v3 + - name: Install Go + uses: actions/setup-go@v4 + with: + go-version: stable + - run: go install github.com/pointlander/peg@v1.0.1 + - run: peg -switch -inline roll.peg + - run: go mod tidy + - run: go get + - run: go generate ./... + - name: Go vet + run: go vet ./... + + - name: GolangCI-Lint + uses: golangci/golangci-lint-action@v3 + if: github.event.name == 'pull_request' + with: + go-version: '1.18' + version: 'v1.51.2' + args: '--timeout 9999s' + only-new-issues: true + skip-pkg-cache: true + skip-build-cache: true + + - name: GolangCI-Lint + uses: golangci/golangci-lint-action@v3 + if: github.event.name != 'pull_request' + with: + version: 'v1.51.2' + args: '--timeout 9999s' diff --git a/cmd/main.go b/cmd/main.go index 08eeafb5..dbe2a51a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -25,8 +25,8 @@ func main() { }) if f, err := os.Open(historyFn); err == nil { - line.ReadHistory(f) - f.Close() + _, _ = line.ReadHistory(f) + _ = f.Close() } attrs := map[string]*dice.VMValue{} @@ -66,7 +66,7 @@ func main() { return nil } - for true { + for { if text, err := line.Prompt(">>> "); err == nil { if strings.TrimSpace(text) == "" { continue diff --git a/rollvm.go b/rollvm.go index 385e2694..a97bf334 100644 --- a/rollvm.go +++ b/rollvm.go @@ -36,13 +36,11 @@ func NewVM() *Context { } func (ctx *Context) Run(value string) error { - var err error - // 初始化Parser,这里是分词过程 p := ctx.parser p.Buffer = value - p.Trim(0) // 移动到下一行会报错 - err = p.Init() + p.Trim(0) // 移动到下一行会报错 + err := p.Init() // nolint // 初始化指令栈,默认指令长度512条,会自动增长 p.code = make([]ByteCode, 512) @@ -181,8 +179,8 @@ func (e *Parser) Evaluate() { end int64 spans []BufferSpan } - curPoint := int64(-1) - lastEnd := int64(-1) + curPoint := int64(-1) // nolint + lastEnd := int64(-1) // nolint sort.Sort(spanByBegin(details)) for _, i := range details { @@ -376,7 +374,7 @@ func (e *Parser) Evaluate() { case TypeLogicAnd: a, b := stackPop2() - if a.AsBool() == false { + if !a.AsBool() { stackPush(a) } else { stackPush(b) @@ -676,9 +674,9 @@ func (e *Parser) Evaluate() { wodInit() case TypeWodSetPoints: v := stackPop() - if v.TypeId != VMTypeInt { - // ... - } + // if v.TypeId != VMTypeInt { + // // ... + // } wodState.points = v.MustReadInt() case TypeWodSetThreshold: v := stackPop() diff --git a/rollvm_test.go b/rollvm_test.go index a5c6bd26..a4b3eacd 100644 --- a/rollvm_test.go +++ b/rollvm_test.go @@ -297,7 +297,7 @@ func TestWhile(t *testing.T) { assert.Error(t, err) // 算力上限 vm = NewVM() - err = vm.Run("i = 0; while1 {}") + err = vm.Run("i = 0; while1 {}") // nolint assert.True(t, vm.RestInput == "{}", vm.RestInput) } @@ -772,7 +772,7 @@ func TestBytecodeToString(t *testing.T) { func TestWriteCodeOverflow(t *testing.T) { vm := NewVM() - vm.Run("") + _ = vm.Run("") for i := 0; i < 8193; i++ { vm.parser.WriteCode(TypeNop, nil) } @@ -783,7 +783,7 @@ func TestWriteCodeOverflow(t *testing.T) { func TestGetASM(t *testing.T) { vm := NewVM() - vm.Run("1+1") + _ = vm.Run("1+1") vm.GetAsmText() } @@ -873,9 +873,9 @@ func TestRange(t *testing.T) { func TestDictExpr(t *testing.T) { vm := NewVM() - err := vm.Run("a = {'a': 1}") - if assert.NoError(t, err) { - } + err := vm.Run("a = {'a': 1}") // nolint + // if assert.NoError(t, err) { + // } err = vm.Run("a.a") if assert.NoError(t, err) { @@ -890,18 +890,18 @@ func TestDictExpr(t *testing.T) { func TestDictExpr2(t *testing.T) { vm := NewVM() - err := vm.Run("a = {'a': 1,}") - if assert.NoError(t, err) { - } + err := vm.Run("a = {'a': 1,}") // nolint + // if assert.NoError(t, err) { + // } err = vm.Run("a.a") if assert.NoError(t, err) { assert.True(t, valueEqual(vm.Ret, ni(1))) } vm = NewVM() - err = vm.Run("c = 'c'; a = {c:1,'b':3}") - if assert.NoError(t, err) { - } + err = vm.Run("c = 'c'; a = {c:1,'b':3}") // nolint + // if assert.NoError(t, err) { + // } err = vm.Run("a.c") if assert.NoError(t, err) { assert.True(t, valueEqual(vm.Ret, ni(1))) @@ -940,7 +940,7 @@ func TestCrash1(t *testing.T) { if assert.Error(t, err) { err := vm.Run("/") if assert.Error(t, err) { - assert.True(t, strings.Index(err.Error(), "parse error near") != -1) + assert.True(t, strings.Contains(err.Error(), "parse error near")) } } } diff --git a/types.go b/types.go index 7c1200c2..df1fa19c 100644 --- a/types.go +++ b/types.go @@ -76,7 +76,7 @@ type RollExtraFlags struct { StCallback func(_type string, name string, val *VMValue, op string, detail string) // st回调 // 以下尚未实现 - disableStmts bool // 禁用语句语法(如if while等),仅允许表达式 + // disableStmts bool // 禁用语句语法(如if while等),仅允许表达式 DiceMinMode bool // 骰子以最小值结算,用于获取下界 DiceMaxMode bool // 以最大值结算 获取上界 @@ -1013,7 +1013,7 @@ func (v *VMValue) ItemGet(ctx *Context, index *VMValue) *VMValue { switch v.TypeId { case VMTypeArray: if index.TypeId != VMTypeInt { - ctx.Error = errors.New(fmt.Sprintf("类型错误: 数字下标必须为数字,不能为 %s", index.GetTypeName())) + ctx.Error = fmt.Errorf("类型错误: 数字下标必须为数字,不能为 %s", index.GetTypeName()) } else { return v.ArrayItemGet(ctx, index.MustReadInt()) } @@ -1026,7 +1026,7 @@ func (v *VMValue) ItemGet(ctx *Context, index *VMValue) *VMValue { } case VMTypeString: if index.TypeId != VMTypeInt { - ctx.Error = errors.New(fmt.Sprintf("类型错误: 数字下标必须为数字,不能为 %s", index.GetTypeName())) + ctx.Error = fmt.Errorf("类型错误: 数字下标必须为数字,不能为 %s", index.GetTypeName()) } else { str, _ := v.ReadString() rstr := []rune(str) @@ -1055,7 +1055,7 @@ func (v *VMValue) ItemSet(ctx *Context, index *VMValue, val *VMValue) bool { switch v.TypeId { case VMTypeArray: if index.TypeId != VMTypeInt { - ctx.Error = errors.New(fmt.Sprintf("类型错误: 数字下标必须为数字,不能为 %s", index.GetTypeName())) + ctx.Error = fmt.Errorf("类型错误: 数字下标必须为数字,不能为 %s", index.GetTypeName()) } else { return v.ArrayItemSet(ctx, index.MustReadInt(), val) } @@ -1352,7 +1352,7 @@ func (v *VMValue) FuncInvoke(ctx *Context, params []*VMValue) *VMValue { // 设置参数 if len(cd.Params) != len(params) { - ctx.Error = errors.New(fmt.Sprintf("调用参数个数与函数定义不符,需求%d,传入%d", len(cd.Params), len(params))) + ctx.Error = fmt.Errorf("调用参数个数与函数定义不符,需求%d,传入%d", len(cd.Params), len(params)) return nil } for index, i := range cd.Params { @@ -1419,13 +1419,12 @@ func (v *VMValue) FuncInvokeNative(ctx *Context, params []*VMValue) *VMValue { } if len(cd.Params) != len(params) { - ctx.Error = errors.New(fmt.Sprintf("调用参数个数与函数定义不符,需求%d,传入%d", len(cd.Params), len(params))) + ctx.Error = fmt.Errorf("调用参数个数与函数定义不符,需求%d,传入%d", len(cd.Params), len(params)) return nil } ret := cd.NativeFunc(ctx, cd.Self, params) if ctx.Error != nil { - ctx.Error = ctx.Error return nil } @@ -1439,7 +1438,7 @@ func (v *VMValue) AsDictKey() (string, error) { if v.TypeId == VMTypeString || v.TypeId == VMTypeInt || v.TypeId == VMTypeFloat { return v.ToString(), nil } else { - return "", errors.New(fmt.Sprintf("类型错误: 字典键只能为字符串或数字,不支持 %s", v.GetTypeName())) + return "", fmt.Errorf("类型错误: 字典键只能为字符串或数字,不支持 %s", v.GetTypeName()) } } @@ -1542,10 +1541,7 @@ func VMValueNewArrayRaw(data []*VMValue) *VMValue { func VMValueNewArray(values ...*VMValue) *VMValue { var data []*VMValue - for _, i := range values { - data = append(data, i) - } - + data = append(data, values...) return &VMValue{TypeId: VMTypeArray, Value: &ArrayData{data}} } diff --git a/types_serialization_test.go b/types_serialization_test.go index 3251e964..e46f5cb7 100644 --- a/types_serialization_test.go +++ b/types_serialization_test.go @@ -43,7 +43,7 @@ func TestDumps(t *testing.T) { err = vm.Run(`func a(x) { return 5 }; a`) if assert.NoError(t, err) { ret := vm.Ret - v, err = ret.ToJSON() + v, err = ret.ToJSON() // nolint assert.Equal(t, `{"typeId":8,"value":{"expr":"return 5 ","name":"a","params":["x"]}}`, string(v)) } @@ -56,14 +56,14 @@ func TestDumps(t *testing.T) { v1 := na(ni(1), nf(2.0), ns("test")) ad, _ := v1.ReadArray() ad.List = append(ad.List, v1) - v, err = v1.ToJSON() + v, err = v1.ToJSON() // nolint assert.Error(t, err) vm = NewVM() err = vm.Run(`ceil`) if assert.NoError(t, err) { ret := vm.Ret - v, err = ret.ToJSON() + v, err = ret.ToJSON() // nolint assert.Equal(t, `{"typeId":9,"value":{"name":"ceil"}}`, string(v)) } } From f8a161e448ee48ab64c8c2cd22c2ea0d5746bc02 Mon Sep 17 00:00:00 2001 From: JustAnotherID Date: Tue, 31 Oct 2023 02:27:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ci:=20=E5=BE=AE=E8=B0=83=20ci?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/jsport.yml | 2 -- .github/workflows/test_and_lint.yml | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/jsport.yml b/.github/workflows/jsport.yml index d4be6039..d015d670 100644 --- a/.github/workflows/jsport.yml +++ b/.github/workflows/jsport.yml @@ -3,8 +3,6 @@ name: Build Js Port on: push: branches: [ "main" ] - pull_request: - branches: [ "main" ] jobs: diff --git a/.github/workflows/test_and_lint.yml b/.github/workflows/test_and_lint.yml index fcbd56a3..d0218211 100644 --- a/.github/workflows/test_and_lint.yml +++ b/.github/workflows/test_and_lint.yml @@ -27,7 +27,7 @@ jobs: - run: go mod tidy - run: go get - run: go generate ./... - - run: go test -v -race ./... + - run: go test -v -race -cover ./... lint: runs-on: ubuntu-latest