forked from hirokisan/bybit
/
helper.go
55 lines (48 loc) · 1.04 KB
/
helper.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
package testhelper
import (
"encoding/json"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Compare :
func Compare(
t *testing.T,
goldenFilename string,
got []byte,
) {
switch _, err := os.Stat(goldenFilename); err {
case nil:
want, err := os.ReadFile(goldenFilename)
require.NoError(t, err)
assert.JSONEq(t, string(want), string(got))
case os.ErrNotExist:
// noop
default:
t.Log(string(got))
t.Error(fmt.Errorf("unexpected error happened: %w", err))
}
}
// SaveToFile :
func SaveToFile(name string, b []byte) error {
return os.WriteFile(name, b, 0644)
}
// ConvertToJSON :
func ConvertToJSON(src interface{}) []byte {
res, _ := json.MarshalIndent(src, "", " ")
return res
}
// UpdateFile :
func UpdateFile(t *testing.T, filename string, b []byte) {
var updated bool
updatedString, ok := os.LookupEnv("BYBIT_TEST_UPDATED")
if ok && updatedString == "true" {
updated = true
}
if updated {
require.NoError(t, SaveToFile(filename, b))
t.Log("golden file updated")
}
}