@@ -21,10 +21,14 @@ import (
2121 "bytes"
2222 "compress/gzip"
2323 "crypto/rand"
24+ "errors"
2425 "fmt"
26+ "io/fs"
2527 "os"
2628 "path/filepath"
2729 "testing"
30+
31+ "github.com/go-git/go-git/v5/plumbing/format/gitignore"
2832)
2933
3034type untarTestCase struct {
@@ -35,6 +39,8 @@ type untarTestCase struct {
3539 content []byte
3640 wantErr string
3741 maxUntarSize int
42+ ignore gitignore.Matcher
43+ wantNotExist bool
3844}
3945
4046func TestUntar (t * testing.T ) {
@@ -128,6 +134,39 @@ func TestUntar(t *testing.T) {
128134 targetDir : symlink ,
129135 wantErr : fmt .Sprintf (`dir '%s' must be a directory` , symlink ),
130136 },
137+ {
138+ name : "ignore" ,
139+ fileName : "file1" ,
140+ content : geRandomContent (256 ),
141+ targetDir : targetDirOutput ,
142+ secureTargetDir : targetDirOutput ,
143+ ignore : gitignore .NewMatcher ([]gitignore.Pattern {
144+ gitignore .ParsePattern ("file1" , nil ),
145+ }),
146+ wantNotExist : true ,
147+ },
148+ {
149+ name : "ignore does not match" ,
150+ fileName : "file1" ,
151+ content : geRandomContent (256 ),
152+ targetDir : targetDirOutput ,
153+ secureTargetDir : targetDirOutput ,
154+ ignore : gitignore .NewMatcher ([]gitignore.Pattern {
155+ gitignore .ParsePattern ("no_match" , nil ),
156+ }),
157+ wantNotExist : false ,
158+ },
159+ {
160+ name : "ignore with glob" ,
161+ fileName : "path/to/file.ignored" ,
162+ content : geRandomContent (256 ),
163+ targetDir : targetDirOutput ,
164+ secureTargetDir : targetDirOutput ,
165+ ignore : gitignore .NewMatcher ([]gitignore.Pattern {
166+ gitignore .ParsePattern ("*.ignored" , nil ),
167+ }),
168+ wantNotExist : true ,
169+ },
131170 }
132171
133172 for _ , tt := range cases {
@@ -143,6 +182,9 @@ func TestUntar(t *testing.T) {
143182 if tt .maxUntarSize != 0 {
144183 opts = append (opts , WithMaxUntarSize (tt .maxUntarSize ))
145184 }
185+ if tt .ignore != nil {
186+ opts = append (opts , WithIgnore (tt .ignore ))
187+ }
146188
147189 err = Untar (f , tt .targetDir , opts ... )
148190 var got string
@@ -161,11 +203,21 @@ func TestUntar(t *testing.T) {
161203 if tt .wantErr == "" {
162204 abs := filepath .Join (tt .secureTargetDir , tt .fileName )
163205 fi , err := os .Stat (abs )
164- if err != nil {
206+
207+ gotNotExist := errors .Is (err , fs .ErrNotExist )
208+ if err != nil && gotNotExist != tt .wantNotExist {
165209 t .Errorf ("stat %q: %v" , abs , err )
166210 return
167211 }
168212
213+ if ! gotNotExist && tt .wantNotExist {
214+ t .Errorf ("os.Stat(%q) = (%v, nil), want %v" , abs , fi , fs .ErrNotExist )
215+ }
216+
217+ if tt .wantNotExist {
218+ return
219+ }
220+
169221 if fi .Size () != int64 (len (tt .content )) {
170222 t .Errorf ("file size wanted: %d got: %d" , len (tt .content ), fi .Size ())
171223 }
0 commit comments