diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 8bb1bdd0..d5a0a320 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -28,6 +28,14 @@ jobs: with: files: ./ + - name: Binary Install + continue-on-error: false + run: curl -fsSL https://github.com/xhd2015/xgo/raw/master/install.sh | env INSTALL_TO_BIN=true bash -x + + - name: Check Binary Install + continue-on-error: false + run: xgo revision + - name: Build run: go build -o /dev/null -v ./cmd/xgo diff --git a/cmd/xgo/runtime_gen/core/version.go b/cmd/xgo/runtime_gen/core/version.go index 946d28b9..4e05587a 100755 --- a/cmd/xgo/runtime_gen/core/version.go +++ b/cmd/xgo/runtime_gen/core/version.go @@ -6,9 +6,9 @@ import ( "os" ) -const VERSION = "1.0.25" -const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1" -const NUMBER = 198 +const VERSION = "1.0.26" +const REVISION = "d19c85ac922d26f26038dc99ae3049de0d91d2f6+1" +const NUMBER = 199 // these fields will be filled by compiler const XGO_VERSION = "" @@ -27,27 +27,32 @@ func init() { fmt.Fprintf(os.Stderr, "WARNING: xgo toolchain: %v\nnote: this message can be turned off by setting %s=false\n", err, XGO_CHECK_TOOLCHAIN_VERSION) } } - func checkVersion() error { // xgoVersion, xgoRevision, xgoNumber := XGO_VERSION, XGO_REVISION, XGO_NUMBER // _, _, _ = xgoVersion, xgoRevision, xgoNumber if XGO_VERSION == "" { - return errors.New("failed to detect xgo version, requires xgo >= v1.0.9, consider run 'xgo upgrade'") + return errors.New("failed to detect xgo version, consider install xgo: go install github.com/xhd2015/xgo/cmd/xgo@latest") } if XGO_VERSION == VERSION { // if runtime version is larger, that means if XGO_NUMBER < NUMBER { - return errors.New("newer xgo available, consider run 'xgo upgrade'") + return errors.New("newer xgo available, consider run: xgo upgrade") } // only one case is feasible: XGO_NUMBER >= runtime NUMBER // because xgo can be compatible with older sdk return nil } - var updateCmd string + if XGO_NUMBER == NUMBER { + // good to go + return nil + } + var msg string if XGO_NUMBER < NUMBER { - updateCmd = "xgo upgrade" + updateCmd := "xgo upgrade" + msg = fmt.Sprintf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run: %s", XGO_VERSION, VERSION, updateCmd) } else { - updateCmd = "go get github.com/xhd2015/xgo/runtime@latest" + updateCmd := "go get github.com/xhd2015/xgo/runtime@latest" + msg = fmt.Sprintf("xgo/runtime v%s can be upgraded to v%s, consider run: %s", VERSION, XGO_VERSION, updateCmd) } - return fmt.Errorf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run '%s'", XGO_VERSION, VERSION, updateCmd) + return errors.New(msg) } diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 5ff41c11..c2ce9abd 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -2,9 +2,9 @@ package main import "fmt" -const VERSION = "1.0.25" -const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1" -const NUMBER = 198 +const VERSION = "1.0.26" +const REVISION = "d19c85ac922d26f26038dc99ae3049de0d91d2f6+1" +const NUMBER = 199 func getRevision() string { revSuffix := "" diff --git a/patch/syntax/vars.go b/patch/syntax/vars.go index e6441a50..a8363383 100644 --- a/patch/syntax/vars.go +++ b/patch/syntax/vars.go @@ -485,8 +485,8 @@ func (ctx *BlockContext) traverseExpr(node syntax.Expr, globaleNames map[string] node.X = ctx.traverseExpr(node.X, globaleNames, imports) node.Y = ctx.traverseExpr(node.Y, globaleNames, imports) // if both side are const, then the operation should also - // be wrapped in a const - if node.X != nil && node.Y != nil { + // be wrapped in a const if the operation is not == + if node.X != nil && node.Y != nil && !isBoolOp(node.Op) { xConst := ctx.ConstInfo[node.X] yConst := ctx.ConstInfo[node.Y] isXgoConv := func(node syntax.Expr) bool { @@ -545,6 +545,13 @@ func (ctx *BlockContext) traverseExpr(node syntax.Expr, globaleNames map[string] } return node } +func isBoolOp(op syntax.Operator) bool { + switch op { + case syntax.Eql, syntax.Neq, syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq: + return true + } + return false +} func getConstType(xgoConv *syntax.XgoSimpleConvert) string { return xgoConv.X.(*syntax.CallExpr).Fun.(*syntax.Name).Value diff --git a/runtime/core/version.go b/runtime/core/version.go index 946d28b9..4e05587a 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -6,9 +6,9 @@ import ( "os" ) -const VERSION = "1.0.25" -const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1" -const NUMBER = 198 +const VERSION = "1.0.26" +const REVISION = "d19c85ac922d26f26038dc99ae3049de0d91d2f6+1" +const NUMBER = 199 // these fields will be filled by compiler const XGO_VERSION = "" @@ -27,27 +27,32 @@ func init() { fmt.Fprintf(os.Stderr, "WARNING: xgo toolchain: %v\nnote: this message can be turned off by setting %s=false\n", err, XGO_CHECK_TOOLCHAIN_VERSION) } } - func checkVersion() error { // xgoVersion, xgoRevision, xgoNumber := XGO_VERSION, XGO_REVISION, XGO_NUMBER // _, _, _ = xgoVersion, xgoRevision, xgoNumber if XGO_VERSION == "" { - return errors.New("failed to detect xgo version, requires xgo >= v1.0.9, consider run 'xgo upgrade'") + return errors.New("failed to detect xgo version, consider install xgo: go install github.com/xhd2015/xgo/cmd/xgo@latest") } if XGO_VERSION == VERSION { // if runtime version is larger, that means if XGO_NUMBER < NUMBER { - return errors.New("newer xgo available, consider run 'xgo upgrade'") + return errors.New("newer xgo available, consider run: xgo upgrade") } // only one case is feasible: XGO_NUMBER >= runtime NUMBER // because xgo can be compatible with older sdk return nil } - var updateCmd string + if XGO_NUMBER == NUMBER { + // good to go + return nil + } + var msg string if XGO_NUMBER < NUMBER { - updateCmd = "xgo upgrade" + updateCmd := "xgo upgrade" + msg = fmt.Sprintf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run: %s", XGO_VERSION, VERSION, updateCmd) } else { - updateCmd = "go get github.com/xhd2015/xgo/runtime@latest" + updateCmd := "go get github.com/xhd2015/xgo/runtime@latest" + msg = fmt.Sprintf("xgo/runtime v%s can be upgraded to v%s, consider run: %s", VERSION, XGO_VERSION, updateCmd) } - return fmt.Errorf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run '%s'", XGO_VERSION, VERSION, updateCmd) + return errors.New(msg) } diff --git a/runtime/test/core/version.go b/runtime/test/core/version.go new file mode 100644 index 00000000..9e1af68d --- /dev/null +++ b/runtime/test/core/version.go @@ -0,0 +1,49 @@ +package core + +import ( + "errors" + "fmt" +) + +// copied from core/version.go + +const VERSION = "1.0.26" +const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1" +const NUMBER = 198 + +// these fields will be filled by compiler +const XGO_VERSION = "" +const XGO_REVISION = "" +const XGO_NUMBER = 0 + +// copy from core + +func checkVersion() error { + // xgoVersion, xgoRevision, xgoNumber := XGO_VERSION, XGO_REVISION, XGO_NUMBER + // _, _, _ = xgoVersion, xgoRevision, xgoNumber + if XGO_VERSION == "" { + return errors.New("failed to detect xgo version, consider install xgo: go install github.com/xhd2015/xgo/cmd/xgo@latest") + } + if XGO_VERSION == VERSION { + // if runtime version is larger, that means + if XGO_NUMBER < NUMBER { + return errors.New("newer xgo available, consider run: xgo upgrade") + } + // only one case is feasible: XGO_NUMBER >= runtime NUMBER + // because xgo can be compatible with older sdk + return nil + } + if XGO_NUMBER == NUMBER { + // good to go + return nil + } + var msg string + if XGO_NUMBER < NUMBER { + updateCmd := "xgo upgrade" + msg = fmt.Sprintf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run: %s", XGO_VERSION, VERSION, updateCmd) + } else { + updateCmd := "go get github.com/xhd2015/xgo/runtime@latest" + msg = fmt.Sprintf("xgo/runtime v%s can be upgraded to v%s, consider run: %s", VERSION, XGO_VERSION, updateCmd) + } + return errors.New(msg) +} diff --git a/runtime/test/core/version_test.go b/runtime/test/core/version_test.go new file mode 100644 index 00000000..edb99099 --- /dev/null +++ b/runtime/test/core/version_test.go @@ -0,0 +1,82 @@ +package core + +import ( + "fmt" + "strings" + "testing" + + "github.com/xhd2015/xgo/runtime/mock" +) + +const corePkg = "github.com/xhd2015/xgo/runtime/test/core" + +func TestCheckVersion(t *testing.T) { + tests := []struct { + xgo VersionInfo + runtime VersionInfo + err string + }{ + { + VersionInfo{}, + VersionInfo{}, + "failed to detect xgo version", + }, + { + // good to go if xgo > runtime + VersionInfo{Version: "1.0.1", Revision: "B", Number: 101}, + VersionInfo{Version: "1.0.0", Revision: "A", Number: 100}, + "xgo/runtime v1.0.0 can be upgraded to v1.0.1", + }, + { + // not good to go if xgo < runtime + VersionInfo{Version: "1.0.1", Revision: "B", Number: 101}, + VersionInfo{Version: "1.0.2", Revision: "C", Number: 102}, + "xgo v1.0.1 maybe incompatible with xgo/runtime v1.0.2", + }, + } + for i, tt := range tests { + tt := tt + name := fmt.Sprintf("case_%d", i) + t.Run(name, func(t *testing.T) { + err := testVersion(tt.xgo, tt.runtime) + var errMsg string + if err != nil { + errMsg = err.Error() + } + if (errMsg == "") != (tt.err == "") { + t.Fatalf("expect err msg: %q, actual: %q", tt.err, errMsg) + } + if !strings.Contains(errMsg, tt.err) { + t.Fatalf("expect err: %q, actual: %q", tt.err, errMsg) + } + }) + } +} + +type VersionInfo struct { + Version string + Revision string + Number int +} + +func testVersion(xgo VersionInfo, runtime VersionInfo) error { + mock.PatchByName(corePkg, "VERSION", func() string { + return runtime.Version + }) + mock.PatchByName(corePkg, "REVISION", func() string { + return runtime.Revision + }) + mock.PatchByName(corePkg, "NUMBER", func() int { + return runtime.Number + }) + mock.PatchByName(corePkg, "XGO_VERSION", func() string { + return xgo.Version + }) + mock.PatchByName(corePkg, "XGO_REVISION", func() string { + return xgo.Revision + }) + mock.PatchByName(corePkg, "XGO_NUMBER", func() int { + return xgo.Number + }) + return checkVersion() +} diff --git a/runtime/test/debug/debug_test.go b/runtime/test/debug/debug_test.go index 35b4a10f..5af0ed50 100644 --- a/runtime/test/debug/debug_test.go +++ b/runtime/test/debug/debug_test.go @@ -5,60 +5,12 @@ package debug -import ( - _ "encoding/json" - _ "io" - _ "io/ioutil" - _ "net" - _ "net/http" - _ "os/exec" - "testing" - _ "time" +import "testing" - "github.com/xhd2015/xgo/runtime/core" - "github.com/xhd2015/xgo/runtime/functab" -) +const XGO_VERSION = "" func TestListStdlib(t *testing.T) { - funcs := functab.GetFuncs() - - stdPkgs := map[string]bool{ - "net/http.Get": true, - } - found, missing := getMissing(funcs, stdPkgs, false) - if len(missing) > 0 { - t.Fatalf("expect func list contains: %v, actual %v", missing, found) - } -} - -func getMissing(funcs []*core.FuncInfo, missingPkgs map[string]bool, intf bool) (found []string, missing []string) { - for _, fn := range funcs { - pkg := fn.Pkg - if intf { - if fn.Interface { - // t.Logf("found interface: %v", fn) - key := pkg + "." + fn.RecvType - if _, ok := missingPkgs[key]; ok { - missingPkgs[key] = false - } - } - continue - } - displayName := fn.DisplayName() - // fmt.Printf("found: %s %s\n", pkg, displayName) - // debug - key := pkg + "." + displayName - _, ok := missingPkgs[key] - if ok { - missingPkgs[key] = false - } - } - for k, ok := range missingPkgs { - if ok { - missing = append(missing, k) - } else { - found = append(found, k) - } + if XGO_VERSION == "" { + t.Fatalf("fail") } - return } diff --git a/runtime/test/patch_const/const_compile_test.go b/runtime/test/patch_const/const_compile_test.go new file mode 100644 index 00000000..68358980 --- /dev/null +++ b/runtime/test/patch_const/const_compile_test.go @@ -0,0 +1,37 @@ +package patch_const + +import "testing" + +const XGO_VERSION = "" + +// see https://github.com/xhd2015/xgo/issues/78 +func TestConstCompile(t *testing.T) { + // all operators + if XGO_VERSION != "" { + t.Fatalf("fail") + } + if XGO_VERSION < "" { + t.Fatalf("fail") + } + + if XGO_VERSION > "" { + t.Fatalf("fail") + } + if XGO_VERSION <= "" { + } else { + t.Fatalf("fail") + } + + if XGO_VERSION >= "" { + } else { + t.Fatalf("fail") + } + + if XGO_VERSION == "" { + } else { + t.Fatalf("fail") + } + if "" != XGO_VERSION { + t.Fatalf("fail") + } +}