-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix constant comparison compile issue
- Loading branch information
Showing
9 changed files
with
222 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |