cobra/nushell_completions_test.go

99 lines
2.9 KiB
Go
Raw Normal View History

2022-11-11 19:05:24 -07:00
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra
import (
"bytes"
2024-12-01 21:05:39 -07:00
"errors"
"fmt"
2022-11-11 19:05:24 -07:00
"log"
"os"
2024-12-01 21:05:39 -07:00
"path/filepath"
2022-11-11 19:05:24 -07:00
"testing"
)
func TestGenNushellCompletion(t *testing.T) {
rootCmd := &Command{Use: "kubectl", Run: emptyRun}
2022-11-11 19:05:24 -07:00
rootCmd.PersistentFlags().String("server", "s", "The address and port of the Kubernetes API server")
rootCmd.PersistentFlags().BoolP("skip-headers", "", false, "The address and port of the Kubernetes API serverIf true, avoid header prefixes in the log messages")
getCmd := &Command{
Use: "get",
Short: "Display one or many resources",
ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"},
ValidArgs: []string{"pod", "node", "service", "replicationcontroller"},
Run: emptyRun,
}
rootCmd.AddCommand(getCmd)
buf := new(bytes.Buffer)
assertNoErr(t, rootCmd.GenNushellCompletion(buf, true))
2022-11-11 19:05:24 -07:00
}
func TestGenNushellCompletionFile(t *testing.T) {
2024-12-01 21:05:39 -07:00
tmpFile, err := os.CreateTemp("", "cobra-test")
2022-11-11 19:05:24 -07:00
if err != nil {
log.Fatal(err.Error())
}
2024-12-01 21:05:39 -07:00
defer os.RemoveAll(tmpFile.Name())
2022-11-11 19:05:24 -07:00
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
child := &Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
Run: emptyRun,
}
rootCmd.AddCommand(child)
2024-12-01 21:05:39 -07:00
assertNoErr(t, rootCmd.GenNushellCompletionFile(tmpFile.Name(), true))
2022-11-11 19:05:24 -07:00
}
func TestFailGenNushellCompletionFile(t *testing.T) {
2024-12-01 21:05:39 -07:00
tmpDir, err := os.MkdirTemp("", "cobra-test")
2022-11-11 19:05:24 -07:00
if err != nil {
2024-12-01 21:05:39 -07:00
t.Fatal(err.Error())
2022-11-11 19:05:24 -07:00
}
2024-12-01 21:05:39 -07:00
defer os.RemoveAll(tmpDir)
2022-11-11 19:05:24 -07:00
2024-12-01 21:05:39 -07:00
f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)
2022-11-11 19:05:24 -07:00
defer f.Close()
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
child := &Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
Run: emptyRun,
}
rootCmd.AddCommand(child)
2024-12-08 20:33:48 -07:00
got := rootCmd.GenNushellCompletionFile(f.Name(), false)
2024-12-01 21:05:39 -07:00
if !errors.Is(got, os.ErrPermission) {
t.Errorf("got: %s, want: %s", got.Error(), os.ErrPermission.Error())
2022-11-11 19:05:24 -07:00
}
}
2024-11-30 14:57:31 -07:00
func TestNushellCompletionNoActiveHelp(t *testing.T) {
c := &Command{Use: "c", Run: emptyRun}
buf := new(bytes.Buffer)
assertNoErr(t, c.GenNushellCompletion(buf, true))
output := buf.String()
// check that active help is being disabled
activeHelpVar := activeHelpGlobalEnvVar
check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
}