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

     1  package plugin
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/liamawhite/cli-with-i18n/integration/helpers"
     8  	"github.com/liamawhite/cli-with-i18n/util/generic"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("uninstall-plugin command", func() {
    16  	Describe("help", func() {
    17  		Context("when --help flag is provided", func() {
    18  			It("displays command usage to output", func() {
    19  				session := helpers.CF("uninstall-plugin", "--help")
    20  				Eventually(session.Out).Should(Say("NAME:"))
    21  				Eventually(session.Out).Should(Say("uninstall-plugin - Uninstall CLI plugin"))
    22  				Eventually(session.Out).Should(Say("USAGE:"))
    23  				Eventually(session.Out).Should(Say("cf uninstall-plugin PLUGIN-NAME"))
    24  				Eventually(session.Out).Should(Say("SEE ALSO:"))
    25  				Eventually(session.Out).Should(Say("plugins"))
    26  				Eventually(session).Should(Exit(0))
    27  			})
    28  		})
    29  	})
    30  
    31  	Context("when the plugin is not installed", func() {
    32  		It("informs the user that no such plugin is present and exits 1", func() {
    33  			session := helpers.CF("uninstall-plugin", "bananarama")
    34  			Eventually(session.Err).Should(Say("Plugin bananarama does not exist\\."))
    35  			Eventually(session).Should(Exit(1))
    36  		})
    37  	})
    38  
    39  	Context("when the plugin is installed", func() {
    40  		BeforeEach(func() {
    41  			helpers.InstallConfigurablePlugin("banana-plugin-name-1", "2.0.1", []helpers.PluginCommand{
    42  				{Name: "banana-command-1", Help: "banana-command-1"},
    43  			})
    44  			helpers.InstallConfigurablePlugin("banana-plugin-name-2", "1.4.3", []helpers.PluginCommand{
    45  				{Name: "banana-command-2", Help: "banana-command-2"},
    46  			})
    47  		})
    48  
    49  		Context("when no errors are encountered", func() {
    50  			It("does not list the plugin after it is uninstalled", func() {
    51  				session := helpers.CF("uninstall-plugin", "banana-plugin-name-1")
    52  				Eventually(session.Out).Should(Say("Uninstalling plugin banana-plugin-name-1\\.\\.\\."))
    53  				// Test that RPC works
    54  				Eventually(session.Out).Should(Say("[0-9]{1,5} CLI-MESSAGE-UNINSTALL"))
    55  				Eventually(session.Out).Should(Say("OK"))
    56  				Eventually(session.Out).Should(Say("Plugin banana-plugin-name-1 2\\.0\\.1 successfully uninstalled\\."))
    57  				Eventually(session).Should(Exit(0))
    58  
    59  				session = helpers.CF("plugins")
    60  				Consistently(session.Out).ShouldNot(Say("banana-plugin-name-1"))
    61  				Eventually(session.Out).Should(Say("banana-plugin-name-2"))
    62  				Eventually(session).Should(Exit(0))
    63  			})
    64  
    65  			It("matches the plugin name case insensitive", func() {
    66  				session := helpers.CF("uninstall-plugin", "BaNaNa-PlUgIn-NaMe-1")
    67  				Eventually(session.Out).Should(Say("Uninstalling plugin banana-plugin-name-1\\.\\.\\."))
    68  				Eventually(session.Out).Should(Say("OK"))
    69  				Eventually(session.Out).Should(Say("Plugin banana-plugin-name-1 2\\.0\\.1 successfully uninstalled\\."))
    70  				Eventually(session).Should(Exit(0))
    71  			})
    72  		})
    73  
    74  		Context("when the plugin encounters an error during cleanup", func() {
    75  			BeforeEach(func() {
    76  				helpers.InstallConfigurablePluginFailsUninstall("failing-plugin", "2.0.1", []helpers.PluginCommand{
    77  					{Name: "failing-command-1", Help: "failing-command-1"},
    78  				})
    79  			})
    80  
    81  			It("exits with an error but still uninstalls the plugin", func() {
    82  				session := helpers.CF("uninstall-plugin", "failing-plugin")
    83  				Eventually(session.Out).Should(Say("Uninstalling plugin failing-plugin\\.\\.\\."))
    84  				Eventually(session.Err).Should(Say("I'm failing...I'm failing..."))
    85  				Eventually(session.Out).Should(Say("FAILED"))
    86  				Eventually(session.Err).Should(Say("The plugin's uninstall method returned an unexpected error\\."))
    87  				Eventually(session.Err).Should(Say("The plugin uninstall will proceed\\. Contact the plugin author if you need help\\."))
    88  				Eventually(session.Err).Should(Say("exit status 1"))
    89  				Eventually(session).Should(Exit(1))
    90  
    91  				binaryPath := generic.ExecutableFilename(
    92  					filepath.Join(homeDir, ".cf", "plugins", "failing-plugin"))
    93  				_, err := os.Stat(binaryPath)
    94  				Expect(os.IsNotExist(err)).To(BeTrue())
    95  
    96  				session = helpers.CF("plugins")
    97  				Eventually(session.Out).Should(Say("banana-plugin-name-1"))
    98  				Eventually(session.Out).Should(Say("banana-plugin-name-2"))
    99  				Consistently(session.Out).ShouldNot(Say("failing-plugin"))
   100  				Eventually(session).Should(Exit(0))
   101  			})
   102  		})
   103  
   104  		Context("when the plugin binary has been deleted", func() {
   105  			BeforeEach(func() {
   106  				helpers.InstallConfigurablePlugin(
   107  					"banana-plugin-name-1",
   108  					"2.0.1",
   109  					[]helpers.PluginCommand{
   110  						{
   111  							Name: "banana-command-1",
   112  							Help: "banana-command-1"},
   113  					})
   114  
   115  				binaryPath := generic.ExecutableFilename(
   116  					filepath.Join(homeDir, ".cf", "plugins", "banana-plugin-name-1"))
   117  				Expect(os.Remove(binaryPath)).ToNot(HaveOccurred())
   118  			})
   119  
   120  			It("uninstalls the plugin with no warning or error and exits 0", func() {
   121  				session := helpers.CF("uninstall-plugin", "banana-plugin-name-1")
   122  				Eventually(session).Should(Exit(0))
   123  			})
   124  		})
   125  	})
   126  })