-
Notifications
You must be signed in to change notification settings - Fork 42
/
test.go
181 lines (148 loc) · 3.58 KB
/
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/pkg/errors"
)
// TODO: move to tj/ as a separate program?
// TODO: prefix output
// TODO: terse output
func main() {
println()
defer println()
dir := flag.String("dir", "", "Directory to test.")
oss := flag.Bool("oss", true, "Test oss examples.")
pro := flag.Bool("pro", false, "Test pro examples.")
flag.Parse()
log.SetHandler(cli.Default)
if *dir != "" {
if err := testExample(*dir); err != nil {
log.Fatalf("error: %s", err)
}
return
}
if *oss {
if err := testExamples("oss"); err != nil {
log.Fatalf("error: %s", err)
}
}
if *pro {
if err := testExamples("pro"); err != nil {
log.Fatalf("error: %s", err)
}
}
}
func testExamples(dir string) error {
files, err := ioutil.ReadDir(dir)
if err != nil {
return errors.Wrap(err, "reading dir")
}
for _, f := range files {
if !f.IsDir() {
continue
}
root := filepath.Join(dir, f.Name())
if err := testExample(root); err != nil {
return err
}
}
return nil
}
func testExample(dir string) error {
test := filepath.Join(dir, "test.sh")
_, err := os.Stat(test)
if err != nil {
return nil
}
start := time.Now()
fmt.Printf("\033[;1m==> %s\033[0m\n", filepath.Base(dir))
defer func() {
fmt.Printf("-->\n")
fmt.Printf("\033[;1m==> ok (%s)\033[0m\n\n", time.Since(start))
}()
f, err := os.Open(test)
if err != nil {
return errors.Wrap(err, "opening")
}
defer f.Close()
if err := os.Chdir(dir); err != nil {
return errors.Wrap(err, "chdir")
}
scan := bufio.NewScanner(f)
var stdout bytes.Buffer
var stderr bytes.Buffer
for scan.Scan() {
line := scan.Text()
if isComment(line) {
parts := strings.SplitN(strings.Trim(line, " \t#"), ":", 2)
expect := strings.TrimSpace(parts[1])
actual := strings.TrimSpace(stdout.String())
directive(parts[0], actual, expect)
continue
}
stdout.Reset()
stderr.Reset()
fmt.Printf("--> %s \n", line)
cmd := exec.Command("sh", "-c", line)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// cmd.Stdout = io.MultiWriter(&stdout, os.Stdout)
// cmd.Stderr = io.MultiWriter(&stderr, os.Stderr)
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, stderr.String())
}
}
if err := scan.Err(); err != nil {
return errors.Wrap(err, "scan")
}
if err := os.Chdir(filepath.Join("..", "..")); err != nil {
return errors.Wrap(err, "chdir")
}
return nil
}
func directive(name string, actual, expect string) {
if name == "equals" {
fmt.Printf("--> test equals %q\n", expect)
if actual != expect {
fmt.Printf("\n")
fmt.Printf("\033[31m Expected to equal:\n\n %q\033[0m\n", expect)
fmt.Printf("\n\033[31m Output:\n\n %q\033[0m\n", actual)
}
return
}
if name == "contains" {
fmt.Printf("--> test contains %q\n", expect)
if !strings.Contains(actual, expect) {
fmt.Printf("\n")
fmt.Printf("\033[31m Expected to contain:\n\n %s\033[0m\n", expect)
fmt.Printf("\n\033[31m Output:\n\n %s\033[0m\n", actual)
}
return
}
if name == "not contains" {
fmt.Printf("--> test not contains %q\n", expect)
if strings.Contains(actual, expect) {
fmt.Printf("\n")
fmt.Printf("\033[31m Expected to not contain:\n\n %s\033[0m\n", expect)
fmt.Printf("\n\033[31m Output:\n\n %s\033[0m\n", actual)
}
return
}
panic(fmt.Sprintf("unsupported directive %q", name))
}
func isCommand(s string) bool {
return !isComment(s)
}
func isComment(s string) bool {
return strings.HasPrefix(strings.TrimSpace(s), "#")
}