@@ -70,7 +70,7 @@ func TestStorageConstructor(t *testing.T) {
70
70
71
71
// walks a tar.gz and looks for paths with the basename. It does not match
72
72
// 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 ) {
74
74
f , err := os .Open (tarFile )
75
75
if err != nil {
76
76
return 0 , false , fmt .Errorf ("could not open file: %w" , err )
@@ -93,7 +93,11 @@ func walkTar(tarFile string, match string) (int64, bool, error) {
93
93
}
94
94
95
95
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 :
97
101
if header .Name == match {
98
102
return header .Size , true , nil
99
103
}
@@ -145,13 +149,14 @@ func TestStorage_Archive(t *testing.T) {
145
149
return
146
150
}
147
151
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 ()
149
154
for name , b := range files {
150
155
mustExist := ! (name [0 :1 ] == "!" )
151
156
if ! mustExist {
152
157
name = name [1 :]
153
158
}
154
- s , exist , err := walkTar (storage .LocalPath (artifact ), name )
159
+ s , exist , err := walkTar (storage .LocalPath (artifact ), name , false )
155
160
if err != nil {
156
161
t .Fatalf ("failed reading tarball: %v" , err )
157
162
}
@@ -166,14 +171,32 @@ func TestStorage_Archive(t *testing.T) {
166
171
}
167
172
}
168
173
}
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
+ }
169
191
}
170
192
171
193
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
177
200
}{
178
201
{
179
202
name : "no filter" ,
@@ -195,6 +218,9 @@ func TestStorage_Archive(t *testing.T) {
195
218
".git/config" : nil ,
196
219
"manifest.yaml" : nil ,
197
220
},
221
+ wantDirs : []string {
222
+ "!.git" ,
223
+ },
198
224
filter : SourceIgnoreFilter (nil , nil ),
199
225
want : map [string ][]byte {
200
226
"!.git/config" : nil ,
@@ -218,6 +244,19 @@ func TestStorage_Archive(t *testing.T) {
218
244
},
219
245
wantErr : false ,
220
246
},
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
+ },
221
260
}
222
261
for _ , tt := range tests {
223
262
t .Run (tt .name , func (t * testing.T ) {
@@ -236,7 +275,7 @@ func TestStorage_Archive(t *testing.T) {
236
275
if err := storage .Archive (& artifact , dir , tt .filter ); (err != nil ) != tt .wantErr {
237
276
t .Errorf ("Archive() error = %v, wantErr %v" , err , tt .wantErr )
238
277
}
239
- matchFiles (t , storage , artifact , tt .want )
278
+ matchFiles (t , storage , artifact , tt .want , tt . wantDirs )
240
279
})
241
280
}
242
281
}
0 commit comments