github.com/x-helm/helm@v3.0.0-beta.3+incompatible/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 "helm.sh/helm/internal/test/ensure" 25 "helm.sh/helm/pkg/helmpath" 26 "helm.sh/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 envars map[string]string 35 }{ 36 { 37 name: "defaults", 38 args: "home", 39 }, 40 { 41 name: "with $XDG_CACHE_HOME set", 42 args: "home", 43 envars: map[string]string{xdg.CacheHomeEnvVar: "/bar"}, 44 cachePath: "/bar/helm", 45 }, 46 { 47 name: "with $XDG_CONFIG_HOME set", 48 args: "home", 49 envars: map[string]string{xdg.ConfigHomeEnvVar: "/bar"}, 50 configPath: "/bar/helm", 51 }, 52 { 53 name: "with $XDG_DATA_HOME set", 54 args: "home", 55 envars: map[string]string{xdg.DataHomeEnvVar: "/bar"}, 56 dataPath: "/bar/helm", 57 }, 58 } 59 60 for _, tt := range tests { 61 t.Run(tt.name, func(t *testing.T) { 62 defer ensure.HelmHome(t)() 63 64 for k, v := range tt.envars { 65 os.Setenv(k, v) 66 } 67 68 if _, _, err := executeActionCommand(tt.args); err != nil { 69 t.Fatalf("unexpected error: %s", err) 70 } 71 72 // NOTE(bacongobbler): we need to check here after calling ensure.HelmHome so we 73 // load the proper paths after XDG_*_HOME is set 74 if tt.cachePath == "" { 75 tt.cachePath = filepath.Join(os.Getenv(xdg.CacheHomeEnvVar), "helm") 76 } 77 78 if tt.configPath == "" { 79 tt.configPath = filepath.Join(os.Getenv(xdg.ConfigHomeEnvVar), "helm") 80 } 81 82 if tt.dataPath == "" { 83 tt.dataPath = filepath.Join(os.Getenv(xdg.DataHomeEnvVar), "helm") 84 } 85 86 if helmpath.CachePath() != tt.cachePath { 87 t.Errorf("expected cache path %q, got %q", tt.cachePath, helmpath.CachePath()) 88 } 89 if helmpath.ConfigPath() != tt.configPath { 90 t.Errorf("expected config path %q, got %q", tt.configPath, helmpath.ConfigPath()) 91 } 92 if helmpath.DataPath() != tt.dataPath { 93 t.Errorf("expected data path %q, got %q", tt.dataPath, helmpath.DataPath()) 94 } 95 }) 96 } 97 }