man_docs_test.go
This file
Path
doc/man_docs_test.go
221 lines · 6.0 KiB · Go
Last changed by
go.mod: cpuguy83/go-md2man/v2 v2.0.1 (#1460)Sebastiaan van Stijnbefore adoption2a52778
History of this fileTake it
1package doc23import (4 "bufio"5 "bytes"6 "fmt"7 "io/ioutil"8 "os"9 "path/filepath"10 "strings"11 "testing"1213 "github.com/spf13/cobra"14)1516func assertNoErr(t *testing.T, e error) {17 if e != nil {18 t.Error(e)19 }20}2122func translate(in string) string {23 return strings.Replace(in, "-", "\\-", -1)24}2526func TestGenManDoc(t *testing.T) {27 header := &GenManHeader{28 Title: "Project",29 Section: "2",30 }3132 // We generate on a subcommand so we have both subcommands and parents33 buf := new(bytes.Buffer)34 if err := GenMan(echoCmd, header, buf); err != nil {35 t.Fatal(err)36 }37 output := buf.String()3839 // Make sure parent has - in CommandPath() in SEE ALSO:40 parentPath := echoCmd.Parent().CommandPath()41 dashParentPath := strings.Replace(parentPath, " ", "-", -1)42 expected := translate(dashParentPath)43 expected = expected + "(" + header.Section + ")"44 checkStringContains(t, output, expected)4546 checkStringContains(t, output, translate(echoCmd.Name()))47 checkStringContains(t, output, translate(echoCmd.Name()))48 checkStringContains(t, output, "boolone")49 checkStringContains(t, output, "rootflag")50 checkStringContains(t, output, translate(rootCmd.Name()))51 checkStringContains(t, output, translate(echoSubCmd.Name()))52 checkStringOmits(t, output, translate(deprecatedCmd.Name()))53 checkStringContains(t, output, translate("Auto generated"))54}5556func TestGenManNoHiddenParents(t *testing.T) {57 header := &GenManHeader{58 Title: "Project",59 Section: "2",60 }6162 // We generate on a subcommand so we have both subcommands and parents63 for _, name := range []string{"rootflag", "strtwo"} {64 f := rootCmd.PersistentFlags().Lookup(name)65 f.Hidden = true66 defer func() { f.Hidden = false }()67 }68 buf := new(bytes.Buffer)69 if err := GenMan(echoCmd, header, buf); err != nil {70 t.Fatal(err)71 }72 output := buf.String()7374 // Make sure parent has - in CommandPath() in SEE ALSO:75 parentPath := echoCmd.Parent().CommandPath()76 dashParentPath := strings.Replace(parentPath, " ", "-", -1)77 expected := translate(dashParentPath)78 expected = expected + "(" + header.Section + ")"79 checkStringContains(t, output, expected)8081 checkStringContains(t, output, translate(echoCmd.Name()))82 checkStringContains(t, output, translate(echoCmd.Name()))83 checkStringContains(t, output, "boolone")84 checkStringOmits(t, output, "rootflag")85 checkStringContains(t, output, translate(rootCmd.Name()))86 checkStringContains(t, output, translate(echoSubCmd.Name()))87 checkStringOmits(t, output, translate(deprecatedCmd.Name()))88 checkStringContains(t, output, translate("Auto generated"))89 checkStringOmits(t, output, "OPTIONS INHERITED FROM PARENT COMMANDS")90}9192func TestGenManNoGenTag(t *testing.T) {93 echoCmd.DisableAutoGenTag = true94 defer func() { echoCmd.DisableAutoGenTag = false }()9596 header := &GenManHeader{97 Title: "Project",98 Section: "2",99 }100101 // We generate on a subcommand so we have both subcommands and parents102 buf := new(bytes.Buffer)103 if err := GenMan(echoCmd, header, buf); err != nil {104 t.Fatal(err)105 }106 output := buf.String()107108 unexpected := translate("#HISTORY")109 checkStringOmits(t, output, unexpected)110 unexpected = translate("Auto generated by spf13/cobra")111 checkStringOmits(t, output, unexpected)112}113114func TestGenManSeeAlso(t *testing.T) {115 rootCmd := &cobra.Command{Use: "root", Run: emptyRun}116 aCmd := &cobra.Command{Use: "aaa", Run: emptyRun, Hidden: true} // #229117 bCmd := &cobra.Command{Use: "bbb", Run: emptyRun}118 cCmd := &cobra.Command{Use: "ccc", Run: emptyRun}119 rootCmd.AddCommand(aCmd, bCmd, cCmd)120121 buf := new(bytes.Buffer)122 header := &GenManHeader{}123 if err := GenMan(rootCmd, header, buf); err != nil {124 t.Fatal(err)125 }126 scanner := bufio.NewScanner(buf)127128 if err := assertLineFound(scanner, ".SH SEE ALSO"); err != nil {129 t.Fatalf("Couldn't find SEE ALSO section header: %v", err)130 }131 if err := assertNextLineEquals(scanner, ".PP"); err != nil {132 t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err)133 }134 if err := assertNextLineEquals(scanner, `\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP`); err != nil {135 t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err)136 }137}138139func TestManPrintFlagsHidesShortDeperecated(t *testing.T) {140 c := &cobra.Command{}141 c.Flags().StringP("foo", "f", "default", "Foo flag")142 assertNoErr(t, c.Flags().MarkShorthandDeprecated("foo", "don't use it no more"))143144 buf := new(bytes.Buffer)145 manPrintFlags(buf, c.Flags())146147 got := buf.String()148 expected := "**--foo**=\"default\"\n\tFoo flag\n\n"149 if got != expected {150 t.Errorf("Expected %v, got %v", expected, got)151 }152}153154func TestGenManTree(t *testing.T) {155 c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}156 header := &GenManHeader{Section: "2"}157 tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")158 if err != nil {159 t.Fatalf("Failed to create tmpdir: %s", err.Error())160 }161 defer os.RemoveAll(tmpdir)162163 if err := GenManTree(c, header, tmpdir); err != nil {164 t.Fatalf("GenManTree failed: %s", err.Error())165 }166167 if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {168 t.Fatalf("Expected file 'do.2' to exist")169 }170171 if header.Title != "" {172 t.Fatalf("Expected header.Title to be unmodified")173 }174}175176func assertLineFound(scanner *bufio.Scanner, expectedLine string) error {177 for scanner.Scan() {178 line := scanner.Text()179 if line == expectedLine {180 return nil181 }182 }183184 if err := scanner.Err(); err != nil {185 return fmt.Errorf("scan failed: %s", err)186 }187188 return fmt.Errorf("hit EOF before finding %v", expectedLine)189}190191func assertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {192 if scanner.Scan() {193 line := scanner.Text()194 if line == expectedLine {195 return nil196 }197 return fmt.Errorf("got %v, not %v", line, expectedLine)198 }199200 if err := scanner.Err(); err != nil {201 return fmt.Errorf("scan failed: %v", err)202 }203204 return fmt.Errorf("hit EOF before finding %v", expectedLine)205}206207func BenchmarkGenManToFile(b *testing.B) {208 file, err := ioutil.TempFile("", "")209 if err != nil {210 b.Fatal(err)211 }212 defer os.Remove(file.Name())213 defer file.Close()214215 b.ResetTimer()216 for i := 0; i < b.N; i++ {217 if err := GenMan(rootCmd, nil, file); err != nil {218 b.Fatal(err)219 }220 }221}