github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/docs/man_test.go (about)

     1  package docs
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  func translate(in string) string {
    16  	return strings.Replace(in, "-", "\\-", -1)
    17  }
    18  
    19  func TestGenManDoc(t *testing.T) {
    20  	header := &GenManHeader{
    21  		Title:   "Project",
    22  		Section: "1",
    23  	}
    24  
    25  	// We generate on a subcommand so we have both subcommands and parents
    26  	buf := new(bytes.Buffer)
    27  	if err := GenMan(echoCmd, header, buf); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	output := buf.String()
    31  
    32  	// Make sure parent has - in CommandPath() in SEE ALSO:
    33  	parentPath := echoCmd.Parent().CommandPath()
    34  	dashParentPath := strings.Replace(parentPath, " ", "-", -1)
    35  	expected := translate(dashParentPath)
    36  	expected = expected + "(" + header.Section + ")"
    37  	checkStringContains(t, output, expected)
    38  
    39  	checkStringContains(t, output, translate(echoCmd.Name()))
    40  	checkStringContains(t, output, translate(echoCmd.Name()))
    41  	checkStringContains(t, output, "boolone")
    42  	checkStringContains(t, output, "rootflag")
    43  	checkStringContains(t, output, translate(rootCmd.Name()))
    44  	checkStringContains(t, output, translate(echoSubCmd.Name()))
    45  	checkStringOmits(t, output, translate(deprecatedCmd.Name()))
    46  }
    47  
    48  func TestGenManNoHiddenParents(t *testing.T) {
    49  	header := &GenManHeader{
    50  		Title:   "Project",
    51  		Section: "1",
    52  	}
    53  
    54  	// We generate on a subcommand so we have both subcommands and parents
    55  	for _, name := range []string{"rootflag", "strtwo"} {
    56  		f := rootCmd.PersistentFlags().Lookup(name)
    57  		f.Hidden = true
    58  		defer func() { f.Hidden = false }()
    59  	}
    60  	buf := new(bytes.Buffer)
    61  	if err := GenMan(echoCmd, header, buf); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	output := buf.String()
    65  
    66  	// Make sure parent has - in CommandPath() in SEE ALSO:
    67  	parentPath := echoCmd.Parent().CommandPath()
    68  	dashParentPath := strings.Replace(parentPath, " ", "-", -1)
    69  	expected := translate(dashParentPath)
    70  	expected = expected + "(" + header.Section + ")"
    71  	checkStringContains(t, output, expected)
    72  
    73  	checkStringContains(t, output, translate(echoCmd.Name()))
    74  	checkStringContains(t, output, translate(echoCmd.Name()))
    75  	checkStringContains(t, output, "boolone")
    76  	checkStringOmits(t, output, "rootflag")
    77  	checkStringContains(t, output, translate(rootCmd.Name()))
    78  	checkStringContains(t, output, translate(echoSubCmd.Name()))
    79  	checkStringOmits(t, output, translate(deprecatedCmd.Name()))
    80  	checkStringOmits(t, output, "OPTIONS INHERITED FROM PARENT COMMANDS")
    81  }
    82  
    83  func TestGenManSeeAlso(t *testing.T) {
    84  	rootCmd := &cobra.Command{Use: "root", Run: emptyRun}
    85  	aCmd := &cobra.Command{Use: "aaa", Run: emptyRun, Hidden: true} // #229
    86  	bCmd := &cobra.Command{Use: "bbb", Run: emptyRun}
    87  	cCmd := &cobra.Command{Use: "ccc", Run: emptyRun}
    88  	rootCmd.AddCommand(aCmd, bCmd, cCmd)
    89  
    90  	buf := new(bytes.Buffer)
    91  	header := &GenManHeader{}
    92  	if err := GenMan(rootCmd, header, buf); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	scanner := bufio.NewScanner(buf)
    96  	if err := assertLineFound(scanner, ".SH SEE ALSO"); err == nil {
    97  		t.Fatalf("Did not expect SEE ALSO section header")
    98  	}
    99  }
   100  
   101  func TestManPrintFlagsHidesShortDeprecated(t *testing.T) {
   102  	c := &cobra.Command{}
   103  	c.Flags().StringP("foo", "f", "default", "Foo flag")
   104  	_ = c.Flags().MarkShorthandDeprecated("foo", "don't use it no more")
   105  
   106  	buf := new(bytes.Buffer)
   107  	manPrintFlags(buf, c.Flags())
   108  
   109  	got := buf.String()
   110  	expected := "`--foo` `<string>`\n:   Foo flag\n\n"
   111  	if got != expected {
   112  		t.Errorf("Expected %q, got %q", expected, got)
   113  	}
   114  }
   115  
   116  func TestGenManTree(t *testing.T) {
   117  	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
   118  	tmpdir, err := os.MkdirTemp("", "test-gen-man-tree")
   119  	if err != nil {
   120  		t.Fatalf("Failed to create tmpdir: %s", err.Error())
   121  	}
   122  	defer os.RemoveAll(tmpdir)
   123  
   124  	if err := GenManTree(c, tmpdir); err != nil {
   125  		t.Fatalf("GenManTree failed: %s", err.Error())
   126  	}
   127  
   128  	if _, err := os.Stat(filepath.Join(tmpdir, "do.1")); err != nil {
   129  		t.Fatalf("Expected file 'do.1' to exist")
   130  	}
   131  }
   132  
   133  func assertLineFound(scanner *bufio.Scanner, expectedLine string) error {
   134  	for scanner.Scan() {
   135  		line := scanner.Text()
   136  		if line == expectedLine {
   137  			return nil
   138  		}
   139  	}
   140  
   141  	if err := scanner.Err(); err != nil {
   142  		return fmt.Errorf("scan failed: %s", err)
   143  	}
   144  
   145  	return fmt.Errorf("hit EOF before finding %v", expectedLine)
   146  }
   147  
   148  func BenchmarkGenManToFile(b *testing.B) {
   149  	file, err := os.CreateTemp(b.TempDir(), "")
   150  	if err != nil {
   151  		b.Fatal(err)
   152  	}
   153  	defer file.Close()
   154  
   155  	b.ResetTimer()
   156  	for i := 0; i < b.N; i++ {
   157  		if err := GenMan(rootCmd, nil, file); err != nil {
   158  			b.Fatal(err)
   159  		}
   160  	}
   161  }