github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/plugin/uninstall_plugin_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/cloudfoundry/cli/cf/configuration/config_helpers"
     9  	"github.com/cloudfoundry/cli/cf/configuration/plugin_config"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  
    14  	"github.com/cloudfoundry/cli/cf/command_registry"
    15  	"github.com/cloudfoundry/cli/fileutils"
    16  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("Uninstall", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		requirementsFactory *testreq.FakeReqFactory
    25  		fakePluginRepoDir   string
    26  		pluginDir           string
    27  		pluginConfig        *plugin_config.PluginConfig
    28  		deps                command_registry.Dependency
    29  	)
    30  
    31  	updateCommandDependency := func(pluginCall bool) {
    32  		deps.Ui = ui
    33  		deps.PluginConfig = pluginConfig
    34  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("uninstall-plugin").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{}
    39  		requirementsFactory = &testreq.FakeReqFactory{}
    40  
    41  		var err error
    42  		fakePluginRepoDir, err = ioutil.TempDir(os.TempDir(), "plugins")
    43  		Expect(err).ToNot(HaveOccurred())
    44  
    45  		fixtureDir := filepath.Join("..", "..", "..", "fixtures", "plugins")
    46  
    47  		pluginDir = filepath.Join(fakePluginRepoDir, ".cf", "plugins")
    48  		err = os.MkdirAll(pluginDir, 0700)
    49  		Expect(err).NotTo(HaveOccurred())
    50  
    51  		fileutils.CopyFile(filepath.Join(pluginDir, "test_1.exe"), filepath.Join(fixtureDir, "test_1.exe"))
    52  		fileutils.CopyFile(filepath.Join(pluginDir, "test_2.exe"), filepath.Join(fixtureDir, "test_2.exe"))
    53  
    54  		config_helpers.PluginRepoDir = func() string {
    55  			return fakePluginRepoDir
    56  		}
    57  
    58  		pluginConfig = plugin_config.NewPluginConfig(func(err error) { Expect(err).ToNot(HaveOccurred()) })
    59  		pluginConfig.SetPlugin("test_1.exe", plugin_config.PluginMetadata{Location: filepath.Join(pluginDir, "test_1.exe")})
    60  		pluginConfig.SetPlugin("test_2.exe", plugin_config.PluginMetadata{Location: filepath.Join(pluginDir, "test_2.exe")})
    61  
    62  	})
    63  
    64  	AfterEach(func() {
    65  		err := os.RemoveAll(fakePluginRepoDir)
    66  		Expect(err).NotTo(HaveOccurred())
    67  	})
    68  
    69  	runCommand := func(args ...string) bool {
    70  		return testcmd.RunCliCommand("uninstall-plugin", args, requirementsFactory, updateCommandDependency, false)
    71  	}
    72  
    73  	Describe("requirements", func() {
    74  		It("fails with usage when not provided a path to the plugin executable", func() {
    75  			runCommand()
    76  			Expect(ui.Outputs).To(ContainSubstrings(
    77  				[]string{"Incorrect Usage."},
    78  			))
    79  
    80  		})
    81  	})
    82  
    83  	Describe("failures", func() {
    84  		It("if plugin name does not exist", func() {
    85  			runCommand("garbage")
    86  
    87  			Expect(ui.Outputs).To(ContainSubstrings(
    88  				[]string{"Uninstalling plugin garbage..."},
    89  				[]string{"FAILED"},
    90  				[]string{"Plugin name", "garbage", "does not exist"},
    91  			))
    92  		})
    93  	})
    94  
    95  	Describe("success", func() {
    96  
    97  		Context("notifying plugin of uninstalling", func() {
    98  			var path2file string
    99  
   100  			BeforeEach(func() {
   101  				path2file = filepath.Join(os.TempDir(), "uninstall-test-file-for-test_1.exe")
   102  
   103  				f, err := os.Create(path2file)
   104  				Ω(err).ToNot(HaveOccurred())
   105  				defer f.Close()
   106  			})
   107  
   108  			AfterEach(func() {
   109  				os.Remove(path2file)
   110  			})
   111  
   112  			It("notifies the plugin upon uninstalling", func() {
   113  				_, err := os.Stat(path2file)
   114  				Ω(err).ToNot(HaveOccurred())
   115  
   116  				runCommand("test_1.exe")
   117  
   118  				_, err = os.Stat(path2file)
   119  				Ω(err).To(HaveOccurred())
   120  				Ω(os.IsNotExist(err)).To(BeTrue())
   121  			})
   122  		})
   123  
   124  		It("removes the binary from the <FAKE_HOME_DIR>/.cf/plugins dir", func() {
   125  			_, err := os.Stat(filepath.Join(pluginDir, "test_1.exe"))
   126  			Expect(err).ToNot(HaveOccurred())
   127  
   128  			runCommand("test_1.exe")
   129  
   130  			_, err = os.Stat(filepath.Join(pluginDir, "test_1.exe"))
   131  			Expect(err).To(HaveOccurred())
   132  			Expect(os.IsNotExist(err)).To(BeTrue())
   133  		})
   134  
   135  		It("removes the entry from the config.json", func() {
   136  			plugins := pluginConfig.Plugins()
   137  			Expect(plugins).To(HaveKey("test_1.exe"))
   138  
   139  			runCommand("test_1.exe")
   140  
   141  			plugins = pluginConfig.Plugins()
   142  			Expect(plugins).NotTo(HaveKey("test_1.exe"))
   143  		})
   144  
   145  		It("prints success text", func() {
   146  			runCommand("test_1.exe")
   147  
   148  			Expect(ui.Outputs).To(ContainSubstrings(
   149  				[]string{"Uninstalling plugin test_1.exe..."},
   150  				[]string{"OK"},
   151  				[]string{"Plugin", "test_1.exe", "successfully uninstalled."},
   152  			))
   153  		})
   154  
   155  	})
   156  
   157  })