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