content/static: move code generation to new package
This change prevents ./cmd/golangorg and other binaries that depend on
./content/static from transitively importing ./markdown. At this time,
./markdown is only needed for code generation (via go generate) and
for testing. It does not need to be linked into ./cmd/golangorg.
x/blog imports ./content/static. The transitive import of
github.com/yuin/goldmark causes Go 1.12 builders to fail because they
run in GOPATH mode, and they forbid dependencies outside the
golang.org/x repos.
Fixes golang/go#37337
Change-Id: I938c06cdea66d6e08ac27d28f089026b42db9062
Reviewed-on: https://go-review.googlesource.com/c/website/+/220358
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/content/README.md b/content/README.md
index 66741d0..b5badce 100644
--- a/content/README.md
+++ b/content/README.md
@@ -28,7 +28,7 @@
1) Make changes to an existing file such as `static/style.css`.
2) If a new file is being added to the `static` directory, add it to the `files`
-slice in `static/gen.go`.
+slice in `static/internal/gen/gen.go`.
3) Run `go generate golang.org/x/website/content/static` so `static/static.go` is
up to date.
diff --git a/content/static/doc.go b/content/static/doc.go
index 8024657..19db511 100644
--- a/content/static/doc.go
+++ b/content/static/doc.go
@@ -6,3 +6,5 @@
// user interface. The map should be used with the mapfs package, see
// golang.org/x/tools/godoc/vfs/mapfs.
package static // import "golang.org/x/website/content/static"
+
+//go:generate go run makestatic.go
diff --git a/content/static/gen_test.go b/content/static/gen_test.go
deleted file mode 100644
index ca2adad..0000000
--- a/content/static/gen_test.go
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright 2018 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package static
-
-import (
- "bytes"
- "io/ioutil"
- "strconv"
- "testing"
- "unicode"
-)
-
-func TestStaticIsUpToDate(t *testing.T) {
- oldBuf, err := ioutil.ReadFile("static.go")
- if err != nil {
- t.Errorf("error while reading static.go: %v\n", err)
- }
-
- newBuf, err := Generate()
- if err != nil {
- t.Errorf("error while generating static.go: %v\n", err)
- }
-
- if bytes.Compare(oldBuf, newBuf) != 0 {
- t.Error(`static.go is stale. Run:
- $ go generate golang.org/x/website/content/static
- $ git diff
-to see the differences.`)
-
- }
-}
-
-// TestAppendQuote ensures that AppendQuote produces a valid literal.
-func TestAppendQuote(t *testing.T) {
- var in, out bytes.Buffer
- for r := rune(0); r < unicode.MaxRune; r++ {
- in.WriteRune(r)
- }
- appendQuote(&out, in.Bytes())
- in2, err := strconv.Unquote(out.String())
- if err != nil {
- t.Fatalf("AppendQuote produced invalid string literal: %v", err)
- }
- if got, want := in2, in.String(); got != want {
- t.Fatal("AppendQuote modified string") // no point printing got/want: huge
- }
-}
diff --git a/content/static/gen.go b/content/static/internal/gen/gen.go
similarity index 92%
rename from content/static/gen.go
rename to content/static/internal/gen/gen.go
index c273582..62517a2 100644
--- a/content/static/gen.go
+++ b/content/static/internal/gen/gen.go
@@ -2,9 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package static
-
-//go:generate go run makestatic.go
+// Package gen is used by content/static/makestatic.go
+// to generate content/static/static.go.
+//
+// This is a separate package so that it can be tested without
+// build constraints. cmd/golangorg and other binaries should not
+// depend on it.
+package gen
import (
"bytes"
diff --git a/content/static/internal/gen/gen_test.go b/content/static/internal/gen/gen_test.go
new file mode 100644
index 0000000..f25804c
--- /dev/null
+++ b/content/static/internal/gen/gen_test.go
@@ -0,0 +1,28 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package gen
+
+import (
+ "bytes"
+ "strconv"
+ "testing"
+ "unicode"
+)
+
+// TestAppendQuote ensures that AppendQuote produces a valid literal.
+func TestAppendQuote(t *testing.T) {
+ var in, out bytes.Buffer
+ for r := rune(0); r < unicode.MaxRune; r++ {
+ in.WriteRune(r)
+ }
+ appendQuote(&out, in.Bytes())
+ in2, err := strconv.Unquote(out.String())
+ if err != nil {
+ t.Fatalf("AppendQuote produced invalid string literal: %v", err)
+ }
+ if got, want := in2, in.String(); got != want {
+ t.Fatal("AppendQuote modified string") // no point printing got/want: huge
+ }
+}
diff --git a/content/static/makestatic.go b/content/static/makestatic.go
index ea64659..2b00c33 100644
--- a/content/static/makestatic.go
+++ b/content/static/makestatic.go
@@ -5,7 +5,7 @@
// +build ignore
// Command makestatic writes the generated file buffer to "static.go".
-// It is intended to be invoked via "go generate" (directive in "gen.go").
+// It is intended to be invoked via "go generate" (directive in "doc.go").
package main
import (
@@ -13,7 +13,7 @@
"io/ioutil"
"os"
- "golang.org/x/website/content/static"
+ "golang.org/x/website/content/static/internal/gen"
)
func main() {
@@ -24,7 +24,7 @@
}
func makestatic() error {
- buf, err := static.Generate()
+ buf, err := gen.Generate()
if err != nil {
return fmt.Errorf("error while generating static.go: %v\n", err)
}
diff --git a/content/static/static_test.go b/content/static/static_test.go
new file mode 100644
index 0000000..6893529
--- /dev/null
+++ b/content/static/static_test.go
@@ -0,0 +1,33 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package static
+
+import (
+ "bytes"
+ "io/ioutil"
+ "testing"
+
+ "golang.org/x/website/content/static/internal/gen"
+)
+
+func TestStaticIsUpToDate(t *testing.T) {
+ oldBuf, err := ioutil.ReadFile("static.go")
+ if err != nil {
+ t.Errorf("error while reading static.go: %v\n", err)
+ }
+
+ newBuf, err := gen.Generate()
+ if err != nil {
+ t.Errorf("error while generating static.go: %v\n", err)
+ }
+
+ if bytes.Compare(oldBuf, newBuf) != 0 {
+ t.Error(`static.go is stale. Run:
+ $ go generate golang.org/x/website/content/static
+ $ git diff
+to see the differences.`)
+
+ }
+}