Skip to content
Myr

md_docs_test.go

This file

Path

doc/md_docs_test.go

125 lines · 3.5 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 TestGenMdDoc(t *testing.T) {27	// We generate on subcommand so we have both subcommands and parents.28	buf := new(bytes.Buffer)29	if err := GenMarkdown(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	checkStringContains(t, output, "Options inherited from parent commands")42}4344func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {45	// We generate on subcommand so we have both subcommands and parents.46	buf := new(bytes.Buffer)47	if err := GenMarkdown(dummyCmd, buf); err != nil {48		t.Fatal(err)49	}50	output := buf.String()5152	checkStringContains(t, output, dummyCmd.Example)53	checkStringContains(t, output, dummyCmd.Short)54	checkStringContains(t, output, "Options inherited from parent commands")55	checkStringOmits(t, output, "### Synopsis")56}5758func TestGenMdNoHiddenParents(t *testing.T) {59	// We generate on subcommand so we have both subcommands and parents.60	for _, name := range []string{"rootflag", "strtwo"} {61		f := rootCmd.PersistentFlags().Lookup(name)62		f.Hidden = true63		defer func() { f.Hidden = false }()64	}65	buf := new(bytes.Buffer)66	if err := GenMarkdown(echoCmd, buf); err != nil {67		t.Fatal(err)68	}69	output := buf.String()7071	checkStringContains(t, output, echoCmd.Long)72	checkStringContains(t, output, echoCmd.Example)73	checkStringContains(t, output, "boolone")74	checkStringOmits(t, output, "rootflag")75	checkStringContains(t, output, rootCmd.Short)76	checkStringContains(t, output, echoSubCmd.Short)77	checkStringOmits(t, output, deprecatedCmd.Short)78	checkStringOmits(t, output, "Options inherited from parent commands")79}8081func TestGenMdNoTag(t *testing.T) {82	rootCmd.DisableAutoGenTag = true83	defer func() { rootCmd.DisableAutoGenTag = false }()8485	buf := new(bytes.Buffer)86	if err := GenMarkdown(rootCmd, buf); err != nil {87		t.Fatal(err)88	}89	output := buf.String()9091	checkStringOmits(t, output, "Auto generated")92}9394func TestGenMdTree(t *testing.T) {95	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}96	tmpdir, err := os.MkdirTemp("", "test-gen-md-tree")97	if err != nil {98		t.Fatalf("Failed to create tmpdir: %v", err)99	}100	defer os.RemoveAll(tmpdir)101102	if err := GenMarkdownTree(c, tmpdir); err != nil {103		t.Fatalf("GenMarkdownTree failed: %v", err)104	}105106	if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {107		t.Fatalf("Expected file 'do.md' to exist")108	}109}110111func BenchmarkGenMarkdownToFile(b *testing.B) {112	file, err := os.CreateTemp("", "")113	if err != nil {114		b.Fatal(err)115	}116	defer os.Remove(file.Name())117	defer file.Close()118119	b.ResetTimer()120	for i := 0; i < b.N; i++ {121		if err := GenMarkdown(rootCmd, file); err != nil {122			b.Fatal(err)123		}124	}125}