github.com/zsuzhengdu/helm@v3.0.0-beta.3+incompatible/pkg/engine/files_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package engine 17 18 import ( 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 var cases = []struct { 25 path, data string 26 }{ 27 {"ship/captain.txt", "The Captain"}, 28 {"ship/stowaway.txt", "Legatt"}, 29 {"story/name.txt", "The Secret Sharer"}, 30 {"story/author.txt", "Joseph Conrad"}, 31 {"multiline/test.txt", "bar\nfoo"}, 32 } 33 34 func getTestFiles() files { 35 a := make(files, len(cases)) 36 for _, c := range cases { 37 a[c.path] = []byte(c.data) 38 } 39 return a 40 } 41 42 func TestNewFiles(t *testing.T) { 43 files := getTestFiles() 44 if len(files) != len(cases) { 45 t.Errorf("Expected len() = %d, got %d", len(cases), len(files)) 46 } 47 48 for i, f := range cases { 49 if got := string(files.GetBytes(f.path)); got != f.data { 50 t.Errorf("%d: expected %q, got %q", i, f.data, got) 51 } 52 if got := files.Get(f.path); got != f.data { 53 t.Errorf("%d: expected %q, got %q", i, f.data, got) 54 } 55 } 56 } 57 58 func TestFileGlob(t *testing.T) { 59 as := assert.New(t) 60 61 f := getTestFiles() 62 63 matched := f.Glob("story/**") 64 65 as.Len(matched, 2, "Should be two files in glob story/**") 66 as.Equal("Joseph Conrad", matched.Get("story/author.txt")) 67 } 68 69 func TestToConfig(t *testing.T) { 70 as := assert.New(t) 71 72 f := getTestFiles() 73 out := f.Glob("**/captain.txt").AsConfig() 74 as.Equal("captain.txt: The Captain", out) 75 76 out = f.Glob("ship/**").AsConfig() 77 as.Equal("captain.txt: The Captain\nstowaway.txt: Legatt", out) 78 } 79 80 func TestToSecret(t *testing.T) { 81 as := assert.New(t) 82 83 f := getTestFiles() 84 85 out := f.Glob("ship/**").AsSecrets() 86 as.Equal("captain.txt: VGhlIENhcHRhaW4=\nstowaway.txt: TGVnYXR0", out) 87 } 88 89 func TestLines(t *testing.T) { 90 as := assert.New(t) 91 92 f := getTestFiles() 93 94 out := f.Lines("multiline/test.txt") 95 as.Len(out, 2) 96 97 as.Equal("bar", out[0]) 98 }