...

Source file src/github.com/spf13/cobra/doc/yaml_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  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func TestGenYamlDoc(t *testing.T) {
    29  	// We generate on s subcommand so we have both subcommands and parents
    30  	buf := new(bytes.Buffer)
    31  	if err := GenYaml(echoCmd, buf); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	output := buf.String()
    35  
    36  	checkStringContains(t, output, echoCmd.Long)
    37  	checkStringContains(t, output, echoCmd.Example)
    38  	checkStringContains(t, output, "boolone")
    39  	checkStringContains(t, output, "rootflag")
    40  	checkStringContains(t, output, rootCmd.Short)
    41  	checkStringContains(t, output, echoSubCmd.Short)
    42  	checkStringContains(t, output, fmt.Sprintf("- %s - %s", echoSubCmd.CommandPath(), echoSubCmd.Short))
    43  }
    44  
    45  func TestGenYamlNoTag(t *testing.T) {
    46  	rootCmd.DisableAutoGenTag = true
    47  	defer func() { rootCmd.DisableAutoGenTag = false }()
    48  
    49  	buf := new(bytes.Buffer)
    50  	if err := GenYaml(rootCmd, buf); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	output := buf.String()
    54  
    55  	checkStringOmits(t, output, "Auto generated")
    56  }
    57  
    58  func TestGenYamlTree(t *testing.T) {
    59  	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
    60  
    61  	tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")
    62  	if err != nil {
    63  		t.Fatalf("Failed to create tmpdir: %s", err.Error())
    64  	}
    65  	defer os.RemoveAll(tmpdir)
    66  
    67  	if err := GenYamlTree(c, tmpdir); err != nil {
    68  		t.Fatalf("GenYamlTree failed: %s", err.Error())
    69  	}
    70  
    71  	if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {
    72  		t.Fatalf("Expected file 'do.yaml' to exist")
    73  	}
    74  }
    75  
    76  func TestGenYamlDocRunnable(t *testing.T) {
    77  	// Testing a runnable command: should contain the "usage" field
    78  	buf := new(bytes.Buffer)
    79  	if err := GenYaml(rootCmd, buf); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	output := buf.String()
    83  
    84  	checkStringContains(t, output, "usage: "+rootCmd.Use)
    85  }
    86  
    87  func BenchmarkGenYamlToFile(b *testing.B) {
    88  	file, err := ioutil.TempFile("", "")
    89  	if err != nil {
    90  		b.Fatal(err)
    91  	}
    92  	defer os.Remove(file.Name())
    93  	defer file.Close()
    94  
    95  	b.ResetTimer()
    96  	for i := 0; i < b.N; i++ {
    97  		if err := GenYaml(rootCmd, file); err != nil {
    98  			b.Fatal(err)
    99  		}
   100  	}
   101  }
   102  

View as plain text