Skip to content

Commit 687aaf5

Browse files
authored
Merge pull request #116 from barrettj12/flush-index
fix: correctly write index in documentation command
2 parents cc2574f + 65c6e80 commit 687aaf5

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

documentation.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,9 @@ func (c *documentationCommand) dumpSeveralFiles() error {
182182
return err
183183
}
184184

185-
writer := bufio.NewWriter(f)
186-
_, err = writer.WriteString(c.commandsIndex())
185+
err = c.writeIndex(f)
187186
if err != nil {
188-
return err
187+
return fmt.Errorf("writing index: %w", err)
189188
}
190189
f.Close()
191190
}
@@ -271,9 +270,9 @@ func (c *documentationCommand) dumpEntries(w io.Writer) error {
271270
}
272271

273272
if !c.noIndex {
274-
_, err := fmt.Fprintf(w, "%s", c.commandsIndex())
273+
err := c.writeIndex(w)
275274
if err != nil {
276-
return err
275+
return fmt.Errorf("writing index: %w", err)
277276
}
278277
}
279278

@@ -310,19 +309,26 @@ func (c *documentationCommand) writeSections(w io.Writer, superCommands []string
310309
return nil
311310
}
312311

313-
func (c *documentationCommand) commandsIndex() string {
314-
index := "# Index\n"
312+
// writeIndex writes the command index to the specified writer.
313+
func (c *documentationCommand) writeIndex(w io.Writer) error {
314+
_, err := fmt.Fprintf(w, "# Index\n")
315+
if err != nil {
316+
return err
317+
}
315318

316319
listCommands := c.getSortedListCommands()
317320
for id, name := range listCommands {
318321
if isDefaultCommand(name) {
319322
continue
320323
}
321-
index += fmt.Sprintf("%d. [%s](%s)\n", id, name, c.linkForCommand(name))
324+
_, err = fmt.Fprintf(w, "%d. [%s](%s)\n", id, name, c.linkForCommand(name))
325+
if err != nil {
326+
return err
327+
}
322328
// TODO: handle subcommands ??
323329
}
324-
index += "---\n\n"
325-
return index
330+
_, err = fmt.Fprintf(w, "---\n\n")
331+
return err
326332
}
327333

328334
// Return the URL/location for the given command

documentation_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmd_test
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
57

68
"github.com/juju/gnuflag"
79
gc "gopkg.in/check.v1"
@@ -209,3 +211,24 @@ formatted YAML-formatted file.
209211
c.Check(cmd.EscapeMarkdown(t.input), gc.Equals, t.output)
210212
}
211213
}
214+
215+
// TestWriteIndex checks that the index file is successfully written.
216+
func (*documentationSuite) TestWriteIndex(c *gc.C) {
217+
// Make temp dir to hold docs
218+
docsDir := c.MkDir()
219+
220+
// Create a supercommand and run the documentation command
221+
superCmd := cmd.NewSuperCommand(cmd.SuperCommandParams{})
222+
superCmd.SetFlags(&gnuflag.FlagSet{})
223+
err := superCmd.Init([]string{"documentation", "--split", "--out", docsDir})
224+
c.Assert(err, gc.IsNil)
225+
err = superCmd.Run(&cmd.Context{})
226+
c.Assert(err, gc.IsNil)
227+
228+
// Check the index file
229+
indexPath := filepath.Join(docsDir, "index.md")
230+
indexContents, err := os.ReadFile(indexPath)
231+
c.Assert(err, gc.IsNil)
232+
// Index should be non-empty
233+
c.Assert(string(indexContents), gc.Matches, "(?m).*Index.*")
234+
}

0 commit comments

Comments
 (0)