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 ensure 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 24 "helm.sh/helm/v3/pkg/helmpath" 25 "helm.sh/helm/v3/pkg/helmpath/xdg" 26 ) 27 28 // HelmHome sets up a Helm Home in a temp dir. 29 func HelmHome(t *testing.T) { 30 t.Helper() 31 base := t.TempDir() 32 os.Setenv(xdg.CacheHomeEnvVar, base) 33 os.Setenv(xdg.ConfigHomeEnvVar, base) 34 os.Setenv(xdg.DataHomeEnvVar, base) 35 os.Setenv(helmpath.CacheHomeEnvVar, "") 36 os.Setenv(helmpath.ConfigHomeEnvVar, "") 37 os.Setenv(helmpath.DataHomeEnvVar, "") 38 } 39 40 // TempFile ensures a temp file for unit testing purposes. 41 // 42 // It returns the path to the directory (to which you will still need to join the filename) 43 // 44 // The returned directory is automatically removed when the test and all its subtests complete. 45 // 46 // tempdir := TempFile(t, "foo", []byte("bar")) 47 // filename := filepath.Join(tempdir, "foo") 48 func TempFile(t *testing.T, name string, data []byte) string { 49 path := t.TempDir() 50 filename := filepath.Join(path, name) 51 if err := os.WriteFile(filename, data, 0755); err != nil { 52 t.Fatal(err) 53 } 54 return path 55 } 56