Skip to content
Myr

md_docs_test.go

This file

Path

md_docs_test.go

67 lines · 1.9 KiB · Go

Last changed by

Deprecated subcommandsEric Parisbefore adoptionc3e48f9

History of this file

Take it

1package cobra23import (4	"bytes"5	"fmt"6	"os"7	"strings"8	"testing"9)1011var _ = fmt.Println12var _ = os.Stderr1314func TestGenMdDoc(t *testing.T) {15	c := initializeWithRootCmd()16	// Need two commands to run the command alphabetical sort17	cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)18	c.AddCommand(cmdPrint, cmdEcho)19	cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)2021	out := new(bytes.Buffer)2223	// We generate on s subcommand so we have both subcommands and parents24	GenMarkdown(cmdEcho, out)25	found := out.String()2627	// Our description28	expected := cmdEcho.Long29	if !strings.Contains(found, expected) {30		t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)31	}3233	// Better have our example34	expected = cmdEcho.Example35	if !strings.Contains(found, expected) {36		t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)37	}3839	// A local flag40	expected = "boolone"41	if !strings.Contains(found, expected) {42		t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)43	}4445	// persistent flag on parent46	expected = "rootflag"47	if !strings.Contains(found, expected) {48		t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)49	}5051	// We better output info about our parent52	expected = cmdRootWithRun.Short53	if !strings.Contains(found, expected) {54		t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)55	}5657	// And about subcommands58	expected = cmdEchoSub.Short59	if !strings.Contains(found, expected) {60		t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)61	}6263	unexpected := cmdDeprecated.Short64	if strings.Contains(found, unexpected) {65		t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)66	}67}