github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/plugin/plugins_command_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/pluginaction"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/plugin"
    11  	"code.cloudfoundry.org/cli/command/plugin/pluginfakes"
    12  	"code.cloudfoundry.org/cli/command/translatableerror"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("plugins Command", func() {
    21  	var (
    22  		cmd        PluginsCommand
    23  		testUI     *ui.UI
    24  		fakeConfig *commandfakes.FakeConfig
    25  		executeErr error
    26  		fakeActor  *pluginfakes.FakePluginsActor
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  		fakeActor = new(pluginfakes.FakePluginsActor)
    33  		cmd = PluginsCommand{UI: testUI, Config: fakeConfig, Actor: fakeActor}
    34  		cmd.Checksum = false
    35  
    36  		fakeConfig.BinaryNameReturns("faceman")
    37  	})
    38  
    39  	JustBeforeEach(func() {
    40  		executeErr = cmd.Execute(nil)
    41  	})
    42  
    43  	When("there are no plugins installed", func() {
    44  		It("displays the empty table", func() {
    45  			Expect(executeErr).NotTo(HaveOccurred())
    46  
    47  			Expect(testUI.Out).To(Say("Listing installed plugins..."))
    48  			Expect(testUI.Out).To(Say(""))
    49  			Expect(testUI.Out).To(Say(`plugin\s+version\s+command name\s+command help`))
    50  			Expect(testUI.Out).To(Say(""))
    51  			Expect(testUI.Out).To(Say(`Use 'faceman repo-plugins' to list plugins in registered repos available to install\.`))
    52  			Expect(testUI.Out).ToNot(Say("[A-Za-z0-9]+"))
    53  		})
    54  
    55  		When("the --checksum flag is provided", func() {
    56  			BeforeEach(func() {
    57  				cmd.Checksum = true
    58  			})
    59  
    60  			It("displays the empty checksums table", func() {
    61  				Expect(executeErr).NotTo(HaveOccurred())
    62  
    63  				Expect(testUI.Out).To(Say("Computing sha1 for installed plugins, this may take a while..."))
    64  				Expect(testUI.Out).To(Say(""))
    65  				Expect(testUI.Out).To(Say(`plugin\s+version\s+sha1`))
    66  				Expect(testUI.Out).ToNot(Say("[A-Za-z0-9]+"))
    67  			})
    68  		})
    69  
    70  	})
    71  
    72  	When("there are plugins installed", func() {
    73  		var plugins []configv3.Plugin
    74  
    75  		BeforeEach(func() {
    76  			plugins = []configv3.Plugin{
    77  				{
    78  					Name: "Sorted-first",
    79  					Version: configv3.PluginVersion{
    80  						Major: 1,
    81  						Minor: 1,
    82  						Build: 0,
    83  					},
    84  					Commands: []configv3.PluginCommand{
    85  						{
    86  							Name:     "command-2",
    87  							HelpText: "help-command-2",
    88  						},
    89  						{
    90  							Name:     "command-1",
    91  							Alias:    "c",
    92  							HelpText: "help-command-1",
    93  						},
    94  					},
    95  				},
    96  				{
    97  					Name: "sorted-second",
    98  					Version: configv3.PluginVersion{
    99  						Major: 0,
   100  						Minor: 0,
   101  						Build: 0,
   102  					},
   103  					Commands: []configv3.PluginCommand{
   104  						{
   105  							Name:     "foo",
   106  							HelpText: "help-foo",
   107  						},
   108  						{
   109  							Name:     "bar",
   110  							HelpText: "help-bar",
   111  						},
   112  					},
   113  				},
   114  			}
   115  			fakeConfig.PluginsReturns(plugins)
   116  		})
   117  
   118  		It("displays the plugins in alphabetical order and their commands", func() {
   119  			Expect(executeErr).NotTo(HaveOccurred())
   120  
   121  			Expect(testUI.Out).To(Say("Listing installed plugins..."))
   122  			Expect(testUI.Out).To(Say(""))
   123  			Expect(testUI.Out).To(Say(`plugin\s+version\s+command name\s+command help`))
   124  			Expect(testUI.Out).To(Say(`Sorted-first\s+1\.1\.0\s+command-1, c\s+help-command-1`))
   125  			Expect(testUI.Out).To(Say(`Sorted-first\s+1\.1\.0\s+command-2\s+help-command-2`))
   126  			Expect(testUI.Out).To(Say(`sorted-second\s+N/A\s+bar\s+help-bar`))
   127  			Expect(testUI.Out).To(Say(`sorted-second\s+N/A\s+foo\s+help-foo`))
   128  			Expect(testUI.Out).To(Say(""))
   129  			Expect(testUI.Out).To(Say(`Use 'faceman repo-plugins' to list plugins in registered repos available to install\.`))
   130  		})
   131  
   132  		When("the --checksum flag is provided", func() {
   133  			var (
   134  				file *os.File
   135  			)
   136  
   137  			BeforeEach(func() {
   138  				cmd.Checksum = true
   139  
   140  				var err error
   141  				file, err = ioutil.TempFile("", "")
   142  				defer file.Close()
   143  				Expect(err).NotTo(HaveOccurred())
   144  
   145  				err = ioutil.WriteFile(file.Name(), []byte("some-text"), 0600)
   146  				Expect(err).NotTo(HaveOccurred())
   147  
   148  				plugins[0].Location = file.Name()
   149  
   150  				plugins[1].Location = "/wut/wut/"
   151  			})
   152  
   153  			AfterEach(func() {
   154  				err := os.Remove(file.Name())
   155  				Expect(err).NotTo(HaveOccurred())
   156  			})
   157  
   158  			It("displays the plugin checksums", func() {
   159  				Expect(executeErr).NotTo(HaveOccurred())
   160  
   161  				Expect(testUI.Out).To(Say("Computing sha1 for installed plugins, this may take a while..."))
   162  				Expect(testUI.Out).To(Say(""))
   163  				Expect(testUI.Out).To(Say(`plugin\s+version\s+sha1`))
   164  				Expect(testUI.Out).To(Say(`Sorted-first\s+1\.1\.0\s+2142a57cb8587400fa7f4ee492f25cf07567f4a5`))
   165  				Expect(testUI.Out).To(Say(`sorted-second\s+N/A\s+N/A`))
   166  			})
   167  		})
   168  
   169  		When("the --outdated flag is provided", func() {
   170  			BeforeEach(func() {
   171  				cmd.Outdated = true
   172  			})
   173  
   174  			When("there are no repositories", func() {
   175  				BeforeEach(func() {
   176  					fakeConfig.PluginRepositoriesReturns(nil)
   177  				})
   178  
   179  				It("returns the 'No plugin repositories added' error", func() {
   180  					Expect(executeErr).To(MatchError(translatableerror.NoPluginRepositoriesError{}))
   181  					Expect(testUI.Out).NotTo(Say("Searching"))
   182  				})
   183  			})
   184  
   185  			When("there are repositories", func() {
   186  				BeforeEach(func() {
   187  					fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   188  						{Name: "repo-1", URL: "https://repo-1.plugins.com"},
   189  						{Name: "repo-2", URL: "https://repo-2.plugins.com"},
   190  					})
   191  				})
   192  
   193  				When("the actor returns GettingRepositoryError", func() {
   194  					BeforeEach(func() {
   195  						fakeActor.GetOutdatedPluginsReturns(nil, actionerror.GettingPluginRepositoryError{
   196  							Name:    "repo-1",
   197  							Message: "404",
   198  						})
   199  					})
   200  					It("displays the repository and the error", func() {
   201  						Expect(executeErr).To(MatchError(actionerror.GettingPluginRepositoryError{
   202  							Name:    "repo-1",
   203  							Message: "404",
   204  						}))
   205  
   206  						Expect(testUI.Out).To(Say("Searching repo-1, repo-2 for newer versions of installed plugins..."))
   207  					})
   208  				})
   209  
   210  				When("there are no outdated plugins", func() {
   211  					It("displays the empty outdated table", func() {
   212  						Expect(executeErr).NotTo(HaveOccurred())
   213  
   214  						Expect(testUI.Out).To(Say("Searching repo-1, repo-2 for newer versions of installed plugins..."))
   215  						Expect(testUI.Out).To(Say(""))
   216  						Expect(testUI.Out).To(Say(`plugin\s+version\s+latest version\n\nUse 'faceman install-plugin' to update a plugin to the latest version\.`))
   217  
   218  						Expect(fakeActor.GetOutdatedPluginsCallCount()).To(Equal(1))
   219  					})
   220  				})
   221  
   222  				When("plugins are outdated", func() {
   223  					BeforeEach(func() {
   224  						fakeActor.GetOutdatedPluginsReturns([]pluginaction.OutdatedPlugin{
   225  							{Name: "plugin-1", CurrentVersion: "1.0.0", LatestVersion: "2.0.0"},
   226  							{Name: "plugin-2", CurrentVersion: "2.0.0", LatestVersion: "3.0.0"},
   227  						}, nil)
   228  					})
   229  
   230  					It("displays the outdated plugins", func() {
   231  						Expect(executeErr).NotTo(HaveOccurred())
   232  
   233  						Expect(fakeActor.GetOutdatedPluginsCallCount()).To(Equal(1))
   234  
   235  						Expect(testUI.Out).To(Say("Searching repo-1, repo-2 for newer versions of installed plugins..."))
   236  						Expect(testUI.Out).To(Say(""))
   237  						Expect(testUI.Out).To(Say(`plugin\s+version\s+latest version`))
   238  						Expect(testUI.Out).To(Say(`plugin-1\s+1.0.0\s+2.0.0`))
   239  						Expect(testUI.Out).To(Say(`plugin-2\s+2.0.0\s+3.0.0`))
   240  						Expect(testUI.Out).To(Say(""))
   241  						Expect(testUI.Out).To(Say(`Use 'faceman install-plugin' to update a plugin to the latest version\.`))
   242  					})
   243  				})
   244  			})
   245  		})
   246  	})
   247  })