github.com/loafoe/cli@v7.1.0+incompatible/command/plugin/uninstall_plugin_command_test.go (about) 1 package plugin_test 2 3 import ( 4 "errors" 5 "os" 6 7 "code.cloudfoundry.org/cli/actor/actionerror" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/plugin" 10 "code.cloudfoundry.org/cli/command/plugin/pluginfakes" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 "code.cloudfoundry.org/cli/util/configv3" 13 "code.cloudfoundry.org/cli/util/ui" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("uninstall-plugin command", func() { 20 var ( 21 cmd UninstallPluginCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeActor *pluginfakes.FakeUninstallPluginActor 25 executeErr error 26 ) 27 28 BeforeEach(func() { 29 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 30 fakeConfig = new(commandfakes.FakeConfig) 31 somePlugin := configv3.Plugin{ 32 Name: "some-plugin", 33 Version: configv3.PluginVersion{ 34 Major: 1, 35 Minor: 2, 36 Build: 3, 37 }, 38 } 39 fakeConfig.GetPluginCaseInsensitiveReturns(somePlugin, true) 40 fakeActor = new(pluginfakes.FakeUninstallPluginActor) 41 42 cmd = UninstallPluginCommand{ 43 UI: testUI, 44 Config: fakeConfig, 45 Actor: fakeActor, 46 } 47 48 cmd.RequiredArgs.PluginName = "some-plugin" 49 }) 50 51 JustBeforeEach(func() { 52 executeErr = cmd.Execute(nil) 53 }) 54 55 When("the plugin is installed", func() { 56 BeforeEach(func() { 57 fakeActor.UninstallPluginReturns(nil) 58 }) 59 60 When("no errors are encountered", func() { 61 It("uninstalls the plugin and outputs the success message", func() { 62 Expect(executeErr).ToNot(HaveOccurred()) 63 64 Expect(testUI.Out).To(Say("Uninstalling plugin some-plugin...")) 65 Expect(testUI.Out).To(Say("OK")) 66 Expect(testUI.Out).To(Say("Plugin some-plugin 1.2.3 successfully uninstalled.")) 67 68 Expect(fakeActor.UninstallPluginCallCount()).To(Equal(1)) 69 _, pluginName := fakeActor.UninstallPluginArgsForCall(0) 70 Expect(pluginName).To(Equal("some-plugin")) 71 }) 72 }) 73 74 When("uninstalling the plugin returns a plugin binary remove failed error", func() { 75 var pathError error 76 77 BeforeEach(func() { 78 pathError = &os.PathError{ 79 Op: "some-op", 80 Err: errors.New("some error"), 81 } 82 83 fakeActor.UninstallPluginReturns(actionerror.PluginBinaryRemoveFailedError{ 84 Err: pathError, 85 }) 86 }) 87 88 It("returns a PluginBinaryRemoveFailedError", func() { 89 Expect(executeErr).To(MatchError(translatableerror.PluginBinaryRemoveFailedError{ 90 Err: pathError, 91 })) 92 }) 93 }) 94 95 When("uninstalling the plugin returns a plugin execute error", func() { 96 var pathError error 97 98 BeforeEach(func() { 99 pathError = &os.PathError{ 100 Op: "some-op", 101 Err: errors.New("some error"), 102 } 103 104 fakeActor.UninstallPluginReturns(actionerror.PluginExecuteError{Err: pathError}) 105 }) 106 107 It("returns a PluginBinaryUninstallError", func() { 108 Expect(executeErr).To(MatchError(translatableerror.PluginBinaryUninstallError{ 109 Err: pathError, 110 })) 111 }) 112 }) 113 114 When("uninstalling the plugin encounters any other error", func() { 115 var expectedErr error 116 117 BeforeEach(func() { 118 expectedErr = errors.New("some error") 119 fakeActor.UninstallPluginReturns(expectedErr) 120 }) 121 122 It("returns the error", func() { 123 Expect(executeErr).To(MatchError(expectedErr)) 124 }) 125 }) 126 }) 127 128 When("the plugin is not installed", func() { 129 BeforeEach(func() { 130 fakeActor.UninstallPluginReturns( 131 actionerror.PluginNotFoundError{ 132 PluginName: "some-plugin", 133 }, 134 ) 135 }) 136 137 It("returns a PluginNotFoundError", func() { 138 Expect(testUI.Out).To(Say("Uninstalling plugin some-plugin...")) 139 Expect(executeErr).To(MatchError(actionerror.PluginNotFoundError{ 140 PluginName: "some-plugin", 141 })) 142 }) 143 }) 144 })