Skip to content

Commit d7fbc5f

Browse files
committed
test(logic): improve test coverage for filtered vfs
1 parent e8916f8 commit d7fbc5f

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

x/logic/fs/filtered/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020

2121
// NewFS creates a new filtered filesystem that wraps the provided filesystem.
2222
// The whitelist and blacklist are used to filter the paths that can be accessed.
23-
func NewFS(underlyingFS fs.FS, whitelist, blacklist []*url.URL) fs.FS {
23+
func NewFS(underlyingFS fs.FS, whitelist, blacklist []*url.URL) fs.ReadFileFS {
2424
return &vfs{fs: underlyingFS, whitelist: whitelist, blacklist: blacklist}
2525
}
2626

x/logic/fs/filtered/fs_test.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/axone-protocol/axoned/v8/x/logic/util"
1717
)
1818

19-
func TestSourceFile(t *testing.T) {
19+
func TestFilteredVFS(t *testing.T) {
2020
Convey("Given test cases", t, func() {
2121
ctrl := gomock.NewController(t)
2222
defer ctrl.Finish()
@@ -74,25 +74,30 @@ func TestSourceFile(t *testing.T) {
7474
for nc, tc := range cases {
7575
Convey(fmt.Sprintf("Given the test case #%d - file %s", nc, tc.file), func() {
7676
Convey("and a mocked file system", func() {
77-
mockedFS := testutil.NewMockFS(ctrl)
78-
mockedFS.EXPECT().Open(tc.file).Times(lo.If(util.IsNil(tc.wantError), 1).Else(0)).
77+
content := []byte("42")
78+
mockedFS := testutil.NewMockReadFileFS(ctrl)
79+
mockedFS.EXPECT().Open(tc.file).AnyTimes().
7980
DoAndReturn(func(file string) (fs.File, error) {
8081
return wasm.NewVirtualFile(
8182
file,
82-
[]byte("42"),
83+
content,
8384
time.Unix(1681389446, 0)), nil
8485
})
86+
mockedFS.EXPECT().ReadFile(tc.file).AnyTimes().
87+
DoAndReturn(func(file string) ([]byte, error) {
88+
return content, nil
89+
})
8590
Convey("and a filtered file system under test", func() {
8691
filteredFS := NewFS(
8792
mockedFS,
8893
lo.Map(tc.whitelist, util.Indexed(util.ParseURLMust)),
8994
lo.Map(tc.blacklist, util.Indexed(util.ParseURLMust)),
9095
)
9196

92-
Convey(fmt.Sprintf(`When the open("%s") is called`, tc.file), func() {
97+
Convey(fmt.Sprintf(`when the open("%s") is called`, tc.file), func() {
9398
result, err := filteredFS.Open(tc.file)
9499

95-
Convey("Then the result should be as expected", func() {
100+
Convey("then the result should be as expected", func() {
96101
if util.IsNil(tc.wantError) {
97102
So(err, ShouldBeNil)
98103

@@ -107,9 +112,42 @@ func TestSourceFile(t *testing.T) {
107112
}
108113
})
109114
})
115+
116+
Convey(fmt.Sprintf(`when the readFile("%s") is called`, tc.file), func() {
117+
result, err := filteredFS.ReadFile(tc.file)
118+
119+
Convey("Then the result should be as expected", func() {
120+
if util.IsNil(tc.wantError) {
121+
So(err, ShouldBeNil)
122+
So(result, ShouldResemble, content)
123+
} else {
124+
So(err, ShouldNotBeNil)
125+
So(err, ShouldResemble, tc.wantError)
126+
}
127+
})
128+
})
110129
})
111130
})
112131
})
113132
}
114133
})
134+
135+
Convey("Given a mocked fs that does not implement ReadFileFS", t, func() {
136+
ctrl := gomock.NewController(t)
137+
defer ctrl.Finish()
138+
139+
mockedFS := testutil.NewMockFS(ctrl)
140+
Convey("and a filtered file system under test", func() {
141+
filteredFS := NewFS(mockedFS, nil, nil)
142+
143+
Convey("when readFile is called", func() {
144+
_, err := filteredFS.ReadFile("file")
145+
146+
Convey("then an error should be returned", func() {
147+
So(err, ShouldNotBeNil)
148+
So(err, ShouldEqual, &fs.PathError{Op: "readfile", Path: "file", Err: fs.ErrInvalid})
149+
})
150+
})
151+
})
152+
})
115153
}

0 commit comments

Comments
 (0)