@@ -70,7 +70,7 @@ func TestStorageConstructor(t *testing.T) {
7070
7171// walks a tar.gz and looks for paths with the basename. It does not match
7272// symlinks properly at this time because that's painful.
73- func walkTar (tarFile string , match string ) (int64 , bool , error ) {
73+ func walkTar (tarFile string , match string , dir bool ) (int64 , bool , error ) {
7474 f , err := os .Open (tarFile )
7575 if err != nil {
7676 return 0 , false , fmt .Errorf ("could not open file: %w" , err )
@@ -93,7 +93,11 @@ func walkTar(tarFile string, match string) (int64, bool, error) {
9393 }
9494
9595 switch header .Typeflag {
96- case tar .TypeDir , tar .TypeReg :
96+ case tar .TypeDir :
97+ if header .Name == match && dir {
98+ return 0 , true , nil
99+ }
100+ case tar .TypeReg :
97101 if header .Name == match {
98102 return header .Size , true , nil
99103 }
@@ -145,13 +149,14 @@ func TestStorage_Archive(t *testing.T) {
145149 return
146150 }
147151
148- matchFiles := func (t * testing.T , storage * Storage , artifact sourcev1.Artifact , files map [string ][]byte ) {
152+ matchFiles := func (t * testing.T , storage * Storage , artifact sourcev1.Artifact , files map [string ][]byte , dirs []string ) {
153+ t .Helper ()
149154 for name , b := range files {
150155 mustExist := ! (name [0 :1 ] == "!" )
151156 if ! mustExist {
152157 name = name [1 :]
153158 }
154- s , exist , err := walkTar (storage .LocalPath (artifact ), name )
159+ s , exist , err := walkTar (storage .LocalPath (artifact ), name , false )
155160 if err != nil {
156161 t .Fatalf ("failed reading tarball: %v" , err )
157162 }
@@ -166,14 +171,32 @@ func TestStorage_Archive(t *testing.T) {
166171 }
167172 }
168173 }
174+ for _ , name := range dirs {
175+ mustExist := ! (name [0 :1 ] == "!" )
176+ if ! mustExist {
177+ name = name [1 :]
178+ }
179+ _ , exist , err := walkTar (storage .LocalPath (artifact ), name , true )
180+ if err != nil {
181+ t .Fatalf ("failed reading tarball: %v" , err )
182+ }
183+ if exist != mustExist {
184+ if mustExist {
185+ t .Errorf ("could not find dir %q in tarball" , name )
186+ } else {
187+ t .Errorf ("tarball contained excluded file %q" , name )
188+ }
189+ }
190+ }
169191 }
170192
171193 tests := []struct {
172- name string
173- files map [string ][]byte
174- filter ArchiveFileFilter
175- want map [string ][]byte
176- wantErr bool
194+ name string
195+ files map [string ][]byte
196+ filter ArchiveFileFilter
197+ want map [string ][]byte
198+ wantDirs []string
199+ wantErr bool
177200 }{
178201 {
179202 name : "no filter" ,
@@ -195,6 +218,9 @@ func TestStorage_Archive(t *testing.T) {
195218 ".git/config" : nil ,
196219 "manifest.yaml" : nil ,
197220 },
221+ wantDirs : []string {
222+ "!.git" ,
223+ },
198224 filter : SourceIgnoreFilter (nil , nil ),
199225 want : map [string ][]byte {
200226 "!.git/config" : nil ,
@@ -218,6 +244,19 @@ func TestStorage_Archive(t *testing.T) {
218244 },
219245 wantErr : false ,
220246 },
247+ {
248+ name : "including directories" ,
249+ files : map [string ][]byte {
250+ "test/.gitkeep" : nil ,
251+ },
252+ filter : SourceIgnoreFilter ([]gitignore.Pattern {
253+ gitignore .ParsePattern ("custom" , nil ),
254+ }, nil ),
255+ wantDirs : []string {
256+ "test" ,
257+ },
258+ wantErr : false ,
259+ },
221260 }
222261 for _ , tt := range tests {
223262 t .Run (tt .name , func (t * testing.T ) {
@@ -236,7 +275,7 @@ func TestStorage_Archive(t *testing.T) {
236275 if err := storage .Archive (& artifact , dir , tt .filter ); (err != nil ) != tt .wantErr {
237276 t .Errorf ("Archive() error = %v, wantErr %v" , err , tt .wantErr )
238277 }
239- matchFiles (t , storage , artifact , tt .want )
278+ matchFiles (t , storage , artifact , tt .want , tt . wantDirs )
240279 })
241280 }
242281}
0 commit comments