...

Source file src/github.com/spf13/cobra/doc/md_docs_test.go

Documentation: github.com/spf13/cobra/doc

     1  // Copyright 2013-2023 The Cobra Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package doc
    16  
    17  import (
    18  	"bytes"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func TestGenMdDoc(t *testing.T) {
    28  	// We generate on subcommand so we have both subcommands and parents.
    29  	buf := new(bytes.Buffer)
    30  	if err := GenMarkdown(echoCmd, buf); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	output := buf.String()
    34  
    35  	checkStringContains(t, output, echoCmd.Long)
    36  	checkStringContains(t, output, echoCmd.Example)
    37  	checkStringContains(t, output, "boolone")
    38  	checkStringContains(t, output, "rootflag")
    39  	checkStringContains(t, output, rootCmd.Short)
    40  	checkStringContains(t, output, echoSubCmd.Short)
    41  	checkStringOmits(t, output, deprecatedCmd.Short)
    42  	checkStringContains(t, output, "Options inherited from parent commands")
    43  }
    44  
    45  func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {
    46  	// We generate on subcommand so we have both subcommands and parents.
    47  	buf := new(bytes.Buffer)
    48  	if err := GenMarkdown(dummyCmd, buf); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	output := buf.String()
    52  
    53  	checkStringContains(t, output, dummyCmd.Example)
    54  	checkStringContains(t, output, dummyCmd.Short)
    55  	checkStringContains(t, output, "Options inherited from parent commands")
    56  	checkStringOmits(t, output, "### Synopsis")
    57  }
    58  
    59  func TestGenMdNoHiddenParents(t *testing.T) {
    60  	// We generate on subcommand so we have both subcommands and parents.
    61  	for _, name := range []string{"rootflag", "strtwo"} {
    62  		f := rootCmd.PersistentFlags().Lookup(name)
    63  		f.Hidden = true
    64  		defer func() { f.Hidden = false }()
    65  	}
    66  	buf := new(bytes.Buffer)
    67  	if err := GenMarkdown(echoCmd, buf); err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	output := buf.String()
    71  
    72  	checkStringContains(t, output, echoCmd.Long)
    73  	checkStringContains(t, output, echoCmd.Example)
    74  	checkStringContains(t, output, "boolone")
    75  	checkStringOmits(t, output, "rootflag")
    76  	checkStringContains(t, output, rootCmd.Short)
    77  	checkStringContains(t, output, echoSubCmd.Short)
    78  	checkStringOmits(t, output, deprecatedCmd.Short)
    79  	checkStringOmits(t, output, "Options inherited from parent commands")
    80  }
    81  
    82  func TestGenMdNoTag(t *testing.T) {
    83  	rootCmd.DisableAutoGenTag = true
    84  	defer func() { rootCmd.DisableAutoGenTag = false }()
    85  
    86  	buf := new(bytes.Buffer)
    87  	if err := GenMarkdown(rootCmd, buf); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	output := buf.String()
    91  
    92  	checkStringOmits(t, output, "Auto generated")
    93  }
    94  
    95  func TestGenMdTree(t *testing.T) {
    96  	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
    97  	tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")
    98  	if err != nil {
    99  		t.Fatalf("Failed to create tmpdir: %v", err)
   100  	}
   101  	defer os.RemoveAll(tmpdir)
   102  
   103  	if err := GenMarkdownTree(c, tmpdir); err != nil {
   104  		t.Fatalf("GenMarkdownTree failed: %v", err)
   105  	}
   106  
   107  	if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {
   108  		t.Fatalf("Expected file 'do.md' to exist")
   109  	}
   110  }
   111  
   112  func BenchmarkGenMarkdownToFile(b *testing.B) {
   113  	file, err := ioutil.TempFile("", "")
   114  	if err != nil {
   115  		b.Fatal(err)
   116  	}
   117  	defer os.Remove(file.Name())
   118  	defer file.Close()
   119  
   120  	b.ResetTimer()
   121  	for i := 0; i < b.N; i++ {
   122  		if err := GenMarkdown(rootCmd, file); err != nil {
   123  			b.Fatal(err)
   124  		}
   125  	}
   126  }
   127  

View as plain text