From 439ab0339d9ac6972caabaa55fa41887ace839cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 30 Mar 2019 09:12:49 +0100 Subject: [PATCH] resources: Fix image publish ordering issue Fixes #5730 --- resources/image.go | 3 +++ resources/image_test.go | 44 +++++++++++++++++++++++++++++++++++ resources/testhelpers_test.go | 3 ++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/resources/image.go b/resources/image.go index 202b54fc2..f1aae2996 100644 --- a/resources/image.go +++ b/resources/image.go @@ -510,6 +510,9 @@ func (i *Image) encodeTo(conf imageConfig, img image.Image, w io.Writer) error { func (i *Image) clone() *Image { g := *i.genericResource g.resourceContent = &resourceContent{} + if g.publishOnce != nil { + g.publishOnce = &publishOnce{logger: g.publishOnce.logger} + } return &Image{ imaging: i.imaging, diff --git a/resources/image_test.go b/resources/image_test.go index fd1eb5410..6639dbb24 100644 --- a/resources/image_test.go +++ b/resources/image_test.go @@ -152,6 +152,50 @@ func TestImageTransformLongFilename(t *testing.T) { assert.Equal("/a/_hu59e56ffff1bc1d8d122b1403d34e039f_90587_c876768085288f41211f768147ba2647.jpg", resized.RelPermalink()) } +// https://github.com/gohugoio/hugo/issues/5730 +func TestImagePermalinkPublishOrder(t *testing.T) { + for _, checkOriginalFirst := range []bool{true, false} { + name := "OriginalFirst" + if !checkOriginalFirst { + name = "ResizedFirst" + } + + t.Run(name, func(t *testing.T) { + + assert := require.New(t) + spec := newTestResourceOsFs(assert) + + check1 := func(img *Image) { + resizedLink := "/a/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_100x50_resize_q75_box.jpg" + assert.Equal(resizedLink, img.RelPermalink()) + assertImageFile(assert, spec.PublishFs, resizedLink, 100, 50) + } + + check2 := func(img *Image) { + assert.Equal("/a/sunset.jpg", img.RelPermalink()) + assertImageFile(assert, spec.PublishFs, "a/sunset.jpg", 900, 562) + } + + orignal := fetchImageForSpec(spec, assert, "sunset.jpg") + assert.NotNil(orignal) + + if checkOriginalFirst { + check2(orignal) + } + + resized, err := orignal.Resize("100x50") + assert.NoError(err) + + check1(resized) + + if !checkOriginalFirst { + check2(orignal) + } + }) + } + +} + func TestImageTransformConcurrent(t *testing.T) { var wg sync.WaitGroup diff --git a/resources/testhelpers_test.go b/resources/testhelpers_test.go index 200a795e3..d064fa570 100644 --- a/resources/testhelpers_test.go +++ b/resources/testhelpers_test.go @@ -135,13 +135,14 @@ func fetchResourceForSpec(spec *Spec, assert *require.Assertions, name string) r factory := newTargetPaths("/a") - r, err := spec.New(ResourceSourceDescriptor{TargetPaths: factory, SourceFilename: name}) + r, err := spec.New(ResourceSourceDescriptor{TargetPaths: factory, LazyPublish: true, SourceFilename: name}) assert.NoError(err) return r.(resource.ContentResource) } func assertImageFile(assert *require.Assertions, fs afero.Fs, filename string, width, height int) { + filename = filepath.Clean(filename) f, err := fs.Open(filename) if err != nil { printFs(fs, "", os.Stdout)