github.com/swaros/contxt/module/runner@v0.0.0-20240305083542-3dbd4436ac40/cobrainit_test.go (about)

     1  package runner_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/swaros/contxt/module/configure"
     9  	"github.com/swaros/contxt/module/runner"
    10  )
    11  
    12  func TestBashCompletionOrigin(t *testing.T) {
    13  	SetDefaultValues()
    14  	cobra := runner.NewCobraCmds()
    15  	cmpltn := new(bytes.Buffer)
    16  	cobra.RootCmd.GenBashCompletion(cmpltn)
    17  	cobraResult := cmpltn.String()
    18  	err := cobra.RootCmd.Execute()
    19  
    20  	if err != nil {
    21  		t.Errorf("Error executing completion command: %s", err)
    22  	}
    23  
    24  	if cobraResult == "" {
    25  		t.Errorf("Completion result is empty")
    26  	}
    27  
    28  	// testing some of the expected methods exists
    29  	// __contxt_handle_reply
    30  	// __contxt_handle_go_custom_completion
    31  	// __contxt_handle_subdirs_in_dir_flag
    32  	expectedSlice := []string{
    33  		`__contxt_index_of_word()`,
    34  		`__contxt_init_completion()`,
    35  		`__contxt_contains_word()`,
    36  		`__contxt_handle_go_custom_completion()`,
    37  		`__contxt_handle_reply()`,
    38  	}
    39  
    40  	assertStringSliceInContent(t, cobraResult, expectedSlice, IgnoreMultiSpaces, IgnoreTabs, IgnoreNewLines, IgnoreTabs, IgnoreSpaces)
    41  
    42  }
    43  
    44  // testing renamed binary name for bash completion
    45  func TestBashCompletionRenamed(t *testing.T) {
    46  	configure.SetBinaryName("RenamedBin")
    47  	cobra := runner.NewCobraCmds()
    48  	cmpltn := new(bytes.Buffer)
    49  	cobra.RootCmd.GenBashCompletion(cmpltn)
    50  	cobraResult := cmpltn.String()
    51  	err := cobra.RootCmd.Execute()
    52  
    53  	if err != nil {
    54  		t.Errorf("Error executing completion command: %s", err)
    55  	}
    56  
    57  	if cobraResult == "" {
    58  		t.Errorf("Completion result is empty")
    59  	}
    60  
    61  	// testing some of the expected methods exists
    62  	// __contxt_handle_reply
    63  	// __contxt_handle_go_custom_completion
    64  	// __contxt_handle_subdirs_in_dir_flag
    65  	expectedSlice := []string{
    66  		`__RenamedBin_index_of_word()`,
    67  		`__RenamedBin_init_completion()`,
    68  		`__RenamedBin_contains_word()`,
    69  		`__RenamedBin_handle_go_custom_completion()`,
    70  		`__RenamedBin_debug "${FUNCNAME[0]}`,
    71  		`__RenamedBin_handle_reply()`,
    72  	}
    73  
    74  	assertStringSliceInContent(t, cobraResult, expectedSlice, IgnoreMultiSpaces, IgnoreTabs, IgnoreNewLines, IgnoreTabs, IgnoreSpaces)
    75  
    76  }
    77  
    78  // testing zsh completion with default values
    79  func TestCompletionZshOrgin(t *testing.T) {
    80  	SetDefaultValues()
    81  	result := runCobraCommand(func(cobra *runner.SessionCobra, w io.Writer) {
    82  		cobra.RootCmd.GenZshCompletion(w)
    83  	})
    84  
    85  	if result == "" {
    86  		t.Errorf("Completion result is empty")
    87  	}
    88  
    89  	expectedSlice := []string{
    90  		`#compdef contxt`,
    91  		`__contxt_debug`,
    92  	}
    93  	assertStringSliceInContent(t, result, expectedSlice)
    94  }
    95  
    96  // testing renamed binary name for zsh completion
    97  func TestCompletionZshRenamedBin(t *testing.T) {
    98  	configure.SetBinaryName("contxtV2")
    99  	result := runCobraCommand(func(cobra *runner.SessionCobra, w io.Writer) {
   100  		cobra.RootCmd.GenZshCompletion(w)
   101  	})
   102  
   103  	if result == "" {
   104  		t.Errorf("Completion result is empty")
   105  	}
   106  
   107  	expectedSlice := []string{
   108  		`#compdef contxtV2`,
   109  		`__contxtV2_debug`,
   110  	}
   111  	assertStringSliceInContent(t, result, expectedSlice)
   112  }
   113  
   114  // testing fish completion with default values
   115  func TestCompletionFishOrgin(t *testing.T) {
   116  	SetDefaultValues()
   117  	result := runCobraCommand(func(cobra *runner.SessionCobra, w io.Writer) {
   118  		cobra.RootCmd.GenFishCompletion(w, true)
   119  	})
   120  	expectedSlice := []string{
   121  		`fish completion for contxt`,
   122  		`function __contxt_debug`,
   123  		`__contxt_perform_completion`,
   124  		`__contxt_perform_completion_once_result`,
   125  		`function __contxt_prepare_completions`,
   126  	}
   127  	assertStringSliceInContent(t, result, expectedSlice)
   128  }
   129  
   130  // testing renamed binary name for fish completion
   131  func TestCompletionFishRenamedBin(t *testing.T) {
   132  	configure.SetBinaryName("contxtV2")
   133  	result := runCobraCommand(func(cobra *runner.SessionCobra, w io.Writer) {
   134  		cobra.RootCmd.GenFishCompletion(w, true)
   135  	})
   136  	expectedSlice := []string{
   137  		`fish completion for contxtV2`,
   138  		`function __contxtV2_debug`,
   139  		`__contxtV2_perform_completion`,
   140  		`__contxtV2_perform_completion_once_result`,
   141  		`function __contxtV2_prepare_completions`,
   142  	}
   143  	assertStringSliceInContent(t, result, expectedSlice)
   144  }
   145  
   146  // testing powershell completion with default values
   147  func TestCompletionPowershellOrgin(t *testing.T) {
   148  	SetDefaultValues()
   149  	result := runCobraCommand(func(cobra *runner.SessionCobra, w io.Writer) {
   150  		cobra.RootCmd.GenPowerShellCompletion(w)
   151  	})
   152  	expectedSlice := []string{
   153  		`powershell completion for contxt`,
   154  		`function __contxt_debug`,
   155  		`CONTXT_ACTIVE_HELP`,
   156  	}
   157  	assertStringSliceInContent(t, result, expectedSlice)
   158  }
   159  
   160  // testing renamed binary name for powershell completion
   161  func TestCompletionPowershellRenamedBin(t *testing.T) {
   162  	configure.SetBinaryName("contxtV2")
   163  	result := runCobraCommand(func(cobra *runner.SessionCobra, w io.Writer) {
   164  		cobra.RootCmd.GenPowerShellCompletion(w)
   165  	})
   166  	expectedSlice := []string{
   167  		`powershell completion for contxtV2`,
   168  		`function __contxtV2_debug`,
   169  		`CONTXTV2_ACTIVE_HELP`,
   170  	}
   171  	assertStringSliceInContent(t, result, expectedSlice)
   172  }