From 141322c91110cba94f50939dfb9e7e715101a2a4 Mon Sep 17 00:00:00 2001 From: Tyler Bui-Palsulich Date: Tue, 22 Jun 2021 11:42:11 -0400 Subject: [PATCH 1/2] feat(internal/godocfx): different metadata for different modules --- internal/godocfx/go.mod | 1 + internal/godocfx/godocfx_test.go | 56 ++++++++++++++++++++++++++++++++ internal/godocfx/main.go | 26 ++++++++++++--- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/internal/godocfx/go.mod b/internal/godocfx/go.mod index a958cb2c4ed..4a36b56fe3c 100644 --- a/internal/godocfx/go.mod +++ b/internal/godocfx/go.mod @@ -7,6 +7,7 @@ require ( cloud.google.com/go/bigquery v1.8.0 cloud.google.com/go/datastore v1.1.0 cloud.google.com/go/storage v1.11.0 + github.com/google/go-cmp v0.5.6 github.com/yuin/goldmark v1.3.8 golang.org/x/tools v0.1.3 gopkg.in/yaml.v2 v2.4.0 diff --git a/internal/godocfx/godocfx_test.go b/internal/godocfx/godocfx_test.go index d227b0b89bf..4128866e5e5 100644 --- a/internal/godocfx/godocfx_test.go +++ b/internal/godocfx/godocfx_test.go @@ -17,14 +17,19 @@ package main import ( + "bytes" "flag" + "fmt" "io/ioutil" "os" "path/filepath" "testing" + "time" _ "cloud.google.com/go/bigquery" // Implicitly required by test. _ "cloud.google.com/go/storage" // Implicitly required by test. + "github.com/google/go-cmp/cmp" + "golang.org/x/tools/go/packages" ) var updateGoldens bool @@ -198,3 +203,54 @@ func TestHasPrefix(t *testing.T) { } } } + +func TestWriteMetadata(t *testing.T) { + now := time.Now() + + want := fmt.Sprintf(`update_time { + seconds: %d + nanos: %d +} +name: "cloud.google.com/go" +version: "100.0.0" +language: "go" +`, now.Unix(), now.Nanosecond()) + + wantAppEngine := fmt.Sprintf(`update_time { + seconds: %d + nanos: %d +} +name: "google.golang.org/appengine/v2" +version: "2.0.0" +language: "go" +stem: "/appengine/docs/standard/go/reference" +`, now.Unix(), now.Nanosecond()) + + tests := []struct { + path string + version string + want string + }{ + { + path: "cloud.google.com/go", + version: "100.0.0", + want: want, + }, + { + path: "google.golang.org/appengine/v2", + version: "2.0.0", + want: wantAppEngine, + }, + } + for _, test := range tests { + var buf bytes.Buffer + module := &packages.Module{ + Path: test.path, + Version: test.version, + } + writeMetadata(&buf, now, module) + if diff := cmp.Diff(test.want, buf.String()); diff != "" { + t.Errorf("writeMetadata(%q) got unexpected diff (-want +got):\n\n%s", test.path, diff) + } + } +} diff --git a/internal/godocfx/main.go b/internal/godocfx/main.go index 73cf4092e56..a3e6681c388 100644 --- a/internal/godocfx/main.go +++ b/internal/godocfx/main.go @@ -49,6 +49,7 @@ import ( "strings" "time" + "golang.org/x/tools/go/packages" "gopkg.in/yaml.v2" ) @@ -245,12 +246,27 @@ func write(outDir string, r *result) error { } defer f.Close() now := time.Now().UTC() - fmt.Fprintf(f, `update_time { - seconds: %d - nanos: %d + writeMetadata(f, now, r.module) + return nil +} + +func writeMetadata(w io.Writer, now time.Time, module *packages.Module) { + fmt.Fprintf(w, `update_time { + seconds: %d + nanos: %d } name: %q version: %q -language: "go"`, now.Unix(), now.Nanosecond(), r.module.Path, r.module.Version) - return nil +language: "go" +`, now.Unix(), now.Nanosecond(), module.Path, module.Version) + + // Some modules specify a different path to serve from. + // The URL will be /[stem]/[version]/[pkg path relative to module]. + // Alternatively, we could plumb this through command line flags. + switch module.Path { + case "google.golang.org/appengine": + fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go111/reference\"\n") + case "google.golang.org/appengine/v2": + fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go/reference\"\n") + } } From 04c6c3f586a8501f6b7c7beb1e2a6413b697e8b5 Mon Sep 17 00:00:00 2001 From: Tyler Bui-Palsulich Date: Tue, 22 Jun 2021 13:31:25 -0400 Subject: [PATCH 2/2] fix: update appengine v2 stem path --- internal/godocfx/godocfx_test.go | 2 +- internal/godocfx/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/godocfx/godocfx_test.go b/internal/godocfx/godocfx_test.go index 4128866e5e5..4b92a84a4d2 100644 --- a/internal/godocfx/godocfx_test.go +++ b/internal/godocfx/godocfx_test.go @@ -223,7 +223,7 @@ language: "go" name: "google.golang.org/appengine/v2" version: "2.0.0" language: "go" -stem: "/appengine/docs/standard/go/reference" +stem: "/appengine/docs/standard/go/reference/services/bundled" `, now.Unix(), now.Nanosecond()) tests := []struct { diff --git a/internal/godocfx/main.go b/internal/godocfx/main.go index a3e6681c388..44ad0a21e40 100644 --- a/internal/godocfx/main.go +++ b/internal/godocfx/main.go @@ -267,6 +267,6 @@ language: "go" case "google.golang.org/appengine": fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go111/reference\"\n") case "google.golang.org/appengine/v2": - fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go/reference\"\n") + fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go/reference/services/bundled\"\n") } }