diff options
| author | Jonathan Amsterdam <jba@google.com> | 2022-02-16 13:16:19 -0500 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2022-02-17 16:50:32 +0000 |
| commit | 0635252b550fc4c24dcd58e68436708035d2993f (patch) | |
| tree | 746abf8175dd007dc10d821c3da885597f26da55 /cmd/pkgsite | |
| parent | c40ac83821b82e745621052588f0e4f374d89980 (diff) | |
| download | go-x-pkgsite-0635252b550fc4c24dcd58e68436708035d2993f.tar.xz | |
cmd/pkgsite: refactor and add test
Create all the getters in one place so we can test that.
Change-Id: I748a823562ee6e247ea9eece8e77d65bc68d68d4
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/386195
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'cmd/pkgsite')
| -rw-r--r-- | cmd/pkgsite/main.go | 70 | ||||
| -rw-r--r-- | cmd/pkgsite/main_test.go | 81 |
2 files changed, 118 insertions, 33 deletions
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go index 43e27860..0ec7626e 100644 --- a/cmd/pkgsite/main.go +++ b/cmd/pkgsite/main.go @@ -134,7 +134,11 @@ func main() { } } - server, err := newServer(ctx, paths, *gopathMode, modCacheDir, cacheMods, prox) + getters, err := buildGetters(ctx, paths, *gopathMode, modCacheDir, cacheMods, prox) + if err != nil { + die("%s", err) + } + server, err := newServer(getters, prox) if err != nil { die("%s", err) } @@ -145,12 +149,6 @@ func main() { die("%v", http.ListenAndServe(*httpAddr, mw(router))) } -func die(format string, args ...interface{}) { - fmt.Fprintf(os.Stderr, format, args...) - fmt.Fprintln(os.Stderr) - os.Exit(1) -} - func collectPaths(args []string) []string { var paths []string for _, arg := range args { @@ -159,8 +157,8 @@ func collectPaths(args []string) []string { return paths } -func newServer(ctx context.Context, paths []string, gopathMode bool, downloadDir string, cacheMods []internal.Modver, prox *proxy.Client) (*frontend.Server, error) { - getters := buildGetters(ctx, paths, gopathMode) +func buildGetters(ctx context.Context, paths []string, gopathMode bool, downloadDir string, cacheMods []internal.Modver, prox *proxy.Client) ([]fetch.ModuleGetter, error) { + getters := buildPathGetters(ctx, paths, gopathMode) if downloadDir != "" { g, err := fetch.NewFSProxyModuleGetter(downloadDir, cacheMods) if err != nil { @@ -171,30 +169,10 @@ func newServer(ctx context.Context, paths []string, gopathMode bool, downloadDir if prox != nil { getters = append(getters, fetch.NewProxyModuleGetter(prox, source.NewClient(time.Second))) } - lds := fetchdatasource.Options{ - Getters: getters, - ProxyClientForLatest: prox, - BypassLicenseCheck: true, - }.New() - server, err := frontend.NewServer(frontend.ServerConfig{ - DataSourceGetter: func(context.Context) internal.DataSource { return lds }, - TemplateFS: template.TrustedFSFromEmbed(static.FS), - StaticFS: static.FS, - ThirdPartyFS: thirdparty.FS, - }) - if err != nil { - return nil, err - } - for _, g := range getters { - p, fsys := g.SourceFS() - if p != "" { - server.InstallFS(p, fsys) - } - } - return server, nil + return getters, nil } -func buildGetters(ctx context.Context, paths []string, gopathMode bool) []fetch.ModuleGetter { +func buildPathGetters(ctx context.Context, paths []string, gopathMode bool) []fetch.ModuleGetter { var getters []fetch.ModuleGetter loaded := len(paths) for _, path := range paths { @@ -221,6 +199,30 @@ func buildGetters(ctx context.Context, paths []string, gopathMode bool) []fetch. return getters } +func newServer(getters []fetch.ModuleGetter, prox *proxy.Client) (*frontend.Server, error) { + lds := fetchdatasource.Options{ + Getters: getters, + ProxyClientForLatest: prox, + BypassLicenseCheck: true, + }.New() + server, err := frontend.NewServer(frontend.ServerConfig{ + DataSourceGetter: func(context.Context) internal.DataSource { return lds }, + TemplateFS: template.TrustedFSFromEmbed(static.FS), + StaticFS: static.FS, + ThirdPartyFS: thirdparty.FS, + }) + if err != nil { + return nil, err + } + for _, g := range getters { + p, fsys := g.SourceFS() + if p != "" { + server.InstallFS(p, fsys) + } + } + return server, nil +} + func defaultCacheDir() (string, error) { out, err := runGo("", "env", "GOMODCACHE") if err != nil { @@ -282,3 +284,9 @@ func listModsForPaths(paths []string, cacheDir string) ([]string, []internal.Mod } return outPaths, cacheMods, nil } + +func die(format string, args ...interface{}) { + fmt.Fprintf(os.Stderr, format, args...) + fmt.Fprintln(os.Stderr) + os.Exit(1) +} diff --git a/cmd/pkgsite/main_test.go b/cmd/pkgsite/main_test.go index 86d5cab6..a0dfac3a 100644 --- a/cmd/pkgsite/main_test.go +++ b/cmd/pkgsite/main_test.go @@ -17,6 +17,7 @@ import ( "github.com/google/go-cmp/cmp" "golang.org/x/net/html" "golang.org/x/pkgsite/internal" + "golang.org/x/pkgsite/internal/proxy" "golang.org/x/pkgsite/internal/proxy/proxytest" "golang.org/x/pkgsite/internal/testing/htmlcheck" ) @@ -32,7 +33,7 @@ var ( } ) -func Test(t *testing.T) { +func TestBuildGetters(t *testing.T) { repoPath := func(fn string) string { return filepath.Join("..", "..", fn) } abs := func(dir string) string { @@ -43,13 +44,89 @@ func Test(t *testing.T) { return a } + ctx := context.Background() localModule := repoPath("internal/fetch/testdata/has_go_mod") cacheDir := repoPath("internal/fetch/testdata/modcache") testModules := proxytest.LoadTestModules(repoPath("internal/proxy/testdata")) prox, teardown := proxytest.SetupTestClient(t, testModules) defer teardown() - server, err := newServer(context.Background(), []string{localModule}, false, cacheDir, nil, prox) + localGetter := "Dir(example.com/testmod, " + abs(localModule) + ")" + cacheGetter := "FSProxy(" + abs(cacheDir) + ")" + for _, test := range []struct { + name string + paths []string + cmods []internal.Modver + cacheDir string + prox *proxy.Client + want []string + }{ + { + name: "local only", + paths: []string{localModule}, + want: []string{localGetter}, + }, + { + name: "cache", + cacheDir: cacheDir, + want: []string{cacheGetter}, + }, + { + name: "proxy", + prox: prox, + want: []string{"Proxy"}, + }, + { + name: "all three", + paths: []string{localModule}, + cacheDir: cacheDir, + prox: prox, + want: []string{localGetter, cacheGetter, "Proxy"}, + }, + { + name: "list", + paths: []string{localModule}, + cacheDir: cacheDir, + cmods: []internal.Modver{{Path: "foo", Version: "v1.2.3"}}, + want: []string{localGetter, "FSProxy(" + abs(cacheDir) + ", foo@v1.2.3)"}, + }, + } { + t.Run(test.name, func(t *testing.T) { + getters, err := buildGetters(ctx, test.paths, false, test.cacheDir, test.cmods, test.prox) + if err != nil { + t.Fatal(err) + } + var got []string + for _, g := range getters { + got = append(got, g.String()) + } + + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("mismatch (-want, +got):\n%s", diff) + } + }) + } +} + +func TestServer(t *testing.T) { + repoPath := func(fn string) string { return filepath.Join("..", "..", fn) } + + abs := func(dir string) string { + a, err := filepath.Abs(dir) + if err != nil { + t.Fatal(err) + } + return a + } + + localModule := repoPath("internal/fetch/testdata/has_go_mod") + cacheDir := repoPath("internal/fetch/testdata/modcache") + testModules := proxytest.LoadTestModules(repoPath("internal/proxy/testdata")) + prox, teardown := proxytest.SetupTestClient(t, testModules) + defer teardown() + + getters, err := buildGetters(context.Background(), []string{localModule}, false, cacheDir, nil, prox) + server, err := newServer(getters, prox) if err != nil { t.Fatal(err) } |
