Skip to content

Commit 04510fa

Browse files
committed
embedfs: Add support for prefix setting
Signed-off-by: Paulo Gomes <[email protected]>
1 parent 07c5b30 commit 04510fa

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

fixtures.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
//nolint:gochecknoglobals
17-
var Filesystem = embedfs.New(&data)
17+
var Filesystem = embedfs.New(&data, "data")
1818

1919
//go:embed data
2020
var data embed.FS

internal/embedfs/embed.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import (
1818

1919
type Embed struct {
2020
underlying *embed.FS
21+
dir string
2122
}
2223

23-
func New(efs *embed.FS) *Embed {
24+
func New(efs *embed.FS, dir string) *Embed {
2425
fs := &Embed{
2526
underlying: efs,
27+
dir: dir,
2628
}
2729

2830
if efs == nil {
@@ -36,8 +38,16 @@ func (fs *Embed) Root() string {
3638
return ""
3739
}
3840

41+
func (fs *Embed) prefix(filename string) string {
42+
if fs.dir != "" && !strings.HasPrefix(filename, fs.dir+"/") {
43+
return fs.dir + "/" + filename
44+
}
45+
46+
return filename
47+
}
48+
3949
func (fs *Embed) Stat(filename string) (os.FileInfo, error) {
40-
f, err := fs.underlying.Open(filename)
50+
f, err := fs.underlying.Open(fs.prefix(filename))
4151
if err != nil {
4252
return nil, err
4353
}
@@ -54,6 +64,7 @@ func (fs *Embed) OpenFile(filename string, flag int, _ os.FileMode) (billy.File,
5464
return nil, billy.ErrReadOnly
5565
}
5666

67+
filename = fs.prefix(filename)
5768
f, err := fs.underlying.Open(filename)
5869
if err != nil {
5970
return nil, err

internal/embedfs/embed_test.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestOpen(t *testing.T) {
2727

2828
tests := []struct {
2929
name string
30+
dir string
3031
want []byte
3132
wantErr bool
3233
}{
@@ -38,6 +39,16 @@ func TestOpen(t *testing.T) {
3839
name: "testdata/empty2.txt",
3940
want: []byte("test"),
4041
},
42+
{
43+
name: "empty.txt",
44+
dir: "testdata",
45+
want: []byte(""),
46+
},
47+
{
48+
name: "empty2.txt",
49+
dir: "testdata",
50+
want: []byte("test"),
51+
},
4152
{
4253
name: "non-existent",
4354
wantErr: true,
@@ -49,7 +60,7 @@ func TestOpen(t *testing.T) {
4960
t.Run(tc.name, func(t *testing.T) {
5061
t.Parallel()
5162

52-
fs := embedfs.New(&testdataDir)
63+
fs := embedfs.New(&testdataDir, tc.dir)
5364

5465
var got []byte
5566
f, err := fs.Open(tc.name)
@@ -123,7 +134,7 @@ func TestOpenFileFlags(t *testing.T) {
123134
t.Run(tc.name, func(t *testing.T) {
124135
t.Parallel()
125136

126-
fs := embedfs.New(&testdataDir)
137+
fs := embedfs.New(&testdataDir, "")
127138

128139
_, err := fs.OpenFile(tc.file, tc.flag, 0o700)
129140
if tc.wantErr != "" {
@@ -140,6 +151,7 @@ func TestStat(t *testing.T) {
140151

141152
tests := []struct {
142153
name string
154+
dir string
143155
want string
144156
isDir bool
145157
wantErr bool
@@ -152,6 +164,16 @@ func TestStat(t *testing.T) {
152164
name: "testdata/empty2.txt",
153165
want: "empty2.txt",
154166
},
167+
{
168+
name: "empty.txt",
169+
dir: "testdata",
170+
want: "empty.txt",
171+
},
172+
{
173+
name: "empty2.txt",
174+
dir: "testdata",
175+
want: "empty2.txt",
176+
},
155177
{
156178
name: "non-existent",
157179
wantErr: true,
@@ -168,7 +190,7 @@ func TestStat(t *testing.T) {
168190
t.Run(tc.name, func(t *testing.T) {
169191
t.Parallel()
170192

171-
fs := embedfs.New(&testdataDir)
193+
fs := embedfs.New(&testdataDir, tc.dir)
172194

173195
fi, err := fs.Stat(tc.name)
174196
if tc.wantErr {
@@ -227,7 +249,7 @@ func TestReadDir(t *testing.T) {
227249
t.Run(tc.name, func(t *testing.T) {
228250
t.Parallel()
229251

230-
fs := embedfs.New(tc.fs)
252+
fs := embedfs.New(tc.fs, "")
231253

232254
fis, err := fs.ReadDir(tc.path)
233255
if tc.wantErr {
@@ -255,7 +277,7 @@ func TestReadDir(t *testing.T) {
255277
func TestUnsupported(t *testing.T) {
256278
t.Parallel()
257279

258-
fs := embedfs.New(&testdataDir)
280+
fs := embedfs.New(&testdataDir, "")
259281

260282
_, err := fs.Create("test")
261283
require.ErrorIs(t, err, billy.ErrReadOnly)
@@ -273,7 +295,7 @@ func TestUnsupported(t *testing.T) {
273295
func TestFileUnsupported(t *testing.T) {
274296
t.Parallel()
275297

276-
fs := embedfs.New(&testdataDir)
298+
fs := embedfs.New(&testdataDir, "")
277299

278300
f, err := fs.Open("testdata/empty.txt")
279301
require.NoError(t, err)
@@ -288,7 +310,7 @@ func TestFileUnsupported(t *testing.T) {
288310

289311
//nolint:paralleltest
290312
func TestFileSeek(t *testing.T) {
291-
fs := embedfs.New(&testdataDir)
313+
fs := embedfs.New(&testdataDir, "")
292314

293315
f, err := fs.Open("testdata/empty2.txt")
294316
require.NoError(t, err)
@@ -359,7 +381,7 @@ func TestJoin(t *testing.T) {
359381
t.Run(tc.name, func(t *testing.T) {
360382
t.Parallel()
361383

362-
fs := embedfs.New(&empty)
384+
fs := embedfs.New(&empty, "")
363385

364386
got := fs.Join(tc.path...)
365387
assert.Equal(t, tc.want, got)

0 commit comments

Comments
 (0)