Skip to content

Commit f9978ed

Browse files
committed
Image resource refactor
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
1 parent 58d4c0a commit f9978ed

34 files changed

+2593
-1475
lines changed

common/herrors/errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func FprintStackTrace(w io.Writer, err error) {
5252
// defer herrors.Recover()
5353
func Recover(args ...interface{}) {
5454
if r := recover(); r != nil {
55+
fmt.Println("ERR:", r)
5556
args = append(args, "stacktrace from panic: \n"+string(debug.Stack()), "\n")
5657
fmt.Println(args...)
5758
}

htesting/test_helpers.go

+19
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
package htesting
1515

1616
import (
17+
"math/rand"
1718
"runtime"
1819
"strings"
20+
"time"
1921

2022
"github.com/spf13/afero"
2123
)
@@ -37,3 +39,20 @@ func CreateTempDir(fs afero.Fs, prefix string) (string, func(), error) {
3739
}
3840
return tempDir, func() { fs.RemoveAll(tempDir) }, nil
3941
}
42+
43+
// BailOut panics with a stack trace after the given duration. Useful for
44+
// hanging tests.
45+
func BailOut(after time.Duration) {
46+
time.AfterFunc(after, func() {
47+
buf := make([]byte, 1<<16)
48+
runtime.Stack(buf, true)
49+
panic(string(buf))
50+
})
51+
52+
}
53+
54+
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
55+
56+
func RandIntn(n int) int {
57+
return rnd.Intn(n)
58+
}

hugolib/assets/images/sunset.jpg

88.5 KB
Loading

hugolib/pagebundler_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ import (
4242
)
4343

4444
func TestPageBundlerSiteRegular(t *testing.T) {
45-
t.Parallel()
46-
45+
c := qt.New(t)
4746
baseBaseURL := "https://example.com"
4847

4948
for _, baseURLPath := range []string{"", "/hugo"} {
@@ -55,15 +54,14 @@ func TestPageBundlerSiteRegular(t *testing.T) {
5554
}
5655
ugly := ugly
5756
canonify := canonify
58-
t.Run(fmt.Sprintf("ugly=%t,canonify=%t,path=%s", ugly, canonify, baseURLPathId),
59-
func(t *testing.T) {
60-
t.Parallel()
57+
c.Run(fmt.Sprintf("ugly=%t,canonify=%t,path=%s", ugly, canonify, baseURLPathId),
58+
func(c *qt.C) {
59+
c.Parallel()
6160
baseURL := baseBaseURL + baseURLPath
6261
relURLBase := baseURLPath
6362
if canonify {
6463
relURLBase = ""
6564
}
66-
c := qt.New(t)
6765
fs, cfg := newTestBundleSources(t)
6866
cfg.Set("baseURL", baseURL)
6967
cfg.Set("canonifyURLs", canonify)

hugolib/resource_chain_test.go

+64-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package hugolib
1515

1616
import (
17+
"io"
1718
"os"
1819
"path/filepath"
1920
"testing"
@@ -167,6 +168,64 @@ T1: {{ $r.Content }}
167168

168169
}
169170

171+
func TestResourceChainBasic(t *testing.T) {
172+
t.Parallel()
173+
174+
b := newTestSitesBuilder(t)
175+
b.WithTemplatesAdded("index.html", `
176+
{{ $hello := "<h1> Hello World! </h1>" | resources.FromString "hello.html" | fingerprint "sha512" | minify | fingerprint }}
177+
178+
HELLO: {{ $hello.Name }}|{{ $hello.RelPermalink }}|{{ $hello.Content | safeHTML }}
179+
180+
{{ $img := resources.Get "images/sunset.jpg" }}
181+
{{ $fit := $img.Fit "200x200" }}
182+
{{ $fit2 := $fit.Fit "100x200" }}
183+
{{ $img = $img | fingerprint }}
184+
SUNSET: {{ $img.Name }}|{{ $img.RelPermalink }}|{{ $img.Width }}|{{ len $img.Content }}
185+
FIT: {{ $fit.Name }}|{{ $fit.RelPermalink }}|{{ $fit.Width }}
186+
`)
187+
188+
fs := b.Fs.Source
189+
190+
imageDir := filepath.Join("assets", "images")
191+
b.Assert(os.MkdirAll(imageDir, 0777), qt.IsNil)
192+
src, err := os.Open("testdata/sunset.jpg")
193+
b.Assert(err, qt.IsNil)
194+
out, err := fs.Create(filepath.Join(imageDir, "sunset.jpg"))
195+
b.Assert(err, qt.IsNil)
196+
_, err = io.Copy(out, src)
197+
b.Assert(err, qt.IsNil)
198+
out.Close()
199+
200+
b.Running()
201+
202+
for i := 0; i < 2; i++ {
203+
204+
b.Build(BuildCfg{})
205+
206+
b.AssertFileContent("public/index.html",
207+
`
208+
SUNSET: images/sunset.jpg|/images/sunset.a9bf1d944e19c0f382e0d8f51de690f7d0bc8fa97390c4242a86c3e5c0737e71.jpg|900|90587
209+
FIT: images/sunset.jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_fit_q75_box.jpg|200
210+
211+
`)
212+
213+
b.EditFiles("page1.md", `
214+
---
215+
title: "Page 1 edit"
216+
summary: "Edited summary"
217+
---
218+
219+
Edited content.
220+
221+
`)
222+
223+
b.Assert(b.Fs.Destination.Remove("public"), qt.IsNil)
224+
b.H.ResourceSpec.ClearCaches()
225+
226+
}
227+
}
228+
170229
func TestResourceChain(t *testing.T) {
171230
t.Parallel()
172231

@@ -353,9 +412,11 @@ Publish 2: {{ $cssPublish2.Permalink }}
353412
"Publish 1: body{color:blue} /external1.min.css",
354413
"Publish 2: http://example.com/external2.min.css",
355414
)
356-
c.Assert(b.CheckExists("public/external2.min.css"), qt.Equals, true)
357-
c.Assert(b.CheckExists("public/external1.min.css"), qt.Equals, true)
358-
c.Assert(b.CheckExists("public/inline.min.css"), qt.Equals, false)
415+
b.Assert(b.CheckExists("public/external2.css"), qt.Equals, false)
416+
b.Assert(b.CheckExists("public/external1.css"), qt.Equals, false)
417+
b.Assert(b.CheckExists("public/external2.min.css"), qt.Equals, true)
418+
b.Assert(b.CheckExists("public/external1.min.css"), qt.Equals, true)
419+
b.Assert(b.CheckExists("public/inline.min.css"), qt.Equals, false)
359420
}},
360421

361422
{"unmarshal", func() bool { return true }, func(b *sitesBuilder) {

hugolib/testhelpers_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ func (s *sitesBuilder) changeEvents() []fsnotify.Event {
536536
}
537537

538538
func (s *sitesBuilder) build(cfg BuildCfg, shouldFail bool) *sitesBuilder {
539+
s.Helper()
539540
defer func() {
540541
s.changedFiles = nil
541542
}()

0 commit comments

Comments
 (0)