forked from matthewmueller/joy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_test.go
188 lines (157 loc) · 4.13 KB
/
all_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
182
183
184
185
186
187
188
package joy_test
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/matthewmueller/joy/api/build"
"github.com/matthewmueller/joy/internal/paths"
"github.com/sanity-io/litter"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/apex/log"
"github.com/apex/log/handlers/text"
"github.com/matthewmueller/joy/internal/chrome"
"github.com/pkg/errors"
)
// little invisibles helper
func invisibles(str string) string {
str = strings.Replace(str, " ", "·", -1)
str = strings.Replace(str, "\r", "¬", -1)
str = strings.Replace(str, "\n", "¬", -1)
str = strings.Replace(str, "\t", "‣", -1)
return str
}
func Test(t *testing.T) {
ctx := context.Background()
log.SetHandler(text.New(os.Stderr))
dirs, err := ioutil.ReadDir("./testdata")
if err != nil {
t.Fatal(err)
}
cwd, e := os.Getwd()
if e != nil {
t.Fatal(e)
}
chromePath, err := paths.Chrome()
if err != nil {
t.Fatal(err)
}
ch, err := chrome.Start(ctx, chromePath)
if err != nil {
t.Fatal(err)
}
defer ch.Close()
gosrc := path.Join(os.Getenv("GOPATH"), "src")
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
// subtests
t.Run(dir.Name(), func(t *testing.T) {
if strings.HasPrefix(dir.Name(), "_") {
log.Warnf("Skipping '%s'", dir.Name())
return
}
testdir := path.Join(cwd, "testdata", dir.Name())
var inputs []string
// support single and multipage tests
inps, e := filepath.Glob(path.Join(testdir, "input.go"))
if e != nil {
t.Fatal(e)
}
inputs = append(inputs, inps...)
inps, e = filepath.Glob(path.Join(testdir, "*", "input.go"))
if e != nil {
t.Fatal(e)
}
inputs = append(inputs, inps...)
var pages []string
for _, input := range inputs {
pages = append(pages, path.Dir(input))
}
// compile the file
scripts, err := build.Build(&build.Config{
Context: ctx,
Packages: pages,
})
if err != nil {
t.Fatal(errors.Wrap(err, "compile error"))
}
if len(scripts) == 0 {
t.Fatal("expected atleast 1 script to be built")
}
for _, script := range scripts {
jspath := path.Join(gosrc, script.Name(), "expected.js.txt")
resultpath := path.Join(gosrc, script.Name(), "expected.txt")
// read the expected js source
js, err := ioutil.ReadFile(jspath)
if err != nil {
t.Fatalf("no '%s' file found", jspath)
}
// compile the code
if script.Source() != string(js) {
if err := ioutil.WriteFile(jspath, []byte(script.Source()), 0755); err != nil {
t.Fatal(err)
}
t.Fatal(formatted(string(js), script.Source()))
}
// try reading the result path
if _, err := os.Stat(resultpath); os.IsNotExist(err) {
log.Warnf("'%s' doesn't exist, skipping node execution", resultpath)
return
}
// create a target
tar, err := ch.Target()
if err != nil {
t.Fatal(err)
}
// run the code in a headless chrome target
actual, err := tar.Run(script.Source())
if err != nil {
t.Fatal(err)
}
expected, e := ioutil.ReadFile(resultpath)
if e != nil {
t.Fatalf("no '%s' file found", resultpath)
}
// compare the result path
if strings.TrimSpace(string(actual)) != strings.TrimSpace(string(expected)) {
t.Fatal(fmt.Sprintf("\n## Expected ##\n\n%s\n\n## Actual ##\n\n%s", string(expected), string(actual)))
}
if e := tar.Close(); e != nil {
t.Fatal(e)
}
}
})
}
}
func formatted(expected, actual interface{}) string {
e := litter.Sdump(expected)
a := litter.Sdump(actual)
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(e, a, false)
var buf bytes.Buffer
for _, diff := range diffs {
switch diff.Type {
case diffmatchpatch.DiffInsert:
buf.WriteString("\x1b[102m\x1b[30m")
buf.WriteString(diff.Text)
buf.WriteString("\x1b[0m")
case diffmatchpatch.DiffDelete:
buf.WriteString("\x1b[101m\x1b[30m")
buf.WriteString(diff.Text)
buf.WriteString("\x1b[0m")
case diffmatchpatch.DiffEqual:
buf.WriteString(diff.Text)
}
}
result := buf.String()
result = strings.Replace(result, "\\n", "\n", -1)
result = strings.Replace(result, "\\t", "\t", -1)
return result
}