github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/config_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/command_registry"
     5  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     6  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     7  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     8  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
     9  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    10  
    11  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("config command", func() {
    17  	var (
    18  		ui                  *testterm.FakeUI
    19  		configRepo          core_config.Repository
    20  		requirementsFactory *testreq.FakeReqFactory
    21  		deps                command_registry.Dependency
    22  	)
    23  
    24  	updateCommandDependency := func(pluginCall bool) {
    25  		deps.Ui = ui
    26  		deps.Config = configRepo
    27  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("config").SetDependency(deps, pluginCall))
    28  	}
    29  
    30  	BeforeEach(func() {
    31  		ui = &testterm.FakeUI{}
    32  		configRepo = testconfig.NewRepositoryWithDefaults()
    33  		requirementsFactory = &testreq.FakeReqFactory{}
    34  	})
    35  
    36  	runCommand := func(args ...string) {
    37  		testcmd.RunCliCommand("config", args, requirementsFactory, updateCommandDependency, false)
    38  	}
    39  	It("fails requirements when no flags are provided", func() {
    40  		runCommand()
    41  		Expect(ui.Outputs).To(ContainSubstrings(
    42  			[]string{"Incorrect Usage"},
    43  		))
    44  	})
    45  
    46  	Context("--async-timeout flag", func() {
    47  
    48  		It("stores the timeout in minutes when the --async-timeout flag is provided", func() {
    49  			runCommand("--async-timeout", "12")
    50  			Expect(configRepo.AsyncTimeout()).Should(Equal(uint(12)))
    51  		})
    52  
    53  		It("fails with usage when a invalid async timeout value is passed", func() {
    54  			runCommand("--async-timeout", "-1")
    55  			Expect(ui.Outputs).To(ContainSubstrings(
    56  				[]string{"Incorrect Usage"},
    57  			))
    58  		})
    59  
    60  		It("fails with usage when a negative timout is passed", func() {
    61  			runCommand("--async-timeout", "-555")
    62  			Expect(ui.Outputs).To(ContainSubstrings(
    63  				[]string{"Incorrect Usage"},
    64  			))
    65  			Expect(configRepo.AsyncTimeout()).To(Equal(uint(0)))
    66  		})
    67  	})
    68  
    69  	Context("--trace flag", func() {
    70  		It("stores the trace value when --trace flag is provided", func() {
    71  			runCommand("--trace", "true")
    72  			Expect(configRepo.Trace()).Should(Equal("true"))
    73  
    74  			runCommand("--trace", "false")
    75  			Expect(configRepo.Trace()).Should(Equal("false"))
    76  
    77  			runCommand("--trace", "some/file/lol")
    78  			Expect(configRepo.Trace()).Should(Equal("some/file/lol"))
    79  		})
    80  	})
    81  
    82  	Context("--color flag", func() {
    83  		It("stores the color value when --color flag is provided", func() {
    84  			runCommand("--color", "true")
    85  			Expect(configRepo.ColorEnabled()).Should(Equal("true"))
    86  
    87  			runCommand("--color", "false")
    88  			Expect(configRepo.ColorEnabled()).Should(Equal("false"))
    89  		})
    90  
    91  		It("fails with usage when a non-bool value is provided", func() {
    92  			runCommand("--color", "plaid")
    93  			Expect(ui.Outputs).To(ContainSubstrings(
    94  				[]string{"Incorrect Usage"},
    95  			))
    96  		})
    97  	})
    98  
    99  	Context("--locale flag", func() {
   100  		It("stores the locale value when --locale [locale] is provided", func() {
   101  			runCommand("--locale", "zh_Hans")
   102  			Expect(configRepo.Locale()).Should(Equal("zh_Hans"))
   103  		})
   104  
   105  		It("informs the user of known locales if an unknown locale is provided", func() {
   106  			runCommand("--locale", "foo_BAR")
   107  			Expect(ui.Outputs).To(ContainSubstrings(
   108  				[]string{"known locales are"},
   109  				[]string{"en_US"},
   110  				[]string{"fr_FR"},
   111  			))
   112  		})
   113  
   114  		Context("when the locale is already set", func() {
   115  			BeforeEach(func() {
   116  				configRepo.SetLocale("fr_FR")
   117  				Expect(configRepo.Locale()).Should(Equal("fr_FR"))
   118  			})
   119  
   120  			It("clears the locale when the '--locale clear' flag is provided", func() {
   121  				runCommand("--locale", "CLEAR")
   122  				Expect(configRepo.Locale()).Should(Equal(""))
   123  			})
   124  		})
   125  	})
   126  })