| 1 |
// Copyright 2026 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugio_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/common/hugio" |
| 20 |
"github.com/gohugoio/hugo/hugolib" |
| 21 |
) |
| 22 |
|
| 23 |
func TestTrimBOM(t *testing.T) { |
| 24 |
files := ` |
| 25 |
-- hugo.toml -- |
| 26 |
baseURL = "http://example.org/" |
| 27 |
-- i18n/en.yaml -- |
| 28 |
hello: "Hello" |
| 29 |
-- data/mydata.yaml -- |
| 30 |
foo: bar |
| 31 |
-- assets/mycss.css -- |
| 32 |
body { background: #fff; } |
| 33 |
-- assets/myjs.js -- |
| 34 |
console.log("Hello, world!"); |
| 35 |
-- assets/myyaml.yaml -- |
| 36 |
foo: bar |
| 37 |
-- content/page.md -- |
| 38 |
--- |
| 39 |
title: "Page with BOM" |
| 40 |
--- |
| 41 |
-- content/mysection/_content.gotmpl -- |
| 42 |
{{ .AddPage (dict "path" "p2" "title" "p2") }} |
| 43 |
-- layouts/all.html -- |
| 44 |
Title: {{ .Title }}| |
| 45 |
Unmarshal resource to map: {{ resources.Get "myyaml.yaml" | transform.Unmarshal }}| |
| 46 |
Unmarshal resource.Content to map: {{ (resources.Get "myyaml.yaml").Content | transform.Unmarshal }}| |
| 47 |
{{ $myyamlFronOsReadFile := os.ReadFile "assets/myyaml.yaml" }} |
| 48 |
Unmarshal os.ReadFile result to map: {{ $myyamlFronOsReadFile | transform.Unmarshal }}| |
| 49 |
Data: {{ .Site.Data.mydata.foo }}| |
| 50 |
I18n: {{ i18n "hello" }}| |
| 51 |
CSS1: {{ resources.Get "mycss.css" | minify | resources.Publish }}| |
| 52 |
CSS2: {{ resources.Get "mycss.css" | css.Build | resources.Publish }}| |
| 53 |
JS: {{ resources.Get "myjs.js" | js.Build | resources.Publish }}| |
| 54 |
|
| 55 |
` |
| 56 |
b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) { |
| 57 |
c.FileContentPrefix = hugio.BOM |
| 58 |
c.NeedsOsFS = true |
| 59 |
})) |
| 60 |
|
| 61 |
// No BOM in the published text files. |
| 62 |
b.AssertStringPublished("! " + hugio.BOM) |
| 63 |
|
| 64 |
b.AssertFileContent( |
| 65 |
"public/page/index.html", |
| 66 |
"Title: Page with BOM|", |
| 67 |
"Unmarshal resource to map: map[foo:bar]|", |
| 68 |
"Unmarshal resource.Content to map: map[foo:bar]|", |
| 69 |
"Unmarshal os.ReadFile result to map: map[foo:bar]|", |
| 70 |
"I18n: Hello|", |
| 71 |
) |
| 72 |
b.AssertFileContent("public/mysection/p2/index.html", "Title: p2|") |
| 73 |
} |