Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 89d7b83 point at it.

update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a

line 1why, one step back
1// Copyright 2013-2023 The Cobra Authors

add missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9

lines 2–14why, one step back
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 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.14

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 15–18why, one step back
15package cobra
1617import (18 "bytes"

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 19why, one step back
19 "errors"

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 20why, one step back
20 "fmt"

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

line 21why, one step back
21 "os"

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 22why, one step back
22 "path/filepath"

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 23–86why, one step back
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")

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 87–98why, one step back
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))

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

lines 99–101why, one step back
99}
100101func TestGenFishCompletionFile(t *testing.T) {

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 102why, one step back
102 tmpFile, err := os.CreateTemp("", "cobra-test")

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

line 103why, one step back
103 if err != nil {

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 104why, one step back
104 t.Fatal(err.Error())

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

lines 105–106why, one step back
105 }
106

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 107why, one step back
107 defer os.Remove(tmpFile.Name())

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

lines 108–116why, one step back
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

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 117why, one step back
117 assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

lines 118–120why, one step back
118}
119120func TestFailGenFishCompletionFile(t *testing.T) {

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 121why, one step back
121 tmpDir, err := os.MkdirTemp("", "cobra-test")

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

line 122why, one step back
122 if err != nil {

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 123why, one step back
123 t.Fatal(err.Error())

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

lines 124–125why, one step back
124 }
125

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 126why, one step back
126 defer os.RemoveAll(tmpDir)

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

line 127why, one step back
127

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

line 128why, one step back
128 f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

lines 129–138why, one step back
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)138

test: make fish_completions_test more robust (#1980)Branch Vincentbefore adoption988bd76

lines 139–141why, one step back
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())

Add unit test for fish completion (#1515)kidabefore adoptioned7bb9d

line 142why, one step back
142 }

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 143why, one step back
143}