Skip to content

Commit b51609f

Browse files
committed
Return cloned fixtures
Signed-off-by: Paulo Gomes <[email protected]>
1 parent 04510fa commit b51609f

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

fixtures.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"slices"
89
"strings"
910
"testing"
1011

@@ -195,7 +196,12 @@ var fixtures = Fixtures{{
195196
}}
196197

197198
func All() Fixtures {
198-
return fixtures
199+
all := make(Fixtures, 0, len(fixtures))
200+
for _, f := range fixtures {
201+
all = append(all, f.Clone())
202+
}
203+
204+
return all
199205
}
200206

201207
func Basic() Fixtures {
@@ -290,6 +296,20 @@ func (f *Fixture) DotGit(opts ...Option) billy.Filesystem {
290296
return fs
291297
}
292298

299+
func (f *Fixture) Clone() *Fixture {
300+
nf := &Fixture{
301+
URL: f.URL,
302+
DotGitHash: f.DotGitHash,
303+
Head: f.Head,
304+
PackfileHash: f.PackfileHash,
305+
WorktreeHash: f.WorktreeHash,
306+
ObjectsCount: f.ObjectsCount,
307+
}
308+
nf.Tags = slices.Clone(f.Tags)
309+
310+
return nf
311+
}
312+
293313
// EnsureIsBare overrides the config file with one where bare is true.
294314
func EnsureIsBare(fs billy.Filesystem) error {
295315
if _, err := fs.Stat("config"); err != nil {
@@ -359,14 +379,14 @@ func (g Fixtures) One() *Fixture {
359379
return nil
360380
}
361381

362-
return g[0]
382+
return g[0].Clone()
363383
}
364384

365385
func (g Fixtures) ByTag(tag string) Fixtures {
366386
r := make(Fixtures, 0, len(g))
367387
for _, f := range g {
368388
if f.Is(tag) {
369-
r = append(r, f)
389+
r = append(r, f.Clone())
370390
}
371391
}
372392

@@ -377,7 +397,7 @@ func (g Fixtures) ByURL(url string) Fixtures {
377397
r := make(Fixtures, 0, len(g))
378398
for _, f := range g {
379399
if f.URL == url {
380-
r = append(r, f)
400+
r = append(r, f.Clone())
381401
}
382402
}
383403

@@ -388,7 +408,7 @@ func (g Fixtures) Exclude(tag string) Fixtures {
388408
r := make(Fixtures, 0, len(g))
389409
for _, f := range g {
390410
if !f.Is(tag) {
391-
r = append(r, f)
411+
r = append(r, f.Clone())
392412
}
393413
}
394414

0 commit comments

Comments
 (0)