...

Source file src/github.com/spf13/cobra/doc/rest_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 TestGenRSTDoc(t *testing.T) {
    28  	// We generate on a subcommand so we have both subcommands and parents
    29  	buf := new(bytes.Buffer)
    30  	if err := GenReST(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  }
    43  
    44  func TestGenRSTNoHiddenParents(t *testing.T) {
    45  	// We generate on a subcommand so we have both subcommands and parents
    46  	for _, name := range []string{"rootflag", "strtwo"} {
    47  		f := rootCmd.PersistentFlags().Lookup(name)
    48  		f.Hidden = true
    49  		defer func() { f.Hidden = false }()
    50  	}
    51  	buf := new(bytes.Buffer)
    52  	if err := GenReST(echoCmd, buf); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	output := buf.String()
    56  
    57  	checkStringContains(t, output, echoCmd.Long)
    58  	checkStringContains(t, output, echoCmd.Example)
    59  	checkStringContains(t, output, "boolone")
    60  	checkStringOmits(t, output, "rootflag")
    61  	checkStringContains(t, output, rootCmd.Short)
    62  	checkStringContains(t, output, echoSubCmd.Short)
    63  	checkStringOmits(t, output, deprecatedCmd.Short)
    64  	checkStringOmits(t, output, "Options inherited from parent commands")
    65  }
    66  
    67  func TestGenRSTNoTag(t *testing.T) {
    68  	rootCmd.DisableAutoGenTag = true
    69  	defer func() { rootCmd.DisableAutoGenTag = false }()
    70  
    71  	buf := new(bytes.Buffer)
    72  	if err := GenReST(rootCmd, buf); err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	output := buf.String()
    76  
    77  	unexpected := "Auto generated"
    78  	checkStringOmits(t, output, unexpected)
    79  }
    80  
    81  func TestGenRSTTree(t *testing.T) {
    82  	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
    83  
    84  	tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree")
    85  	if err != nil {
    86  		t.Fatalf("Failed to create tmpdir: %s", err.Error())
    87  	}
    88  	defer os.RemoveAll(tmpdir)
    89  
    90  	if err := GenReSTTree(c, tmpdir); err != nil {
    91  		t.Fatalf("GenReSTTree failed: %s", err.Error())
    92  	}
    93  
    94  	if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {
    95  		t.Fatalf("Expected file 'do.rst' to exist")
    96  	}
    97  }
    98  
    99  func BenchmarkGenReSTToFile(b *testing.B) {
   100  	file, err := ioutil.TempFile("", "")
   101  	if err != nil {
   102  		b.Fatal(err)
   103  	}
   104  	defer os.Remove(file.Name())
   105  	defer file.Close()
   106  
   107  	b.ResetTimer()
   108  	for i := 0; i < b.N; i++ {
   109  		if err := GenReST(rootCmd, file); err != nil {
   110  			b.Fatal(err)
   111  		}
   112  	}
   113  }
   114  

View as plain text