1
+ // Copyright 2022 The Witness Contributors
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 cmd
16
+
17
+ import (
18
+ "bytes"
19
+ "io"
20
+ "os"
21
+ "testing"
22
+
23
+ "github.com/spf13/cobra"
24
+ "github.com/stretchr/testify/assert"
25
+ "github.com/stretchr/testify/require"
26
+ )
27
+
28
+ func Test_CompletionCmd (t * testing.T ) {
29
+ cmd := CompletionCmd ()
30
+ require .NotNil (t , cmd )
31
+ assert .Equal (t , "completion [bash|zsh|fish|powershell]" , cmd .Use )
32
+ assert .Equal (t , true , cmd .DisableFlagsInUseLine )
33
+ assert .Equal (t , true , cmd .DisableAutoGenTag )
34
+
35
+ // Test that ValidArgs contains the expected values
36
+ expectedValidArgs := []string {"bash" , "zsh" , "fish" , "powershell" }
37
+ assert .Equal (t , expectedValidArgs , cmd .ValidArgs )
38
+
39
+ // Test that Args validation works as expected
40
+ assert .NoError (t , cmd .Args (cmd , []string {"bash" }))
41
+ assert .NoError (t , cmd .Args (cmd , []string {"zsh" }))
42
+ assert .NoError (t , cmd .Args (cmd , []string {"fish" }))
43
+ assert .NoError (t , cmd .Args (cmd , []string {"powershell" }))
44
+ assert .Error (t , cmd .Args (cmd , []string {"invalid" }))
45
+ assert .Error (t , cmd .Args (cmd , []string {}))
46
+ assert .Error (t , cmd .Args (cmd , []string {"bash" , "extra" }))
47
+ }
48
+
49
+ func Test_CompletionCmd_Run (t * testing.T ) {
50
+ tests := []struct {
51
+ name string
52
+ shell string
53
+ contains string // just check for a common string that should be in all completions
54
+ }{
55
+ {
56
+ name : "bash" ,
57
+ shell : "bash" ,
58
+ contains : "# bash completion" ,
59
+ },
60
+ {
61
+ name : "zsh" ,
62
+ shell : "zsh" ,
63
+ contains : "#compdef" ,
64
+ },
65
+ {
66
+ name : "fish" ,
67
+ shell : "fish" ,
68
+ contains : "# fish completion" ,
69
+ },
70
+ {
71
+ name : "powershell" ,
72
+ shell : "powershell" ,
73
+ contains : "Register-ArgumentCompleter" ,
74
+ },
75
+ }
76
+
77
+ for _ , tc := range tests {
78
+ t .Run (tc .name , func (t * testing.T ) {
79
+ // Create a root command for completions to work with
80
+ rootCmd := & cobra.Command {Use : "witness" }
81
+
82
+ // Add the completion command to the root
83
+ completionCmd := CompletionCmd ()
84
+ rootCmd .AddCommand (completionCmd )
85
+
86
+ // Redirect stdout to capture output
87
+ oldStdout := os .Stdout
88
+ r , w , _ := os .Pipe ()
89
+ os .Stdout = w
90
+
91
+ // Execute the completion command directly
92
+ completionCmd .Run (completionCmd , []string {tc .shell })
93
+
94
+ // Restore stdout
95
+ w .Close ()
96
+ os .Stdout = oldStdout
97
+
98
+ // Read the captured output
99
+ var buf bytes.Buffer
100
+ io .Copy (& buf , r )
101
+
102
+ // Check the output contains the expected content
103
+ assert .Contains (t , buf .String (), tc .contains )
104
+ })
105
+ }
106
+ }
0 commit comments