From: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date: Mon, 1 Jan 2018 10:07:23 +0000 (+0100)
Subject: hugolib: Add test for image processing from shortcodes
X-Git-Tag: v0.32.1~7
X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=bf8a61fde5696f56a77473f062bbff58d4b219ca;p=brevno-suite%2Fhugo

hugolib: Add test for image processing from shortcodes

See #4202
---

diff --git a/hugolib/page_bundler_capture_test.go b/hugolib/page_bundler_capture_test.go
index 6ff18222..c7715491 100644
--- a/hugolib/page_bundler_capture_test.go
+++ b/hugolib/page_bundler_capture_test.go
@@ -143,7 +143,7 @@ F:
 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
diff --git a/hugolib/page_bundler_test.go b/hugolib/page_bundler_test.go
index ab629d6a..d3f3df20 100644
--- a/hugolib/page_bundler_test.go
+++ b/hugolib/page_bundler_test.go
@@ -20,6 +20,8 @@ import (
 	"strings"
 	"testing"
 
+	"io"
+
 	"github.com/spf13/afero"
 
 	"github.com/gohugoio/hugo/media"
@@ -104,10 +106,10 @@ func TestPageBundlerSite(t *testing.T) {
 				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 @@ func TestPageBundlerSite(t *testing.T) {
 
 				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 TestPageBundlerSiteWitSymbolicLinksInContent(t *testing.T) {
 
 func newTestBundleSources(t *testing.T) (*viper.Viper, *hugofs.Fs) {
 	cfg, fs := newTestCfg()
+	assert := require.New(t)
 
 	workDir := "/work"
 	cfg.Set("workingDir", workDir)
@@ -181,6 +190,17 @@ date: 2017-10-09
 ---
 
 TheContent.
+`
+
+	pageWithImageShortcodeContent := `---
+title: "Bundle Galore"
+slug: pageslug
+date: 2017-10-09
+---
+
+TheContent.
+
+{{< myShort >}}
 `
 
 	pageContentNoSlug := `---
@@ -191,10 +211,32 @@ date: 2017-10-09
 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)
@@ -213,12 +255,30 @@ TheContent.
 	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 @@ TheContent.
 	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++ {
diff --git a/hugolib/testdata/sunset.jpg b/hugolib/testdata/sunset.jpg
new file mode 100644
index 00000000..7d7307be
Binary files /dev/null and b/hugolib/testdata/sunset.jpg differ