Merge "RIC:1060: Change in PTL"
[ric-plt/sdlgo.git] / internal / cli / completion_test.go
1 /*
2    Copyright (c) 2022 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20  */
21
22 package cli_test
23
24 import (
25         "bytes"
26         "errors"
27         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/cli"
28         "github.com/stretchr/testify/assert"
29         "testing"
30 )
31
32 func TestCompletionHelp(t *testing.T) {
33         var expOkErr error
34         expHelp := "To load bash completions:\nsource <(sdlcli completion bash)"
35         expNokErr := errors.New("invalid argument \"zsh\" for \"completion\"")
36         expNokHelp := "Usage:\n  completion [bash]"
37         tests := []struct {
38                 args      string
39                 expErr    error
40                 expOutput string
41         }{
42                 {args: "-h", expErr: expOkErr, expOutput: expHelp},
43                 {args: "--help", expErr: expOkErr, expOutput: expHelp},
44                 {args: "zsh", expErr: expNokErr, expOutput: expNokHelp},
45         }
46
47         for _, test := range tests {
48                 buf := new(bytes.Buffer)
49                 cmd := cli.NewCompletionCmdForTest(buf)
50                 cmd.SetOut(buf)
51                 cmd.SetErr(buf)
52                 cmd.SetArgs([]string{test.args})
53
54                 err := cmd.Execute()
55                 result := buf.String()
56                 assert.Equal(t, test.expErr, err)
57                 assert.Contains(t, result, test.expOutput)
58         }
59 }
60
61 func TestCompletionBash(t *testing.T) {
62         buf := new(bytes.Buffer)
63         cmd := cli.NewCompletionCmdForTest(buf)
64         cmd.SetArgs([]string{"bash"})
65
66         err := cmd.Execute()
67         result := buf.String()
68         assert.Nil(t, err)
69         assert.Contains(t, result, "# bash completion for completion")
70 }