shithub: hugo

Download patch

ref: bf8a61fde5696f56a77473f062bbff58d4b219ca
parent: 72158ab2e8dbb6d38dc588576ad17d36eac80e5e
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Jan 1 06:07:23 EST 2018

hugolib: Add test for image processing from shortcodes

See #4202

--- a/hugolib/page_bundler_capture_test.go
+++ b/hugolib/page_bundler_capture_test.go
@@ -143,7 +143,7 @@
 D:
 __bundle/en/work/base/_index.md/resources/en/work/base/_1.png
 __bundle/en/work/base/a/b/index.md/resources/en/work/base/a/b/ab1.md
-__bundle/en/work/base/b/index.md/resources/en/work/base/b/1.md|en/work/base/b/2.md|en/work/base/b/c/logo.png|en/work/base/b/custom-mime.bep
+__bundle/en/work/base/b/index.md/resources/en/work/base/b/1.md|en/work/base/b/2.md|en/work/base/b/c/logo.png|en/work/base/b/custom-mime.bep|en/work/base/b/sunset1.jpg|en/work/base/b/sunset2.jpg
 C:
 /work/base/assets/pic1.png
 /work/base/assets/pic2.png
--- a/hugolib/page_bundler_test.go
+++ b/hugolib/page_bundler_test.go
@@ -20,6 +20,8 @@
 	"strings"
 	"testing"
 
+	"io"
+
 	"github.com/spf13/afero"
 
 	"github.com/gohugoio/hugo/media"
@@ -104,10 +106,10 @@
 				secondPage := pageResources[1].(*Page)
 				assert.Equal(filepath.FromSlash("b/1.md"), firstPage.pathOrTitle(), secondPage.pathOrTitle())
 				assert.Contains(firstPage.Content, "TheContent")
-				assert.Len(leafBundle1.Resources, 4) // 2 pages 1 image 1 custom mime type
+				assert.Len(leafBundle1.Resources, 6) // 2 pages 3 images 1 custom mime type
 
 				imageResources := leafBundle1.Resources.ByType("image")
-				assert.Len(imageResources, 1)
+				assert.Len(imageResources, 3)
 				image := imageResources[0]
 
 				altFormat := leafBundle1.OutputFormats().Get("CUSTOMO")
