Debian Codegraph @

osfixture.go

raw symbols SCIP
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 // OSFixture wraps a Fixture and provides OS filesystem-based access to fixture
12 // files instead of using the embedded filesystem. This is useful when tests
13 // require real filesystem operations to exercise a specific execution path.
14 type OSFixture struct {
15 *Fixture
16
17 // dir is the base directory where temporary files will be created.
18 dir string
19 }
20
21 // NewOSFixture converts a Fixture which is based on embedfs, into
22 // an OS-based fixture. The dir parameter specifies the base directory
23 // where temporary files will be created when accessing packfiles, indexes,
24 // or rev files.
25 func NewOSFixture(f *Fixture, dir string) *OSFixture {
26 return &OSFixture{
27 Fixture: f,
28 dir: dir,
29 }
30 }
31
32 // Is reports whether the fixture has the specified tag.
33 func (f *OSFixture) Is(tag string) bool {
34 return f.Fixture.Is(tag)
35 }
36
37 // Packfile returns the packfile as an OS-based file.
38 func (f *OSFixture) Packfile() (billy.File, error) {
39 file, err := f.Fixture.Packfile()
40 if err != nil {
41 return nil, err
42 }
43
44 return embedToOsfs(f.dir, file)
45 }
46
47 // Idx returns the packfile index as an OS-based file.
48 func (f *OSFixture) Idx() (billy.File, error) {
49 file, err := f.Fixture.Idx()
50 if err != nil {
51 return nil, err
52 }
53
54 return embedToOsfs(f.dir, file)
55 }
56
57 // Rev returns the reverse index file as an OS-based file.
58 func (f *OSFixture) Rev() (billy.File, error) {
59 file, err := f.Fixture.Rev()
60 if err != nil {
61 return nil, err
62 }
63
64 return embedToOsfs(f.dir, file)
65 }
66
67 // DotGit returns the .git directory filesystem. This delegates to the
68 // underlying Fixture's DotGit method.
69 func (f *OSFixture) DotGit(opts ...Option) (billy.Filesystem, error) {
70 return f.Fixture.DotGit(opts...)
71 }
72
73 // Clone creates a deep copy of the OSFixture.
74 func (f *OSFixture) Clone() *OSFixture {
75 nf := &OSFixture{
76 Fixture: &Fixture{
77 URL: f.URL,
78 DotGitHash: f.DotGitHash,
79 Head: f.Head,
80 PackfileHash: f.PackfileHash,
81 WorktreeHash: f.WorktreeHash,
82 ObjectsCount: f.ObjectsCount,
83 Tags: slices.Clone(f.Tags),
84 ObjectFormat: f.ObjectFormat,
85 },
86 dir: f.dir,
87 }
88
89 return nf
90 }
91
92 // Worktree returns the worktree filesystem. This delegates to the
93 // underlying Fixture's Worktree method.
94 func (f *OSFixture) Worktree(opts ...Option) (billy.Filesystem, error) {
95 return f.Fixture.Worktree(opts...)
96 }
97
98 func embedToOsfs(dir string, f billy.File) (billy.File, error) {
99 defer f.Close()
100
101 fs := osfs.New(dir)
102
103 out, err := fs.TempFile("", "embed")
104 if err != nil {
105 return nil, err
106 }
107
108 _, err = io.Copy(out, f)
109 if err != nil {
110 return nil, err
111 }
112
113 _, err = out.Seek(0, io.SeekStart)
114 if err != nil {
115 return nil, err
116 }
117
118 return out, nil
119 }