/
end_to_end_test.go
58 lines (46 loc) · 1.14 KB
/
end_to_end_test.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
package apidoc2proto
import (
"bufio"
"os"
"path/filepath"
"testing"
"fmt"
"github.com/emicklei/proto"
"github.com/jamesk/apidoc-proto/apidoc"
"github.com/jamesk/apidoc-proto/aproto"
)
const (
testFolder = "files_for_test"
testSpecExtension = ".json"
testProtoExtension = ".proto"
testSpecGlob = "*" + testSpecExtension
)
func TestEndToEnd(t *testing.T) {
fileNames, err := filepath.Glob(filepath.Join(testFolder, testSpecGlob))
if err != nil {
t.Fatalf("Error getting test fileNames directory: %v", err)
}
for _, fileName := range fileNames {
//test the file
aSpec, err := apidoc.GetSpecFromFile(fileName)
if err != nil {
t.Error(err)
}
pFile := aproto.GetProtoFromAPISpec(aSpec)
protoFileName := fmt.Sprintf("%v%v", fileName[:len(fileName)-len(testSpecExtension)], testProtoExtension)
err = outputProtoFile(protoFileName, pFile)
if err != nil {
t.Error(err)
}
}
}
func outputProtoFile(outputPath string, pFile proto.Proto) error {
f, err := os.Create(outputPath)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
proto.NewFormatter(w, " ").Format(&pFile)
return w.Flush()
}