Skip to content

Commit

Permalink
Add OnlyHTMLFS test
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Sep 10, 2024
1 parent 3df1d43 commit e21fcce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
30 changes: 30 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ func TestOnlyFilesFS_Open_err(t *testing.T) {
assert.Nil(t, file)
}

func TestOnlyHTMLFS_Open(t *testing.T) {
var testFile *os.File
mockFS := &mockFileSystem{
open: func(name string) (http.File, error) {
return testFile, nil
},
}
fs := &OnlyHTMLFS{FileSystem: mockFS}

file, err := fs.Open("foo")

require.NoError(t, err)
assert.Equal(t, testFile, file)
}

func TestOnlyHTMLFS_Open_err(t *testing.T) {
testError := errors.New("mock")
mockFS := &mockFileSystem{
open: func(_ string) (http.File, error) {
return nil, testError
},
}
fs := &OnlyHTMLFS{FileSystem: mockFS}

file, err := fs.Open("foo")

require.ErrorIs(t, err, testError)
assert.Nil(t, file)
}

func Test_neuteredReaddirFile_Readdir(t *testing.T) {
n := neutralizedReaddirFile{}

Expand Down
14 changes: 7 additions & 7 deletions gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,16 @@ func TestLoadHTMLFilesFuncMap(t *testing.T) {
assert.Equal(t, "Date: 2017/07/01", string(resp))
}

//go:embed testdata/template/*
var htmlFS embed.FS
//go:embed testdata/template/*.tmpl
var tmplFS embed.FS

func TestLoadHTMLFSTestMode(t *testing.T) {
ts := setupHTMLFiles(
t,
TestMode,
false,
func(router *Engine) {
router.LoadHTMLFS(http.FS(htmlFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
router.LoadHTMLFS(http.FS(tmplFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
},
)
defer ts.Close()
Expand All @@ -355,7 +355,7 @@ func TestLoadHTMLFSDebugMode(t *testing.T) {
DebugMode,
false,
func(router *Engine) {
router.LoadHTMLFS(http.FS(htmlFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
router.LoadHTMLFS(http.FS(tmplFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
},
)
defer ts.Close()
Expand All @@ -375,7 +375,7 @@ func TestLoadHTMLFSReleaseMode(t *testing.T) {
ReleaseMode,
false,
func(router *Engine) {
router.LoadHTMLFS(http.FS(htmlFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
router.LoadHTMLFS(http.FS(tmplFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
},
)
defer ts.Close()
Expand All @@ -395,7 +395,7 @@ func TestLoadHTMLFSUsingTLS(t *testing.T) {
TestMode,
true,
func(router *Engine) {
router.LoadHTMLFS(http.FS(htmlFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
router.LoadHTMLFS(http.FS(tmplFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
},
)
defer ts.Close()
Expand All @@ -422,7 +422,7 @@ func TestLoadHTMLFSFuncMap(t *testing.T) {
TestMode,
false,
func(router *Engine) {
router.LoadHTMLFS(http.FS(htmlFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
router.LoadHTMLFS(http.FS(tmplFS), "testdata/template/hello.tmpl", "testdata/template/raw.tmpl")
},
)
defer ts.Close()
Expand Down

0 comments on commit e21fcce

Please sign in to comment.