-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain_test.go
79 lines (75 loc) · 1.66 KB
/
main_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"testing"
"os/exec"
"sync"
"os"
"bytes"
"fmt"
)
func TestCmdHelp(ot *testing.T){
buildCmdOnce()
cmd:=exec.Command("bin/yaml2json","--help")
outBuf:=&bytes.Buffer{}
cmd.Stdout = outBuf
err:=cmd.Run()
ok(err!=nil)
ok(err.Error()=="exit status 1")
ok(outBuf.String()==helpInfo,outBuf.String())
}
func TestCmdVersion(ot *testing.T){
// fix https://github.com/bronze1man/yaml2json/issues/16
buildCmdOnce()
cmd:=exec.Command("bin/yaml2json","--version")
outBuf:=&bytes.Buffer{}
cmd.Stdout = outBuf
err:=cmd.Run()
ok(err!=nil)
ok(err.Error()=="exit status 1")
ok(outBuf.String()==Version+"\n",outBuf.String())
}
func TestCmdExampleInReadme(ot *testing.T){
buildCmdOnce()
cmd:=exec.Command("bin/yaml2json")
cmd.Stdin = bytes.NewReader([]byte("a: 1"))
outBuf:=&bytes.Buffer{}
cmd.Stdout = outBuf
err:=cmd.Run()
if err!=nil{
panic(err)
}
ok(outBuf.String()==`{"a":1}`+"\n",outBuf.String())
}
func TestCmdErrHandle(ot *testing.T){
buildCmdOnce()
cmd:=exec.Command("bin/yaml2json")
cmd.Stdin = bytes.NewReader([]byte(`a :::: asfe234""""
sadfq23
`))
errBuf:=&bytes.Buffer{}
cmd.Stderr = errBuf
outBuf:=&bytes.Buffer{}
cmd.Stdout = outBuf
err:=cmd.Run()
ok(err!=nil)
ok(err.Error()=="exit status 2")
ok(outBuf.Len()==0)
ok(errBuf.String()==`yaml: line 2: could not find expected ':'`+"\n",errBuf.String())
}
var gBuildOnce sync.Once
func buildCmdOnce(){
gBuildOnce.Do(func(){
cmd:=exec.Command("go","build","-o","bin/yaml2json","./")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err:=cmd.Run()
if err!=nil{
panic(err)
}
})
}
func ok(b bool,objList ...interface{}){
if b==false{
panic(`fail `+fmt.Sprintln(objList...))
}
}