github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/app/help_test.go (about)

     1  package app_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/cloudfoundry/cli/cf/api"
     9  	"github.com/cloudfoundry/cli/cf/app"
    10  	"github.com/cloudfoundry/cli/cf/command_factory"
    11  	"github.com/cloudfoundry/cli/cf/configuration/config_helpers"
    12  	testPluginConfig "github.com/cloudfoundry/cli/cf/configuration/plugin_config/fakes"
    13  	"github.com/cloudfoundry/cli/cf/manifest"
    14  	"github.com/cloudfoundry/cli/cf/net"
    15  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    16  	io_helpers "github.com/cloudfoundry/cli/testhelpers/io"
    17  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    18  	"github.com/codegangsta/cli"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("Help", func() {
    24  	It("shows help for all commands", func() {
    25  		commandFactory := createCommandFactory()
    26  
    27  		dummyTemplate := `
    28  {{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    29  {{.Name}}
    30  {{end}}{{end}}{{end}}
    31  `
    32  		output := io_helpers.CaptureOutput(func() {
    33  			app.ShowHelp(dummyTemplate, createApp(commandFactory))
    34  		})
    35  
    36  		for _, metadata := range commandFactory.CommandMetadatas() {
    37  			Expect(commandInOutput(metadata.Name, output)).To(BeTrue(), metadata.Name+" not in help")
    38  		}
    39  	})
    40  
    41  	It("shows help for all installed plugin's commands", func() {
    42  		config_helpers.PluginRepoDir = func() string {
    43  			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
    44  		}
    45  
    46  		commandFactory := createCommandFactory()
    47  
    48  		dummyTemplate := `
    49  {{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    50  {{.Name}}
    51  {{end}}{{end}}{{end}}
    52  `
    53  		output := io_helpers.CaptureOutput(func() {
    54  			app.ShowHelp(dummyTemplate, createApp(commandFactory))
    55  		})
    56  
    57  		Expect(commandInOutput("test1_cmd2", output)).To(BeTrue(), "plugin command: test1_cmd2 not in help")
    58  		Expect(commandInOutput("test2_cmd1", output)).To(BeTrue(), "plugin command: test2_cmd1 not in help")
    59  		Expect(commandInOutput("test2_cmd2", output)).To(BeTrue(), "plugin command: test2_cmd2 not in help")
    60  
    61  	})
    62  
    63  	It("shows command's alias in help for installed plugin", func() {
    64  		config_helpers.PluginRepoDir = func() string {
    65  			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
    66  		}
    67  
    68  		commandFactory := createCommandFactory()
    69  
    70  		dummyTemplate := `
    71  {{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    72  {{.Name}}
    73  {{end}}{{end}}{{end}}
    74  `
    75  		output := io_helpers.CaptureOutput(func() {
    76  			app.ShowHelp(dummyTemplate, createApp(commandFactory))
    77  		})
    78  
    79  		Expect(commandInOutput("test1_cmd1, test1_cmd1_alias", output)).To(BeTrue(), "plugin command alias: test1_cmd1_alias not in help")
    80  	})
    81  
    82  })
    83  
    84  func createCommandFactory() command_factory.Factory {
    85  	fakeUI := &testterm.FakeUI{}
    86  	configRepo := testconfig.NewRepository()
    87  	pluginConfig := &testPluginConfig.FakePluginConfiguration{}
    88  
    89  	manifestRepo := manifest.NewManifestDiskRepository()
    90  	apiRepoLocator := api.NewRepositoryLocator(configRepo, map[string]net.Gateway{
    91  		"auth":             net.NewUAAGateway(configRepo, fakeUI),
    92  		"cloud-controller": net.NewCloudControllerGateway(configRepo, time.Now, fakeUI),
    93  		"uaa":              net.NewUAAGateway(configRepo, fakeUI),
    94  	})
    95  
    96  	return command_factory.NewFactory(fakeUI, configRepo, manifestRepo, apiRepoLocator, pluginConfig)
    97  }
    98  
    99  func createApp(commandFactory command_factory.Factory) *cli.App {
   100  	new_app := cli.NewApp()
   101  	new_app.Commands = []cli.Command{}
   102  	for _, metadata := range commandFactory.CommandMetadatas() {
   103  		new_app.Commands = append(new_app.Commands, cli.Command{Name: metadata.Name})
   104  	}
   105  
   106  	return new_app
   107  }
   108  
   109  func commandInOutput(cmdName string, output []string) bool {
   110  	for _, line := range output {
   111  		if strings.TrimSpace(line) == strings.TrimSpace(cmdName) {
   112  			return true
   113  		}
   114  	}
   115  	return false
   116  }