Skip to content
Myr

rest_docs_test.go

This file

Path

doc/rest_docs_test.go

99 lines · 2.5 KiB · Go

Last changed by

Not known.

History of this file

Take it

1package doc23import (4	"bytes"5	"io/ioutil"6	"os"7	"path/filepath"8	"testing"910	"github.com/spf13/cobra"11)1213func TestGenRSTDoc(t *testing.T) {14	// We generate on a subcommand so we have both subcommands and parents15	buf := new(bytes.Buffer)16	if err := GenReST(echoCmd, buf); err != nil {17		t.Fatal(err)18	}19	output := buf.String()2021	checkStringContains(t, output, echoCmd.Long)22	checkStringContains(t, output, echoCmd.Example)23	checkStringContains(t, output, "boolone")24	checkStringContains(t, output, "rootflag")25	checkStringContains(t, output, rootCmd.Short)26	checkStringContains(t, output, echoSubCmd.Short)27	checkStringOmits(t, output, deprecatedCmd.Short)28}2930func TestGenRSTNoHiddenParents(t *testing.T) {31	// We generate on a subcommand so we have both subcommands and parents32	for _, name := range []string{"rootflag", "strtwo"} {33		f := rootCmd.PersistentFlags().Lookup(name)34		f.Hidden = true35		defer func() { f.Hidden = false }()36	}37	buf := new(bytes.Buffer)38	if err := GenReST(echoCmd, buf); err != nil {39		t.Fatal(err)40	}41	output := buf.String()4243	checkStringContains(t, output, echoCmd.Long)44	checkStringContains(t, output, echoCmd.Example)45	checkStringContains(t, output, "boolone")46	checkStringOmits(t, output, "rootflag")47	checkStringContains(t, output, rootCmd.Short)48	checkStringContains(t, output, echoSubCmd.Short)49	checkStringOmits(t, output, deprecatedCmd.Short)50	checkStringOmits(t, output, "Options inherited from parent commands")51}5253func TestGenRSTNoTag(t *testing.T) {54	rootCmd.DisableAutoGenTag = true55	defer func() { rootCmd.DisableAutoGenTag = false }()5657	buf := new(bytes.Buffer)58	if err := GenReST(rootCmd, buf); err != nil {59		t.Fatal(err)60	}61	output := buf.String()6263	unexpected := "Auto generated"64	checkStringOmits(t, output, unexpected)65}6667func TestGenRSTTree(t *testing.T) {68	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}6970	tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree")71	if err != nil {72		t.Fatalf("Failed to create tmpdir: %s", err.Error())73	}74	defer os.RemoveAll(tmpdir)7576	if err := GenReSTTree(c, tmpdir); err != nil {77		t.Fatalf("GenReSTTree failed: %s", err.Error())78	}7980	if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {81		t.Fatalf("Expected file 'do.rst' to exist")82	}83}8485func BenchmarkGenReSTToFile(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()9293	b.ResetTimer()94	for i := 0; i < b.N; i++ {95		if err := GenReST(rootCmd, file); err != nil {96			b.Fatal(err)97		}98	}99}