github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/tools/cli/help/command_test.go (about)

     1  package help
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/wawandco/oxpecker/plugins"
     9  )
    10  
    11  func TestFindCommand(t *testing.T) {
    12  	hp := Command{
    13  		commands: []plugins.Command{},
    14  	}
    15  
    16  	migrate := &subPl{}
    17  	pop := &testPlugin{}
    18  	pop.Receive([]plugins.Plugin{
    19  		migrate,
    20  	})
    21  
    22  	hp.commands = append(hp.commands, pop)
    23  
    24  	t.Run("not enough arguments", func(*testing.T) {
    25  		result, names := hp.findCommand([]string{"help"})
    26  		if result != nil || names != nil {
    27  			t.Fatal("Should be nil")
    28  		}
    29  	})
    30  
    31  	t.Run("top level command", func(*testing.T) {
    32  		result, names := hp.findCommand([]string{"help", "pop"})
    33  		expected := []string{
    34  			"pop",
    35  		}
    36  		if result.Name() != "pop" || strings.Join(names, " ") != strings.Join(expected, " ") {
    37  			t.Fatal("didn't find our guy")
    38  		}
    39  	})
    40  
    41  	t.Run("subcommand lookup", func(*testing.T) {
    42  		result, names := hp.findCommand([]string{"help", "pop", "migrate"})
    43  		expected := []string{
    44  			"pop",
    45  			"migrate",
    46  		}
    47  
    48  		ht, ok := result.(plugins.HelpTexter)
    49  		if result.Name() != "migrate" || !ok || ht.HelpText() != migrate.HelpText() || strings.Join(names, " ") != strings.Join(expected, " ") {
    50  			t.Fatal("didn't find our guy")
    51  		}
    52  	})
    53  
    54  	t.Run("extra args on non-subcommander", func(*testing.T) {
    55  		result, names := hp.findCommand([]string{"help", "pop", "migrate", "other", "thing"})
    56  		expected := []string{
    57  			"pop",
    58  			"migrate",
    59  		}
    60  		ht, ok := result.(plugins.HelpTexter)
    61  		if result.Name() != "migrate" || !ok || ht.HelpText() != migrate.HelpText() || strings.Join(names, " ") != strings.Join(expected, " ") {
    62  			t.Fatal("didn't find our guy")
    63  		}
    64  	})
    65  
    66  }
    67  
    68  type testPlugin struct {
    69  	subcommands []plugins.Command
    70  }
    71  
    72  func (tp testPlugin) Name() string {
    73  	return "pop"
    74  }
    75  
    76  func (tp testPlugin) ParentName() string {
    77  	return ""
    78  }
    79  
    80  func (tp testPlugin) HelpText() string {
    81  	return "pop help text"
    82  }
    83  
    84  func (tp *testPlugin) Run(ctx context.Context, root string, args []string) error {
    85  	return nil
    86  }
    87  
    88  func (tp *testPlugin) Receive(pls []plugins.Plugin) {
    89  	for _, pl := range pls {
    90  		c, ok := pl.(plugins.Command)
    91  		if !ok || c.ParentName() != tp.Name() {
    92  			continue
    93  		}
    94  
    95  		tp.subcommands = append(tp.subcommands, c)
    96  	}
    97  }
    98  
    99  func (tp *testPlugin) Subcommands() []plugins.Command {
   100  	return tp.subcommands
   101  }
   102  
   103  type subPl struct{}
   104  
   105  func (tp subPl) Name() string {
   106  	return "migrate"
   107  }
   108  
   109  func (tp subPl) ParentName() string {
   110  	return "pop"
   111  }
   112  
   113  func (tp subPl) HelpText() string {
   114  	return "migrate help text"
   115  }
   116  
   117  func (tp subPl) Run(ctx context.Context, root string, args []string) error {
   118  	return nil
   119  }