X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=internal%2Fcli%2Fcompletion_test.go;fp=internal%2Fcli%2Fcompletion_test.go;h=7d72068645f75c0f89d47bfe1f4bf0a8779de0e2;hb=644e678612dbb9645da865d3d0d459a4570aa2af;hp=0000000000000000000000000000000000000000;hpb=7e262d4f4e530aec98903e99b195d9e2d07dbcb0;p=ric-plt%2Fsdlgo.git diff --git a/internal/cli/completion_test.go b/internal/cli/completion_test.go new file mode 100644 index 0000000..7d72068 --- /dev/null +++ b/internal/cli/completion_test.go @@ -0,0 +1,70 @@ +/* + Copyright (c) 2022 Nokia. + + 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. +*/ + +/* + * This source code is part of the near-RT RIC (RAN Intelligent Controller) + * platform project (RICP). + */ + +package cli_test + +import ( + "bytes" + "errors" + "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/cli" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCompletionHelp(t *testing.T) { + var expOkErr error + expHelp := "To load bash completions:\nsource <(sdlcli completion bash)" + expNokErr := errors.New("invalid argument \"zsh\" for \"completion\"") + expNokHelp := "Usage:\n completion [bash]" + tests := []struct { + args string + expErr error + expOutput string + }{ + {args: "-h", expErr: expOkErr, expOutput: expHelp}, + {args: "--help", expErr: expOkErr, expOutput: expHelp}, + {args: "zsh", expErr: expNokErr, expOutput: expNokHelp}, + } + + for _, test := range tests { + buf := new(bytes.Buffer) + cmd := cli.NewCompletionCmdForTest(buf) + cmd.SetOut(buf) + cmd.SetErr(buf) + cmd.SetArgs([]string{test.args}) + + err := cmd.Execute() + result := buf.String() + assert.Equal(t, test.expErr, err) + assert.Contains(t, result, test.expOutput) + } +} + +func TestCompletionBash(t *testing.T) { + buf := new(bytes.Buffer) + cmd := cli.NewCompletionCmdForTest(buf) + cmd.SetArgs([]string{"bash"}) + + err := cmd.Execute() + result := buf.String() + assert.Nil(t, err) + assert.Contains(t, result, "# bash completion for completion") +}