Skip to content
Myr

yaml_docs_test.go

This file

Path

doc/yaml_docs_test.go

85 lines · 1.9 KiB · Go

Last changed by

YAML documentation contains "Usage" (#1037)Alessandro (Ale) Segalabefore adoptioned7b60e

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 TestGenYamlDoc(t *testing.T) {14	// We generate on s subcommand so we have both subcommands and parents15	buf := new(bytes.Buffer)16	if err := GenYaml(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}2829func TestGenYamlNoTag(t *testing.T) {30	rootCmd.DisableAutoGenTag = true31	defer func() { rootCmd.DisableAutoGenTag = false }()3233	buf := new(bytes.Buffer)34	if err := GenYaml(rootCmd, buf); err != nil {35		t.Fatal(err)36	}37	output := buf.String()3839	checkStringOmits(t, output, "Auto generated")40}4142func TestGenYamlTree(t *testing.T) {43	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}4445	tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")46	if err != nil {47		t.Fatalf("Failed to create tmpdir: %s", err.Error())48	}49	defer os.RemoveAll(tmpdir)5051	if err := GenYamlTree(c, tmpdir); err != nil {52		t.Fatalf("GenYamlTree failed: %s", err.Error())53	}5455	if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {56		t.Fatalf("Expected file 'do.yaml' to exist")57	}58}5960func TestGenYamlDocRunnable(t *testing.T) {61	// Testing a runnable command: should contain the "usage" field62	buf := new(bytes.Buffer)63	if err := GenYaml(rootCmd, buf); err != nil {64		t.Fatal(err)65	}66	output := buf.String()6768	checkStringContains(t, output, "usage: "+rootCmd.Use)69}7071func BenchmarkGenYamlToFile(b *testing.B) {72	file, err := ioutil.TempFile("", "")73	if err != nil {74		b.Fatal(err)75	}76	defer os.Remove(file.Name())77	defer file.Close()7879	b.ResetTimer()80	for i := 0; i < b.N; i++ {81		if err := GenYaml(rootCmd, file); err != nil {82			b.Fatal(err)83		}84	}85}