fish_completions_test.go
This file
Path
fish_completions_test.go
143 lines · 3.9 KiB · Go
Last changed by
test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76
History of this fileTake it
1// Copyright 2013-2023 The Cobra Authors2//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 at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415package cobra1617import (18 "bytes"19 "errors"20 "fmt"21 "os"22 "path/filepath"23 "testing"24)2526func 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)3435 buf := new(bytes.Buffer)36 assertNoErr(t, rootCmd.GenFishCompletion(buf, false))37 output := buf.String()3839 check(t, output, ShellCompNoDescRequestCmd)40}4142func 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)5051 buf := new(bytes.Buffer)52 assertNoErr(t, rootCmd.GenFishCompletion(buf, true))53 output := buf.String()5455 check(t, output, ShellCompRequestCmd)56 checkOmit(t, output, ShellCompNoDescRequestCmd)57}5859func 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()6465 // Functions name should have replace the '-'66 check(t, output, "__root_dash_perform_completion")67 checkOmit(t, output, "__root-dash_perform_completion")6869 // The command name should not have replaced the '-'70 check(t, output, "-c root-dash")71 checkOmit(t, output, "-c root_dash")72}7374func 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()7980 // Functions name should have replace the ':'81 check(t, output, "__root_colon_perform_completion")82 checkOmit(t, output, "__root:colon_perform_completion")8384 // The command name should not have replaced the ':'85 check(t, output, "-c root:colon")86 checkOmit(t, output, "-c root_colon")87}8889func TestFishCompletionNoActiveHelp(t *testing.T) {90 c := &Command{Use: "c", Run: emptyRun}9192 buf := new(bytes.Buffer)93 assertNoErr(t, c.GenFishCompletion(buf, true))94 output := buf.String()9596 // check that active help is being disabled97 activeHelpVar := activeHelpEnvVar(c.Name())98 check(t, output, fmt.Sprintf("%s=0", activeHelpVar))99}100101func TestGenFishCompletionFile(t *testing.T) {102 tmpFile, err := os.CreateTemp("", "cobra-test")103 if err != nil {104 t.Fatal(err.Error())105 }106107 defer os.Remove(tmpFile.Name())108109 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)116117 assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))118}119120func TestFailGenFishCompletionFile(t *testing.T) {121 tmpDir, err := os.MkdirTemp("", "cobra-test")122 if err != nil {123 t.Fatal(err.Error())124 }125126 defer os.RemoveAll(tmpDir)127128 f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)129 defer f.Close()130131 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)138139 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}