github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/configuration/pluginconfig/plugin_config_test.go (about)

     1  package pluginconfig_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/cloudfoundry/cli/cf/configuration"
     9  	"github.com/cloudfoundry/cli/cf/configuration/confighelpers"
    10  	. "github.com/cloudfoundry/cli/cf/configuration/pluginconfig"
    11  	"github.com/cloudfoundry/cli/plugin"
    12  
    13  	"github.com/cloudfoundry/cli/cf/configuration/configurationfakes"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("PluginConfig", func() {
    19  	Describe(".ListCommands", func() {
    20  		var (
    21  			pluginConfig *PluginConfig
    22  		)
    23  
    24  		BeforeEach(func() {
    25  			pluginConfig = NewPluginConfig(
    26  				func(err error) { Fail(err.Error()) },
    27  				new(configurationfakes.FakePersistor),
    28  				"some-path",
    29  			)
    30  
    31  			plugin1 := PluginMetadata{
    32  				Commands: []plugin.Command{
    33  					{
    34  						Name: "plugin1-command1",
    35  					},
    36  					{
    37  						Name: "plugin1-command2",
    38  					},
    39  				},
    40  			}
    41  
    42  			pluginConfig.SetPlugin("plugin1", plugin1)
    43  		})
    44  
    45  		It("should list the plugin commands", func() {
    46  			Expect(pluginConfig.ListCommands()).To(Equal([]string{
    47  				"plugin1-command1",
    48  				"plugin1-command2",
    49  			}))
    50  		})
    51  	})
    52  
    53  	Describe("Integration tests", func() {
    54  		var (
    55  			metadata     PluginMetadata
    56  			commands1    []plugin.Command
    57  			commands2    []plugin.Command
    58  			pluginConfig *PluginConfig
    59  			plugins      map[string]PluginMetadata
    60  		)
    61  
    62  		BeforeEach(func() {
    63  			commands1 = []plugin.Command{
    64  				{
    65  					Name:     "test_1_cmd1",
    66  					HelpText: "help text for test1 cmd1",
    67  				},
    68  				{
    69  					Name:     "test_1_cmd2",
    70  					HelpText: "help text for test1 cmd2",
    71  				},
    72  			}
    73  
    74  			commands2 = []plugin.Command{
    75  				{
    76  					Name:     "test_2_cmd1",
    77  					HelpText: "help text for test2 cmd1",
    78  				},
    79  				{
    80  					Name:     "test_2_cmd2",
    81  					HelpText: "help text for test2 cmd2",
    82  				},
    83  			}
    84  
    85  			metadata = PluginMetadata{
    86  				Location: "../../../fixtures/plugins/test_1.exe",
    87  				Commands: commands1,
    88  			}
    89  		})
    90  
    91  		JustBeforeEach(func() {
    92  			pluginPath := filepath.Join(confighelpers.PluginRepoDir(), ".cf", "plugins")
    93  			pluginConfig = NewPluginConfig(
    94  				func(err error) {
    95  					if err != nil {
    96  						Fail(fmt.Sprintf("Config error: %s", err))
    97  					}
    98  				},
    99  				configuration.NewDiskPersistor(filepath.Join(pluginPath, "config.json")),
   100  				pluginPath,
   101  			)
   102  			plugins = pluginConfig.Plugins()
   103  		})
   104  
   105  		Describe("Reading configuration data", func() {
   106  			BeforeEach(func() {
   107  				confighelpers.PluginRepoDir = func() string {
   108  					return filepath.Join("..", "..", "..", "fixtures", "config", "plugin-config")
   109  				}
   110  			})
   111  
   112  			It("returns a list of plugin executables and their location", func() {
   113  				Expect(plugins["Test1"].Location).To(Equal("../../../fixtures/plugins/test_1.exe"))
   114  				Expect(plugins["Test1"].Commands).To(Equal(commands1))
   115  				Expect(plugins["Test2"].Location).To(Equal("../../../fixtures/plugins/test_2.exe"))
   116  				Expect(plugins["Test2"].Commands).To(Equal(commands2))
   117  			})
   118  		})
   119  
   120  		Describe("Writing configuration data", func() {
   121  			BeforeEach(func() {
   122  				confighelpers.PluginRepoDir = func() string { return os.TempDir() }
   123  			})
   124  
   125  			AfterEach(func() {
   126  				os.Remove(filepath.Join(os.TempDir(), ".cf", "plugins", "config.json"))
   127  			})
   128  
   129  			It("saves plugin location and executable information", func() {
   130  				pluginConfig.SetPlugin("foo", metadata)
   131  				plugins = pluginConfig.Plugins()
   132  				Expect(plugins["foo"].Commands).To(Equal(commands1))
   133  			})
   134  		})
   135  
   136  		Describe("Removing configuration data", func() {
   137  			BeforeEach(func() {
   138  				confighelpers.PluginRepoDir = func() string { return os.TempDir() }
   139  			})
   140  
   141  			AfterEach(func() {
   142  				os.Remove(filepath.Join(os.TempDir()))
   143  			})
   144  
   145  			It("removes plugin location and executable information", func() {
   146  				pluginConfig.SetPlugin("foo", metadata)
   147  				plugins = pluginConfig.Plugins()
   148  				Expect(plugins).To(HaveKey("foo"))
   149  
   150  				pluginConfig.RemovePlugin("foo")
   151  				plugins = pluginConfig.Plugins()
   152  				Expect(plugins).NotTo(HaveKey("foo"))
   153  			})
   154  
   155  			It("handles when the config is not yet initialized", func() {
   156  				pluginConfig.RemovePlugin("foo")
   157  				plugins = pluginConfig.Plugins()
   158  				Expect(plugins).NotTo(HaveKey("foo"))
   159  			})
   160  		})
   161  	})
   162  })