git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/cobra/fish_completions_test.go (about) 1 // Copyright 2013-2022 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 "fmt" 20 "log" 21 "os" 22 "testing" 23 ) 24 25 func TestCompleteNoDesCmdInFishScript(t *testing.T) { 26 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} 27 child := &Command{ 28 Use: "child", 29 ValidArgsFunction: validArgsFunc, 30 Run: emptyRun, 31 } 32 rootCmd.AddCommand(child) 33 34 buf := new(bytes.Buffer) 35 assertNoErr(t, rootCmd.GenFishCompletion(buf, false)) 36 output := buf.String() 37 38 check(t, output, ShellCompNoDescRequestCmd) 39 } 40 41 func TestCompleteCmdInFishScript(t *testing.T) { 42 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} 43 child := &Command{ 44 Use: "child", 45 ValidArgsFunction: validArgsFunc, 46 Run: emptyRun, 47 } 48 rootCmd.AddCommand(child) 49 50 buf := new(bytes.Buffer) 51 assertNoErr(t, rootCmd.GenFishCompletion(buf, true)) 52 output := buf.String() 53 54 check(t, output, ShellCompRequestCmd) 55 checkOmit(t, output, ShellCompNoDescRequestCmd) 56 } 57 58 func TestProgWithDash(t *testing.T) { 59 rootCmd := &Command{Use: "root-dash", Args: NoArgs, Run: emptyRun} 60 buf := new(bytes.Buffer) 61 assertNoErr(t, rootCmd.GenFishCompletion(buf, false)) 62 output := buf.String() 63 64 // Functions name should have replace the '-' 65 check(t, output, "__root_dash_perform_completion") 66 checkOmit(t, output, "__root-dash_perform_completion") 67 68 // The command name should not have replaced the '-' 69 check(t, output, "-c root-dash") 70 checkOmit(t, output, "-c root_dash") 71 } 72 73 func TestProgWithColon(t *testing.T) { 74 rootCmd := &Command{Use: "root:colon", Args: NoArgs, Run: emptyRun} 75 buf := new(bytes.Buffer) 76 assertNoErr(t, rootCmd.GenFishCompletion(buf, false)) 77 output := buf.String() 78 79 // Functions name should have replace the ':' 80 check(t, output, "__root_colon_perform_completion") 81 checkOmit(t, output, "__root:colon_perform_completion") 82 83 // The command name should not have replaced the ':' 84 check(t, output, "-c root:colon") 85 checkOmit(t, output, "-c root_colon") 86 } 87 88 func TestFishCompletionNoActiveHelp(t *testing.T) { 89 c := &Command{Use: "c", Run: emptyRun} 90 91 buf := new(bytes.Buffer) 92 assertNoErr(t, c.GenFishCompletion(buf, true)) 93 output := buf.String() 94 95 // check that active help is being disabled 96 activeHelpVar := activeHelpEnvVar(c.Name()) 97 check(t, output, fmt.Sprintf("%s=0", activeHelpVar)) 98 } 99 100 func TestGenFishCompletionFile(t *testing.T) { 101 err := os.Mkdir("./tmp", 0755) 102 if err != nil { 103 log.Fatal(err.Error()) 104 } 105 106 defer os.RemoveAll("./tmp") 107 108 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} 109 child := &Command{ 110 Use: "child", 111 ValidArgsFunction: validArgsFunc, 112 Run: emptyRun, 113 } 114 rootCmd.AddCommand(child) 115 116 assertNoErr(t, rootCmd.GenFishCompletionFile("./tmp/test", false)) 117 } 118 119 func TestFailGenFishCompletionFile(t *testing.T) { 120 err := os.Mkdir("./tmp", 0755) 121 if err != nil { 122 log.Fatal(err.Error()) 123 } 124 125 defer os.RemoveAll("./tmp") 126 127 f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400) 128 defer f.Close() 129 130 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} 131 child := &Command{ 132 Use: "child", 133 ValidArgsFunction: validArgsFunc, 134 Run: emptyRun, 135 } 136 rootCmd.AddCommand(child) 137 138 got := rootCmd.GenFishCompletionFile("./tmp/test", false) 139 if got == nil { 140 t.Error("should raise permission denied error") 141 } 142 143 if os.Getenv("MSYSTEM") == "MINGW64" { 144 if got.Error() != "open ./tmp/test: Access is denied." { 145 t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: Access is denied.") 146 } 147 } else { 148 if got.Error() != "open ./tmp/test: permission denied" { 149 t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied") 150 } 151 } 152 }