-
Notifications
You must be signed in to change notification settings - Fork 22
/
helpers.go
119 lines (105 loc) · 3.27 KB
/
helpers.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
package comparable
import (
"encoding/json"
"fmt"
spec2 "github.com/attestantio/go-eth2-client/spec"
"github.com/bloxapp/ssv-spec/types"
ssz "github.com/ferranbt/fastssz"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"strings"
"testing"
)
func NoErrorEncoding(obj ssz.Marshaler) []byte {
ret, err := obj.MarshalSSZ()
if err != nil {
panic(err.Error())
}
return ret
}
// FixIssue178 fixes consensus data fields which are nil instead of empty slice
// If we change the fields in ssv_msgs.go it will break a lot of roots, we're slowly fixing them
// SHOULD BE REMOVED once all tests are fixes
// see https://github.com/bloxapp/ssv-spec/issues/178
func FixIssue178(input *types.ConsensusData, version spec2.DataVersion) *types.ConsensusData {
byts, err := input.Encode()
if err != nil {
panic(err.Error())
}
ret := &types.ConsensusData{}
if err := ret.Decode(byts); err != nil {
panic(err.Error())
}
ret.Version = version
return ret
}
// UnmarshalStateComparison reads a json derived from 'testName' and unmarshals it into 'targetState'
func UnmarshalStateComparison[T types.Root](basedir string, testName string, testType string, targetState T) (T,
error) {
var nilT T
basedir = filepath.Join(basedir, "generate")
scDir := GetSCDir(basedir, testType)
path := filepath.Join(scDir, fmt.Sprintf("%s.json", testName))
byteValue, err := os.ReadFile(path)
if err != nil {
return nilT, err
}
err = json.Unmarshal(byteValue, targetState)
if err != nil {
return nilT, err
}
return targetState, nil
}
// readStateComparison reads a json derived from 'testName' and unmarshals it into a json object
func readStateComparison(basedir string, testName string, testType string) (map[string]interface{}, error) {
basedir = filepath.Join(basedir, "generate")
scDir := GetSCDir(basedir, testType)
path := filepath.Join(scDir, fmt.Sprintf("%s.json", testName))
byteValue, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(byteValue, &result)
if err != nil {
return nil, err
}
return result, nil
}
func GetExpectedStateFromScFile(testName string, testType string) (map[string]interface{}, error) {
basedir, err := os.Getwd()
if err != nil {
return nil, err
}
expectedState, err := readStateComparison(basedir, testName, testType)
if err != nil {
return nil, err
}
return expectedState, nil
}
// GetSCDir returns the path to the state comparison folder for the given test type
func GetSCDir(basedir string, testType string) string {
testType = strings.NewReplacer(
"*", "",
".", "_").
Replace(testType)
return filepath.Join(basedir, "state_comparison", testType)
}
// CompareWithJson compares the given test with the expected state from the state comparison folder
func CompareWithJson(t *testing.T, test any, testName string, testType string) {
// marshal test into json
byts, err := json.Marshal(test)
require.NoError(t, err)
//unmarshal json into map
var testMap map[string]interface{}
err = json.Unmarshal(byts, &testMap)
require.NoError(t, err)
expectedTestMap, err := GetExpectedStateFromScFile(testName, testType)
require.NoError(t, err)
diff := cmp.Diff(testMap, expectedTestMap)
if diff != "" {
t.Errorf("%s inputs changed. %v", testName, diff)
}
}