-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
81 lines (66 loc) · 2.04 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
80
81
package main
import (
"bytes"
"flag"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"testing"
)
var update = flag.Bool("update", false, "update test files with results")
func TestCLI_New(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
requestBody, _ := ioutil.ReadAll(req.Body)
assert.Equal(t, `{"content":"测试内容"}`, string(requestBody[:]))
rw.Write([]byte(`{"code":0,"message":"记录成功"}`))
}))
defer server.Close()
if err := exec.Command("go", "run", ".", "new", "--api", server.URL, "测试内容").Run(); err != nil {
log.Fatal(err)
}
}
func TestCLI_New_WithTag(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
requestBody, _ := ioutil.ReadAll(req.Body)
assert.Equal(t, `{"content":"测试内容\n\n#test"}`, string(requestBody[:]))
rw.Write([]byte(`{"code":0,"message":"记录成功"}`))
}))
defer server.Close()
if err := exec.Command("go", "run", ".", "new", "--api", server.URL, "--tag", "test", "测试内容").Run(); err != nil {
log.Fatal(err)
}
}
func TestCLI_New_Pipe(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
requestBody, _ := ioutil.ReadAll(req.Body)
assert.Equal(t, `{"content":"测试内容"}`, string(requestBody[:]))
rw.Write([]byte(`{"code":0,"message":"记录成功"}`))
}))
defer server.Close()
command1 := exec.Command("echo", "测试内容")
command2 := exec.Command("go", "run", ".", "new", "--api", server.URL)
r, w := io.Pipe()
command1.Stdout = w
command2.Stdin = r
var command2Buffer bytes.Buffer
command2.Stdout = &command2Buffer
logFatal(command1.Start())
logFatal(command2.Start())
logFatal(command1.Wait())
logFatal(w.Close())
logFatal(command2.Wait())
logFatal(func() error {
_, err := io.Copy(os.Stdout, &command2Buffer)
return err
}())
}
func logFatal(err error) {
if err != nil {
log.Fatal(err)
}
}