@@ -18,6 +18,7 @@ package library
1818import (
1919 "reflect"
2020 "testing"
21+ "strings"
2122)
2223
2324func TestValidateStackVersionTag (t * testing.T ) {
@@ -130,3 +131,82 @@ func TestSplitVersionFromStack(t *testing.T) {
130131 })
131132 }
132133}
134+
135+ func TestCleanFilepath (t * testing.T ) {
136+ tests := []struct {
137+ name string
138+ parentPath string
139+ childPath string
140+ expectedPath string
141+ }{
142+ {
143+ name : "Absolute child path with leading slash" ,
144+ parentPath : "." ,
145+ childPath : "/test/tmp" ,
146+ expectedPath : "test/tmp" ,
147+ },
148+ {
149+ name : "Absolute child path without leading slash" ,
150+ parentPath : "." ,
151+ childPath : "test/tmp" ,
152+ expectedPath : "test/tmp" ,
153+ },
154+ {
155+ name : "Relative child path without leading slash" ,
156+ parentPath : "." ,
157+ childPath : "../../../../test/tmp" ,
158+ expectedPath : "test/tmp" ,
159+ },
160+ {
161+ name : "Relative child path with leading slash" ,
162+ parentPath : "." ,
163+ childPath : "/../../../../test/tmp" ,
164+ expectedPath : "test/tmp" ,
165+ },
166+ {
167+ name : "Absolute child path with leading slash and escape capabilities" ,
168+ parentPath : "." ,
169+ childPath : "/home/../../../../test/tmp" ,
170+ expectedPath : "test/tmp" ,
171+ },
172+ {
173+ name : "Absolute child path with leading slash and escape capabilities (parent path not current dir)" ,
174+ parentPath : "newHome/dir" ,
175+ childPath : "/home/../../../../test/tmp" ,
176+ expectedPath : "newHome/dir/test/tmp" ,
177+ },
178+ {
179+ name : "Relative child path without leading slash and escape capabilities (parent path not current dir)" ,
180+ parentPath : "newHome/dir" ,
181+ childPath : "../home/../../../../test/tmp" ,
182+ expectedPath : "newHome/dir/test/tmp" ,
183+ },
184+ {
185+ name : "Blank child path" ,
186+ parentPath : "dir" ,
187+ childPath : "" ,
188+ expectedPath : "dir" ,
189+ },
190+ {
191+ name : "Child path only escape characters" ,
192+ parentPath : "dir" ,
193+ childPath : "../../../../../" ,
194+ expectedPath : "dir" ,
195+ },
196+ {
197+ name : "Single file as child path" ,
198+ parentPath : "dir" ,
199+ childPath : "test.txt" ,
200+ expectedPath : "dir/test.txt" ,
201+ },
202+ }
203+
204+ for _ , test := range tests {
205+ t .Run (test .name , func (t * testing.T ) {
206+ path := CleanFilepath (test .parentPath , test .childPath )
207+ if ! strings .EqualFold (test .expectedPath , path ) {
208+ t .Errorf ("Expected: %s, Got: %s" , test .expectedPath , path )
209+ }
210+ })
211+ }
212+ }
0 commit comments