-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
125 lines (104 loc) · 2.89 KB
/
main.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 main
import (
"encoding/json"
"fmt"
"github.com/bloxapp/ssv-spec/qbft/spectest/tests"
comparable2 "github.com/bloxapp/ssv-spec/types/testingutils/comparable"
"github.com/pkg/errors"
"log"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/bloxapp/ssv-spec/qbft/spectest"
)
//go:generate go run main.go
func main() {
clearStateComparisonFolder()
all := map[string]tests.SpecTest{}
for _, testF := range spectest.AllTests {
test := testF()
// write json test
n := reflect.TypeOf(test).String() + "_" + test.TestName()
if all[n] != nil {
panic(fmt.Sprintf("duplicate test: %s\n", n))
}
all[n] = test
}
byts, err := json.Marshal(all)
if err != nil {
panic(err.Error())
}
if len(all) != len(spectest.AllTests) {
panic("did not generate all tests\n")
}
log.Printf("found %d tests\n", len(all))
writeJson(byts)
for _, testF := range spectest.AllTests {
test := testF()
// generate post state comparison
post, err := test.GetPostState()
if err != nil {
panic(errors.Wrapf(err, "failed to get post state for test: %s", test.TestName()).Error())
}
writeJsonStateComparison(test.TestName(), reflect.TypeOf(test).String(), post)
}
}
func clearStateComparisonFolder() {
_, basedir, _, ok := runtime.Caller(0)
if !ok {
panic("no caller info")
}
dir := filepath.Join(strings.TrimSuffix(basedir, "main.go"), "state_comparison")
if err := os.RemoveAll(dir); err != nil {
panic(err.Error())
}
if err := os.Mkdir(dir, 0700); err != nil {
panic(err.Error())
}
}
func writeJsonStateComparison(name, testType string, post interface{}) {
if post == nil { // If nil, test not supporting post state comparison yet
log.Printf("skipping state comparison json, not supported: %s\n", name)
return
}
log.Printf("writing state comparison json: %s\n", name)
byts, err := json.MarshalIndent(post, "", " ")
if err != nil {
panic(err.Error())
}
scDir := scDir(testType)
// try to create directory if it doesn't exist
if err := os.MkdirAll(scDir, 0700); err != nil && !os.IsExist(err) {
panic(err.Error())
}
file := filepath.Join(scDir, fmt.Sprintf("%s.json", name))
log.Printf("writing state comparison json: %s\n", file)
if err := os.WriteFile(file, byts, 0644); err != nil {
panic(err.Error())
}
}
func scDir(testType string) string {
_, basedir, _, ok := runtime.Caller(0)
if !ok {
panic("no caller info")
}
basedir = strings.TrimSuffix(basedir, "main.go")
scDir := comparable2.GetSCDir(basedir, testType)
return scDir
}
func writeJson(data []byte) {
_, basedir, _, ok := runtime.Caller(0)
if !ok {
panic("no caller info")
}
basedir = strings.TrimSuffix(basedir, "main.go")
// try to create directory if it doesn't exist
_ = os.Mkdir(basedir, os.ModeDir)
file := filepath.Join(basedir, "tests.json")
log.Printf("writing spec tests json to: %s\n", file)
if err := os.WriteFile(file, data, 0644); err != nil {
panic(err.Error())
}
}