-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_test.go
112 lines (96 loc) · 2.23 KB
/
run_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bytes"
"os"
"testing"
"github.com/greensea/so/common"
"github.com/greensea/so/sys"
)
func TestExtractCodeBlocks1(t *testing.T) {
raw := "```\nfoo\n```\n```\nbar\n```"
cmds, err := extractCodeBlocks([]byte(raw))
if err != nil {
t.Fatalf("Error: %v", err)
}
if cmds[0] != "foo" {
t.Fatalf("Expected foo, got %s", cmds[0])
}
if cmds[1] != "bar" {
t.Fatalf("Expected bar, got %s", cmds[1])
}
}
func TestExtractCodeBlocks2(t *testing.T) {
raw := "```bash\nfoo\n```\n```bash\nbar\n```"
cmds, err := extractCodeBlocks([]byte(raw))
if err != nil {
t.Fatalf("Error: %v", err)
}
if cmds[0] != "foo" {
t.Fatalf("Expected foo, got %s", cmds[0])
}
if cmds[1] != "bar" {
t.Fatalf("Expected bar, got %s", cmds[1])
}
}
func TestExtractCodeBlocks3(t *testing.T) {
raw := "```bash\nfoo\nbar\n```\n```bash\nfoobar\n```"
cmds, err := extractCodeBlocks([]byte(raw))
if err != nil {
t.Fatalf("Error: %v", err)
}
if cmds[0] != "foo\nbar" {
t.Fatalf("Expected foo\nbar, got %s", cmds[0])
}
}
func TestLang(t *testing.T) {
os.Setenv("LANG", "zh")
if common.Lang() != "zh" {
t.Fatalf("Expected zh, got %s", common.Lang())
}
}
func TestExecCmd(t *testing.T) {
s := sys.Get()
if s.Shell == "" {
s.Shell = "sh"
}
input := "for i in {1..2}; do echo $i; done"
buf := new(bytes.Buffer)
err := ExecCmd(input, buf, nil)
if err != nil {
t.Fatalf("Command failed: %v", err)
}
if buf.String() != "1\n2\n" {
t.Fatalf("Expected 1\n2\n, got %s", buf.String())
}
}
func TestExecCmd2(t *testing.T) {
s := sys.Get()
if s.Shell == "" {
s.Shell = "sh"
}
input := "for i in {1..2}; do echo $i; done | wc -l"
buf := new(bytes.Buffer)
err := ExecCmd(input, buf, nil)
if err != nil {
t.Fatalf("Command failed: %v", err)
}
if buf.String() != "2\n" {
t.Fatalf("Expected `2\n', got `%s'", buf.String())
}
}
func TestExecCmd3(t *testing.T) {
s := sys.Get()
if s.Shell == "" {
s.Shell = "sh"
}
input := `find /not_exist -type f -exec ls {} \; | wc -l`
// input := `find . -type f -mtime +10 -print | wc -l`
buf := new(bytes.Buffer)
err := ExecCmd(input, buf, nil)
if err != nil {
t.Fatalf("Command failed: %v", err)
}
if buf.String() != "0\n" {
t.Fatalf("Expected `0\n', got `%s'", buf.String())
}
}