github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/helpers/plugin.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  type PluginCommand struct {
    13  	Name  string
    14  	Alias string
    15  	Help  string
    16  }
    17  
    18  func InstallConfigurablePlugin(name string, version string, pluginCommands []PluginCommand) {
    19  	path := BuildConfigurablePlugin("configurable_plugin", name, version, pluginCommands)
    20  	Eventually(CF("install-plugin", "-f", path)).Should(Exit(0))
    21  	Eventually(CFWithEnv(
    22  		map[string]string{"CF_CLI_EXPERIMENTAL": "true"},
    23  		"install-plugin", "-f", path)).Should(Exit(0))
    24  }
    25  
    26  func InstallConfigurablePluginFailsUninstall(name string, version string, pluginCommands []PluginCommand) {
    27  	path := BuildConfigurablePlugin("configurable_plugin_fails_uninstall", name, version, pluginCommands)
    28  	Eventually(CF("install-plugin", "-f", path)).Should(Exit(0))
    29  }
    30  
    31  func BuildConfigurablePlugin(pluginType string, name string, version string, pluginCommands []PluginCommand) string {
    32  	commands := []string{}
    33  	commandHelps := []string{}
    34  	commandAliases := []string{}
    35  	for _, command := range pluginCommands {
    36  		commands = append(commands, command.Name)
    37  		commandAliases = append(commandAliases, command.Alias)
    38  		commandHelps = append(commandHelps, command.Help)
    39  	}
    40  
    41  	pluginPath, err := Build(fmt.Sprintf("code.cloudfoundry.org/cli/integration/assets/%s", pluginType),
    42  		"-o",
    43  		name,
    44  		"-ldflags",
    45  		fmt.Sprintf("-X main.pluginName=%s -X main.version=%s -X main.commands=%s -X main.commandHelps=%s -X main.commandAliases=%s",
    46  			name,
    47  			version,
    48  			strings.Join(commands, ","),
    49  			strings.Join(commandHelps, ","),
    50  			strings.Join(commandAliases, ",")))
    51  	Expect(err).ToNot(HaveOccurred())
    52  
    53  	// gexec.Build builds the plugin with the name of the dir in the plugin path (configurable_plugin)
    54  	// in case this function is called multiple times, the plugins need to be unique to be installed
    55  
    56  	// also remove the .exe that gexec adds on Windows so the filename is always the
    57  	// same in tests
    58  	uniquePath := fmt.Sprintf("%s.%s", strings.TrimSuffix(pluginPath, ".exe"), name)
    59  	err = os.Rename(pluginPath, uniquePath)
    60  	Expect(err).ToNot(HaveOccurred())
    61  
    62  	return uniquePath
    63  }