github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/help/help_test.go (about)

     1  package help_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/confighelpers"
     9  	"code.cloudfoundry.org/cli/cf/help"
    10  
    11  	"code.cloudfoundry.org/cli/util/testhelpers/io"
    12  
    13  	"os"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	"github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("Help", func() {
    21  	var buffer *gbytes.Buffer
    22  	BeforeEach(func() {
    23  		buffer = gbytes.NewBuffer()
    24  	})
    25  
    26  	AfterEach(func() {
    27  		buffer.Close()
    28  	})
    29  
    30  	It("shows help for all commands", func() {
    31  		dummyTemplate := `
    32  {{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    33  {{.Name}}
    34  {{end}}{{end}}{{end}}
    35  `
    36  		help.ShowHelp(buffer, dummyTemplate)
    37  
    38  		Expect(buffer).To(gbytes.Say("login"))
    39  		for _, metadata := range commandregistry.Commands.Metadatas() {
    40  			if metadata.Hidden {
    41  				continue
    42  			}
    43  			Expect(buffer.Contents()).To(ContainSubstring(metadata.Name))
    44  		}
    45  	})
    46  
    47  	It("shows help for all installed plugin's commands", func() {
    48  		confighelpers.PluginRepoDir = func() string {
    49  			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
    50  		}
    51  
    52  		dummyTemplate := `
    53  {{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    54  {{.Name}}
    55  {{end}}{{end}}{{end}}
    56  `
    57  		help.ShowHelp(buffer, dummyTemplate)
    58  		Expect(buffer.Contents()).To(ContainSubstring("test1_cmd2"))
    59  		Expect(buffer.Contents()).To(ContainSubstring("test2_cmd1"))
    60  		Expect(buffer.Contents()).To(ContainSubstring("test2_cmd2"))
    61  		Expect(buffer.Contents()).To(ContainSubstring("test2_really_long_really_long_really_long_command_name"))
    62  	})
    63  
    64  	It("adjusts the output format to the longest length of plugin command name", func() {
    65  		confighelpers.PluginRepoDir = func() string {
    66  			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
    67  		}
    68  
    69  		dummyTemplate := `
    70  {{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    71  {{.Name}}%%%{{.Description}}
    72  {{end}}{{end}}{{end}}
    73  `
    74  		output := io.CaptureOutput(func() {
    75  			help.ShowHelp(os.Stdout, dummyTemplate)
    76  		})
    77  
    78  		cmdNameLen := len(strings.Split(output[2], "%%%")[0])
    79  
    80  		for _, line := range output {
    81  			if strings.TrimSpace(line) == "" {
    82  				continue
    83  			}
    84  
    85  			expectedLen := len(strings.Split(line, "%%%")[0])
    86  			Expect(cmdNameLen).To(Equal(expectedLen))
    87  		}
    88  
    89  	})
    90  
    91  	It("does not show command's alias in help for installed plugin", func() {
    92  		confighelpers.PluginRepoDir = func() string {
    93  			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
    94  		}
    95  
    96  		dummyTemplate := `
    97  {{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    98  {{.Name}}
    99  {{end}}{{end}}{{end}}
   100  `
   101  		help.ShowHelp(buffer, dummyTemplate)
   102  		Expect(buffer).ToNot(gbytes.Say("test1_cmd1_alias"))
   103  	})
   104  })