cmd/pkgsite: add a test
Add a small test to the pkgsite command to make sure it's working.
For golang/go#47780
Change-Id: I276feeacbc11b18ceb054902fe6b0c1c2c2ab923
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/343630
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go
index ed03191..6d3cb58 100644
--- a/cmd/pkgsite/main.go
+++ b/cmd/pkgsite/main.go
@@ -55,36 +55,39 @@
paths = "."
}
- lds := localdatasource.New(source.NewClient(time.Second))
- dsg := func(context.Context) internal.DataSource { return lds }
- server, err := frontend.NewServer(frontend.ServerConfig{
- DataSourceGetter: dsg,
- StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value),
- })
+ server, err := newServer(ctx, strings.Split(paths, ","), *gopathMode)
if err != nil {
- log.Fatalf(ctx, "frontend.NewServer: %v", err)
+ log.Fatalf(ctx, "newServer: %v", err)
}
-
- load(ctx, lds, paths)
-
router := dcensus.NewRouter(frontend.TagRoute)
server.Install(router.Handle, nil, nil)
-
mw := middleware.Timeout(54 * time.Second)
log.Infof(ctx, "Listening on addr %s", *httpAddr)
log.Fatal(ctx, http.ListenAndServe(*httpAddr, mw(router)))
}
+func newServer(ctx context.Context, paths []string, gopathMode bool) (*frontend.Server, error) {
+ lds := localdatasource.New(source.NewClient(time.Second))
+ server, err := frontend.NewServer(frontend.ServerConfig{
+ DataSourceGetter: func(context.Context) internal.DataSource { return lds },
+ StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value),
+ })
+ if err != nil {
+ return nil, err
+ }
+ addGetters(ctx, lds, paths, gopathMode)
+ return server, nil
+}
+
// load loads local modules from pathList.
-func load(ctx context.Context, ds *localdatasource.DataSource, pathList string) {
- paths := strings.Split(pathList, ",")
+func addGetters(ctx context.Context, ds *localdatasource.DataSource, paths []string, gopathMode bool) {
loaded := len(paths)
for _, path := range paths {
var (
mg fetch.ModuleGetter
err error
)
- if *gopathMode {
+ if gopathMode {
mg, err = localdatasource.NewGOPATHModuleGetter(path)
} else {
mg, err = fetch.NewDirectoryModuleGetter("", path)
@@ -98,6 +101,6 @@
}
if loaded == 0 {
- log.Fatalf(ctx, "failed to load module(s) at %s", pathList)
+ log.Fatalf(ctx, "failed to load module(s) at %v", paths)
}
}
diff --git a/cmd/pkgsite/main_test.go b/cmd/pkgsite/main_test.go
new file mode 100644
index 0000000..f0db81d
--- /dev/null
+++ b/cmd/pkgsite/main_test.go
@@ -0,0 +1,29 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "context"
+ "flag"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+func Test(t *testing.T) {
+ flag.Set("static", "../../static")
+ server, err := newServer(context.Background(), []string{"../../internal/fetch/testdata/has_go_mod"}, false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ mux := http.NewServeMux()
+ server.Install(mux.Handle, nil, nil)
+ w := httptest.NewRecorder()
+
+ mux.ServeHTTP(w, httptest.NewRequest("GET", "/example.com/testmod", nil))
+ if w.Code != http.StatusOK {
+ t.Errorf("%q: got status code = %d, want %d", "/testmod", w.Code, http.StatusOK)
+ }
+}
diff --git a/internal/fetch/fetchlocal_test.go b/internal/fetch/fetchlocal_test.go
index 914c052..6cd4f77 100644
--- a/internal/fetch/fetchlocal_test.go
+++ b/internal/fetch/fetchlocal_test.go
@@ -16,7 +16,7 @@
if err != nil {
t.Fatal(err)
}
- if want := "testmod"; g.modulePath != want {
+ if want := "example.com/testmod"; g.modulePath != want {
t.Errorf("got %q, want %q", g.modulePath, want)
}
diff --git a/internal/fetch/testdata/has_go_mod/go.mod b/internal/fetch/testdata/has_go_mod/go.mod
index c5553d5..c390929 100644
--- a/internal/fetch/testdata/has_go_mod/go.mod
+++ b/internal/fetch/testdata/has_go_mod/go.mod
@@ -1 +1 @@
-module testmod
+module example.com/testmod
diff --git a/internal/localdatasource/datasource.go b/internal/localdatasource/datasource.go
index 0188e34..bc08c21 100644
--- a/internal/localdatasource/datasource.go
+++ b/internal/localdatasource/datasource.go
@@ -16,10 +16,12 @@
"path/filepath"
"strings"
"sync"
+ "time"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/fetch"
+ "golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/source"
)
@@ -81,7 +83,12 @@
// fetch fetches a module using the configured ModuleGetters.
// It tries each getter in turn until it finds one that has the module.
-func (ds *DataSource) fetch(ctx context.Context, modulePath, version string) (*internal.Module, error) {
+func (ds *DataSource) fetch(ctx context.Context, modulePath, version string) (_ *internal.Module, err error) {
+ log.Infof(ctx, "local DataSource: fetching %s@%s", modulePath, version)
+ start := time.Now()
+ defer func() {
+ log.Infof(ctx, "local DataSource: fetched %s@%s in %s with error %v", modulePath, version, time.Since(start), err)
+ }()
for _, g := range ds.getters {
fr := fetch.FetchModule(ctx, modulePath, version, g, ds.sourceClient)
if fr.Error == nil {