github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/completion_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  package cmd
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	"helm.sh/helm/v3/pkg/chart"
    25  	"helm.sh/helm/v3/pkg/release"
    26  )
    27  
    28  // Check if file completion should be performed according to parameter 'shouldBePerformed'
    29  func checkFileCompletion(t *testing.T, cmdName string, shouldBePerformed bool) {
    30  	storage := storageFixture()
    31  	storage.Create(&release.Release{
    32  		Name: "myrelease",
    33  		Info: &release.Info{Status: release.StatusDeployed},
    34  		Chart: &chart.Chart{
    35  			Metadata: &chart.Metadata{
    36  				Name:    "Myrelease-Chart",
    37  				Version: "1.2.3",
    38  			},
    39  		},
    40  		Version: 1,
    41  	})
    42  
    43  	testcmd := fmt.Sprintf("__complete %s ''", cmdName)
    44  	_, out, err := executeActionCommandC(storage, testcmd)
    45  	if err != nil {
    46  		t.Errorf("unexpected error, %s", err)
    47  	}
    48  	if !strings.Contains(out, "ShellCompDirectiveNoFileComp") != shouldBePerformed {
    49  		if shouldBePerformed {
    50  			t.Errorf("Unexpected directive ShellCompDirectiveNoFileComp when completing '%s'", cmdName)
    51  		} else {
    52  
    53  			t.Errorf("Did not receive directive ShellCompDirectiveNoFileComp when completing '%s'", cmdName)
    54  		}
    55  		t.Log(out)
    56  	}
    57  }
    58  
    59  func TestCompletionFileCompletion(t *testing.T) {
    60  	checkFileCompletion(t, "completion", false)
    61  	checkFileCompletion(t, "completion bash", false)
    62  	checkFileCompletion(t, "completion zsh", false)
    63  	checkFileCompletion(t, "completion fish", false)
    64  }
    65  
    66  func checkReleaseCompletion(t *testing.T, cmdName string, multiReleasesAllowed bool) {
    67  	multiReleaseTestGolden := "output/empty_nofile_comp.txt"
    68  	if multiReleasesAllowed {
    69  		multiReleaseTestGolden = "output/release_list_repeat_comp.txt"
    70  	}
    71  	tests := []cmdTestCase{{
    72  		name:   "completion for uninstall",
    73  		cmd:    fmt.Sprintf("__complete %s ''", cmdName),
    74  		golden: "output/release_list_comp.txt",
    75  		rels: []*release.Release{
    76  			release.Mock(&release.MockReleaseOptions{Name: "athos"}),
    77  			release.Mock(&release.MockReleaseOptions{Name: "porthos"}),
    78  			release.Mock(&release.MockReleaseOptions{Name: "aramis"}),
    79  		},
    80  	}, {
    81  		name:   "completion for uninstall repetition",
    82  		cmd:    fmt.Sprintf("__complete %s porthos ''", cmdName),
    83  		golden: multiReleaseTestGolden,
    84  		rels: []*release.Release{
    85  			release.Mock(&release.MockReleaseOptions{Name: "athos"}),
    86  			release.Mock(&release.MockReleaseOptions{Name: "porthos"}),
    87  			release.Mock(&release.MockReleaseOptions{Name: "aramis"}),
    88  		},
    89  	}}
    90  	for _, test := range tests {
    91  		runTestCmd(t, []cmdTestCase{test})
    92  	}
    93  }