Skip to content

Commit

Permalink
Image resource refactor
Browse files Browse the repository at this point in the history
This commit pulls most of the image related logic into its own package, to make it easier to reason about and extend.

This is also a rewrite of the transformation logic used in Hugo Pipes, mostly to allow constructs like the one below:

    {{ ($myimg | fingerprint ).Width }}

Fixes gohugoio#5903
Fixes gohugoio#6234
Fixes gohugoio#6266
  • Loading branch information
bep committed Aug 26, 2019
1 parent ad1d6d6 commit e98eb9b
Show file tree
Hide file tree
Showing 34 changed files with 2,562 additions and 1,476 deletions.
1 change: 1 addition & 0 deletions common/herrors/errors.go
Expand Up @@ -52,6 +52,7 @@ func FprintStackTrace(w io.Writer, err error) {
// defer herrors.Recover()
func Recover(args ...interface{}) {
if r := recover(); r != nil {
fmt.Println("ERR:", r)
args = append(args, "stacktrace from panic: \n"+string(debug.Stack()), "\n")
fmt.Println(args...)
}
Expand Down
19 changes: 19 additions & 0 deletions htesting/test_helpers.go
Expand Up @@ -14,8 +14,10 @@
package htesting

import (
"math/rand"
"runtime"
"strings"
"time"

"github.com/spf13/afero"
)
Expand All @@ -37,3 +39,20 @@ func CreateTempDir(fs afero.Fs, prefix string) (string, func(), error) {
}
return tempDir, func() { fs.RemoveAll(tempDir) }, nil
}

// BailOut panics with a stack trace after the given duration. Useful for
// hanging tests.
func BailOut(after time.Duration) {
time.AfterFunc(after, func() {
buf := make([]byte, 1<<16)
runtime.Stack(buf, true)
panic(string(buf))
})

}

var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

func RandIntn(n int) int {
return rnd.Intn(n)
}
Binary file added hugolib/assets/images/sunset.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 4 additions & 6 deletions hugolib/pagebundler_test.go
Expand Up @@ -42,8 +42,7 @@ import (
)

func TestPageBundlerSiteRegular(t *testing.T) {
t.Parallel()

c := qt.New(t)
baseBaseURL := "https://example.com"

for _, baseURLPath := range []string{"", "/hugo"} {
Expand All @@ -55,15 +54,14 @@ func TestPageBundlerSiteRegular(t *testing.T) {
}
ugly := ugly
canonify := canonify
t.Run(fmt.Sprintf("ugly=%t,canonify=%t,path=%s", ugly, canonify, baseURLPathId),
func(t *testing.T) {
t.Parallel()
c.Run(fmt.Sprintf("ugly=%t,canonify=%t,path=%s", ugly, canonify, baseURLPathId),
func(c *qt.C) {
c.Parallel()
baseURL := baseBaseURL + baseURLPath
relURLBase := baseURLPath
if canonify {
relURLBase = ""
}
c := qt.New(t)
fs, cfg := newTestBundleSources(t)
cfg.Set("baseURL", baseURL)
cfg.Set("canonifyURLs", canonify)
Expand Down
67 changes: 64 additions & 3 deletions hugolib/resource_chain_test.go
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -167,6 +168,64 @@ T1: {{ $r.Content }}

}

func TestResourceChainBasic(t *testing.T) {
t.Parallel()

b := newTestSitesBuilder(t)
b.WithTemplatesAdded("index.html", `
{{ $hello := "<h1> Hello World! </h1>" | resources.FromString "hello.html" | fingerprint "sha512" | minify | fingerprint }}
HELLO: {{ $hello.Name }}|{{ $hello.RelPermalink }}|{{ $hello.Content | safeHTML }}
{{ $img := resources.Get "images/sunset.jpg" }}
{{ $fit := $img.Fit "200x200" }}
{{ $fit2 := $fit.Fit "100x200" }}
{{ $img = $img | fingerprint }}
SUNSET: {{ $img.Name }}|{{ $img.RelPermalink }}|{{ $img.Width }}|{{ len $img.Content }}
FIT: {{ $fit.Name }}|{{ $fit.RelPermalink }}|{{ $fit.Width }}
`)

fs := b.Fs.Source

imageDir := filepath.Join("assets", "images")
b.Assert(os.MkdirAll(imageDir, 0777), qt.IsNil)
src, err := os.Open("testdata/sunset.jpg")
b.Assert(err, qt.IsNil)
out, err := fs.Create(filepath.Join(imageDir, "sunset.jpg"))
b.Assert(err, qt.IsNil)
_, err = io.Copy(out, src)
b.Assert(err, qt.IsNil)
out.Close()

b.Running()

for i := 0; i < 2; i++ {

b.Build(BuildCfg{})

b.AssertFileContent("public/index.html",
`
SUNSET: images/sunset.jpg|/images/sunset.jpg|900|90587
FIT: images/sunset.jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_fit_q75_box.jpg|200
`)

b.EditFiles("page1.md", `
---
title: "Page 1 edit"
summary: "Edited summary"
---
Edited content.
`)

b.Assert(b.Fs.Destination.Remove("public"), qt.IsNil)
b.H.ResourceSpec.ClearCaches()

}
}

func TestResourceChain(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -353,9 +412,11 @@ Publish 2: {{ $cssPublish2.Permalink }}
"Publish 1: body{color:blue} /external1.min.css",
"Publish 2: http://example.com/external2.min.css",
)
c.Assert(b.CheckExists("public/external2.min.css"), qt.Equals, true)
c.Assert(b.CheckExists("public/external1.min.css"), qt.Equals, true)
c.Assert(b.CheckExists("public/inline.min.css"), qt.Equals, false)
b.Assert(b.CheckExists("public/external2.css"), qt.Equals, false)
b.Assert(b.CheckExists("public/external1.css"), qt.Equals, false)
b.Assert(b.CheckExists("public/external2.min.css"), qt.Equals, true)
b.Assert(b.CheckExists("public/external1.min.css"), qt.Equals, true)
b.Assert(b.CheckExists("public/inline.min.css"), qt.Equals, false)
}},

{"unmarshal", func() bool { return true }, func(b *sitesBuilder) {
Expand Down
1 change: 1 addition & 0 deletions hugolib/testhelpers_test.go
Expand Up @@ -536,6 +536,7 @@ func (s *sitesBuilder) changeEvents() []fsnotify.Event {
}

func (s *sitesBuilder) build(cfg BuildCfg, shouldFail bool) *sitesBuilder {
s.Helper()
defer func() {
s.changedFiles = nil
}()
Expand Down

0 comments on commit e98eb9b

Please sign in to comment.