Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(internal/godocfx): different metadata for different modules #4297

Merged
merged 4 commits into from Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/godocfx/go.mod
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions internal/godocfx/godocfx_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
26 changes: 21 additions & 5 deletions internal/godocfx/main.go
Expand Up @@ -49,6 +49,7 @@ import (
"strings"
"time"

"golang.org/x/tools/go/packages"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -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")
}
}