Skip to content

Commit

Permalink
test explorer: improve UI responsivenes, supporting sub cases dynamic…
Browse files Browse the repository at this point in the history
…ally,see component update: xhd2015/coverage-visualizer#4
  • Loading branch information
xhd2015 committed Jun 23, 2024
1 parent cb5d302 commit 6cae8c1
Show file tree
Hide file tree
Showing 18 changed files with 1,007 additions and 457 deletions.
31 changes: 7 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ mkdir demo
cd demo
go mod init demo
```
3. Add `demo_test.go` with following code:
3. Add `demo_test.go`:
```go
package demo_test

Expand All @@ -98,16 +98,14 @@ func TestFuncMock(t *testing.T) {
}
}
```
4. Get the `xgo/runtime` dependency:
4. Get the `github.com/xhd2015/xgo/runtime` dependency:
```sh
go get github.com/xhd2015/xgo/runtime
```
5. Run the code:
5. Test the code:
```sh
# NOTE: xgo will take some time
# for the first time to setup.
# It will be as fast as go after setup.
xgo test -v ./
# NOTE: xgo will take some time to setup for the first time
```

Output:
Expand All @@ -118,26 +116,11 @@ PASS
ok demo
```

If you run this with go, it would fail:
```sh
go test -v ./
```
NOTE: `xgo` is used to test your code, not just `go`.

Output:
```sh
WARNING: failed to link __xgo_link_on_init_finished(requires xgo).
WARNING: failed to link __xgo_link_on_goexit(requires xgo).
=== RUN TestFuncMock
WARNING: failed to link __xgo_link_set_trap(requires xgo).
WARNING: failed to link __xgo_link_init_finished(requires xgo).
demo_test.go:21: expect MyFunc() to be 'mock func', actual: my func
--- FAIL: TestFuncMock (0.00s)
FAIL
FAIL demo 0.771s
FAIL
```
Under the hood, `xgo` preprocess your code to add mock hooks, and then calls `go` to do remaining jobs.

The above demo can be found at [doc/demo](./doc/demo).
The above code can be found at [doc/demo](./doc/demo).

# API

Expand Down
2 changes: 1 addition & 1 deletion README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestFuncMock(t *testing.T) {
}
}
```
4. 获取`xgo/runtime`的依赖:
4. 获取`github.com/xhd2015/xgo/runtime`的依赖:
```sh
go get github.com/xhd2015/xgo/runtime
```
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.40"
const REVISION = "1a16a856a24d88d7f84f61a39340a2b264e99b10+1"
const NUMBER = 266
const REVISION = "cb5d3025fad60e00be0dfe8fdc7eb1bef97dedc6+1"
const NUMBER = 267

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
28 changes: 19 additions & 9 deletions cmd/xgo/test-explorer/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test_explorer

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
Expand All @@ -14,6 +15,7 @@ import (

type DebugRequest struct {
Item *TestingItem `json:"item"`
Path []string `json:"path"`
}
type DebugResponse struct {
ID string `json:"id"`
Expand All @@ -30,10 +32,12 @@ type DebugDestroyRequest struct {
ID string `json:"id"`
}

// deprecated
func setupDebugHandler(server *http.ServeMux, projectDir string, getTestConfig func() (*TestConfig, error)) {
setupPollHandler(server, "/debug", projectDir, getTestConfig, debug)
}

// deprecated
func debug(ctx *RunContext) error {
projectDir := ctx.ProjectDir
file := ctx.File
Expand All @@ -46,6 +50,17 @@ func debug(ctx *RunContext) error {
args := ctx.Args
env := ctx.Env

err := debugTest(goCmd, projectDir, file, buildFlags, []string{"./" + filepath.Dir(relPath)}, fmt.Sprintf("^%s$", name), stdout, stderr, args, env)
if err != nil {
return err
}
return nil
}

func debugTest(goCmd string, dir string, file string, buildFlags []string, buildArgs []string, runNames string, stdout io.Writer, stderr io.Writer, args []string, env []string) error {
if goCmd == "" {
goCmd = "go"
}
tmpDir, err := os.MkdirTemp("", "go-test-debug")
if err != nil {
return err
Expand All @@ -63,17 +78,16 @@ func debug(ctx *RunContext) error {
// if err != nil {
// return err
// }
relPathDir := filepath.Dir(relPath)
tmpBin := filepath.Join(tmpDir, binName)

flags := []string{"test", "-c", "-o", tmpBin, "-gcflags=all=-N -l"}
flags = append(flags, buildFlags...)
flags = append(flags, "./"+relPathDir)
err = cmd.Dir(projectDir).Debug().Stderr(stderr).Stdout(stdout).Run(goCmd, flags...)
flags = append(flags, buildArgs...)
err = cmd.Dir(dir).Debug().Stderr(stderr).Stdout(stdout).Run(goCmd, flags...)
if err != nil {
return err
}
err = netutil.ServePort("localhost", 2345, true, 500*time.Millisecond, func(port int) {
return netutil.ServePort("localhost", 2345, true, 500*time.Millisecond, func(port int) {
fmt.Fprintln(stderr, debug_util.FormatDlvPrompt(port))
}, func(port int) error {
// dlv exec --api-version=2 --listen=localhost:2345 --accept-multiclient --headless ./debug.bin
Expand All @@ -89,12 +103,8 @@ func debug(ctx *RunContext) error {
"--headless",
// "--allow-non-terminal-interactive=true",
fmt.Sprintf("--listen=localhost:%d", port),
tmpBin, "--", "-test.v", "-test.run", fmt.Sprintf("^%s$", name),
tmpBin, "--", "-test.v", "-test.run", runNames,
}, args...)...,
)
})
if err != nil {
return err
}
return nil
}
7 changes: 4 additions & 3 deletions cmd/xgo/test-explorer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
window.onload = function(){
const {renderReact,UrlXgoTestingExplorer} = Open
renderReact(root,UrlXgoTestingExplorer,{
apiPrefix:"http://localhost:8080"
apiPrefix: "http://localhost:8080"
})
}
</script>
Expand All @@ -20,6 +20,7 @@
</body>
<!--after body because document.body may be null-->
<!--available in CN and Global-->
<script src="https://cdn.jsdelivr.net/npm/xgo-explorer@0.0.9/index.js"></script>
<!-- <script src="http://127.0.0.1:8081/npm-publish/index.js"></script> -->
<script src="https://cdn.jsdelivr.net/npm/xgo-explorer@0.0.11/index.js"></script>
<!-- <script src="http://127.0.0.1:8080/build/index.js"></script> -->
<!-- <script src="http://127.0.0.1:8080/npm-publish/index.js"></script> -->
</html>
Loading

0 comments on commit 6cae8c1

Please sign in to comment.