-
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.
- Loading branch information
Showing
38 changed files
with
1,601 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Go Next | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
|
||
tests-with-go-next: | ||
strategy: | ||
matrix: | ||
os: [ ubuntu-latest] | ||
go: [ '1.23rc1' ] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
run: | | ||
curl -fsSL -o go.tar.gz "https://go.dev/dl/go${{matrix.go}}.linux-amd64.tar.gz" | ||
mkdir setup | ||
tar -C setup -xzf go.tar.gz | ||
ls setup | ||
GOROOT=$PWD/setup/go PATH=$PWD/setup/go/bin:$PATH go version | ||
- name: Test | ||
run: GOROOT=$PWD/setup/go PATH=$PWD/setup/go/bin:$PATH go run ./script/run-test --reset-instrument --debug -v |
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
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,124 @@ | ||
package assert | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"reflect" | ||
|
||
"github.com/xhd2015/xgo/support/cmd" | ||
"github.com/xhd2015/xgo/support/fileutil" | ||
) | ||
|
||
// Diff compares two values, resulting in a human readable diff format. | ||
func Diff(expected interface{}, actual interface{}) string { | ||
if res, ok := tryDiffError(expected, actual); ok { | ||
return res | ||
} | ||
// check if any is error type | ||
expectedTxt, err := toDiffableText(expected) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
actualTxt, err := toDiffableText(actual) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
diff, err := diffText([]byte(expectedTxt), []byte(actualTxt)) | ||
if err != nil { | ||
sep := "" | ||
if diff != "" { | ||
sep = ", " | ||
} | ||
diff += sep + "err: " + err.Error() | ||
} | ||
return diff | ||
} | ||
|
||
func toDiffableText(v interface{}) (string, error) { | ||
if v == nil { | ||
return "nil", nil | ||
} | ||
if v, ok := toPlainString(v); ok { | ||
return tryPrettyJSONText(v) | ||
} | ||
text, err := prettyObj(v) | ||
if err != nil { | ||
return "", fmt.Errorf("marshal %T: %v", v, err) | ||
} | ||
return string(text), nil | ||
} | ||
|
||
func prettyObj(v interface{}) ([]byte, error) { | ||
return json.MarshalIndent(v, "", " ") | ||
} | ||
|
||
func toPlainString(v interface{}) (string, bool) { | ||
switch v := v.(type) { | ||
case string: | ||
return v, true | ||
case []byte: | ||
return string(v), true | ||
case json.RawMessage: | ||
return string(v), true | ||
} | ||
|
||
// slow | ||
rv := reflect.ValueOf(v) | ||
switch rv.Kind() { | ||
case reflect.String: | ||
return rv.String(), true | ||
case reflect.Slice: | ||
if rv.Elem().Kind() == reflect.Uint8 { | ||
// []byte | ||
return string(rv.Bytes()), true | ||
} | ||
} | ||
return "", false | ||
} | ||
|
||
func tryPrettyJSONText(s string) (string, error) { | ||
if s == "" || s == "null" { | ||
return s, nil | ||
} | ||
var v interface{} | ||
err := json.Unmarshal([]byte(s), &v) | ||
if err != nil { | ||
return s, nil | ||
} | ||
text, err := json.MarshalIndent(v, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(text), nil | ||
} | ||
|
||
func diffText(expected []byte, actual []byte) (string, error) { | ||
tmpDir, err := os.MkdirTemp("", "diff") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
// var jsonExpected []byte | ||
// var jsonActual []byte | ||
|
||
err = fileutil.WriteFile(filepath.Join(tmpDir, "expected"), expected) | ||
if err != nil { | ||
return "", err | ||
} | ||
err = fileutil.WriteFile(filepath.Join(tmpDir, "actual"), actual) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
diff, err := cmd.Dir(tmpDir).Output("git", "diff", "--no-index", "--no-color", "--", "expected", "actual") | ||
if err != nil { | ||
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() != 0 { | ||
err = nil | ||
} | ||
} | ||
return diff, err | ||
} |
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,44 @@ | ||
package assert | ||
|
||
import "fmt" | ||
|
||
func tryDiffError(expected interface{}, actual interface{}) (string, bool) { | ||
expectedErr, expectedOK := expected.(error) | ||
actualErr, actualOK := actual.(error) | ||
if expectedOK != actualOK { | ||
if expectedOK { | ||
return fmt.Sprintf("expect: error %s, actual: %T %s", expectedErr.Error(), actual, actual), true | ||
} | ||
return fmt.Sprintf("expect: %T %s, actual: error %s", expectedErr, expectedErr, actualErr.Error()), true | ||
} | ||
if !expectedOK { | ||
return "", false | ||
} | ||
res := diffError(expectedErr, actualErr) | ||
if res == nil { | ||
return "", true | ||
} | ||
return res.Error(), true | ||
} | ||
|
||
func diffError(expected error, actual error) error { | ||
if expected == actual { | ||
return nil | ||
} | ||
if expected == nil { | ||
if actual != nil { | ||
return fmt.Errorf("expect no error, actual: %v", actual) | ||
} | ||
return nil | ||
} | ||
if actual == nil { | ||
return fmt.Errorf("expect error: %v, actual nil", expected) | ||
} | ||
|
||
e := expected.Error() | ||
a := actual.Error() | ||
if e != a { | ||
return fmt.Errorf("expect err: %v, actual: %v", e, a) | ||
} | ||
return nil | ||
} |
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
Oops, something went wrong.