@@ -9,15 +9,11 @@ import (
99 "testing"
1010
1111 "github.com/go-git/go-billy/v5"
12- "github.com/go-git/go-billy/v5/osfs"
13- "github.com/go-git/go-git-fixtures/v4/internal/tgz"
14- "gopkg.in/check.v1"
12+ "github.com/go-git/go-git-fixtures/v5/internal/embedfs"
13+ "github.com/go-git/go-git-fixtures/v5/internal/tgz"
1514)
1615
17- var (
18- files = make (map [string ]string )
19- Filesystem = osfs .New (os .TempDir ())
20- )
16+ var Filesystem = embedfs .New (& data )
2117
2218//go:embed data
2319var data embed.FS
@@ -231,35 +227,8 @@ func (f *Fixture) Is(tag string) bool {
231227 return false
232228}
233229
234- func (f * Fixture ) file (path string ) (billy.File , error ) {
235- if fpath , ok := files [path ]; ok {
236- return Filesystem .Open (fpath )
237- }
238-
239- bytes , err := data .ReadFile ("data/" + path )
240- if err != nil {
241- return nil , err
242- }
243-
244- file , err := Filesystem .TempFile ("" , "go-git-fixtures" )
245- if err != nil {
246- return nil , err
247- }
248-
249- if _ , err := file .Write (bytes ); err != nil {
250- return nil , err
251- }
252-
253- if err := file .Close (); err != nil {
254- return nil , err
255- }
256-
257- files [path ] = file .Name ()
258- return Filesystem .Open (file .Name ())
259- }
260-
261230func (f * Fixture ) Packfile () billy.File {
262- file , err := f . file (fmt .Sprintf ("pack-%s.pack" , f .PackfileHash ))
231+ file , err := Filesystem . Open (fmt .Sprintf ("data/ pack-%s.pack" , f .PackfileHash ))
263232 if err != nil {
264233 panic (err )
265234 }
@@ -268,7 +237,7 @@ func (f *Fixture) Packfile() billy.File {
268237}
269238
270239func (f * Fixture ) Idx () billy.File {
271- file , err := f . file (fmt .Sprintf ("pack-%s.idx" , f .PackfileHash ))
240+ file , err := Filesystem . Open (fmt .Sprintf ("data/ pack-%s.idx" , f .PackfileHash ))
272241 if err != nil {
273242 panic (err )
274243 }
@@ -277,7 +246,7 @@ func (f *Fixture) Idx() billy.File {
277246}
278247
279248func (f * Fixture ) Rev () billy.File {
280- file , err := f . file (fmt .Sprintf ("pack-%s.rev" , f .PackfileHash ))
249+ file , err := Filesystem . Open (fmt .Sprintf ("data/ pack-%s.rev" , f .PackfileHash ))
281250 if err != nil {
282251 panic (err )
283252 }
@@ -287,18 +256,28 @@ func (f *Fixture) Rev() billy.File {
287256
288257// DotGit creates a new temporary directory and unpacks the repository .git
289258// directory into it. Multiple calls to DotGit returns different directories.
290- func (f * Fixture ) DotGit () billy.Filesystem {
259+ func (f * Fixture ) DotGit (opts ... Option ) billy.Filesystem {
260+ o := newOptions ()
261+ for _ , opt := range opts {
262+ opt (o )
263+ }
264+
291265 if f .DotGitHash == "" && f .WorktreeHash != "" {
292- fs , _ := f .Worktree ().Chroot (".git" )
293- return fs .(billy. Filesystem )
266+ fs , _ := f .Worktree (opts ... ).Chroot (".git" )
267+ return fs
294268 }
295269
296- file , err := f . file (fmt .Sprintf ("git-%s.tgz" , f .DotGitHash ))
270+ file , err := Filesystem . Open (fmt .Sprintf ("data/ git-%s.tgz" , f .DotGitHash ))
297271 if err != nil {
298272 panic (err )
299273 }
300274
301- fs , err , _ := tgz .Extract (Filesystem , file .Name ())
275+ fs , err := o .fsFactory ()
276+ if err != nil {
277+ panic (err )
278+ }
279+
280+ err = tgz .Extract (file , fs )
302281 if err != nil {
303282 panic (err )
304283 }
@@ -332,13 +311,23 @@ func EnsureIsBare(fs billy.Filesystem) error {
332311 return err
333312}
334313
335- func (f * Fixture ) Worktree () billy.Filesystem {
336- file , err := f .file (fmt .Sprintf ("worktree-%s.tgz" , f .WorktreeHash ))
314+ func (f * Fixture ) Worktree (opts ... Option ) billy.Filesystem {
315+ o := newOptions ()
316+ for _ , opt := range opts {
317+ opt (o )
318+ }
319+
320+ file , err := Filesystem .Open (fmt .Sprintf ("data/worktree-%s.tgz" , f .WorktreeHash ))
321+ if err != nil {
322+ panic (err )
323+ }
324+
325+ fs , err := o .fsFactory ()
337326 if err != nil {
338327 panic (err )
339328 }
340329
341- fs , err , _ : = tgz .Extract (Filesystem , file . Name () )
330+ err = tgz .Extract (file , fs )
342331 if err != nil {
343332 panic (err )
344333 }
@@ -348,17 +337,9 @@ func (f *Fixture) Worktree() billy.Filesystem {
348337
349338type Fixtures []* Fixture
350339
351- // Deprecated as part of removing check from the code base.
352- // Use Run instead.
353- func (g Fixtures ) Test (c * check.C , test func (* Fixture )) {
354- for _ , f := range g {
355- c .Logf ("executing test at %s %s" , f .URL , f .Tags )
356- test (f )
357- }
358- }
359-
360340// Run calls test within a t.Run for each fixture in g.
361341func (g Fixtures ) Run (t * testing.T , test func (* testing.T , * Fixture )) {
342+ t .Helper ()
362343 for _ , f := range g {
363344 name := fmt .Sprintf ("fixture run (%q, %q)" , f .URL , f .Tags )
364345 t .Run (name , func (t * testing.T ) {
@@ -368,11 +349,14 @@ func (g Fixtures) Run(t *testing.T, test func(*testing.T, *Fixture)) {
368349}
369350
370351func (g Fixtures ) One () * Fixture {
352+ if len (g ) == 0 {
353+ return nil
354+ }
371355 return g [0 ]
372356}
373357
374358func (g Fixtures ) ByTag (tag string ) Fixtures {
375- r := make (Fixtures , 0 )
359+ r := make (Fixtures , 0 , len ( g ) )
376360 for _ , f := range g {
377361 if f .Is (tag ) {
378362 r = append (r , f )
@@ -383,7 +367,7 @@ func (g Fixtures) ByTag(tag string) Fixtures {
383367}
384368
385369func (g Fixtures ) ByURL (url string ) Fixtures {
386- r := make (Fixtures , 0 )
370+ r := make (Fixtures , 0 , len ( g ) )
387371 for _ , f := range g {
388372 if f .URL == url {
389373 r = append (r , f )
@@ -394,7 +378,7 @@ func (g Fixtures) ByURL(url string) Fixtures {
394378}
395379
396380func (g Fixtures ) Exclude (tag string ) Fixtures {
397- r := make (Fixtures , 0 )
381+ r := make (Fixtures , 0 , len ( g ) )
398382 for _ , f := range g {
399383 if ! f .Is (tag ) {
400384 r = append (r , f )
@@ -403,21 +387,3 @@ func (g Fixtures) Exclude(tag string) Fixtures {
403387
404388 return r
405389}
406-
407- // Clean cleans all the temporal files created
408- func Clean () error {
409- for fname , f := range files {
410- if err := Filesystem .Remove (f ); err != nil {
411- return err
412- }
413- delete (files , fname )
414- }
415- return nil
416- }
417-
418- type Suite struct {}
419-
420- // Deprecated as part of removing check from the code base.
421- func (s * Suite ) TearDownSuite (c * check.C ) {
422- Clean ()
423- }
0 commit comments