...

Source file src/github.com/spf13/cobra/fish_completions_test.go

Documentation: github.com/spf13/cobra

     1  // Copyright 2013-2023 The Cobra Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cobra
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  )
    25  
    26  func TestCompleteNoDesCmdInFishScript(t *testing.T) {
    27  	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
    28  	child := &Command{
    29  		Use:               "child",
    30  		ValidArgsFunction: validArgsFunc,
    31  		Run:               emptyRun,
    32  	}
    33  	rootCmd.AddCommand(child)
    34  
    35  	buf := new(bytes.Buffer)
    36  	assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
    37  	output := buf.String()
    38  
    39  	check(t, output, ShellCompNoDescRequestCmd)
    40  }
    41  
    42  func TestCompleteCmdInFishScript(t *testing.T) {
    43  	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
    44  	child := &Command{
    45  		Use:               "child",
    46  		ValidArgsFunction: validArgsFunc,
    47  		Run:               emptyRun,
    48  	}
    49  	rootCmd.AddCommand(child)
    50  
    51  	buf := new(bytes.Buffer)
    52  	assertNoErr(t, rootCmd.GenFishCompletion(buf, true))
    53  	output := buf.String()
    54  
    55  	check(t, output, ShellCompRequestCmd)
    56  	checkOmit(t, output, ShellCompNoDescRequestCmd)
    57  }
    58  
    59  func TestProgWithDash(t *testing.T) {
    60  	rootCmd := &Command{Use: "root-dash", Args: NoArgs, Run: emptyRun}
    61  	buf := new(bytes.Buffer)
    62  	assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
    63  	output := buf.String()
    64  
    65  	// Functions name should have replace the '-'
    66  	check(t, output, "__root_dash_perform_completion")
    67  	checkOmit(t, output, "__root-dash_perform_completion")
    68  
    69  	// The command name should not have replaced the '-'
    70  	check(t, output, "-c root-dash")
    71  	checkOmit(t, output, "-c root_dash")
    72  }
    73  
    74  func TestProgWithColon(t *testing.T) {
    75  	rootCmd := &Command{Use: "root:colon", Args: NoArgs, Run: emptyRun}
    76  	buf := new(bytes.Buffer)
    77  	assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
    78  	output := buf.String()
    79  
    80  	// Functions name should have replace the ':'
    81  	check(t, output, "__root_colon_perform_completion")
    82  	checkOmit(t, output, "__root:colon_perform_completion")
    83  
    84  	// The command name should not have replaced the ':'
    85  	check(t, output, "-c root:colon")
    86  	checkOmit(t, output, "-c root_colon")
    87  }
    88  
    89  func TestFishCompletionNoActiveHelp(t *testing.T) {
    90  	c := &Command{Use: "c", Run: emptyRun}
    91  
    92  	buf := new(bytes.Buffer)
    93  	assertNoErr(t, c.GenFishCompletion(buf, true))
    94  	output := buf.String()
    95  
    96  	// check that active help is being disabled
    97  	activeHelpVar := activeHelpEnvVar(c.Name())
    98  	check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
    99  }
   100  
   101  func TestGenFishCompletionFile(t *testing.T) {
   102  	tmpFile, err := os.CreateTemp("", "cobra-test")
   103  	if err != nil {
   104  		t.Fatal(err.Error())
   105  	}
   106  
   107  	defer os.Remove(tmpFile.Name())
   108  
   109  	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
   110  	child := &Command{
   111  		Use:               "child",
   112  		ValidArgsFunction: validArgsFunc,
   113  		Run:               emptyRun,
   114  	}
   115  	rootCmd.AddCommand(child)
   116  
   117  	assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))
   118  }
   119  
   120  func TestFailGenFishCompletionFile(t *testing.T) {
   121  	tmpDir, err := os.MkdirTemp("", "cobra-test")
   122  	if err != nil {
   123  		t.Fatal(err.Error())
   124  	}
   125  
   126  	defer os.RemoveAll(tmpDir)
   127  
   128  	f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)
   129  	defer f.Close()
   130  
   131  	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
   132  	child := &Command{
   133  		Use:               "child",
   134  		ValidArgsFunction: validArgsFunc,
   135  		Run:               emptyRun,
   136  	}
   137  	rootCmd.AddCommand(child)
   138  
   139  	got := rootCmd.GenFishCompletionFile(f.Name(), false)
   140  	if !errors.Is(got, os.ErrPermission) {
   141  		t.Errorf("got: %s, want: %s", got.Error(), os.ErrPermission.Error())
   142  	}
   143  }
   144  

View as plain text