-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move OnlyHTMLFS to internal and add test
- Loading branch information
1 parent
e21fcce
commit eb4a5e1
Showing
9 changed files
with
133 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package fs | ||
|
||
import ( | ||
"io/fs" | ||
"net/http" | ||
) | ||
|
||
// FileSystem implements an [fs.FS]. | ||
type FileSystem struct { | ||
http.FileSystem | ||
} | ||
|
||
// Open passes `Open` to the upstream implementation and return an [fs.File]. | ||
func (o FileSystem) Open(name string) (fs.File, error) { | ||
f, err := o.FileSystem.Open(name) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return fs.File(f), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fs | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockFileSystem struct { | ||
open func(name string) (http.File, error) | ||
} | ||
|
||
func (m *mockFileSystem) Open(name string) (http.File, error) { | ||
return m.open(name) | ||
} | ||
|
||
func TesFileSystem_Open(t *testing.T) { | ||
var testFile *os.File | ||
mockFS := &mockFileSystem{ | ||
open: func(name string) (http.File, error) { | ||
return testFile, nil | ||
}, | ||
} | ||
fs := &FileSystem{mockFS} | ||
|
||
file, err := fs.Open("foo") | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, testFile, file) | ||
} | ||
|
||
func TestFileSystem_Open_err(t *testing.T) { | ||
testError := errors.New("mock") | ||
mockFS := &mockFileSystem{ | ||
open: func(_ string) (http.File, error) { | ||
return nil, testError | ||
}, | ||
} | ||
fs := &FileSystem{mockFS} | ||
|
||
file, err := fs.Open("foo") | ||
|
||
require.ErrorIs(t, err, testError) | ||
assert.Nil(t, file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters