github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/cli_app_factory/app_help_test.go (about)

     1  package cli_app_factory_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"strings"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gbytes"
    11  
    12  	"github.com/cloudfoundry-incubator/ltc/cli_app_factory"
    13  	"github.com/cloudfoundry-incubator/ltc/config"
    14  	"github.com/cloudfoundry-incubator/ltc/config/persister"
    15  	"github.com/cloudfoundry-incubator/ltc/receptor_client/fake_receptor_client_creator"
    16  	"github.com/cloudfoundry-incubator/ltc/terminal"
    17  	"github.com/codegangsta/cli"
    18  )
    19  
    20  var _ = Describe("AppHelp", func() {
    21  	var (
    22  		cliApp                    *cli.App
    23  		outputBuffer              *gbytes.Buffer
    24  		fakeReceptorClientCreator *fake_receptor_client_creator.FakeCreator
    25  	)
    26  
    27  	dummyTemplate := `
    28  		{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
    29  		{{.Name}}
    30  		{{end}}{{end}}{{end}}`
    31  
    32  	subCommandTemplate := `NAME:
    33     {{.Name}} - {{.Usage}}
    34  USAGE:
    35     {{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...]
    36  `
    37  	BeforeEach(func() {
    38  		outputBuffer = gbytes.NewBuffer()
    39  
    40  		fakeReceptorClientCreator = &fake_receptor_client_creator.FakeCreator{}
    41  
    42  		cliApp = cli_app_factory.MakeCliApp(
    43  			"",
    44  			"",
    45  			"~/",
    46  			nil,
    47  			config.New(persister.NewMemPersister()),
    48  			nil,
    49  			fakeReceptorClientCreator,
    50  			nil,
    51  			terminal.NewUI(nil, outputBuffer, nil),
    52  		)
    53  	})
    54  
    55  	Describe("ShowHelp", func() {
    56  
    57  		It("shows help for all commands", func() {
    58  			Expect(cliApp.Commands).NotTo(BeEmpty())
    59  
    60  			cli_app_factory.ShowHelp(outputBuffer, dummyTemplate, cliApp)
    61  
    62  			outputBytes, err := ioutil.ReadAll(outputBuffer)
    63  			Expect(err).NotTo(HaveOccurred())
    64  			for _, command := range cliApp.Commands {
    65  				commandName := strings.TrimSpace(strings.Join(command.Names(), ", "))
    66  				Expect(string(outputBytes)).To(ContainSubstring(commandName))
    67  			}
    68  		})
    69  
    70  		It("shows help for a specific command", func() {
    71  			subCommand := cli.Command{Name: "print-a-command"}
    72  
    73  			cli_app_factory.ShowHelp(outputBuffer, subCommandTemplate, subCommand)
    74  
    75  			outputBytes, err := ioutil.ReadAll(outputBuffer)
    76  			Expect(err).NotTo(HaveOccurred())
    77  			Expect(string(outputBytes)).To(ContainSubstring(subCommand.Name))
    78  		})
    79  
    80  		It("panics for other type", func() {
    81  			showHelp := func() func() {
    82  				return func() { cli_app_factory.ShowHelp(outputBuffer, dummyTemplate, struct{}{}) }
    83  			}
    84  
    85  			Consistently(showHelp).Should(Panic(), "unsupported type but help didn't panic")
    86  		})
    87  
    88  		Context("when writer is busted", func() {
    89  			It("panics showing help for all commands", func() {
    90  				showHelp := func() func() {
    91  					return func() { cli_app_factory.ShowHelp(errorWriter{}, dummyTemplate, cliApp) }
    92  				}
    93  
    94  				Consistently(showHelp).Should(Panic(), "writer bailed but help didn't panic")
    95  			})
    96  
    97  			It("panics showing help for a specific command", func() {
    98  				showHelp := func() func() {
    99  					return func() { cli_app_factory.ShowHelp(errorWriter{}, dummyTemplate, cli.Command{}) }
   100  				}
   101  
   102  				Consistently(showHelp).Should(Panic(), "writer bailed but help didn't panic")
   103  			})
   104  		})
   105  
   106  	})
   107  })
   108  
   109  type errorWriter struct{}
   110  
   111  func (errorWriter) Write(p []byte) (n int, err error) {
   112  	return -1, errors.New("no bueno")
   113  }