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