-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
276 lines (234 loc) · 4.87 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
type checkFunc func(id int, cmd string, t *testing.T)
var (
pageLayout = []byte(`<html lang="en">
<head>
<title>{{.Page.Title}} - {{.Site.Title}}</title>
</head>
<body>{{.Page.Body}}</body>
</html>`)
postLayout = []byte(`<html lang="en">
<head>
<title>{{.Post.Title}} - {{.Site.Title}}</title>
</head>
<body>{{.Post.Body}}</body>
</html>`)
indexLayout = []byte(`<html lang="en">
<head>
<title>{{.Site.Title}}</title>
</head>
<body>
{{range $i, $p := .Posts}}
<strong>{{$p.Title}}</strong>
<div>{{$p.Description}}</div>
{{end}}
</body>
</html>`)
categoryIndexLayout = []byte(`<html lang="en">
<head>
<title>{{.Site.Title}} - {{.Category.Name}}</title>
</head>
<body>
{{range $i, $p := .Posts}}
<strong>{{$p.Title}}</strong>
<div>{{$p.Description}}</div>
{{end}}
</body>
</html>`)
)
func cleanup(tmpdir string) {
for _, dir := range dirs {
os.RemoveAll(dir)
}
os.Remove("jrnl.toml")
os.RemoveAll(tmpdir)
}
func checkInitDirs(id int, cmd string, t *testing.T) {
for _, dir := range dirs {
if _, err := os.Stat(dir); err != nil {
t.Fatalf("tests[%d](%s)) - check failed, could not stat dir %s: %s\n", id, cmd, dir, err)
}
}
page, err := os.Create(filepath.Join(layoutsDir, "page"))
if err != nil {
t.Fatal(err)
}
defer page.Close()
if _, err := page.Write(pageLayout); err != nil {
t.Fatal(err)
}
post, err := os.Create(filepath.Join(layoutsDir, "post"))
if err != nil {
t.Fatal(err)
}
defer post.Close()
if _, err := post.Write(postLayout); err != nil {
t.Fatal(err)
}
index, err := os.Create(filepath.Join(layoutsDir, "index"))
if err != nil {
t.Fatal(err)
}
if _, err := index.Write(indexLayout); err != nil {
t.Fatal(err)
}
catindex, err := os.Create(filepath.Join(layoutsDir, "category-index"))
if err != nil {
t.Fatal(err)
}
if _, err := catindex.Write(categoryIndexLayout); err != nil {
t.Fatal(err)
}
}
func checkPublished(paths ...string) checkFunc {
return func(id int, cmd string, t *testing.T) {
for _, path := range paths {
if _, err := os.Stat(filepath.Join(siteDir, path)); err != nil {
t.Fatalf("tests[%d](%s) - failed to stat %q: %s\n", id, cmd, path, err)
}
}
}
}
func checkPublishedRemote(remote string, paths ...string) checkFunc {
return func(id int, cmd string, t *testing.T) {
for _, path := range paths {
if _, err := os.Stat(filepath.Join(remote, path)); err != nil {
t.Fatalf("tests[%d](%s) - failed to stat %q: %s\n", id, cmd, path, err)
}
}
}
}
func splitargs(argv string) []string {
args := make([]string, 0)
n := 0
off := 0
quote := false
end := len(argv) - 1
for i, r := range argv {
if r == '\'' {
quote = !quote
off = 1
}
if r == ' ' || i == end {
if i == end {
i++
}
if !quote {
args = append(args, argv[n+off:i-off])
n = i + 1
off = 0
}
}
}
return args
}
func Test_Cmd(t *testing.T) {
cmd := os.Getenv("TEST_CMD")
if cmd == "" {
t.Skip("TEST_CMD not set, skipping...")
}
if err := run(splitargs(cmd)); err != nil {
t.Fatalf("failed to run cmd %q: %s\n", cmd, err)
}
}
func Test_Jrnl(t *testing.T) {
if err := os.Chdir("testdata"); err != nil {
t.Fatal(err)
}
dir, err := ioutil.TempDir("", "jrnl-remote-*")
if err != nil {
t.Fatal(err)
}
defer cleanup(dir)
now := time.Now()
date := strings.Replace(now.Format("2006-01-02"), "-", string(os.PathSeparator), -1)
tests := []struct {
cmd string
shouldErr bool
check checkFunc
}{
{
"jrnl init",
false,
checkInitDirs,
},
{
"jrnl page -l page about",
false,
nil,
},
{
"jrnl post -l post 'First Post'",
false,
nil,
},
{
"jrnl post -l post -c Programming 'Go 101'",
false,
nil,
},
{
"jrnl post -l post 'Second Post'",
false,
nil,
},
{
"jrnl config site.title 'My blog'",
false,
nil,
},
{
"jrnl config site.remote " + dir,
false,
nil,
},
{
"jrnl publish",
false,
checkPublishedRemote(
dir,
filepath.Join(date, "first-post", "index.html"),
filepath.Join("programming", date, "go-101", "index.html"),
filepath.Join(date, "second-post", "index.html"),
),
},
{
"jrnl rm second-post",
false,
nil,
},
{
"jrnl publish",
false,
checkPublishedRemote(
dir,
filepath.Join(date, "first-post", "index.html"),
filepath.Join("programming", date, "go-101", "index.html"),
),
},
}
os.Setenv("EDITOR", "true")
for i, test := range tests {
cmd := exec.Command(os.Args[0], "-test.run=Test_Cmd")
cmd.Env = append(os.Environ(), "TEST_CMD="+test.cmd)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if !test.shouldErr {
t.Fatalf("tests[%d](%s) - Failed to run test: %s\n", i, test.cmd, err)
}
}
if test.check != nil {
test.check(i, test.cmd, t)
}
}
}