@@ -2,6 +2,7 @@ package docs
2
2
3
3
import (
4
4
"bytes"
5
+ "embed"
5
6
"io"
6
7
"io/fs"
7
8
"net/mail"
@@ -12,14 +13,30 @@ import (
12
13
"github.com/urfave/cli/v3"
13
14
)
14
15
16
+ var (
17
+ //go:embed testdata
18
+ testdata embed.FS
19
+ )
20
+
15
21
func expectFileContent (t * testing.T , file , got string ) {
16
- data , err := os .ReadFile (file )
17
- // Ignore windows line endings
18
- data = bytes .ReplaceAll (data , []byte ("\r \n " ), []byte ("\n " ))
22
+ data , err := testdata .ReadFile (file )
19
23
20
24
r := require .New (t )
21
25
r .NoError (err )
22
- r .Equal (got , string (data ))
26
+ r .Equal (
27
+ string (normalizeNewlines ([]byte (got ))),
28
+ string (normalizeNewlines (data )),
29
+ )
30
+ }
31
+
32
+ func normalizeNewlines (d []byte ) []byte {
33
+ return bytes .ReplaceAll (
34
+ bytes .ReplaceAll (
35
+ d ,
36
+ []byte ("\r \n " ), []byte ("\n " ),
37
+ ),
38
+ []byte ("\r " ), []byte ("\n " ),
39
+ )
23
40
}
24
41
25
42
func buildExtendedTestCommand () * cli.Command {
@@ -137,13 +154,8 @@ Should be a part of the same code block
137
154
}
138
155
139
156
func TestToMarkdownFull (t * testing.T ) {
140
- // Given
141
157
cmd := buildExtendedTestCommand ()
142
-
143
- // When
144
158
res , err := ToMarkdown (cmd )
145
-
146
- // Then
147
159
require .NoError (t , err )
148
160
expectFileContent (t , "testdata/expected-doc-full.md" , res )
149
161
}
@@ -152,28 +164,19 @@ func TestToTabularMarkdown(t *testing.T) {
152
164
app := buildExtendedTestCommand ()
153
165
154
166
t .Run ("full" , func (t * testing.T ) {
155
- // When
156
167
res , err := ToTabularMarkdown (app , "app" )
157
-
158
- // Then
159
168
require .NoError (t , err )
160
169
expectFileContent (t , "testdata/expected-tabular-markdown-full.md" , res )
161
170
})
162
171
163
172
t .Run ("with empty path" , func (t * testing.T ) {
164
- // When
165
173
res , err := ToTabularMarkdown (app , "" )
166
-
167
- // Then
168
174
require .NoError (t , err )
169
175
expectFileContent (t , "testdata/expected-tabular-markdown-full.md" , res )
170
176
})
171
177
172
178
t .Run ("with custom app path" , func (t * testing.T ) {
173
- // When
174
179
res , err := ToTabularMarkdown (app , "/usr/local/bin" )
175
-
176
- // Then
177
180
require .NoError (t , err )
178
181
expectFileContent (t , "testdata/expected-tabular-markdown-custom-app-path.md" , res )
179
182
})
@@ -183,7 +186,7 @@ func TestToTabularMarkdownFailed(t *testing.T) {
183
186
tpl := MarkdownTabularDocTemplate
184
187
t .Cleanup (func () { MarkdownTabularDocTemplate = tpl })
185
188
186
- MarkdownTabularDocTemplate = "{{ .Foo }}" // invalid template
189
+ MarkdownTabularDocTemplate = "{{ .Foo }}"
187
190
188
191
app := buildExtendedTestCommand ()
189
192
@@ -196,17 +199,11 @@ func TestToTabularMarkdownFailed(t *testing.T) {
196
199
}
197
200
198
201
func TestToTabularToFileBetweenTags (t * testing.T ) {
199
- expectedDocs , fErr := os .ReadFile ("testdata/expected-tabular-markdown-full.md" )
202
+ expectedDocs , fErr := testdata .ReadFile ("testdata/expected-tabular-markdown-full.md" )
200
203
201
204
r := require .New (t )
202
205
r .NoError (fErr )
203
206
204
- // normalizes \r\n (windows) and \r (mac) into \n (unix) (required for tests to pass on windows)
205
- normalizeNewlines := func (d []byte ) []byte {
206
- d = bytes .ReplaceAll (d , []byte {13 , 10 }, []byte {10 }) // replace CR LF \r\n (windows) with LF \n (unix)
207
- return bytes .ReplaceAll (d , []byte {13 }, []byte {10 }) // replace CF \r (mac) with LF \n (unix)
208
- }
209
-
210
207
t .Run ("default tags" , func (t * testing.T ) {
211
208
tmpFile , err := os .CreateTemp ("" , "" )
212
209
@@ -238,7 +235,7 @@ Some other text`)
238
235
Some description
239
236
240
237
<!--GENERATED:CLI_DOCS-->
241
- <!-- Documentation inside this block generated by github.com/urfave/cli; DO NOT EDIT -->
238
+ <!-- Documentation inside this block generated by github.com/urfave/cli-docs/v3 ; DO NOT EDIT -->
242
239
` + string (expectedDocs ) + `
243
240
<!--/GENERATED:CLI_DOCS-->
244
241
@@ -278,7 +275,7 @@ Some other text`)
278
275
Some description
279
276
280
277
foo_BAR|baz
281
- <!-- Documentation inside this block generated by github.com/urfave/cli; DO NOT EDIT -->
278
+ <!-- Documentation inside this block generated by github.com/urfave/cli-docs/v3 ; DO NOT EDIT -->
282
279
` + string (expectedDocs ) + `
283
280
lorem+ipsum
284
281
@@ -302,44 +299,46 @@ Some other text`))
302
299
})
303
300
}
304
301
305
- func TestToMarkdownNoFlags (t * testing.T ) {
306
- app := buildExtendedTestCommand ()
307
- app .Flags = nil
302
+ func TestToMarkdown (t * testing.T ) {
303
+ t .Run ("no flags" , func (t * testing.T ) {
304
+ app := buildExtendedTestCommand ()
305
+ app .Flags = nil
308
306
309
- res , err := ToMarkdown (app )
307
+ res , err := ToMarkdown (app )
310
308
311
- require .NoError (t , err )
312
- expectFileContent (t , "testdata/expected-doc-no-flags.md" , res )
313
- }
309
+ require .NoError (t , err )
310
+ expectFileContent (t , "testdata/expected-doc-no-flags.md" , res )
311
+ })
314
312
315
- func TestToMarkdownNoCommands (t * testing.T ) {
316
- app := buildExtendedTestCommand ()
317
- app .Commands = nil
313
+ t . Run ( "no commands" , func (t * testing.T ) {
314
+ app := buildExtendedTestCommand ()
315
+ app .Commands = nil
318
316
319
- res , err := ToMarkdown (app )
317
+ res , err := ToMarkdown (app )
320
318
321
- require .NoError (t , err )
322
- expectFileContent (t , "testdata/expected-doc-no-commands.md" , res )
323
- }
319
+ require .NoError (t , err )
320
+ expectFileContent (t , "testdata/expected-doc-no-commands.md" , res )
321
+ })
324
322
325
- func TestToMarkdownNoAuthors (t * testing.T ) {
326
- app := buildExtendedTestCommand ()
327
- app .Authors = []any {}
323
+ t . Run ( "no authors" , func (t * testing.T ) {
324
+ app := buildExtendedTestCommand ()
325
+ app .Authors = []any {}
328
326
329
- res , err := ToMarkdown (app )
327
+ res , err := ToMarkdown (app )
330
328
331
- require .NoError (t , err )
332
- expectFileContent (t , "testdata/expected-doc-no-authors.md" , res )
333
- }
329
+ require .NoError (t , err )
330
+ expectFileContent (t , "testdata/expected-doc-no-authors.md" , res )
331
+ })
334
332
335
- func TestToMarkdownNoUsageText (t * testing.T ) {
336
- app := buildExtendedTestCommand ()
337
- app .UsageText = ""
333
+ t . Run ( "no usage text" , func (t * testing.T ) {
334
+ app := buildExtendedTestCommand ()
335
+ app .UsageText = ""
338
336
339
- res , err := ToMarkdown (app )
337
+ res , err := ToMarkdown (app )
340
338
341
- require .NoError (t , err )
342
- expectFileContent (t , "testdata/expected-doc-no-usagetext.md" , res )
339
+ require .NoError (t , err )
340
+ expectFileContent (t , "testdata/expected-doc-no-usagetext.md" , res )
341
+ })
343
342
}
344
343
345
344
func TestToMan (t * testing.T ) {
0 commit comments