github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/cmd/helm/root_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 main 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/stefanmcshane/helm/internal/test/ensure" 25 "github.com/stefanmcshane/helm/pkg/helmpath" 26 "github.com/stefanmcshane/helm/pkg/helmpath/xdg" 27 ) 28 29 func TestRootCmd(t *testing.T) { 30 defer resetEnv()() 31 32 tests := []struct { 33 name, args, cachePath, configPath, dataPath string 34 envvars map[string]string 35 }{ 36 { 37 name: "defaults", 38 args: "env", 39 }, 40 { 41 name: "with $XDG_CACHE_HOME set", 42 args: "env", 43 envvars: map[string]string{xdg.CacheHomeEnvVar: "/bar"}, 44 cachePath: "/bar/helm", 45 }, 46 { 47 name: "with $XDG_CONFIG_HOME set", 48 args: "env", 49 envvars: map[string]string{xdg.ConfigHomeEnvVar: "/bar"}, 50 configPath: "/bar/helm", 51 }, 52 { 53 name: "with $XDG_DATA_HOME set", 54 args: "env", 55 envvars: map[string]string{xdg.DataHomeEnvVar: "/bar"}, 56 dataPath: "/bar/helm", 57 }, 58 { 59 name: "with $HELM_CACHE_HOME set", 60 args: "env", 61 envvars: map[string]string{helmpath.CacheHomeEnvVar: "/foo/helm"}, 62 cachePath: "/foo/helm", 63 }, 64 { 65 name: "with $HELM_CONFIG_HOME set", 66 args: "env", 67 envvars: map[string]string{helmpath.ConfigHomeEnvVar: "/foo/helm"}, 68 configPath: "/foo/helm", 69 }, 70 { 71 name: "with $HELM_DATA_HOME set", 72 args: "env", 73 envvars: map[string]string{helmpath.DataHomeEnvVar: "/foo/helm"}, 74 dataPath: "/foo/helm", 75 }, 76 } 77 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 defer ensure.HelmHome(t)() 81 82 for k, v := range tt.envvars { 83 os.Setenv(k, v) 84 } 85 86 if _, _, err := executeActionCommand(tt.args); err != nil { 87 t.Fatalf("unexpected error: %s", err) 88 } 89 90 // NOTE(bacongobbler): we need to check here after calling ensure.HelmHome so we 91 // load the proper paths after XDG_*_HOME is set 92 if tt.cachePath == "" { 93 tt.cachePath = filepath.Join(os.Getenv(xdg.CacheHomeEnvVar), "helm") 94 } 95 96 if tt.configPath == "" { 97 tt.configPath = filepath.Join(os.Getenv(xdg.ConfigHomeEnvVar), "helm") 98 } 99 100 if tt.dataPath == "" { 101 tt.dataPath = filepath.Join(os.Getenv(xdg.DataHomeEnvVar), "helm") 102 } 103 104 if helmpath.CachePath() != tt.cachePath { 105 t.Errorf("expected cache path %q, got %q", tt.cachePath, helmpath.CachePath()) 106 } 107 if helmpath.ConfigPath() != tt.configPath { 108 t.Errorf("expected config path %q, got %q", tt.configPath, helmpath.ConfigPath()) 109 } 110 if helmpath.DataPath() != tt.dataPath { 111 t.Errorf("expected data path %q, got %q", tt.dataPath, helmpath.DataPath()) 112 } 113 }) 114 } 115 } 116 117 func TestUnknownSubCmd(t *testing.T) { 118 _, _, err := executeActionCommand("foobar") 119 120 if err == nil || err.Error() != `unknown command "foobar" for "helm"` { 121 t.Errorf("Expect unknown command error, got %q", err) 122 } 123 } 124 125 // Need the release of Cobra following 1.0 to be able to disable 126 // file completion on the root command. Until then, we cannot 127 // because it would break 'helm help <TAB>' 128 // 129 // func TestRootFileCompletion(t *testing.T) { 130 // checkFileCompletion(t, "", false) 131 // }