aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/shell_completion_test.go
blob: 9bd821d8dbda04fca7a153fd18b531251f6d2faf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
	The purpose of this test is to keep a consistent
	and great shell autocompletion experience.

	This test ensures that each command and flag has a shell completion
	function set. (except boolean, hidden and deprecated flags)

	Shell completion functions are defined in:
	- "github.com/containers/podman/v3/cmd/podman/common/completion.go"
	- "github.com/containers/common/pkg/completion"
	and are called Autocomplete...

	To apply such function to a command use the ValidArgsFunction field.
	To apply such function to a flag use cmd.RegisterFlagCompletionFunc(name,func)

	If there are any questions/problems please tag Luap99.
*/

package main

import (
	"testing"

	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

func TestShellCompletionFunctions(t *testing.T) {
	rootCmd := parseCommands()
	checkCommand(t, rootCmd)
}

func checkCommand(t *testing.T, cmd *cobra.Command) {
	if cmd.HasSubCommands() {
		for _, childCmd := range cmd.Commands() {
			checkCommand(t, childCmd)
		}

		// if not check if completion for that command is provided
	} else if cmd.ValidArgsFunction == nil && cmd.ValidArgs == nil {
		t.Errorf("%s command has no shell completion function set", cmd.CommandPath())
	}

	// loop over all local flags
	cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
		// an error means that there is a completion function for this flag
		err := cmd.RegisterFlagCompletionFunc(flag.Name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
			return nil, cobra.ShellCompDirectiveDefault
		})

		switch {
		case flag.Value.Type() == "bool" && err != nil:
			// make sure bool flags don't have a completion function
			t.Errorf(`%s --%s is a bool flag but has a shell completion function set.
You have to remove this shell completion function.`, cmd.CommandPath(), flag.Name)
			return

		case flag.Value.Type() == "bool" || flag.Hidden || len(flag.Deprecated) > 0:
			// skip bool, hidden and deprecated flags
			return

		case err == nil:
			// there is no shell completion function
			t.Errorf("%s --%s flag has no shell completion function set", cmd.CommandPath(), flag.Name)
		}
	})
}