Skip to content

Commit d1ab4d7

Browse files
authored
Merge pull request #543 from bigkevmcd/include-directories
2 parents 636884c + c397ff9 commit d1ab4d7

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

controllers/storage.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ func SourceIgnoreFilter(ps []gitignore.Pattern, domain []string) ArchiveFileFilt
154154
matcher = sourceignore.NewMatcher(ps)
155155
}
156156
return func(p string, fi os.FileInfo) bool {
157-
// The directory is always false as the archiver does already skip
158-
// directories.
159-
return matcher.Match(strings.Split(p, string(filepath.Separator)), false)
157+
return matcher.Match(strings.Split(p, string(filepath.Separator)), fi.IsDir())
160158
}
161159
}
162160

@@ -191,8 +189,8 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
191189
return err
192190
}
193191

194-
// Ignore anything that is not a file (directories, symlinks)
195-
if !fi.Mode().IsRegular() {
192+
// Ignore anything that is not a file or directories e.g. symlinks
193+
if m := fi.Mode(); !(m.IsRegular() || m.IsDir()) {
196194
return nil
197195
}
198196

@@ -231,6 +229,9 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
231229
return err
232230
}
233231

232+
if !fi.Mode().IsRegular() {
233+
return nil
234+
}
234235
f, err := os.Open(p)
235236
if err != nil {
236237
f.Close()

controllers/storage_test.go

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)