Repository navigation
Expand file tree
/
Copy pathapp_test.go
More file actions
252 lines (231 loc) · 6.51 KB
/
Copy pathapp_test.go
File metadata and controls
252 lines (231 loc) · 6.51 KB
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
package internal_test
import (
"bytes"
"errors"
"io/fs"
"maps"
"path"
"slices"
"strings"
"testing"
"github.com/powerman/check"
"github.com/powerman/go-scripts-example/scripts/toc/internal"
)
var (
errFind = errors.New("stub find error")
errRead = errors.New("stub read error")
errWrite = errors.New("stub write error")
)
// world is a stateful fake of a repository with files.
type world struct {
*FakeWorld
Files map[string]string
}
func newWorld(files map[string]string) *world {
w := &world{FakeWorld: NewFakeWorld(), Files: make(map[string]string)}
maps.Copy(w.Files, files)
w.TOCFilesFunc.SetDefaultHook(w.tocFiles)
w.ReadFileFunc.SetDefaultHook(w.readFile)
w.WriteFileFunc.SetDefaultHook(w.writeFile)
return w
}
func (w *world) tocFiles() ([]string, error) {
var names []string
for name, data := range w.Files {
if path.Ext(name) == ".md" && slices.Contains(strings.Split(data, "\n"), "<!-- toc -->") {
names = append(names, name)
}
}
slices.Sort(names)
return names, nil
}
func (w *world) readFile(name string) ([]byte, error) {
data, ok := w.Files[name]
if !ok {
return nil, fs.ErrNotExist
}
return []byte(data), nil
}
func (w *world) writeFile(name string, data []byte) error {
w.Files[name] = string(data)
return nil
}
// md joins lines of a markdown file.
func md(lines ...string) string { return strings.Join(lines, "\n") + "\n" }
var (
opts = internal.Options{MinLevel: 2, MaxLevel: 3}
readme = md(
"# Title",
"",
"<!-- toc -->",
"<!-- tocstop -->",
"",
"## One",
"### One `a`",
"#### One a x",
"## Two",
)
readmeTOC = md(
"# Title",
"",
"<!-- toc -->",
"",
"- [One](#one)",
" - [One `a`](#one-a)",
"- [Two](#two)",
"",
"<!-- tocstop -->",
"",
"## One",
"### One `a`",
"#### One a x",
"## Two",
)
plain = md("# Plain", "## Section")
)
func TestApp_Update(tt *testing.T) {
tt.Parallel()
tests := []struct {
name string
files map[string]string
opts internal.Options
wantFiles map[string]string
wantOut string
wantErr error
}{
{
name: "AllFiles",
files: map[string]string{"README.md": readme, "docs/a.md": readme, "plain.md": plain, "x.txt": readme},
opts: opts,
wantFiles: map[string]string{"README.md": readmeTOC, "docs/a.md": readmeTOC, "plain.md": plain, "x.txt": readme},
wantOut: "updated README.md\nupdated docs/a.md\n",
},
{
name: "GivenFiles",
files: map[string]string{"a.md": readme, "b.md": readme},
opts: internal.Options{Files: []string{"b.md"}, MinLevel: 2, MaxLevel: 3},
wantFiles: map[string]string{"a.md": readme, "b.md": readmeTOC},
wantOut: "updated b.md\n",
},
{
name: "UpToDate",
files: map[string]string{"README.md": readmeTOC},
opts: opts,
wantFiles: map[string]string{"README.md": readmeTOC},
},
{
name: "Check",
files: map[string]string{"a.md": readme, "b.md": readmeTOC},
opts: internal.Options{MinLevel: 2, MaxLevel: 3, Check: true},
wantFiles: map[string]string{"a.md": readme, "b.md": readmeTOC},
wantErr: internal.ErrOutdated,
},
{
name: "Levels",
files: map[string]string{"a.md": readme},
opts: internal.Options{MinLevel: 3, MaxLevel: 4},
wantFiles: map[string]string{"a.md": strings.Replace(readme, "<!-- toc -->\n", md(
"<!-- toc -->",
"",
"- [One `a`](#one-a)",
" - [One a x](#one-a-x)",
"",
), 1)},
wantOut: "updated a.md\n",
},
{
name: "SkippedLevel",
files: map[string]string{"a.md": md("<!-- toc -->", "<!-- tocstop -->", "#### Deep", "## Two", "#### Deep")},
opts: internal.Options{MinLevel: 2, MaxLevel: 4},
wantFiles: map[string]string{"a.md": md(
"<!-- toc -->",
"",
"- [Deep](#deep)",
"- [Two](#two)",
" - [Deep](#deep-1)",
"",
"<!-- tocstop -->",
"#### Deep",
"## Two",
"#### Deep",
)},
wantOut: "updated a.md\n",
},
{
name: "NoHeadings",
files: map[string]string{"a.md": md("# Title", "<!-- toc -->", "", "- [Old](#old)", "", "<!-- tocstop -->")},
opts: opts,
wantFiles: map[string]string{"a.md": md("# Title", "<!-- toc -->", "", "<!-- tocstop -->")},
wantOut: "updated a.md\n",
},
{
name: "MarkerInCode",
files: map[string]string{"a.md": md("```", "<!-- toc -->", "<!-- tocstop -->", "```")},
opts: opts,
wantFiles: map[string]string{"a.md": md("```", "<!-- toc -->", "<!-- tocstop -->", "```")},
},
{
name: "NoMarker",
files: map[string]string{"a.md": plain},
opts: internal.Options{Files: []string{"a.md"}, MinLevel: 2, MaxLevel: 3},
wantFiles: map[string]string{"a.md": plain},
wantErr: internal.ErrNoMarker,
},
{
name: "NoEndMarker",
files: map[string]string{"a.md": md("<!-- tocstop -->", "<!-- toc -->")},
opts: opts,
wantFiles: map[string]string{"a.md": md("<!-- tocstop -->", "<!-- toc -->")},
wantErr: internal.ErrNoEndMarker,
},
{
name: "NoFile",
opts: internal.Options{Files: []string{"a.md"}, MinLevel: 2, MaxLevel: 3},
wantFiles: make(map[string]string),
wantErr: fs.ErrNotExist,
},
{
name: "InvalidLevels",
files: map[string]string{"a.md": readme},
opts: internal.Options{MinLevel: 3, MaxLevel: 2},
wantFiles: map[string]string{"a.md": readme},
wantErr: internal.ErrInvalidLevels,
},
}
for _, tc := range tests {
tt.Run(tc.name, func(tt *testing.T) {
tt.Parallel()
t := check.T(tt).MustAll()
w := newWorld(tc.files)
var out bytes.Buffer
err := internal.NewApp(w, &out).Update(tc.opts)
t.Err(err, tc.wantErr)
t.DeepEqual(w.Files, tc.wantFiles)
t.Equal(out.String(), tc.wantOut)
})
}
}
func TestApp_Update_Errors(tt *testing.T) {
tt.Parallel()
tt.Run("TOCFiles", func(tt *testing.T) {
tt.Parallel()
t := check.T(tt).MustAll()
w := newWorld(nil)
w.TOCFilesFunc.PushReturn(nil, errFind)
err := internal.NewApp(w, &bytes.Buffer{}).Update(opts)
t.Err(err, errFind)
})
tt.Run("ContinueAfterError", func(tt *testing.T) {
tt.Parallel()
t := check.T(tt).MustAll()
w := newWorld(map[string]string{"a.md": readme, "b.md": readme, "c.md": readme})
w.ReadFileFunc.PushHook(w.readFile) // a.md
w.ReadFileFunc.PushReturn(nil, errRead)
w.WriteFileFunc.PushReturn(errWrite) // a.md
var out bytes.Buffer
err := internal.NewApp(w, &out).Update(opts)
t.Match(err, `^a.md: stub write error\nb.md: stub read error$`)
t.DeepEqual(w.Files, map[string]string{"a.md": readme, "b.md": readme, "c.md": readmeTOC})
t.Equal(out.String(), "updated c.md\n")
})
}