Skip to content

Commit

Permalink
Add more usage and invocation context test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vuil committed Apr 25, 2024
1 parent cab6e41 commit 1d05261
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 2 deletions.
130 changes: 130 additions & 0 deletions plugin/invoke_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package plugin

import (
"fmt"
"os"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
)

func SetupTestPlugin(t *testing.T) *Plugin {
var topCmd *cobra.Command

// build long Command chain to attach to the root command of the plugin
for i := 7; i >= 0; i-- {
cmdName := fmt.Sprintf("c%d", i)
cmd := &cobra.Command{
Use: cmdName,
}
if topCmd == nil {
topCmd = cmd
} else {
cmd.AddCommand(topCmd)
topCmd = cmd
}
}

var descriptor = PluginDescriptor{
Name: "test",
Target: types.TargetGlobal,
Aliases: []string{"t"},
Description: "Test CLI invocation context",
Group: AdminCmdGroup,
Version: "v1.1.0",
BuildSHA: "1234567",
}

p, err := NewPlugin(&descriptor)
assert.Nil(t, err)

p.AddCommands(
topCmd,
)

return p
}

func subCommandAtLevel(cmd *cobra.Command, level int) *cobra.Command {
curr := cmd
for l := 0; l <= level; l++ {
wantCommandName := fmt.Sprintf("c%d", l)
var found bool
for _, cmd := range curr.Commands() {
cname := cmd.Name()
//if cmd.Name() == wantCommandName {
if cname == wantCommandName {
curr = cmd
found = true
break
}
}
if !found {
return nil
}
}
return curr
}

func TestInvocationContext(t *testing.T) {
defer func() {
os.Unsetenv("TANZU_CLI_COMMAND_MAPPED_FROM")
os.Unsetenv("TANZU_CLI_INVOKED_COMMAND")
os.Unsetenv("TANZU_CLI_INVOKED_GROUP")
}()

p := SetupTestPlugin(t)
assert.NotNil(t, p)

os.Setenv("TANZU_CLI_COMMAND_MAPPED_FROM", "c0 c1 c2 c3")
os.Setenv("TANZU_CLI_INVOKED_COMMAND", "To3")
os.Setenv("TANZU_CLI_INVOKED_GROUP", "jump")

ic := GetInvocationContext()
assert.NotNil(t, ic)

result := ic.MappedSourceCommandPath()
assert.Equal(t, "c0 c1 c2 c3", result)

result = ic.InvokedCommandName()
assert.Equal(t, "To3", result)

result = ic.InvokedGroupPath()
assert.Equal(t, "jump", result)

result = ic.CLIInvocationString()
assert.Equal(t, "jump To3", result)

// right at mapped command
cmd := subCommandAtLevel(p.Cmd, 3)
assert.NotNil(t, cmd)
result = ic.CLIInvocationStringForCommand(cmd)
assert.Equal(t, "jump To3", result)

// command not found below mapped command
cmd = subCommandAtLevel(p.Cmd, 2)
assert.NotNil(t, cmd)
result = ic.CLIInvocationStringForCommand(cmd)
assert.Equal(t, "jump To3", result)

// deeper command
cmd = subCommandAtLevel(p.Cmd, 5)
assert.NotNil(t, cmd)
result = ic.CLIInvocationStringForCommand(cmd)
assert.Equal(t, "jump To3 c4 c5", result)

// same command under different invocation context
os.Setenv("TANZU_CLI_COMMAND_MAPPED_FROM", "c0 c1")
os.Setenv("TANZU_CLI_INVOKED_COMMAND", "To1")
os.Setenv("TANZU_CLI_INVOKED_GROUP", "")
ic = GetInvocationContext()
assert.NotNil(t, ic)
result = ic.CLIInvocationStringForCommand(cmd)
assert.Equal(t, "To1 c2 c3 c4 c5", result)
}
143 changes: 141 additions & 2 deletions plugin/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,27 @@ func SampleTestPlugin(t *testing.T, target types.Target) *Plugin {
}

