fixing golint issues to try to figure out test flakiness

pull/8/head
Stephen Searles 7 years ago
parent be1aefa439
commit 029d1ed5b3
  1. 6
      caddyhugo.go
  2. 11
      doc_test.go
  3. 12
      http.go
  4. 8
      media.go
  5. 12
      templates.go

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"net/http" "net/http"
_ "net/http/pprof"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
@ -31,6 +30,7 @@ func init() {
// ... there are others. See the godoc. // ... there are others. See the godoc.
} }
// CaddyHugo implements the plugin
type CaddyHugo struct { type CaddyHugo struct {
ServerType string ServerType string
Site *httpserver.SiteConfig Site *httpserver.SiteConfig
@ -49,6 +49,7 @@ type CaddyHugo struct {
ltime uint64 ltime uint64
} }
// Build rebuilds the cached state of the site. TODO: determine if this republishes
func (ch *CaddyHugo) Build() error { func (ch *CaddyHugo) Build() error {
err := ch.HugoSites.Build(hugolib.BuildCfg{ResetState: true}) err := ch.HugoSites.Build(hugolib.BuildCfg{ResetState: true})
if err != nil { if err != nil {
@ -58,6 +59,7 @@ func (ch *CaddyHugo) Build() error {
return nil return nil
} }
// BasePath returns the directory that the CaddyHugo internal/author pages are under
func (ch *CaddyHugo) BasePath() string { func (ch *CaddyHugo) BasePath() string {
return "/hugo" return "/hugo"
} }
@ -71,6 +73,7 @@ func (ch *CaddyHugo) docFilename(orig string) string {
return filepath.Join(ch.Dir, docname(orig)) return filepath.Join(ch.Dir, docname(orig))
} }
// Publish really renders new content into the public directory
func (ch *CaddyHugo) Publish() error { func (ch *CaddyHugo) Publish() error {
cmd := exec.Command("hugo") cmd := exec.Command("hugo")
cmd.Dir = ch.Dir cmd.Dir = ch.Dir
@ -82,6 +85,7 @@ func (ch *CaddyHugo) Publish() error {
return nil return nil
} }
// TmplData collects data for template execution
func (ch *CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} { func (ch *CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} {
var doc *acedoc.Document var doc *acedoc.Document
if docref != nil { if docref != nil {

@ -14,23 +14,23 @@ import (
) )
type World struct { type World struct {
CH CaddyHugo CH *CaddyHugo
BlogFolder string BlogFolder string
} }
func (w World) Clean() { func (w *World) Clean() {
if w.BlogFolder != "" { if w.BlogFolder != "" {
os.RemoveAll(w.BlogFolder) os.RemoveAll(w.BlogFolder)
} }
} }
func NewWorld(t *testing.T) World { func NewWorld(t *testing.T) *World {
dir, err := ioutil.TempDir("", "caddy-hugo2-test-") dir, err := ioutil.TempDir("", "caddy-hugo2-test-")
if err != nil { if err != nil {
t.Fatalf("error initializing test environment: %v", err) t.Fatalf("error initializing test environment: %v", err)
} }
w := World{BlogFolder: dir} w := &World{BlogFolder: dir}
cmd := exec.Command("hugo", "new", "site", dir) cmd := exec.Command("hugo", "new", "site", dir)
cmd.Dir = dir cmd.Dir = dir
@ -39,6 +39,7 @@ func NewWorld(t *testing.T) World {
t.Fatalf("error initializing test site: %v\n\n%v", err, string(out)) t.Fatalf("error initializing test site: %v\n\n%v", err, string(out))
} }
w.CH = &CaddyHugo{}
w.CH.Setup(dir) w.CH.Setup(dir)
return w return w
@ -232,7 +233,7 @@ func TestDeltasDouble(t *testing.T) {
// send the second message, via clientB // send the second message, via clientB
clientB.ReceiveJSON(w.CH.Message(acedoc.Insert(0, 0, "b"))) clientB.ReceiveJSON(w.CH.Message(acedoc.Insert(0, 0, "b")))
time.Sleep(100 * time.Millisecond) time.Sleep(200 * time.Millisecond)
clientA.mtx.Lock() clientA.mtx.Lock()
clientB.mtx.Lock() clientB.mtx.Lock()

@ -69,7 +69,7 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Control
return 404, nil return 404, nil
} }
func (ch CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) {
name := r.FormValue("name") name := r.FormValue("name")
ctype := r.FormValue("type") ctype := r.FormValue("type")
@ -83,7 +83,7 @@ func (ch CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int
http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound) http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound)
return http.StatusFound, nil return http.StatusFound, nil
} }
func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware { func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return func(next httpserver.Handler) httpserver.Handler { return func(next httpserver.Handler) httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return ch.ServeHTTPWithNext(next, c, w, r) return ch.ServeHTTPWithNext(next, c, w, r)
@ -91,11 +91,11 @@ func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
} }
} }
func (ch CaddyHugo) Auth(r *http.Request) bool { func (ch *CaddyHugo) Auth(r *http.Request) bool {
return true return true
} }
func (ch CaddyHugo) Match(r *http.Request) bool { func (ch *CaddyHugo) Match(r *http.Request) bool {
if strings.HasPrefix(r.URL.Path, "/media/") { if strings.HasPrefix(r.URL.Path, "/media/") {
return true return true
} }
@ -107,7 +107,7 @@ func (ch CaddyHugo) Match(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, "/hugo/") return strings.HasPrefix(r.URL.Path, "/hugo/")
} }
func (ch CaddyHugo) Admin() httpserver.Handler { func (ch *CaddyHugo) Admin() httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil)) err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil))
if err != nil { if err != nil {
@ -120,7 +120,7 @@ func (ch CaddyHugo) Admin() httpserver.Handler {
}) })
} }
func (ch CaddyHugo) AuthorHome() httpserver.Handler { func (ch *CaddyHugo) AuthorHome() httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
td := ch.TmplData(r, nil) td := ch.TmplData(r, nil)
err := ch.authorTmpl.Execute(w, td) err := ch.authorTmpl.Execute(w, td)

@ -3,9 +3,9 @@ package caddyhugo
import ( import (
"fmt" "fmt"
"image" "image"
_ "image/gif" _ "image/gif" // for processing images
"image/jpeg" "image/jpeg"
_ "image/png" _ "image/png" // for processing images
"io" "io"
"net/http" "net/http"
"os" "os"
@ -217,6 +217,9 @@ func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, e
for { for {
part, err := mr.NextPart() part, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return 400, nil return 400, nil
@ -344,7 +347,6 @@ func parseSizeString(str string, actual image.Rectangle) (image.Rectangle, error
// w was the only dimension given, so set it to the greater dimension // w was the only dimension given, so set it to the greater dimension
// of the actual image size // of the actual image size
if actual.Dx() > actual.Dy() { if actual.Dx() > actual.Dy() {
w = w
h = 0 h = 0
} else { } else {
h = w h = w

@ -14,11 +14,11 @@ import (
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
) )
func (t tmplData) Content() ([]Content, error) { func (t *tmplData) Content() ([]Content, error) {
return GetContent(t.Site.Root, t.HugoSites) return GetContent(t.Site.Root, t.HugoSites)
} }
func (t tmplData) ContentTypes() ([]string, error) { func (t *tmplData) ContentTypes() ([]string, error) {
nameMap := map[string]struct{}{"default": struct{}{}} nameMap := map[string]struct{}{"default": struct{}{}}
names, err := t.contentTypes(path.Join(t.Site.Root, "archetypes")) names, err := t.contentTypes(path.Join(t.Site.Root, "archetypes"))
@ -45,7 +45,7 @@ func (t tmplData) ContentTypes() ([]string, error) {
return out, nil return out, nil
} }
func (t tmplData) contentTypes(dir string) ([]string, error) { func (t *tmplData) contentTypes(dir string) ([]string, error) {
layoutDir, err := os.Open(path.Join(t.Site.Root, "archetypes")) layoutDir, err := os.Open(path.Join(t.Site.Root, "archetypes"))
if err != nil { if err != nil {
fmt.Println("opening layout dir", err) fmt.Println("opening layout dir", err)
@ -65,12 +65,12 @@ func (t tmplData) contentTypes(dir string) ([]string, error) {
type tmplData struct { type tmplData struct {
Site *httpserver.SiteConfig Site *httpserver.SiteConfig
R *http.Request R *http.Request
CaddyHugo *CaddyHugo
Doc *acedoc.Document Doc *acedoc.Document
docref *docref docref *docref
} }
func (t tmplData) LoadContent() (string, error) { func (t *tmplData) LoadContent() (string, error) {
return t.Doc.Contents(), nil return t.Doc.Contents(), nil
} }
@ -79,7 +79,7 @@ func baseNoExt(name string) string {
return base[:len(base)-len(path.Ext(base))] return base[:len(base)-len(path.Ext(base))]
} }
func (t tmplData) IframeSource() string { func (t *tmplData) IframeSource() string {
name := baseNoExt(t.docref.name) name := baseNoExt(t.docref.name)
ctype := baseNoExt(path.Dir(t.docref.name)) ctype := baseNoExt(path.Dir(t.docref.name))
if ctype == "content" { if ctype == "content" {

Loading…
Cancel
Save