github.com/replicatedhq/ship@v0.55.0/pkg/templates/util_test.go (about) 1 package templates 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/replicatedhq/ship/pkg/testing/logger" 8 "github.com/spf13/afero" 9 "github.com/spf13/viper" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestBuildDir(t *testing.T) { 14 type file struct { 15 contents string 16 path string 17 } 18 19 tests := []struct { 20 name string 21 buildPath string 22 inputFiles []file 23 outputFiles []file 24 }{ 25 { 26 name: "no templates", 27 buildPath: "dir", 28 inputFiles: []file{ 29 { 30 contents: "notATemplate", 31 path: "dir/file.txt", 32 }, 33 }, 34 outputFiles: []file{ 35 { 36 contents: "notATemplate", 37 path: "dir/file.txt", 38 }, 39 }, 40 }, 41 { 42 name: "template not in dir", 43 buildPath: "dir", 44 inputFiles: []file{ 45 { 46 contents: "notATemplate", 47 path: "dir/file.txt", 48 }, 49 { 50 contents: `{{repl ConfigOption "option_1"}}`, 51 path: "notDir/template.txt", 52 }, 53 }, 54 outputFiles: []file{ 55 { 56 contents: "notATemplate", 57 path: "dir/file.txt", 58 }, 59 { 60 contents: `{{repl ConfigOption "option_1"}}`, 61 path: "notDir/template.txt", 62 }, 63 }, 64 }, 65 { 66 name: "template in dir", 67 buildPath: "dir", 68 inputFiles: []file{ 69 { 70 contents: "notATemplate", 71 path: "dir/file.txt", 72 }, 73 { 74 contents: `{{repl ConfigOption "option_1"}}`, 75 path: "dir/template.txt", 76 }, 77 }, 78 outputFiles: []file{ 79 { 80 contents: "notATemplate", 81 path: "dir/file.txt", 82 }, 83 { 84 contents: "Option 1", 85 path: "dir/template.txt", 86 }, 87 }, 88 }, 89 { 90 name: "template in subdir", 91 buildPath: "anotherdir", 92 inputFiles: []file{ 93 { 94 contents: "notATemplate", 95 path: "anotherdir/file.txt", 96 }, 97 { 98 contents: `{{repl ConfigOption "option_2"}}`, 99 path: "anotherdir/subdir/template.txt", 100 }, 101 }, 102 outputFiles: []file{ 103 { 104 contents: "notATemplate", 105 path: "anotherdir/file.txt", 106 }, 107 { 108 contents: "Option 2", 109 path: "anotherdir/subdir/template.txt", 110 }, 111 }, 112 }, 113 } 114 for _, tt := range tests { 115 t.Run(tt.name, func(t *testing.T) { 116 req := require.New(t) 117 118 fs := afero.Afero{Fs: afero.NewMemMapFs()} 119 120 for _, file := range tt.inputFiles { 121 req.NoError(fs.WriteFile(file.path, []byte(file.contents), os.FileMode(777))) 122 } 123 124 builderBuilder := &BuilderBuilder{ 125 Logger: &logger.TestLogger{T: t}, 126 Viper: viper.New(), 127 } 128 129 builder := builderBuilder.NewBuilder( 130 builderBuilder.NewStaticContext(), 131 testContext{}, 132 ) 133 134 err := BuildDir(tt.buildPath, &fs, &builder) 135 req.NoError(err) 136 137 for _, file := range tt.outputFiles { 138 actualContents, err := fs.ReadFile(file.path) 139 req.NoError(err) 140 req.Equal(file.contents, string(actualContents)) 141 } 142 }) 143 } 144 }