forked from iov-one/weave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testgen.go
69 lines (61 loc) · 1.38 KB
/
testgen.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
package commands
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/gogo/protobuf/proto"
"github.com/iov-one/weave/x/sigs"
)
// Example will be written out to a file, .json and .bin
// Filename should have no path and no extension
type Example struct {
Filename string
Obj proto.Message
}
// TestGenCmd generates sample protobuf and json encodings
// of various objects to test against.
func TestGenCmd(examples []Example, args []string) error {
outdir := "testdata"
if len(args) > 0 {
outdir = args[0]
}
err := os.MkdirAll(outdir, 0755)
if err != nil {
return err
}
for _, ex := range examples {
// write json data
js, err := json.Marshal(ex.Obj)
if err != nil {
return err
}
jsFile := filepath.Join(outdir, ex.Filename+".json")
err = ioutil.WriteFile(jsFile, js, 0644)
if err != nil {
return err
}
// write protbuf data
pb, err := proto.Marshal(ex.Obj)
if err != nil {
return err
}
pbFile := filepath.Join(outdir, ex.Filename+".bin")
err = ioutil.WriteFile(pbFile, pb, 0644)
if err != nil {
return err
}
if tx, ok := ex.Obj.(sigs.SignedTx); ok {
signed, err := sigs.BuildSignBytesTx(tx, "test-123", 17)
if err != nil {
return err
}
sigFile := filepath.Join(outdir, ex.Filename+".signbytes")
err = ioutil.WriteFile(sigFile, signed, 0644)
if err != nil {
return err
}
}
}
return nil
}