@@ -123,7 +125,13 @@
 
 				if ugly {
 					assert.Equal("/2017/pageslug.html", leafBundle1.RelPermalink())
-					th.assertFileContent(filepath.FromSlash("/work/public/2017/pageslug.html"), "TheContent")
+					th.assertFileContent(filepath.FromSlash("/work/public/2017/pageslug.html"),
+						"TheContent",
+						"Sunset RelPermalink: /2017/pageslug/sunset1.jpg",
+						"Thumb Width: 123",
+						"Short Sunset RelPermalink: /2017/pageslug/sunset2.jpg",
+						"Short Thumb Width: 56",
+					)
 					th.assertFileContent(filepath.FromSlash("/work/public/cpath/2017/pageslug.html"), "TheContent")
 
 					assert.Equal("/a/b.html", leafBundle2.RelPermalink())
@@ -163,6 +171,7 @@
 
 func newTestBundleSources(t *testing.T) (*viper.Viper, *hugofs.Fs) {
 	cfg, fs := newTestCfg()
+	assert := require.New(t)
 
 	workDir := "/work"
 	cfg.Set("workingDir", workDir)
@@ -183,6 +192,17 @@
 TheContent.
 `
 
+	pageWithImageShortcodeContent := `---
+title: "Bundle Galore"
+slug: pageslug
+date: 2017-10-09
+---
+
+TheContent.
+
+{{< myShort >}}
+`
+
 	pageContentNoSlug := `---
 title: "Bundle Galore #2"
 date: 2017-10-09
@@ -191,11 +211,33 @@
 TheContent.
 `
 
-	layout := `{{ .Title }}|{{ .Content }}`
+	singleLayout := `
+Title: {{ .Title }}
+Content: {{ .Content }}
+{{ $sunset := .Resources.GetByPrefix "sunset1" }}
+{{ with $sunset }}
+Sunset RelPermalink: {{ .RelPermalink }}
+{{ $thumb := .Fill "123x123" }}
+Thumb Width: {{ $thumb.Width }}
+{{ end }}
 
-	writeSource(t, fs, filepath.Join(workDir, "layouts", "_default", "single.html"), layout)
-	writeSource(t, fs, filepath.Join(workDir, "layouts", "_default", "list.html"), layout)
+`
 
+	myShort := `
+{{ $sunset := .Page.Resources.GetByPrefix "sunset2" }}
+{{ with $sunset }}
+Short Sunset RelPermalink: {{ .RelPermalink }}
+{{ $thumb := .Fill "56x56" }}
+Short Thumb Width: {{ $thumb.Width }}
+{{ end }}
+`
+
+	listLayout := `{{ .Title }}|{{ .Content }}`
+
+	writeSource(t, fs, filepath.Join(workDir, "layouts", "_default", "single.html"), singleLayout)
+	writeSource(t, fs, filepath.Join(workDir, "layouts", "_default", "list.html"), listLayout)
+	writeSource(t, fs, filepath.Join(workDir, "layouts", "shortcodes", "myShort.html"), myShort)
+
 	writeSource(t, fs, filepath.Join(workDir, "base", "_index.md"), pageContent)
 	writeSource(t, fs, filepath.Join(workDir, "base", "_1.md"), pageContent)
 	writeSource(t, fs, filepath.Join(workDir, "base", "_1.png"), pageContent)
@@ -213,12 +255,30 @@
 	writeSource(t, fs, filepath.Join(workDir, "base", "assets", "pages", "mypage.md"), pageContent)
 
 	// Bundle
-	writeSource(t, fs, filepath.Join(workDir, "base", "b", "index.md"), pageContent)
+	writeSource(t, fs, filepath.Join(workDir, "base", "b", "index.md"), pageWithImageShortcodeContent)
 	writeSource(t, fs, filepath.Join(workDir, "base", "b", "1.md"), pageContent)
 	writeSource(t, fs, filepath.Join(workDir, "base", "b", "2.md"), pageContent)
 	writeSource(t, fs, filepath.Join(workDir, "base", "b", "custom-mime.bep"), "bepsays")
 	writeSource(t, fs, filepath.Join(workDir, "base", "b", "c", "logo.png"), "content")
 
+	// Write a real image into one of the bundle above.
+	src, err := os.Open("testdata/sunset.jpg")
+	assert.NoError(err)
+
+	// We need 2 to test https://github.com/gohugoio/hugo/issues/4202
+	out, err := fs.Source.Create(filepath.Join(workDir, "base", "b", "sunset1.jpg"))
+	assert.NoError(err)
+	out2, err := fs.Source.Create(filepath.Join(workDir, "base", "b", "sunset2.jpg"))
+	assert.NoError(err)
+
+	_, err = io.Copy(out, src)
+	out.Close()
+	src.Seek(0, 0)
+	_, err = io.Copy(out2, src)
+	out2.Close()
+	src.Close()
+	assert.NoError(err)
+
 	return cfg, fs
 }
 
@@ -355,6 +415,10 @@
 	writeSource(t, fs, filepath.Join(workDir, "symcontent3", "s1.png"), "image")
 	writeSource(t, fs, filepath.Join(workDir, "symcontent3", "s2.png"), "image")
 
+	wd, _ := os.Getwd()
+	defer func() {
+		os.Chdir(wd)
+	}()
 	// Symlinked sections inside content.
 	os.Chdir(filepath.Join(workDir, contentDir))
 	for i := 1; i <= 3; i++ {
binary files /dev/null b/hugolib/testdata/sunset.jpg differ