Skip to content

Commit

Permalink
allow xgo auto update via config
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Aug 5, 2024
1 parent 9999158 commit ee7e307
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
24 changes: 23 additions & 1 deletion cmd/xgo/test-explorer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ type TestConfig struct {
Flags []string `json:"flags"`
Args []string `json:"args"`

MockRules []string `json:"mock_rules"`
MockRules []string `json:"mock_rules"`
Xgo *XgoConfig `json:"xgo,omitempty"`
}

type XgoConfig struct {
AutoUpdate bool `json:"auto_update"`
}

func (c *TestConfig) CmdEnv() []string {
Expand Down Expand Up @@ -141,10 +146,27 @@ func parseTestConfig(config string) (*TestConfig, error) {
}
conf.MockRules = list
}
if e, ok := m["xgo"]; ok {
err := copyViaJSON(e, &conf.Xgo)
if err != nil {
return nil, fmt.Errorf("xgo: %w", err)
}
}

return conf, nil
}

func copyViaJSON(src interface{}, dst interface{}) error {
if src == nil {
return nil
}
data, err := json.Marshal(src)
if err != nil {
return err
}
return json.Unmarshal(data, dst)
}

func parseConfigAndMergeOptions(configFile string, opts *Options, configFileRequired bool) (*TestConfig, error) {
var data []byte
if configFile != "" {
Expand Down
29 changes: 23 additions & 6 deletions cmd/xgo/test-explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/fs"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -335,13 +336,15 @@ func handle(opts *Options, args []string) error {
}
return conf, nil
}

// headless mode
conf, err := getTestConfig()
if err != nil {
return err
}
if conf.Xgo != nil && conf.Xgo.AutoUpdate && os.Getenv("XGO_AUTO_UPDATE") != "never" {
autoUpdateXgo()
}
if len(args) > 0 && args[0] == "test" {
// headless mode
conf, err := getTestConfig()
if err != nil {
return err
}
root, err := scanTests(projectRoot, subPath, true, conf.Exclude)
if err != nil {
return err
Expand Down Expand Up @@ -430,6 +433,20 @@ func handle(opts *Options, args []string) error {
})
}

func autoUpdateXgo() {
// auto update
done := make(chan struct{})
go func() {
cmd.Debug().Run("go", "install", "github.com/xhd2015/xgo/cmd/xgo@latest")
close(done)
}()
// at most wait 2 second
select {
case <-done:
case <-time.After(2 * time.Second):
}
}

func openURL(url string) {
openCmd := "open"
if runtime.GOOS == "windows" {
Expand Down
4 changes: 0 additions & 4 deletions cmd/xgo/test-explorer/trace_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ type CallRecord struct {
Children []*CallRecord `json:"children"`
}

func convertTraceFileToCallRecords() []*CallRecord {
return nil
}

type traceConverter struct {
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "fmt"

// auto updated
const VERSION = "1.0.47"
const REVISION = "324b023bc90dfb3c23fa9781cbb8f78e2830912b+1"
const NUMBER = 303
const REVISION = "9999158f6ad9a59b8bb21a3f2cd7bef59bec11f7+1"
const NUMBER = 304

// manually updated
const CORE_VERSION = "1.0.47"
Expand Down
3 changes: 3 additions & 0 deletions test.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"comment": "override not currently supported",
"path": "test/xgo_test",
"go_cmd": "xgo"
},
"xgo": {
"auto_update": false
}
}

0 comments on commit ee7e307

Please sign in to comment.