Skip to content

Commit

Permalink
Add tests for version collection
Browse files Browse the repository at this point in the history
  • Loading branch information
acodereviewersbestfriend544 authored and brendanburns committed Sep 6, 2023
1 parent 8e2d771 commit 1504316
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
22 changes: 19 additions & 3 deletions version_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,38 @@ import (
"strings"
)

type FileSystemInterface interface {
Glob(p string) ([]string, error)
ReadFile(f string) ([]byte, error)
}

type osfs struct{}

func (o osfs) Glob(pattern string) ([]string, error) {
return filepath.Glob(pattern)
}

func (o osfs) ReadFile(file string) ([]byte, error) {
return os.ReadFile(file)
}

type VersionCollection interface {
GetVersions() ([]string, error)
GetWebAssembly(version string) ([]byte, error)
}

type fileSystemVersionCollection struct {
fs FileSystemInterface
dir string
glob string
}

func ForFilesystemGlob(dir, glob string) VersionCollection {
return &fileSystemVersionCollection{dir, glob}
return &fileSystemVersionCollection{osfs{}, dir, glob}
}

func (f *fileSystemVersionCollection) GetVersions() ([]string, error) {
files, err := filepath.Glob(filepath.Join(f.dir, f.glob))
files, err := f.fs.Glob(filepath.Join(f.dir, f.glob))
if err != nil {
return nil, err
}
Expand All @@ -36,7 +52,7 @@ func (f *fileSystemVersionCollection) GetVersions() ([]string, error) {
}

func (f *fileSystemVersionCollection) GetWebAssembly(version string) ([]byte, error) {
return os.ReadFile(filepath.Join(f.dir, version))
return f.fs.ReadFile(filepath.Join(f.dir, version))
}

type urlVersionCollection struct {
Expand Down
74 changes: 74 additions & 0 deletions version_collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package caddy_wasm

import (
"net/http"
"net/http/httptest"
"testing"
"testing/fstest"
)

type testHandler struct {
requests []*http.Request
}

func (t *testHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
t.requests = append(t.requests, req)
res.WriteHeader(200)
res.Write([]byte("Ok"))
}

func TestUrlVersions(t *testing.T) {
handler := &testHandler{}
server := httptest.NewServer(handler)

collection := ForURL(server.URL)

v, e := collection.GetVersions()
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
if len(v) != 1 {
t.Errorf("Unexpected versions: %v", v)
}
d, e := collection.GetWebAssembly(v[0])
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
if string(d) != "Ok" {
t.Errorf("Unexpected data: %v", string(d))
}
}

func TestFileVersions(t *testing.T) {
m := fstest.MapFS{}
m["some/dir/foo.wasm"] = &fstest.MapFile{Data: []byte("foo")}
m["some/dir/bar.wasm"] = &fstest.MapFile{Data: []byte("bar")}
f := &fileSystemVersionCollection{
m,
"some/dir/",
"*.wasm",
}
v, e := f.GetVersions()
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
tests := []struct {
v string
d string
}{
{"foo.wasm", "foo"},
{"bar.wasm", "bar"},
}
if len(v) != len(tests) {
t.Errorf("Unexpected versions: %v", v)
}
for _, test := range tests {
d, e := f.GetWebAssembly(test.v)
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
if string(d) != test.d {
t.Errorf("Unexpected data: %v", string(d))
}
}
}

0 comments on commit 1504316

Please sign in to comment.