-
Notifications
You must be signed in to change notification settings - Fork 26
/
testOne.go
89 lines (75 loc) · 1.99 KB
/
testOne.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
package mandoscontroller
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
mj "github.com/ElrondNetwork/arwen-wasm-vm/mandos-go/json/model"
mjwrite "github.com/ElrondNetwork/arwen-wasm-vm/mandos-go/json/write"
)
// RunSingleJSONTest parses and prepares test, then calls testCallback.
func (r *TestRunner) RunSingleJSONTest(contextPath string) error {
var err error
contextPath, err = filepath.Abs(contextPath)
if err != nil {
return err
}
// Open our jsonFile
var jsonFile *os.File
jsonFile, err = os.Open(contextPath)
// if we os.Open returns an error then handle it
if err != nil {
return err
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return err
}
r.Parser.ValueInterpreter.FileResolver.SetContext(contextPath)
top, parseErr := r.Parser.ParseTestFile(byteValue)
if parseErr != nil {
return parseErr
}
for _, test := range top {
testErr := r.Executor.ExecuteTest(test)
if testErr != nil {
return testErr
}
}
return nil
}
// tool to convert .test.json -> .scen.json
// use with extreme caution
func convertTestToScenario(contextPath string, top []*mj.Test) {
if strings.HasSuffix(contextPath, ".test.json") {
scenario, err := mj.ConvertTestToScenario(top)
if err != nil {
panic(err)
}
scenarioSerialized := mjwrite.ScenarioToJSONString(scenario)
newPath := contextPath[:len(contextPath)-len(".test.json")] + ".scen.json"
err = os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(newPath, []byte(scenarioSerialized), 0644)
if err != nil {
panic(err)
}
}
}
// tool to modify tests
// use with extreme caution
func saveModifiedTest(toPath string, top []*mj.Test) {
resultJSON := mjwrite.TestToJSONString(top)
err := os.MkdirAll(filepath.Dir(toPath), os.ModePerm)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(toPath, []byte(resultJSON), 0644)
if err != nil {
panic(err)
}
}