Skip to content
Permalink
Browse files

resources: Fix image publish ordering issue

Fixes #5730
  • Loading branch information...
bep committed Mar 30, 2019
1 parent b6a60f7 commit 439ab0339d9ac6972caabaa55fa41887ace839cb
Showing with 49 additions and 1 deletion.
  1. +3 −0 resources/image.go
  2. +44 −0 resources/image_test.go
  3. +2 −1 resources/testhelpers_test.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,
@@ -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
@@ -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)

0 comments on commit 439ab03

Please sign in to comment.
You can’t perform that action at this time.