Skip to content
Myr

rest_docs_test.go

This file

Path

doc/rest_docs_test.go

112 lines · 3.0 KiB · Go

Last changed by

Replace deprecated ioutil usage (#2181)Nir Sofferbefore adoption511af59

History of this file

Take it

1// Copyright 2013-2023 The Cobra Authors2//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 at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415package doc1617import (18	"bytes"19	"os"20	"path/filepath"21	"testing"2223	"github.com/spf13/cobra"24)2526func TestGenRSTDoc(t *testing.T) {27	// We generate on a subcommand so we have both subcommands and parents28	buf := new(bytes.Buffer)29	if err := GenReST(echoCmd, buf); err != nil {30		t.Fatal(err)31	}32	output := buf.String()3334	checkStringContains(t, output, echoCmd.Long)35	checkStringContains(t, output, echoCmd.Example)36	checkStringContains(t, output, "boolone")37	checkStringContains(t, output, "rootflag")38	checkStringContains(t, output, rootCmd.Short)39	checkStringContains(t, output, echoSubCmd.Short)40	checkStringOmits(t, output, deprecatedCmd.Short)41}4243func TestGenRSTNoHiddenParents(t *testing.T) {44	// We generate on a subcommand so we have both subcommands and parents45	for _, name := range []string{"rootflag", "strtwo"} {46		f := rootCmd.PersistentFlags().Lookup(name)47		f.Hidden = true48		defer func() { f.Hidden = false }()49	}50	buf := new(bytes.Buffer)51	if err := GenReST(echoCmd, buf); err != nil {52		t.Fatal(err)53	}54	output := buf.String()5556	checkStringContains(t, output, echoCmd.Long)57	checkStringContains(t, output, echoCmd.Example)58	checkStringContains(t, output, "boolone")59	checkStringOmits(t, output, "rootflag")60	checkStringContains(t, output, rootCmd.Short)61	checkStringContains(t, output, echoSubCmd.Short)62	checkStringOmits(t, output, deprecatedCmd.Short)63	checkStringOmits(t, output, "Options inherited from parent commands")64}6566func TestGenRSTNoTag(t *testing.T) {67	rootCmd.DisableAutoGenTag = true68	defer func() { rootCmd.DisableAutoGenTag = false }()6970	buf := new(bytes.Buffer)71	if err := GenReST(rootCmd, buf); err != nil {72		t.Fatal(err)73	}74	output := buf.String()7576	unexpected := "Auto generated"77	checkStringOmits(t, output, unexpected)78}7980func TestGenRSTTree(t *testing.T) {81	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}8283	tmpdir, err := os.MkdirTemp("", "test-gen-rst-tree")84	if err != nil {85		t.Fatalf("Failed to create tmpdir: %s", err.Error())86	}87	defer os.RemoveAll(tmpdir)8889	if err := GenReSTTree(c, tmpdir); err != nil {90		t.Fatalf("GenReSTTree failed: %s", err.Error())91	}9293	if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {94		t.Fatalf("Expected file 'do.rst' to exist")95	}96}9798func BenchmarkGenReSTToFile(b *testing.B) {99	file, err := os.CreateTemp("", "")100	if err != nil {101		b.Fatal(err)102	}103	defer os.Remove(file.Name())104	defer file.Close()105106	b.ResetTimer()107	for i := 0; i < b.N; i++ {108		if err := GenReST(rootCmd, file); err != nil {109			b.Fatal(err)110		}111	}112}