github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/internal/docs/markdown_test.go (about)

     1  package docs
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  func TestGenMdDoc(t *testing.T) {
    14  	// We generate on subcommand so we have both subcommands and parents.
    15  	buf := new(bytes.Buffer)
    16  	if err := GenMarkdown(echoCmd, buf); err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	output := buf.String()
    20  
    21  	checkStringContains(t, output, echoCmd.Long)
    22  	checkStringContains(t, output, echoCmd.Example)
    23  	checkStringContains(t, output, "boolone")
    24  	checkStringContains(t, output, "rootflag")
    25  	checkStringOmits(t, output, rootCmd.Short)
    26  	checkStringOmits(t, output, echoSubCmd.Short)
    27  	checkStringOmits(t, output, deprecatedCmd.Short)
    28  	checkStringContains(t, output, "Options inherited from parent commands")
    29  }
    30  
    31  func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {
    32  	// We generate on subcommand so we have both subcommands and parents.
    33  	buf := new(bytes.Buffer)
    34  	if err := GenMarkdown(dummyCmd, buf); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	output := buf.String()
    38  
    39  	checkStringContains(t, output, dummyCmd.Example)
    40  	checkStringContains(t, output, dummyCmd.Short)
    41  	checkStringContains(t, output, "Options inherited from parent commands")
    42  	checkStringOmits(t, output, "### Synopsis")
    43  }
    44  
    45  func TestGenMdNoHiddenParents(t *testing.T) {
    46  	// We generate on subcommand so we have both subcommands and parents.
    47  	for _, name := range []string{"rootflag", "strtwo"} {
    48  		f := rootCmd.PersistentFlags().Lookup(name)
    49  		f.Hidden = true
    50  		defer func() { f.Hidden = false }()
    51  	}
    52  	buf := new(bytes.Buffer)
    53  	if err := GenMarkdown(echoCmd, buf); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	output := buf.String()
    57  
    58  	checkStringContains(t, output, echoCmd.Long)
    59  	checkStringContains(t, output, echoCmd.Example)
    60  	checkStringContains(t, output, "boolone")
    61  	checkStringOmits(t, output, "rootflag")
    62  	checkStringOmits(t, output, rootCmd.Short)
    63  	checkStringOmits(t, output, echoSubCmd.Short)
    64  	checkStringOmits(t, output, deprecatedCmd.Short)
    65  	checkStringOmits(t, output, "Options inherited from parent commands")
    66  }
    67  
    68  func TestGenMdTree(t *testing.T) {
    69  	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
    70  	tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")
    71  	if err != nil {
    72  		t.Fatalf("Failed to create tmpdir: %v", err)
    73  	}
    74  	defer os.RemoveAll(tmpdir)
    75  
    76  	if err := GenMarkdownTree(c, tmpdir); err != nil {
    77  		t.Fatalf("GenMarkdownTree failed: %v", err)
    78  	}
    79  
    80  	if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {
    81  		t.Fatalf("Expected file 'do.md' to exist")
    82  	}
    83  }
    84  
    85  func BenchmarkGenMarkdownToFile(b *testing.B) {
    86  	file, err := ioutil.TempFile("", "")
    87  	if err != nil {
    88  		b.Fatal(err)
    89  	}
    90  	defer os.Remove(file.Name())
    91  	defer file.Close()
    92  
    93  	b.ResetTimer()
    94  	for i := 0; i < b.N; i++ {
    95  		if err := GenMarkdown(rootCmd, file); err != nil {
    96  			b.Fatal(err)
    97  		}
    98  	}
    99  }