-
Notifications
You must be signed in to change notification settings - Fork 19
/
diff.go
125 lines (113 loc) · 2.5 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.IsNil() {
return "null", true
}
if rv.Type().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)
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
}