Skip to content

Commit a0baa6d

Browse files
committed
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 <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent a4db181 commit a0baa6d

File tree

5 files changed

+57
-18
lines changed

5 files changed

+57
-18
lines changed

cmd/pkgsite/main.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,39 @@ func main() {
5555
paths = "."
5656
}
5757

58-
lds := localdatasource.New(source.NewClient(time.Second))
59-
dsg := func(context.Context) internal.DataSource { return lds }
60-
server, err := frontend.NewServer(frontend.ServerConfig{
61-
DataSourceGetter: dsg,
62-
StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value),
63-
})
58+
server, err := newServer(ctx, strings.Split(paths, ","), *gopathMode)
6459
if err != nil {
65-
log.Fatalf(ctx, "frontend.NewServer: %v", err)
60+
log.Fatalf(ctx, "newServer: %v", err)
6661
}
67-
68-
load(ctx, lds, paths)
69-
7062
router := dcensus.NewRouter(frontend.TagRoute)
7163
server.Install(router.Handle, nil, nil)
72-
7364
mw := middleware.Timeout(54 * time.Second)
7465
log.Infof(ctx, "Listening on addr %s", *httpAddr)
7566
log.Fatal(ctx, http.ListenAndServe(*httpAddr, mw(router)))
7667
}
7768

69+
func newServer(ctx context.Context, paths []string, gopathMode bool) (*frontend.Server, error) {
70+
lds := localdatasource.New(source.NewClient(time.Second))
71+
server, err := frontend.NewServer(frontend.ServerConfig{
72+
DataSourceGetter: func(context.Context) internal.DataSource { return lds },
73+
StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value),
74+
})
75+
if err != nil {
76+
return nil, err
77+
}
78+
addGetters(ctx, lds, paths, gopathMode)
79+
return server, nil
80+
}
81+
7882
// load loads local modules from pathList.
79-
func load(ctx context.Context, ds *localdatasource.DataSource, pathList string) {
80-
paths := strings.Split(pathList, ",")
83+
func addGetters(ctx context.Context, ds *localdatasource.DataSource, paths []string, gopathMode bool) {
8184
loaded := len(paths)
8285
for _, path := range paths {
8386
var (
8487
mg fetch.ModuleGetter
8588
err error
8689
)
87-
if *gopathMode {
90+
if gopathMode {
8891
mg, err = localdatasource.NewGOPATHModuleGetter(path)
8992
} else {
9093
mg, err = fetch.NewDirectoryModuleGetter("", path)
@@ -98,6 +101,6 @@ func load(ctx context.Context, ds *localdatasource.DataSource, pathList string)
98101
}
99102

100103
if loaded == 0 {
101-
log.Fatalf(ctx, "failed to load module(s) at %s", pathList)
104+
log.Fatalf(ctx, "failed to load module(s) at %v", paths)
102105
}
103106
}

cmd/pkgsite/main_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"flag"
10+
"net/http"
11+
"net/http/httptest"
12+
"testing"
13+
)
14+
15+
func Test(t *testing.T) {
16+
flag.Set("static", "../../static")
17+
server, err := newServer(context.Background(), []string{"../../internal/fetch/testdata/has_go_mod"}, false)
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
mux := http.NewServeMux()
22+
server.Install(mux.Handle, nil, nil)
23+
w := httptest.NewRecorder()
24+
25+
mux.ServeHTTP(w, httptest.NewRequest("GET", "/example.com/testmod", nil))
26+
if w.Code != http.StatusOK {
27+
t.Errorf("%q: got status code = %d, want %d", "/testmod", w.Code, http.StatusOK)
28+
}
29+
}

internal/fetch/fetchlocal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestDirectoryModuleGetterEmpty(t *testing.T) {
1616
if err != nil {
1717
t.Fatal(err)
1818
}
19-
if want := "testmod"; g.modulePath != want {
19+
if want := "example.com/testmod"; g.modulePath != want {
2020
t.Errorf("got %q, want %q", g.modulePath, want)
2121
}
2222

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module testmod
1+
module example.com/testmod

internal/localdatasource/datasource.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
"path/filepath"
1717
"strings"
1818
"sync"
19+
"time"
1920

2021
"golang.org/x/pkgsite/internal"
2122
"golang.org/x/pkgsite/internal/derrors"
2223
"golang.org/x/pkgsite/internal/fetch"
24+
"golang.org/x/pkgsite/internal/log"
2325
"golang.org/x/pkgsite/internal/source"
2426
)
2527

@@ -81,7 +83,12 @@ func (ds *DataSource) getFromCache(path, version string) *internal.Module {
8183

8284
// fetch fetches a module using the configured ModuleGetters.
8385
// It tries each getter in turn until it finds one that has the module.
84-
func (ds *DataSource) fetch(ctx context.Context, modulePath, version string) (*internal.Module, error) {
86+
func (ds *DataSource) fetch(ctx context.Context, modulePath, version string) (_ *internal.Module, err error) {
87+
log.Infof(ctx, "local DataSource: fetching %s@%s", modulePath, version)
88+
start := time.Now()
89+
defer func() {
90+
log.Infof(ctx, "local DataSource: fetched %s@%s in %s with error %v", modulePath, version, time.Since(start), err)
91+
}()
8592
for _, g := range ds.getters {
8693
fr := fetch.FetchModule(ctx, modulePath, version, g, ds.sourceClient)
8794
if fr.Error == nil {

0 commit comments

Comments
 (0)