From 0f440460c861d3576579bf4074069b4045e7ddce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 1 Jun 2026 15:32:41 +0200 Subject: [PATCH] tpl/tplimpl/embedded: Prevent leading newline in sitemap template Fixes #14977 --- hugolib/integrationtest_builder.go | 10 ++++++++++ hugolib/sitemap_test.go | 5 ++++- tpl/tplimpl/embedded/templates/sitemap.xml | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/hugolib/integrationtest_builder.go b/hugolib/integrationtest_builder.go index d83a5d640..c840a5eec 100644 --- a/hugolib/integrationtest_builder.go +++ b/hugolib/integrationtest_builder.go @@ -429,6 +429,16 @@ func (s *IntegrationTestBuilder) negate(match string) (string, bool) { return match, negate } +// AssertFileContentStartsWith asserts that the content of the given file starts with s. +func (s *IntegrationTestBuilder) AssertFileContentStartsWith(filename, prefix string) { + s.Helper() + content := strings.TrimSpace(s.FileContent(filename)) + cm := qt.Commentf("File: %s Expect:\n%s Got:\n%s\nWith Space Visuals:\n%s", filename, prefix, content, util.VisualizeSpaces([]byte(content))) + var negate bool + prefix, negate = s.negate(prefix) + s.Assert(strings.HasPrefix(content, prefix), qt.Equals, !negate, cm) +} + func (s *IntegrationTestBuilder) AssertFileContent(filename string, matches ...string) { s.Helper() content := strings.TrimSpace(s.FileContent(filename)) diff --git a/hugolib/sitemap_test.go b/hugolib/sitemap_test.go index 5f536ecf2..c2062caa7 100644 --- a/hugolib/sitemap_test.go +++ b/hugolib/sitemap_test.go @@ -42,7 +42,10 @@ Doc2 b := Test(t, files) - b.AssertFileContent("public/sitemap.xml", " https://example.com/sect/doc1/", "doc2") + b.AssertFileContentStartsWith("public/sitemap.xml", `https://example.com/sect/doc1/", "doc2", + ) } func TestSitemapMultilingual(t *testing.T) { diff --git a/tpl/tplimpl/embedded/templates/sitemap.xml b/tpl/tplimpl/embedded/templates/sitemap.xml index 2e873abe7..a1bc86310 100644 --- a/tpl/tplimpl/embedded/templates/sitemap.xml +++ b/tpl/tplimpl/embedded/templates/sitemap.xml @@ -1,4 +1,4 @@ -{{/* gotmplfmt-ignore-all */}} +{{/* gotmplfmt-ignore-all */ -}} {{ printf "" | safeHTML }}