var pushCmd = &cobra.Command{
Use: "push",
Short: "Push the plugin tests",
Use: "push SOMESTUFF",
Aliases: []string{"psh"},
Short: "Push the plugin tests",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("push")
return nil
},
}

var pushMoreCmd = &cobra.Command{
Use: "more",
Short: "Push more",
Aliases: []string{"mo"},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("push more")
return nil
},
}

pushCmd.AddCommand(pushMoreCmd)

var descriptor = PluginDescriptor{
Name: "testNotUserVisible",
Target: target,
Expand Down Expand Up @@ -100,6 +113,8 @@ func SampleTestPlugin(t *testing.T, target types.Target) *Plugin {

fetchCmd.Example = "sample example usage of the fetch command"

pushCmd.Example = "sample example usage of the push command"

p, err := NewPlugin(&descriptor)
assert.Nil(t, err)

Expand Down Expand Up @@ -518,3 +533,127 @@ Global Flags:
`
assert.Equal(t, expected, got)
}

func TestCommandMappedCommandWithInvocationContext(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
c := make(chan []byte)
go readOutput(t, r, c)

// Set up for our test
stdout := os.Stdout
stderr := os.Stderr

os.Setenv("TANZU_CLI_COMMAND_MAPPED_FROM", "push")
os.Setenv("TANZU_CLI_INVOKED_COMMAND", "pu")
os.Setenv("TANZU_CLI_INVOKED_GROUP", "")
defer func() {
os.Stdout = stdout
os.Stderr = stderr
os.Unsetenv("TANZU_CLI_COMMAND_MAPPED_FROM")
os.Unsetenv("TANZU_CLI_INVOKED_COMMAND")
os.Unsetenv("TANZU_CLI_INVOKED_GROUP")
}()
os.Stdout = w
os.Stderr = w

// Prepare the root command with Global target
p := SampleTestPlugin(t, types.TargetGlobal)

p.Cmd.SetArgs([]string{"push", "--help"})

// Execute the command which will trigger the help
err = p.Execute()
assert.Nil(t, err)

err = w.Close()
assert.Nil(t, err)

got := string(<-c)

expected := `Push the plugin tests
Usage:
tanzu pu SOMESTUFF
tanzu pu [command]
Aliases:
pu, psh
Examples:
sample example usage of the push command
Available Commands:
more Push more
Flags:
-h, --help help for push
Global Flags:
-e, --env string env to test
Use "tanzu pu [command] --help" for more information about a command.
`

assert.Equal(t, expected, got)
}

func TestCommandMappedCommandSubCommandWithInvocationContext(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
c := make(chan []byte)
go readOutput(t, r, c)

// Set up for our test
stdout := os.Stdout
stderr := os.Stderr

os.Setenv("TANZU_CLI_COMMAND_MAPPED_FROM", "push")
os.Setenv("TANZU_CLI_INVOKED_COMMAND", "pu")
os.Setenv("TANZU_CLI_INVOKED_GROUP", "")
defer func() {
os.Stdout = stdout
os.Stderr = stderr
os.Unsetenv("TANZU_CLI_COMMAND_MAPPED_FROM")
os.Unsetenv("TANZU_CLI_INVOKED_COMMAND")
os.Unsetenv("TANZU_CLI_INVOKED_GROUP")
}()
os.Stdout = w
os.Stderr = w

// Prepare the root command with Global target
p := SampleTestPlugin(t, types.TargetGlobal)

p.Cmd.SetArgs([]string{"push", "more", "--help"})

// Execute the command which will trigger the help
err = p.Execute()
assert.Nil(t, err)

err = w.Close()
assert.Nil(t, err)

got := string(<-c)

expected := `Push more
Usage:
tanzu pu more
Aliases:
more, mo
Flags:
-h, --help help for more
Global Flags:
-e, --env string env to test
`

assert.Equal(t, expected, got)
}

0 comments on commit 1d05261

Please sign in to comment.