github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/plugin/plugins_command_test.go (about)

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