github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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  // PluginCommand represents metadata for a CLI plugin command.
    13  type PluginCommand struct {
    14  	Name  string
    15  	Alias string
    16  	Help  string
    17  }
    18  
    19  // InstallConfigurablePlugin builds and installs a plugin called 'configurable_plugin'
    20  // with the given name, version, and commands.
    21  func InstallConfigurablePlugin(pluginType string, name string, version string, pluginCommands []PluginCommand) {
    22  	path := BuildConfigurablePlugin(pluginType, name, version, pluginCommands)
    23  	Eventually(CF("install-plugin", "-f", path)).Should(Exit(0))
    24  }
    25  
    26  // BuildConfigurablePlugin builds a plugin of type pluginType from the integration/assets/<pluginType>
    27  // directory with the given name, version, and commands.
    28  // Available pluginTypes: configurable_plugin, configurable_plugin_fails_uninstall, test_plugin,
    29  // test_plugin_fails_metadata, test_plugin_with_command_overrides, test_plugin_with_panic.
    30  func BuildConfigurablePlugin(pluginType string, name string, version string, pluginCommands []PluginCommand) string {
    31  	commands := []string{}
    32  	commandHelps := []string{}
    33  	commandAliases := []string{}
    34  	for _, command := range pluginCommands {
    35  		commands = append(commands, command.Name)
    36  		commandAliases = append(commandAliases, command.Alias)
    37  		commandHelps = append(commandHelps, command.Help)
    38  	}
    39  
    40  	pluginPath, err := Build(fmt.Sprintf("code.cloudfoundry.org/cli/integration/assets/%s", pluginType),
    41  		"-o",
    42  		name,
    43  		"-ldflags",
    44  		fmt.Sprintf("-X main.pluginName=%s -X main.version=%s -X main.commands=%s -X main.commandHelps=%s -X main.commandAliases=%s",
    45  			name,
    46  			version,
    47  			strings.Join(commands, ","),
    48  			strings.Join(commandHelps, ","),
    49  			strings.Join(commandAliases, ",")))
    50  	Expect(err).ToNot(HaveOccurred())
    51  
    52  	// gexec.Build builds the plugin with the name of the dir in the plugin path (configurable_plugin)
    53  	// in case this function is called multiple times, the plugins need to be unique to be installed
    54  
    55  	// also remove the .exe that gexec adds on Windows so the filename is always the
    56  	// same in tests
    57  	uniquePath := fmt.Sprintf("%s.%s", strings.TrimSuffix(pluginPath, ".exe"), name)
    58  	err = os.Rename(pluginPath, uniquePath)
    59  	Expect(err).ToNot(HaveOccurred())
    60  
    61  	return uniquePath
    62  }