From 0e87b18b66d2c8ba9e2abc429630cb03f5b093d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 23 Apr 2017 22:03:25 +0200 Subject: [PATCH] hugolib: Fix handling of zero-length files This was a regression in Hugo 0.20. This commit makes sure that zeron-length files are not rendered to file. Fixes #3355 --- hugolib/site.go | 4 ++++ hugolib/site_render.go | 4 ++++ hugolib/site_test.go | 14 ++++++++++++++ hugolib/testhelpers_test.go | 6 ++++++ 4 files changed, 28 insertions(+) diff --git a/hugolib/site.go b/hugolib/site.go index f2ebee4ccab..394549c4176 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1939,6 +1939,10 @@ func (s *Site) renderAndWritePage(name string, dest string, p *PageOutput, layou return nil } + if renderBuffer.Len() == 0 { + return nil + } + outBuffer := bp.GetBuffer() defer bp.PutBuffer(outBuffer) diff --git a/hugolib/site_render.go b/hugolib/site_render.go index d5b16975dcb..a82c93137f7 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -340,6 +340,10 @@ func (s *Site) renderRobotsTXT() error { return nil } + if outBuffer.Len() == 0 { + return nil + } + return s.publish("robots.txt", outBuffer) } diff --git a/hugolib/site_test.go b/hugolib/site_test.go index e18456bf900..1df3c81c793 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -376,6 +376,20 @@ func TestNewSiteDefaultLang(t *testing.T) { require.Equal(t, hugofs.Os, s.Fs.Destination) } +// Issue #3355 +func TestShouldNotWriteZeroLengthFilesToDestination(t *testing.T) { + cfg, fs := newTestCfg() + + writeSource(t, fs, filepath.Join("content", "simple.html"), "simple") + writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}") + writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "") + + s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) + th := testHelper{s.Cfg, s.Fs, t} + + th.assertFileNotExist(filepath.Join("public", "index.html")) +} + // Issue #1176 func TestSectionNaming(t *testing.T) { t.Parallel() diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index cb529204a4d..f82399eef4b 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -61,6 +61,12 @@ func (th testHelper) assertFileContentRegexp(filename string, matches ...string) } } +func (th testHelper) assertFileNotExist(filename string) { + exists, err := helpers.Exists(filename, th.Fs.Destination) + require.NoError(th.T, err) + require.False(th.T, exists) +} + func (th testHelper) replaceDefaultContentLanguageValue(value string) string { defaultInSubDir := th.Cfg.GetBool("defaultContentLanguageInSubDir") replace := th.Cfg.GetString("defaultContentLanguage") + "/"