github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/root/root_test.go (about)

     1  package root
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
     8  	"github.com/spf13/cobra"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  var cfgTestInputRoot = RootCmdConfig{Name: "kn\u00A0workflow", Version: metadata.PluginVersion}
    13  
    14  func TestNewRootCommand(t *testing.T) {
    15  	//https://issues.redhat.com/browse/KOGITO-9847
    16  	//t.Run("Test root command name and help", func(t *testing.T) {
    17  	//	cmd := NewRootCommand(cfgTestInputRoot)
    18  	//	output, err := ExecuteCommandSoft(cmd)
    19  	//	require.NoError(t, err, "Error: %v", err)
    20  	//	require.Contains(t, output, "Usage:\n  kn\u00a0workflow [command]\n\nAliases:\n  kn\u00a0workflow, kn-workflow")
    21  	//	require.Contains(t, output, "Use \"kn\u00a0workflow [command] --help\" for more information about a command.")
    22  	//})
    23  
    24  	t.Run("Check subcommands except Cobra generated (help, completion)", func(t *testing.T) {
    25  		expectedSubCommands := []string{
    26  			"create",
    27  			"deploy",
    28  			"quarkus",
    29  			"run",
    30  			"undeploy",
    31  			"gen-manifest",
    32  			"version",
    33  		}
    34  
    35  		cmd := NewRootCommand(cfgTestInputRoot)
    36  		require.True(t, cmd.HasSubCommands())
    37  		require.Equal(t, len(expectedSubCommands), len(cmd.Commands()))
    38  
    39  		for _, e := range expectedSubCommands {
    40  			_, _, err := cmd.Find([]string{e})
    41  			require.NoError(t, err, "Root command should have subcommand `%q`", e)
    42  		}
    43  	})
    44  }
    45  
    46  // ExecuteCommandC execute cobra.command and catch the output
    47  func ExecuteCommandSoftC(root *cobra.Command, args ...string) (c *cobra.Command, output string, err error) {
    48  	buf := new(bytes.Buffer)
    49  	root.SetOut(buf)
    50  	root.SetErr(buf)
    51  	root.SetArgs(args)
    52  	c, err = root.ExecuteC()
    53  	return c, buf.String(), err
    54  }
    55  
    56  // ExecuteCommand similar to ExecuteCommandC but does not return *cobra.Command
    57  func ExecuteCommandSoft(root *cobra.Command, args ...string) (output string, err error) {
    58  	_, o, err := ExecuteCommandSoftC(root, args...)
    59  	return o, err
    60  }