github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/show_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  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"helm.sh/helm/v3/pkg/repo/repotest"
    26  )
    27  
    28  func TestShowPreReleaseChart(t *testing.T) {
    29  	srv, err := repotest.NewTempServerWithCleanup(t, "testdata/testcharts/*.tgz*")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	defer srv.Stop()
    34  
    35  	if err := srv.LinkIndices(); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	tests := []struct {
    40  		name        string
    41  		args        string
    42  		flags       string
    43  		fail        bool
    44  		expectedErr string
    45  	}{
    46  		{
    47  			name:        "show pre-release chart",
    48  			args:        "test/pre-release-chart",
    49  			fail:        true,
    50  			expectedErr: "chart \"pre-release-chart\" matching  not found in test index. (try 'helm repo update'): no chart version found for pre-release-chart-",
    51  		},
    52  		{
    53  			name:        "show pre-release chart",
    54  			args:        "test/pre-release-chart",
    55  			fail:        true,
    56  			flags:       "--version 1.0.0",
    57  			expectedErr: "chart \"pre-release-chart\" matching 1.0.0 not found in test index. (try 'helm repo update'): no chart version found for pre-release-chart-1.0.0",
    58  		},
    59  		{
    60  			name:  "show pre-release chart with 'devel' flag",
    61  			args:  "test/pre-release-chart",
    62  			flags: "--devel",
    63  			fail:  false,
    64  		},
    65  	}
    66  
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			outdir := srv.Root()
    70  			cmd := fmt.Sprintf("show all '%s' %s --repository-config %s --repository-cache %s",
    71  				tt.args,
    72  				tt.flags,
    73  				filepath.Join(outdir, "repositories.yaml"),
    74  				outdir,
    75  			)
    76  			//_, out, err := executeActionCommand(cmd)
    77  			_, _, err := executeActionCommand(cmd)
    78  			if err != nil {
    79  				if tt.fail {
    80  					if !strings.Contains(err.Error(), tt.expectedErr) {
    81  						t.Errorf("%q expected error: %s, got: %s", tt.name, tt.expectedErr, err.Error())
    82  					}
    83  					return
    84  				}
    85  				t.Errorf("%q reported error: %s", tt.name, err)
    86  			}
    87  		})
    88  	}
    89  }
    90  
    91  func TestShowVersionCompletion(t *testing.T) {
    92  	repoFile := "testdata/helmhome/helm/repositories.yaml"
    93  	repoCache := "testdata/helmhome/helm/repository"
    94  
    95  	repoSetup := fmt.Sprintf("--repository-config %s --repository-cache %s", repoFile, repoCache)
    96  
    97  	tests := []cmdTestCase{{
    98  		name:   "completion for show version flag",
    99  		cmd:    fmt.Sprintf("%s __complete show chart testing/alpine --version ''", repoSetup),
   100  		golden: "output/version-comp.txt",
   101  	}, {
   102  		name:   "completion for show version flag, no filter",
   103  		cmd:    fmt.Sprintf("%s __complete show chart testing/alpine --version 0.3", repoSetup),
   104  		golden: "output/version-comp.txt",
   105  	}, {
   106  		name:   "completion for show version flag too few args",
   107  		cmd:    fmt.Sprintf("%s __complete show chart --version ''", repoSetup),
   108  		golden: "output/version-invalid-comp.txt",
   109  	}, {
   110  		name:   "completion for show version flag too many args",
   111  		cmd:    fmt.Sprintf("%s __complete show chart testing/alpine badarg --version ''", repoSetup),
   112  		golden: "output/version-invalid-comp.txt",
   113  	}, {
   114  		name:   "completion for show version flag invalid chart",
   115  		cmd:    fmt.Sprintf("%s __complete show chart invalid/invalid --version ''", repoSetup),
   116  		golden: "output/version-invalid-comp.txt",
   117  	}, {
   118  		name:   "completion for show version flag with all",
   119  		cmd:    fmt.Sprintf("%s __complete show all testing/alpine --version ''", repoSetup),
   120  		golden: "output/version-comp.txt",
   121  	}, {
   122  		name:   "completion for show version flag with readme",
   123  		cmd:    fmt.Sprintf("%s __complete show readme testing/alpine --version ''", repoSetup),
   124  		golden: "output/version-comp.txt",
   125  	}, {
   126  		name:   "completion for show version flag with values",
   127  		cmd:    fmt.Sprintf("%s __complete show values testing/alpine --version ''", repoSetup),
   128  		golden: "output/version-comp.txt",
   129  	}}
   130  	runTestCmd(t, tests)
   131  }
   132  
   133  func TestShowFileCompletion(t *testing.T) {
   134  	checkFileCompletion(t, "show", false)
   135  }
   136  
   137  func TestShowAllFileCompletion(t *testing.T) {
   138  	checkFileCompletion(t, "show all", true)
   139  }
   140  
   141  func TestShowChartFileCompletion(t *testing.T) {
   142  	checkFileCompletion(t, "show chart", true)
   143  }
   144  
   145  func TestShowReadmeFileCompletion(t *testing.T) {
   146  	checkFileCompletion(t, "show readme", true)
   147  }
   148  
   149  func TestShowValuesFileCompletion(t *testing.T) {
   150  	checkFileCompletion(t, "show values", true)
   151  }
   152  
   153  func TestShowCRDsFileCompletion(t *testing.T) {
   154  	checkFileCompletion(t, "show crds", true)
   155  }