github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/cf/commands/pluginrepo/repo_plugins_test.go (about)

     1  package pluginrepo_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/actors/pluginrepo/pluginrepofakes"
     5  	"code.cloudfoundry.org/cli/cf/commands/pluginrepo"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
     9  
    10  	"code.cloudfoundry.org/cli/cf/commandregistry"
    11  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    12  
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  
    16  	clipr "github.com/cloudfoundry-incubator/cli-plugin-repo/web"
    17  
    18  	"code.cloudfoundry.org/cli/cf/flags"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("repo-plugins", func() {
    24  	var (
    25  		ui                  *testterm.FakeUI
    26  		config              coreconfig.Repository
    27  		requirementsFactory *requirementsfakes.FakeFactory
    28  		fakePluginRepo      *pluginrepofakes.FakePluginRepo
    29  		deps                commandregistry.Dependency
    30  		cmd                 *pluginrepo.RepoPlugins
    31  		flagContext         flags.FlagContext
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		fakePluginRepo = new(pluginrepofakes.FakePluginRepo)
    36  		ui = &testterm.FakeUI{}
    37  		requirementsFactory = new(requirementsfakes.FakeFactory)
    38  		config = testconfig.NewRepositoryWithDefaults()
    39  
    40  		deps = commandregistry.Dependency{
    41  			UI:         ui,
    42  			Config:     config,
    43  			PluginRepo: fakePluginRepo,
    44  		}
    45  
    46  		cmd = new(pluginrepo.RepoPlugins)
    47  		cmd = cmd.SetDependency(deps, false).(*pluginrepo.RepoPlugins)
    48  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    49  	})
    50  
    51  	var callRepoPlugins = func(args ...string) error {
    52  		err := flagContext.Parse(args...)
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		cmd.Execute(flagContext)
    58  		return nil
    59  	}
    60  
    61  	Context("when using the default CF-Community repo", func() {
    62  		BeforeEach(func() {
    63  			config.SetPluginRepo(models.PluginRepo{
    64  				Name: "cf",
    65  				URL:  "http://plugins.cloudfoundry.org",
    66  			})
    67  		})
    68  
    69  		It("uses https when pointing to plugins.cloudfoundry.org", func() {
    70  			err := callRepoPlugins()
    71  			Expect(err).NotTo(HaveOccurred())
    72  
    73  			Expect(fakePluginRepo.GetPluginsCallCount()).To(Equal(1))
    74  			Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].Name).To(Equal("cf"))
    75  			Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].URL).To(Equal("https://plugins.cloudfoundry.org"))
    76  			Expect(len(fakePluginRepo.GetPluginsArgsForCall(0))).To(Equal(1))
    77  		})
    78  	})
    79  
    80  	Context("when using other plugin repos", func() {
    81  		BeforeEach(func() {
    82  			config.SetPluginRepo(models.PluginRepo{
    83  				Name: "repo1",
    84  				URL:  "",
    85  			})
    86  
    87  			config.SetPluginRepo(models.PluginRepo{
    88  				Name: "repo2",
    89  				URL:  "",
    90  			})
    91  		})
    92  
    93  		Context("If repo name is provided by '-r'", func() {
    94  			It("list plugins from just the named repo", func() {
    95  				err := callRepoPlugins("-r", "repo2")
    96  				Expect(err).NotTo(HaveOccurred())
    97  
    98  				Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].Name).To(Equal("repo2"))
    99  				Expect(len(fakePluginRepo.GetPluginsArgsForCall(0))).To(Equal(1))
   100  
   101  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"Getting plugins from repository 'repo2'"}))
   102  			})
   103  		})
   104  
   105  		Context("If no repo name is provided", func() {
   106  			It("list plugins from just the named repo", func() {
   107  				err := callRepoPlugins()
   108  				Expect(err).NotTo(HaveOccurred())
   109  
   110  				Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].Name).To(Equal("repo1"))
   111  				Expect(len(fakePluginRepo.GetPluginsArgsForCall(0))).To(Equal(2))
   112  				Expect(fakePluginRepo.GetPluginsArgsForCall(0)[1].Name).To(Equal("repo2"))
   113  
   114  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"Getting plugins from all repositories"}))
   115  			})
   116  		})
   117  
   118  		Context("when GetPlugins returns a list of plugin meta data", func() {
   119  			It("lists all plugin data", func() {
   120  				result := make(map[string][]clipr.Plugin)
   121  				result["repo1"] = []clipr.Plugin{
   122  					{
   123  						Name:        "plugin1",
   124  						Description: "none1",
   125  					},
   126  				}
   127  				result["repo2"] = []clipr.Plugin{
   128  					{
   129  						Name:        "plugin2",
   130  						Description: "none2",
   131  					},
   132  				}
   133  				fakePluginRepo.GetPluginsReturns(result, []string{})
   134  
   135  				err := callRepoPlugins()
   136  				Expect(err).NotTo(HaveOccurred())
   137  
   138  				Expect(ui.Outputs()).NotTo(ContainSubstrings([]string{"Logged errors:"}))
   139  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"repo1"}))
   140  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"plugin1"}))
   141  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"repo2"}))
   142  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"plugin2"}))
   143  			})
   144  		})
   145  
   146  		Context("If errors are reported back from GetPlugins()", func() {
   147  			It("informs user about the errors", func() {
   148  				fakePluginRepo.GetPluginsReturns(nil, []string{
   149  					"error from repo1",
   150  					"error from repo2",
   151  				})
   152  
   153  				err := callRepoPlugins()
   154  				Expect(err).NotTo(HaveOccurred())
   155  
   156  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"Logged errors:"}))
   157  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"error from repo1"}))
   158  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"error from repo2"}))
   159  			})
   160  		})
   161  	})
   162  })