github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/pluginaction/plugin_info_test.go (about)

     1  package pluginaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "github.com/liamawhite/cli-with-i18n/actor/pluginaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/pluginaction/pluginactionfakes"
     8  	"github.com/liamawhite/cli-with-i18n/api/plugin"
     9  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("plugin info actions", func() {
    15  	var (
    16  		actor      *Actor
    17  		fakeClient *pluginactionfakes.FakePluginClient
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeClient = new(pluginactionfakes.FakePluginClient)
    22  		actor = NewActor(nil, fakeClient)
    23  	})
    24  
    25  	Describe("GetPluginInfoFromRepositoriesForPlatform", func() {
    26  		Context("when there is a single repository", func() {
    27  			Context("when getting the plugin repository errors", func() {
    28  				BeforeEach(func() {
    29  					fakeClient.GetPluginRepositoryReturns(plugin.PluginRepository{}, errors.New("some-error"))
    30  				})
    31  
    32  				It("returns a FetchingPluginInfoFromRepositoryError", func() {
    33  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", []configv3.PluginRepository{{Name: "some-repository", URL: "some-url"}}, "some-platform")
    34  					Expect(err).To(MatchError(FetchingPluginInfoFromRepositoryError{
    35  						RepositoryName: "some-repository",
    36  						Err:            errors.New("some-error"),
    37  					}))
    38  				})
    39  			})
    40  
    41  			Context("when getting the plugin repository succeeds", func() {
    42  				BeforeEach(func() {
    43  					fakeClient.GetPluginRepositoryReturns(plugin.PluginRepository{
    44  						Plugins: []plugin.Plugin{
    45  							{
    46  								Name:    "some-plugin",
    47  								Version: "1.2.3",
    48  								Binaries: []plugin.PluginBinary{
    49  									{Platform: "osx", URL: "http://some-darwin-url", Checksum: "somechecksum"},
    50  									{Platform: "win64", URL: "http://some-windows-url", Checksum: "anotherchecksum"},
    51  									{Platform: "linux64", URL: "http://some-linux-url", Checksum: "lastchecksum"},
    52  								},
    53  							},
    54  							{
    55  								Name:    "linux-plugin",
    56  								Version: "1.5.0",
    57  								Binaries: []plugin.PluginBinary{
    58  									{Platform: "osx", URL: "http://some-url", Checksum: "somechecksum"},
    59  									{Platform: "win64", URL: "http://another-url", Checksum: "anotherchecksum"},
    60  									{Platform: "linux64", URL: "http://last-url", Checksum: "lastchecksum"},
    61  								},
    62  							},
    63  							{
    64  								Name:    "osx-plugin",
    65  								Version: "3.0.0",
    66  								Binaries: []plugin.PluginBinary{
    67  									{Platform: "osx", URL: "http://some-url", Checksum: "somechecksum"},
    68  									{Platform: "win64", URL: "http://another-url", Checksum: "anotherchecksum"},
    69  									{Platform: "linux64", URL: "http://last-url", Checksum: "lastchecksum"},
    70  								},
    71  							},
    72  						},
    73  					}, nil)
    74  				})
    75  
    76  				Context("when the specified plugin does not exist in the repository", func() {
    77  					It("returns a PluginNotFoundInRepositoryError", func() {
    78  						_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("plugin-i-dont-exist", []configv3.PluginRepository{{Name: "some-repo", URL: "some-url"}}, "platform-i-dont-exist")
    79  						Expect(err).To(MatchError(PluginNotFoundInAnyRepositoryError{
    80  							PluginName: "plugin-i-dont-exist",
    81  						}))
    82  					})
    83  				})
    84  
    85  				Context("when the specified plugin for the provided platform does not exist in the repository", func() {
    86  					It("returns a NoCompatibleBinaryError", func() {
    87  						_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("linux-plugin", []configv3.PluginRepository{{Name: "some-repo", URL: "some-url"}}, "platform-i-dont-exist")
    88  						Expect(err).To(MatchError(NoCompatibleBinaryError{}))
    89  					})
    90  				})
    91  
    92  				Context("when the specified plugin exists", func() {
    93  					It("returns the plugin info", func() {
    94  						pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", []configv3.PluginRepository{{Name: "some-repo", URL: "some-url"}}, "osx")
    95  						Expect(err).ToNot(HaveOccurred())
    96  						Expect(pluginInfo.Name).To(Equal("some-plugin"))
    97  						Expect(pluginInfo.Version).To(Equal("1.2.3"))
    98  						Expect(pluginInfo.URL).To(Equal("http://some-darwin-url"))
    99  						Expect(repos).To(ConsistOf("some-repo"))
   100  					})
   101  				})
   102  			})
   103  		})
   104  
   105  		Context("when there are multiple repositories", func() {
   106  			var pluginRepositories []configv3.PluginRepository
   107  
   108  			BeforeEach(func() {
   109  				pluginRepositories = []configv3.PluginRepository{
   110  					{Name: "repo1", URL: "url1"},
   111  					{Name: "repo2", URL: "url2"},
   112  					{Name: "repo3", URL: "url3"},
   113  				}
   114  			})
   115  
   116  			Context("when getting a plugin repository errors", func() {
   117  				BeforeEach(func() {
   118  					fakeClient.GetPluginRepositoryReturnsOnCall(0, plugin.PluginRepository{
   119  						Plugins: []plugin.Plugin{
   120  							{
   121  								Name:    "some-plugin",
   122  								Version: "1.2.3",
   123  								Binaries: []plugin.PluginBinary{
   124  									{Platform: "osx", URL: "http://some-darwin-url", Checksum: "somechecksum"},
   125  									{Platform: "win64", URL: "http://some-windows-url", Checksum: "anotherchecksum"},
   126  									{Platform: "linux64", URL: "http://some-linux-url", Checksum: "lastchecksum"},
   127  								},
   128  							},
   129  						},
   130  					}, nil)
   131  					fakeClient.GetPluginRepositoryReturnsOnCall(1, plugin.PluginRepository{}, errors.New("some-error"))
   132  				})
   133  
   134  				It("returns a FetchingPluginInfoFromRepositoryError", func() {
   135  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   136  					Expect(err).To(MatchError(FetchingPluginInfoFromRepositoryError{
   137  						RepositoryName: "repo2",
   138  						Err:            errors.New("some-error")}))
   139  				})
   140  			})
   141  
   142  			Context("when the plugin isn't found", func() {
   143  				It("returns the PluginNotFoundInAnyRepositoryError", func() {
   144  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   145  					Expect(err).To(Equal(PluginNotFoundInAnyRepositoryError{PluginName: "some-plugin"}))
   146  				})
   147  			})
   148  
   149  			Context("when no compatible binaries are found for the plugin", func() {
   150  				BeforeEach(func() {
   151  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   152  						return plugin.PluginRepository{Plugins: []plugin.Plugin{
   153  							{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   154  								{Platform: "incompatible-platform", URL: "some-url", Checksum: "some-checksum"},
   155  							}},
   156  						}}, nil
   157  					}
   158  				})
   159  
   160  				It("returns the NoCompatibleBinaryError", func() {
   161  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   162  					Expect(err).To(MatchError(NoCompatibleBinaryError{}))
   163  				})
   164  			})
   165  
   166  			Context("when some binaries are compatible and some are not", func() {
   167  				BeforeEach(func() {
   168  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   169  						if repoURL == "url1" {
   170  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   171  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   172  									{Platform: "incompatible-platform", URL: "some-url", Checksum: "some-checksum"},
   173  								}},
   174  							}}, nil
   175  						} else {
   176  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   177  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   178  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   179  								}},
   180  							}}, nil
   181  						}
   182  					}
   183  				})
   184  
   185  				It("returns the compatible plugin info and a list of the repositories it was found in", func() {
   186  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   187  
   188  					Expect(err).ToNot(HaveOccurred())
   189  					Expect(pluginInfo).To(Equal(PluginInfo{
   190  						Name:     "some-plugin",
   191  						Version:  "1.2.3",
   192  						URL:      "some-url",
   193  						Checksum: "some-checksum",
   194  					}))
   195  					Expect(repos).To(ConsistOf("repo2", "repo3"))
   196  				})
   197  			})
   198  
   199  			Context("when the plugin is found in one repository", func() {
   200  				BeforeEach(func() {
   201  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   202  						if repoURL == "url1" {
   203  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   204  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   205  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   206  								}},
   207  							}}, nil
   208  						} else {
   209  							return plugin.PluginRepository{}, nil
   210  						}
   211  					}
   212  				})
   213  
   214  				It("returns the plugin info and a list of the repositories it was found in", func() {
   215  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   216  
   217  					Expect(err).ToNot(HaveOccurred())
   218  					Expect(pluginInfo).To(Equal(PluginInfo{
   219  						Name:     "some-plugin",
   220  						Version:  "1.2.3",
   221  						URL:      "some-url",
   222  						Checksum: "some-checksum",
   223  					}))
   224  					Expect(repos).To(ConsistOf("repo1"))
   225  				})
   226  			})
   227  
   228  			Context("when the plugin is found in many repositories", func() {
   229  				BeforeEach(func() {
   230  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   231  						return plugin.PluginRepository{Plugins: []plugin.Plugin{
   232  							{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   233  								{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   234  							}},
   235  						}}, nil
   236  					}
   237  				})
   238  
   239  				It("returns the plugin info and a list of the repositories it was found in", func() {
   240  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   241  
   242  					Expect(err).ToNot(HaveOccurred())
   243  					Expect(pluginInfo).To(Equal(PluginInfo{
   244  						Name:     "some-plugin",
   245  						Version:  "1.2.3",
   246  						URL:      "some-url",
   247  						Checksum: "some-checksum",
   248  					}))
   249  					Expect(repos).To(ConsistOf("repo1", "repo2", "repo3"))
   250  				})
   251  			})
   252  
   253  			Context("when different versions of the plugin are found in all the repositories", func() {
   254  				BeforeEach(func() {
   255  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   256  						switch repoURL {
   257  						case "url1":
   258  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   259  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   260  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   261  								}},
   262  							}}, nil
   263  						case "url2":
   264  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   265  								{Name: "some-plugin", Version: "2.2.3", Binaries: []plugin.PluginBinary{
   266  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   267  								}},
   268  							}}, nil
   269  						default:
   270  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   271  								{Name: "some-plugin", Version: "0.2.3", Binaries: []plugin.PluginBinary{
   272  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   273  								}},
   274  							}}, nil
   275  						}
   276  					}
   277  				})
   278  
   279  				It("returns the newest plugin info and only the repository it was found in", func() {
   280  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   281  
   282  					Expect(err).ToNot(HaveOccurred())
   283  					Expect(pluginInfo).To(Equal(PluginInfo{
   284  						Name:     "some-plugin",
   285  						Version:  "2.2.3",
   286  						URL:      "some-url",
   287  						Checksum: "some-checksum",
   288  					}))
   289  					Expect(repos).To(ConsistOf("repo2"))
   290  				})
   291  			})
   292  
   293  			Context("when some repositories contain a newer version of the plugin than others", func() {
   294  				BeforeEach(func() {
   295  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   296  						switch repoURL {
   297  						case "url1", "url2":
   298  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   299  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   300  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   301  								}},
   302  							}}, nil
   303  						default:
   304  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   305  								{Name: "some-plugin", Version: "0.2.3", Binaries: []plugin.PluginBinary{
   306  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   307  								}},
   308  							}}, nil
   309  						}
   310  					}
   311  				})
   312  
   313  				It("returns only the newest plugin info and the list of repositories it's contained in", func() {
   314  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   315  
   316  					Expect(err).ToNot(HaveOccurred())
   317  					Expect(pluginInfo).To(Equal(PluginInfo{
   318  						Name:     "some-plugin",
   319  						Version:  "1.2.3",
   320  						URL:      "some-url",
   321  						Checksum: "some-checksum",
   322  					}))
   323  					Expect(repos).To(ConsistOf("repo1", "repo2"))
   324  				})
   325  			})
   326  		})
   327  	})
   328  })