github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/configuration/plugin_config/plugin_config_test.go (about)

     1  package plugin_config_test
     2  
     3  import (
     4  	"fmt"
     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  	"github.com/cloudfoundry/cli/plugin"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("PluginConfig", func() {
    17  	var (
    18  		metadata  PluginMetadata
    19  		commands1 []plugin.Command
    20  		commands2 []plugin.Command
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		commands1 = []plugin.Command{
    25  			{
    26  				Name:     "test_1_cmd1",
    27  				HelpText: "help text for test1 cmd1",
    28  			},
    29  			{
    30  				Name:     "test_1_cmd2",
    31  				HelpText: "help text for test1 cmd2",
    32  			},
    33  		}
    34  
    35  		commands2 = []plugin.Command{
    36  			{
    37  				Name:     "test_2_cmd1",
    38  				HelpText: "help text for test2 cmd1",
    39  			},
    40  			{
    41  				Name:     "test_2_cmd2",
    42  				HelpText: "help text for test2 cmd2",
    43  			},
    44  		}
    45  
    46  		metadata = PluginMetadata{
    47  			Location: "../../../fixtures/plugins/test_1.exe",
    48  			Commands: commands1,
    49  		}
    50  	})
    51  
    52  	Describe("Reading configuration data", func() {
    53  		BeforeEach(func() {
    54  
    55  			config_helpers.PluginRepoDir = func() string {
    56  				return filepath.Join("..", "..", "..", "fixtures", "config", "plugin-config")
    57  			}
    58  		})
    59  
    60  		It("returns a list of plugin executables and their location", func() {
    61  			pluginConfig := NewPluginConfig(func(err error) {
    62  				if err != nil {
    63  					panic(fmt.Sprintf("Config error: %s", err))
    64  				}
    65  			})
    66  			plugins := pluginConfig.Plugins()
    67  
    68  			Expect(plugins["Test1"].Location).To(Equal("../../../fixtures/plugins/test_1.exe"))
    69  			Expect(plugins["Test1"].Commands).To(Equal(commands1))
    70  			Expect(plugins["Test2"].Location).To(Equal("../../../fixtures/plugins/test_2.exe"))
    71  			Expect(plugins["Test2"].Commands).To(Equal(commands2))
    72  		})
    73  	})
    74  
    75  	Describe("Writing configuration data", func() {
    76  		BeforeEach(func() {
    77  			config_helpers.PluginRepoDir = func() string { return os.TempDir() }
    78  		})
    79  
    80  		AfterEach(func() {
    81  			os.Remove(filepath.Join(os.TempDir(), ".cf", "plugins", "config.json"))
    82  		})
    83  
    84  		It("saves plugin location and executable information", func() {
    85  			pluginConfig := NewPluginConfig(func(err error) {
    86  				if err != nil {
    87  					panic(fmt.Sprintf("Config error: %s", err))
    88  				}
    89  			})
    90  
    91  			pluginConfig.SetPlugin("foo", metadata)
    92  			plugins := pluginConfig.Plugins()
    93  			Expect(plugins["foo"].Commands).To(Equal(commands1))
    94  		})
    95  	})
    96  
    97  	Describe("Removing configuration data", func() {
    98  		var (
    99  			pluginConfig *PluginConfig
   100  		)
   101  
   102  		BeforeEach(func() {
   103  			config_helpers.PluginRepoDir = func() string { return os.TempDir() }
   104  			pluginConfig = NewPluginConfig(func(err error) {
   105  				if err != nil {
   106  					panic(fmt.Sprintf("Config error: %s", err))
   107  				}
   108  			})
   109  		})
   110  
   111  		AfterEach(func() {
   112  			os.Remove(filepath.Join(os.TempDir()))
   113  		})
   114  
   115  		It("removes plugin location and executable information", func() {
   116  			pluginConfig.SetPlugin("foo", metadata)
   117  
   118  			plugins := pluginConfig.Plugins()
   119  			Expect(plugins).To(HaveKey("foo"))
   120  
   121  			pluginConfig.RemovePlugin("foo")
   122  
   123  			plugins = pluginConfig.Plugins()
   124  			Expect(plugins).NotTo(HaveKey("foo"))
   125  		})
   126  
   127  		It("handles when the config is not yet initialized", func() {
   128  			pluginConfig.RemovePlugin("foo")
   129  
   130  			plugins := pluginConfig.Plugins()
   131  			Expect(plugins).NotTo(HaveKey("foo"))
   132  		})
   133  	})
   134  })