Skip to content

Commit a3c5a03

Browse files
committed
test(docgen): Structure the generation tests as given/when/then
Each assertion now says in one line what would break without it, and the file uses testify rather than four hand-rolled assertion helpers. One assertion was decorative: it looked for the page's own flat filename, which never appears on it — only a link to its parent does. It now looks for the prefix cobra uses, and fails when links are left unrewritten.
1 parent b058cc4 commit a3c5a03

2 files changed

Lines changed: 60 additions & 41 deletions

File tree

‎cmd/docgen/main_test.go‎

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1616
func 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

2934
func 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
}

‎go.mod‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
1212
github.com/spf13/cobra v1.10.2
1313
github.com/spf13/pflag v1.0.9
14+
github.com/stretchr/testify v1.11.1
1415
github.com/zalando/go-keyring v0.2.8
1516
golang.org/x/sync v0.21.0
1617
golang.org/x/term v0.45.0
@@ -32,6 +33,7 @@ require (
3233
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
3334
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
3435
github.com/danieljoos/wincred v1.2.3 // indirect
36+
github.com/davecgh/go-spew v1.1.1 // indirect
3537
github.com/dustin/go-humanize v1.0.1 // indirect
3638
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
3739
github.com/go-resty/resty/v2 v2.17.2 // indirect
@@ -49,11 +51,13 @@ require (
4951
github.com/muesli/cancelreader v0.2.2 // indirect
5052
github.com/muesli/termenv v0.16.0 // indirect
5153
github.com/ohler55/ojg v1.28.1 // indirect
54+
github.com/pmezard/go-difflib v1.0.0 // indirect
5255
github.com/rivo/uniseg v0.4.7 // indirect
5356
github.com/russross/blackfriday/v2 v2.1.0 // indirect
5457
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
5558
go.yaml.in/yaml/v3 v3.0.4 // indirect
5659
golang.org/x/net v0.56.0 // indirect
5760
golang.org/x/sys v0.47.0 // indirect
5861
golang.org/x/text v0.39.0 // indirect
62+
gopkg.in/yaml.v3 v3.0.1 // indirect
5963
)

0 commit comments

Comments
 (0)