-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcommon_test.go
116 lines (106 loc) · 2.35 KB
/
common_test.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
package medtronic
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
)
// Force timezone to match test data.
func init() {
os.Setenv("TZ", "America/New_York")
}
func readBytes(r io.Reader) ([]byte, error) {
var data []byte
for {
var b byte
n, err := fmt.Fscanf(r, "%02x", &b)
if n == 0 {
break
}
if err != nil {
return data, err
}
data = append(data, b)
}
return data, nil
}
func parseBytes(hex string) []byte {
var data []byte
r := strings.NewReader(hex)
data, err := readBytes(r)
if err != nil {
panic(err)
}
return data
}
var layouts = []string{
"2006-01-02T15:04:05.999999999",
"2006-01-02T15:04",
}
func parseTime(s string) time.Time {
var t time.Time
var err error
for _, layout := range layouts {
t, err = time.ParseInLocation(layout, s, time.Local)
if err == nil {
return t
}
}
panic(err)
}
func parseTD(s string) TimeOfDay {
t, err := ParseTimeOfDay(s)
if err != nil {
panic(err)
}
return t
}
func compareDataToJSON(data interface{}, jsonFile string) (bool, string) {
// Write data in JSON format to temporary file.
tmpfile, err := ioutil.TempFile("", "json")
if err != nil {
return false, err.Error()
}
defer os.Remove(tmpfile.Name())
e := json.NewEncoder(tmpfile)
e.SetIndent("", " ")
err = e.Encode(data)
tmpfile.Close()
if err != nil {
return false, err.Error()
}
return diffJSON(jsonFile, tmpfile.Name())
}
func diffJSON(file1, file2 string) (bool, string) {
// Write JSON in canonical form for comparison.
canon1 := canonicalJSON(file1)
defer os.Remove(canon1)
canon2 := canonicalJSON(file2)
defer os.Remove(canon2)
// Find differences.
cmd := exec.Command("diff", "-u", "--label", file1, "--label", file2, canon1, canon2)
diffs, err := cmd.Output()
return err == nil, string(diffs)
}
// canonicalJSON reads the given file and creates a temporary file
// containing equivalent JSON in canonical form
// (using the "jq" command, which must be on the user's PATH).
// It returns the temporary file name; it is the caller's responsibility
// to remove it when done.
func canonicalJSON(file string) string {
canon, err := exec.Command("jq", "-S", ".", file).Output()
if err != nil {
panic(fmt.Sprintf("%s: %v", file, err))
}
tmpfile, err := ioutil.TempFile("", "json")
if err != nil {
panic(err)
}
tmpfile.Write(canon)
tmpfile.Close()
return tmpfile.Name()
}