fish_completions_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. package cobra
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. )
  23. func TestCompleteNoDesCmdInFishScript(t *testing.T) {
  24. rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
  25. child := &Command{
  26. Use: "child",
  27. ValidArgsFunction: validArgsFunc,
  28. Run: emptyRun,
  29. }
  30. rootCmd.AddCommand(child)
  31. buf := new(bytes.Buffer)
  32. assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
  33. output := buf.String()
  34. check(t, output, ShellCompNoDescRequestCmd)
  35. }
  36. func TestCompleteCmdInFishScript(t *testing.T) {
  37. rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
  38. child := &Command{
  39. Use: "child",
  40. ValidArgsFunction: validArgsFunc,
  41. Run: emptyRun,
  42. }
  43. rootCmd.AddCommand(child)
  44. buf := new(bytes.Buffer)
  45. assertNoErr(t, rootCmd.GenFishCompletion(buf, true))
  46. output := buf.String()
  47. check(t, output, ShellCompRequestCmd)
  48. checkOmit(t, output, ShellCompNoDescRequestCmd)
  49. }
  50. func TestProgWithDash(t *testing.T) {
  51. rootCmd := &Command{Use: "root-dash", Args: NoArgs, Run: emptyRun}
  52. buf := new(bytes.Buffer)
  53. assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
  54. output := buf.String()
  55. // Functions name should have replace the '-'
  56. check(t, output, "__root_dash_perform_completion")
  57. checkOmit(t, output, "__root-dash_perform_completion")
  58. // The command name should not have replaced the '-'
  59. check(t, output, "-c root-dash")
  60. checkOmit(t, output, "-c root_dash")
  61. }
  62. func TestProgWithColon(t *testing.T) {
  63. rootCmd := &Command{Use: "root:colon", Args: NoArgs, Run: emptyRun}
  64. buf := new(bytes.Buffer)
  65. assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
  66. output := buf.String()
  67. // Functions name should have replace the ':'
  68. check(t, output, "__root_colon_perform_completion")
  69. checkOmit(t, output, "__root:colon_perform_completion")
  70. // The command name should not have replaced the ':'
  71. check(t, output, "-c root:colon")
  72. checkOmit(t, output, "-c root_colon")
  73. }
  74. func TestFishCompletionNoActiveHelp(t *testing.T) {
  75. c := &Command{Use: "c", Run: emptyRun}
  76. buf := new(bytes.Buffer)
  77. assertNoErr(t, c.GenFishCompletion(buf, true))
  78. output := buf.String()
  79. // check that active help is being disabled
  80. activeHelpVar := activeHelpEnvVar(c.Name())
  81. check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
  82. }
  83. func TestGenFishCompletionFile(t *testing.T) {
  84. tmpFile, err := os.CreateTemp("", "cobra-test")
  85. if err != nil {
  86. t.Fatal(err.Error())
  87. }
  88. defer os.Remove(tmpFile.Name())
  89. rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
  90. child := &Command{
  91. Use: "child",
  92. ValidArgsFunction: validArgsFunc,
  93. Run: emptyRun,
  94. }
  95. rootCmd.AddCommand(child)
  96. assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))
  97. }
  98. func TestFailGenFishCompletionFile(t *testing.T) {
  99. tmpDir, err := os.MkdirTemp("", "cobra-test")
  100. if err != nil {
  101. t.Fatal(err.Error())
  102. }
  103. defer os.RemoveAll(tmpDir)
  104. f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)
  105. defer f.Close()
  106. rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
  107. child := &Command{
  108. Use: "child",
  109. ValidArgsFunction: validArgsFunc,
  110. Run: emptyRun,
  111. }
  112. rootCmd.AddCommand(child)
  113. got := rootCmd.GenFishCompletionFile(f.Name(), false)
  114. if !errors.Is(got, os.ErrPermission) {
  115. t.Errorf("got: %s, want: %s", got.Error(), os.ErrPermission.Error())
  116. }
  117. }