Skip to content
Permalink
Browse files
feat(internal/godocfx): different metadata for different modules (#4297)
  • Loading branch information
tbpg committed Jun 22, 2021
1 parent 37607b4 commit 598f5b93778b2e2e75265ae54484dd54477433f5
Showing with 78 additions and 5 deletions.
  1. +1 −0 internal/godocfx/go.mod
  2. +56 −0 internal/godocfx/godocfx_test.go
  3. +21 −5 internal/godocfx/main.go
@@ -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
@@ -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/services/bundled"
`, 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)
}
}
}
@@ -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/services/bundled\"\n")
}
}

0 comments on commit 598f5b9

Please sign in to comment.