-
Notifications
You must be signed in to change notification settings - Fork 1
/
add.go
99 lines (87 loc) · 1.61 KB
/
add.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
package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/urfave/cli/v2"
)
// Add test case file
func Add(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Println("Please specify test case name")
cli.ShowCommandHelpAndExit(c, "add", 1)
return nil
}
targetName, outPath := extractTarget(c.Args().Get(0))
protoRoot := c.String("proto_path")
if protoRoot == "" {
protoRoot = "./"
}
protoFiles := c.StringSlice("protofiles")
_, err := os.Stat(outPath)
if err == nil {
fmt.Printf("%s is already exists", outPath)
return nil
}
cueImports, err := generateCUEModule(protoRoot, protoFiles)
if err != nil {
if err.Error() == "no protofiles" {
fmt.Println("No protofiles. Will not generate schemas.")
} else {
return err
}
}
tpl := template.New("schema")
tpl.Parse(testCaseSchema)
m := map[string]interface{}{
"Name": targetName,
"Imports": cueImports,
}
var base bytes.Buffer
_ = tpl.Execute(&base, m)
err = os.MkdirAll(filepath.Dir(outPath), 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(outPath, base.Bytes(), 0644)
if err != nil {
return err
}
fmt.Println("create:", outPath)
return nil
}
const testCaseSchema = `{{range .Imports}}import "{{.}}"
{{end}}
name: "{{.Name}}"
Input: {}
Output: {}
TestCase :: {
input: Input
output: Output
}
Test :: {
name: string
method: string
proto?: [...string]
import_path?: [...string]
headers?: [string]: string
input?: Input
output?: Output
tests?: [...TestCase]
}
cases: [...Test] & [
{
name: ""
method: ""
tests: [
{
input: {}
output: {}
}
]
},
]
`