diff --git a/.travis.yml b/.travis.yml index 25342a9..e5fa142 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ go: - tip install: - - go get -t -v . ./pry + - go get -t -v . ./pry ./generate ./playground/server before_install: - if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi @@ -20,5 +20,6 @@ script: - go test -v -race -coverprofile=pry.coverprofile ./pry - go test -v -race -coverprofile=generate.coverprofile ./generate - go test -v -race -coverprofile=main.coverprofile + - go test -v -race -coverprofile=server.coverprofile ./playground/server - $HOME/gopath/bin/gover - $HOME/gopath/bin/goveralls -service=travis-ci -coverprofile=gover.coverprofile diff --git a/playground/server/server.go b/playground/server/server.go index 5bc7282..265dd4e 100644 --- a/playground/server/server.go +++ b/playground/server/server.go @@ -19,6 +19,8 @@ import ( "github.com/pkg/errors" ) +const bundlesDir = "bundles" + var bind = flag.String("bind", ":8080", "address to bind to") func main() { @@ -48,8 +50,8 @@ func normalizePackages(packages string) []string { func generateBundle(w http.ResponseWriter, r *http.Request, packages string) (retErr error) { pkgs := normalizePackages(packages) hash := pkgHash(pkgs) - path := filepath.Join("bundles", hash+".wasm") - goPath := filepath.Join("bundles", hash+".go") + path := filepath.Join(bundlesDir, hash+".wasm") + goPath := filepath.Join(bundlesDir, hash+".go") _, err := os.Stat(path) if err == nil { http.ServeFile(w, r, path) diff --git a/playground/server/server11_test.go b/playground/server/server11_test.go new file mode 100644 index 0000000..28ac0b3 --- /dev/null +++ b/playground/server/server11_test.go @@ -0,0 +1,40 @@ +// +build go1.11 + +package main + +import ( + "net/http" + "net/http/httptest" + "os" + "testing" +) + +func TestGenerateBundle(t *testing.T) { + t.Parallel() + + remove := func() { + if err := os.RemoveAll(bundlesDir); err != nil { + t.Fatal(err) + } + } + + remove() + + if err := os.MkdirAll(bundlesDir, 0755); err != nil { + t.Fatal(err) + } + + r, err := http.NewRequest(http.MethodGet, "/wasm/math,fmt", nil) + if err != nil { + t.Fatal(err) + } + resp := httptest.NewRecorder() + if err := generateBundle(resp, r, "math,fmt"); err != nil { + t.Fatal(err) + } + if resp.Code != http.StatusOK { + t.Fatalf("expected StatusOK got %+v", resp.Code) + } + + remove() +} diff --git a/playground/server/server_test.go b/playground/server/server_test.go new file mode 100644 index 0000000..73c52ad --- /dev/null +++ b/playground/server/server_test.go @@ -0,0 +1,35 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestNormalizePackages(t *testing.T) { + t.Parallel() + + cases := []struct { + in string + want []string + }{ + { + "", + nil, + }, + { + "foo, bar,github.com/d4l3k/go-pry ", + []string{ + "bar", + "foo", + "github.com/d4l3k/go-pry", + }, + }, + } + + for i, c := range cases { + out := normalizePackages(c.in) + if !reflect.DeepEqual(out, c.want) { + t.Errorf("%d. normalizePackages(%q) = %+v; not %+v", i, c.in, out, c.want) + } + } +}