@@ -7,61 +7,76 @@ import (
77 "testing"
88
99 "github.com/spf13/cobra"
10+ "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
1012
1113 "github.com/Flagsmith/flagsmith-cli/v2/internal/cmd"
1214)
1315
14- // Links between pages are recovered from the underscore-joined filenames cobra
15- // writes, so a command name containing one would produce a broken link.
1616func TestCommandNamesAreURLSafe (t * testing.T ) {
17- var check func (* cobra.Command )
18- check = func (c * cobra.Command ) {
19- if strings .ContainsAny (c .Name (), "_ " ) {
20- t .Errorf ("command %q: name must not contain an underscore or space" , c .CommandPath ())
21- }
17+ // Given
18+ root := cmd .Root ()
19+
20+ // When / Then
21+ var visit func (* cobra.Command )
22+ visit = func (c * cobra.Command ) {
23+ assert .NotContains (t , c .Name (), "_" , "an underscore breaks the links between pages" )
24+ assert .NotContains (t , c .Name (), " " , "a space breaks the page's path" )
25+ assert .NotContains (t , c .Name (), "/" , "a slash breaks the page's path" )
26+
2227 for _ , sub := range c .Commands () {
23- check (sub )
28+ visit (sub )
2429 }
2530 }
26- check ( cmd . Root () )
31+ visit ( root )
2732}
2833
2934func TestWriteMirrorsCommandTree (t * testing.T ) {
35+ // Given
3036 dir := t .TempDir ()
31- if err := write (cmd .Root (), dir ); err != nil {
32- t .Fatal (err )
33- }
3437
35- for _ , want := range []string {"_index.md" , "flag/_index.md" , "flag/update.md" } {
36- if _ , err := os .Stat (filepath .Join (dir , want )); err != nil {
37- t .Errorf ("%s: %v" , want , err )
38- }
39- }
40- // A hidden command must not be published.
41- if _ , err := os .Stat (filepath .Join (dir , "flag/create.md" )); err == nil {
42- t .Error ("flag/create.md: hidden command should not have a page" )
43- }
38+ // When
39+ require .NoError (t , write (cmd .Root (), dir ))
40+
41+ // Then
42+ // the root command's own page is the site's home page
43+ assert .FileExists (t , filepath .Join (dir , "_index.md" ))
44+ // a command with subcommands has a directory
45+ assert .FileExists (t , filepath .Join (dir , "flag/_index.md" ))
46+ // a leaf command is a page inside its parent's directory
47+ assert .FileExists (t , filepath .Join (dir , "flag/update.md" ))
48+ // a hidden command is not published
49+ assert .NoFileExists (t , filepath .Join (dir , "flag/create.md" ))
50+ }
4451
52+ func TestWritePage (t * testing.T ) {
53+ // Given
54+ dir := t .TempDir ()
55+ require .NoError (t , write (cmd .Root (), dir ))
56+
57+ // When
4558 page , err := os .ReadFile (filepath .Join (dir , "flag/update.md" ))
46- if err != nil {
47- t .Fatal (err )
48- }
59+ require .NoError (t , err )
4960 got := string (page )
50- for _ , want := range []string {
51- `title: "flagsmith flag update"` , // Hextra renders the page heading from it
52- "](../)" , // link to the parent page, not a flat filename
53- } {
54- if ! strings .Contains (got , want ) {
55- t .Errorf ("page is missing %q:\n %s" , want , got )
56- }
57- }
58- for _ , unwanted := range []string {
59- "Auto generated" , // non-reproducible build
60- "## flagsmith flag update" , // duplicates the front matter title
61- "flagsmith_flag_update.md" , // cobra's flat filenames
62- } {
63- if strings .Contains (got , unwanted ) {
64- t .Errorf ("page should not contain %q:\n %s" , unwanted , got )
65- }
66- }
61+
62+ // Then
63+ // the front matter titles the page with the full command path
64+ assert .Contains (t , got , `title: "flagsmith flag update"` )
65+ // the sidebar entry is the command's own name, unambiguous once nested.
66+ assert .Contains (t , got , `linkTitle: "update"` )
67+ // the description is the command's Short
68+ assert .Contains (t , got , `description: "Change a flag's state in the current environment"` )
69+ // the body is cobra's, taken from the command
70+ assert .Contains (t , got , "flagsmith flag update <feature> [flags]" )
71+ // the link to the parent page is relative,
72+ assert .Contains (t , got , "](../)" )
73+ // no link is left as cobra's flat filename
74+ assert .NotContains (t , got , "flagsmith_" )
75+ // cobra's own heading is gone
76+ assert .NotContains (t , got , "## flagsmith flag update" )
77+ // no generation date is stamped into the page
78+ assert .NotContains (t , got , "Auto generated" )
79+
80+ // Guard against the assertions above passing on an empty file.
81+ require .NotEmpty (t , strings .TrimSpace (got ))
6782}
0 commit comments