-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
277 lines (251 loc) · 7.24 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
277
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
const (
emptyArgs = "path is empty. please set it.\n\nUsage: furit [<option>...] <1st path> <2nd path>...\n you can set multiple paths to search invalid images.\n"
promptStr = "Are you sure to delete these unlinked images? [y/n]: "
canceledStr = "the file deletion process has been canceled: failed to read user input: EOF\n"
)
var (
foundFilesArray = []string{
filepath.Join("lib", "test-data", "markdown", "assets", "画面.bmp"),
filepath.Join("lib", "test-data", "markdown", "テスト.gif"),
}
foundFiles = strings.Join(foundFilesArray, "\n") + "\n"
multiplePathsArray = []string{
filepath.Join("lib", "test-data", "markdown", "assets", "画面.bmp"),
filepath.Join("lib", "test-data", "markdown", "テスト.gif"),
filepath.Join("lib", "test-data", "image-files", "blank.jpg"),
filepath.Join("lib", "test-data", "image-files", "sample.png"),
filepath.Join("lib", "test-data", "image-files", "画像", "テスト.gif"),
filepath.Join("lib", "test-data", "image-files", "画面.bmp"),
}
multiplePaths = strings.Join(multiplePathsArray, "\n") + "\n"
)
func Test_main_markdown(t *testing.T) {
tests := []struct {
name string
args []string
exitCode int
out string
outerr string
}{
{"not found", []string{filepath.Join("lib", "test-data", "markdown0")}, exitCodeOK, "", ""},
{"found files", []string{filepath.Join("lib", "test-data", "markdown")}, exitCodeFoundUnreferencedImages, foundFiles, ""},
{"empty args", []string{}, exitCodeInvalidArgs, "", emptyArgs},
{"multiple paths", []string{filepath.Join("lib", "test-data", "markdown"), filepath.Join("lib", "test-data", "image-files")}, exitCodeFoundUnreferencedImages, multiplePaths, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flagSet := flag.NewFlagSet("testing main markdown", flag.ContinueOnError)
err := flagSet.Parse(tt.args)
if err != nil {
t.Errorf("failed to flagSet.Parse, %v", err)
}
cmdArgs = flagSet.Args()
var exitCodeTest int
exit = func(n int) {
exitCodeTest = n
}
out = new(bytes.Buffer)
outerr = new(bytes.Buffer)
main()
if exitCodeTest != tt.exitCode {
t.Errorf("exit code = %v, want %v", exitCodeTest, tt.exitCode)
}
gotout := out.(*bytes.Buffer).String()
if gotout != tt.out {
t.Errorf("gotout = %v, want %v", gotout, tt.out)
if diff := cmp.Diff(gotout, tt.out); diff != "" {
fmt.Printf("diff:\n%s\n", diff)
}
}
goterr := outerr.(*bytes.Buffer).String()
if goterr != tt.outerr {
t.Errorf("goterr = %v, want %v", goterr, tt.outerr)
if diff := cmp.Diff(goterr, tt.outerr); diff != "" {
fmt.Printf("diff:\n%s\n", diff)
}
}
})
}
}
func Test_main_html(t *testing.T) {
typeFlag = "html"
tests := []struct {
name string
args []string
exitCode int
out string
outerr string
}{
{"found", []string{filepath.Join("lib", "test-data", "html")}, exitCodeFoundUnreferencedImages, filepath.Join("lib", "test-data", "html", "assets", "hoge.png") + "\n", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flagSet := flag.NewFlagSet("testing main html", flag.ContinueOnError)
err := flagSet.Parse(tt.args)
if err != nil {
t.Errorf("failed to flagSet.Parse, %v", err)
}
cmdArgs = flagSet.Args()
var exitCodeTest int
exit = func(n int) {
exitCodeTest = n
}
out = new(bytes.Buffer)
outerr = new(bytes.Buffer)
main()
if exitCodeTest != tt.exitCode {
t.Errorf("exit code = %v, want %v", exitCodeTest, tt.exitCode)
}
gotout := out.(*bytes.Buffer).String()
if gotout != tt.out {
t.Errorf("gotout = %v, want %v", gotout, tt.out)
if diff := cmp.Diff(gotout, tt.out); diff != "" {
fmt.Printf("diff:\n%s\n", diff)
}
}
goterr := outerr.(*bytes.Buffer).String()
if goterr != tt.outerr {
t.Errorf("goterr = %v, want %v", goterr, tt.outerr)
if diff := cmp.Diff(goterr, tt.outerr); diff != "" {
fmt.Printf("diff:\n%s\n", diff)
}
}
})
}
}
func Test_main_help_ver(t *testing.T) {
type args struct {
help bool
ver bool
}
tests := []struct {
name string
args args
exitCode int
out string
outerr string
}{
{"help", args{true, false}, exitCodeOK, fmt.Sprintf("%s\n\n%s\n", usage, flags), ""},
{"version", args{false, true}, exitCodeOK, fmt.Sprintf("%s version %s.%s\n", Name, Version, Revision), ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var exitCodeTest int
exit = func(n int) {
exitCodeTest = n
}
out = new(bytes.Buffer)
outerr = new(bytes.Buffer)
help = tt.args.help
ver = tt.args.ver
main()
if exitCodeTest != tt.exitCode {
t.Errorf("exit code = %v, want %v", exitCodeTest, tt.exitCode)
}
gotout := out.(*bytes.Buffer).String()
if gotout != tt.out {
t.Errorf("gotout = %v, want %v", gotout, tt.out)
}
goterr := outerr.(*bytes.Buffer).String()
if goterr != tt.outerr {
t.Errorf("goterr = %v, want %v", goterr, tt.outerr)
}
})
}
help = false
ver = false
}
func Test_main_delete(t *testing.T) {
help = false
ver = false
dir, err := os.MkdirTemp("test-data", "furit-test")
if err != nil {
t.Errorf("failed to create temp dir: %v", err)
}
defer func() {
e := os.RemoveAll(dir)
if err != nil {
t.Errorf("failed to remove files: %v", e)
}
}()
mdF, err := os.CreateTemp(dir, "test*.md")
if err != nil {
log.Printf("failed to create temporary file: %v", err)
}
defer func() {
e := mdF.Close()
if err != nil {
log.Printf("failed to close temporary file: %v", e)
}
}()
imgF, err := os.CreateTemp(dir, "sample*.png")
if err != nil {
log.Printf("failed to create temporary file: %v", err)
}
defer func() {
e := imgF.Close()
if err != nil {
log.Printf("failed to close temporary file: %v", e)
}
}()
type args struct {
root []string
del bool
force bool
}
tests := []struct {
name string
args args
exitCode int
out string
outerr string
}{
{"delete with prompt", args{[]string{dir}, true, false}, exitCodeFoundUnreferencedImages, fmt.Sprintf("%s\n%s", imgF.Name(), promptStr), canceledStr},
// TODO: failed to run on Windows.
//{"delete forcedly", args{[]string{dir}, true, true}, exitCodeFoundUnreferencedImages, fmt.Sprintf("%v\n", imgF.Name()), ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flagSet := flag.NewFlagSet("testing main", flag.ContinueOnError)
err := flagSet.Parse(tt.args.root)
if err != nil {
t.Errorf("failed to flagSet.Parse, %v", err)
}
cmdArgs = flagSet.Args()
var exitCodeTest int
exit = func(n int) {
exitCodeTest = n
}
out = new(bytes.Buffer)
outerr = new(bytes.Buffer)
delFlag = tt.args.del
forceFlag = tt.args.force
main()
if exitCodeTest != tt.exitCode {
t.Errorf("exit code = %v, want %v", exitCodeTest, tt.exitCode)
}
gotout := out.(*bytes.Buffer).String()
if gotout != tt.out {
t.Errorf("gotout = %v, want %v", gotout, tt.out)
}
goterr := outerr.(*bytes.Buffer).String()
if goterr != tt.outerr {
t.Errorf("goterr = %v, want %v", goterr, tt.outerr)
}
})
}
delFlag = false
forceFlag = false
}