Skip to content

Commit be3e0ac

Browse files
authored
Merge pull request #57 from pjbgf/osfixture
Add a new OSFixture
2 parents ab3c91b + 7feadcc commit be3e0ac

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

osfixture.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package fixtures
2+
3+
import (
4+
"io"
5+
"slices"
6+
7+
"github.com/go-git/go-billy/v6"
8+
"github.com/go-git/go-billy/v6/osfs"
9+
)
10+
11+
type OSFixture struct {
12+
dir string
13+
*Fixture
14+
}
15+
16+
// NewOSFixture converts a Fixture which is based on embedfs, into
17+
// an OS based fixture.
18+
func NewOSFixture(f *Fixture, dir string) *OSFixture {
19+
return &OSFixture{
20+
dir: dir,
21+
Fixture: f,
22+
}
23+
}
24+
25+
func (f *OSFixture) Is(tag string) bool {
26+
return f.Fixture.Is(tag)
27+
}
28+
29+
func (f *OSFixture) Packfile() billy.File {
30+
return embedToOsfs(f.dir, f.Fixture.Packfile())
31+
}
32+
33+
func (f *OSFixture) Idx() billy.File {
34+
return embedToOsfs(f.dir, f.Fixture.Idx())
35+
}
36+
37+
func (f *OSFixture) Rev() billy.File {
38+
return embedToOsfs(f.dir, f.Fixture.Rev())
39+
}
40+
41+
func (f *OSFixture) DotGit(opts ...Option) billy.Filesystem {
42+
return f.Fixture.DotGit(opts...)
43+
}
44+
45+
func (f *OSFixture) Clone() *OSFixture {
46+
nf := &OSFixture{
47+
Fixture: &Fixture{
48+
URL: f.URL,
49+
DotGitHash: f.DotGitHash,
50+
Head: f.Head,
51+
PackfileHash: f.PackfileHash,
52+
WorktreeHash: f.WorktreeHash,
53+
ObjectsCount: f.ObjectsCount,
54+
Tags: slices.Clone(f.Tags),
55+
},
56+
dir: f.dir,
57+
}
58+
59+
return nf
60+
}
61+
62+
func (f *OSFixture) Worktree(opts ...Option) billy.Filesystem {
63+
return f.Fixture.Worktree(opts...)
64+
}
65+
66+
func embedToOsfs(dir string, f billy.File) billy.File {
67+
defer f.Close()
68+
69+
fs := osfs.New(dir)
70+
out, err := fs.TempFile("", "embed")
71+
if err != nil {
72+
panic(err)
73+
}
74+
75+
_, err = io.Copy(out, f)
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
_, err = out.Seek(0, io.SeekStart)
81+
if err != nil {
82+
panic(err)
83+
}
84+
85+
return out
86+
}

0 commit comments

Comments
 (0)