| 1 |
// Copyright 2022s 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 resources_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
"github.com/gohugoio/hugo/hugolib" |
| 21 |
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass" |
| 22 |
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss" |
| 23 |
) |
| 24 |
|
| 25 |
func TestCopy(t *testing.T) { |
| 26 |
t.Parallel() |
| 27 |
|
| 28 |
files := ` |
| 29 |
-- hugo.toml -- |
| 30 |
baseURL = "http://example.com/blog" |
| 31 |
-- assets/images/pixel.png -- |
| 32 |
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== |
| 33 |
-- layouts/home.html -- |
| 34 |
{{/* Image resources */}} |
| 35 |
{{ $img := resources.Get "images/pixel.png" }} |
| 36 |
{{ $imgCopy1 := $img | resources.Copy "images/copy.png" }} |
| 37 |
{{ $imgCopy1 = $imgCopy1.Resize "3x4"}} |
| 38 |
{{ $imgCopy2 := $imgCopy1 | resources.Copy "images/copy2.png" }} |
| 39 |
{{ $imgCopy3 := $imgCopy1 | resources.Copy "images/copy3.png" }} |
| 40 |
Image Orig: {{ $img.RelPermalink}}|{{ $img.MediaType }}|{{ $img.Width }}|{{ $img.Height }}| |
| 41 |
Image Copy1: {{ $imgCopy1.RelPermalink}}|{{ $imgCopy1.MediaType }}|{{ $imgCopy1.Width }}|{{ $imgCopy1.Height }}| |
| 42 |
Image Copy2: {{ $imgCopy2.RelPermalink}}|{{ $imgCopy2.MediaType }}|{{ $imgCopy2.Width }}|{{ $imgCopy2.Height }}| |
| 43 |
Image Copy3: {{ $imgCopy3.MediaType }}|{{ $imgCopy3.Width }}|{{ $imgCopy3.Height }}| |
| 44 |
|
| 45 |
{{/* Generic resources */}} |
| 46 |
{{ $targetPath := "js/vars.js" }} |
| 47 |
{{ $orig := "let foo;" | resources.FromString "js/foo.js" }} |
| 48 |
{{ $copy1 := $orig | resources.Copy "js/copies/bar.js" }} |
| 49 |
{{ $copy2 := $orig | resources.Copy "js/copies/baz.js" | fingerprint "md5" }} |
| 50 |
{{ $copy3 := $copy2 | resources.Copy "js/copies/moo.js" | minify }} |
| 51 |
|
| 52 |
Orig: {{ $orig.RelPermalink}}|{{ $orig.MediaType }}|{{ $orig.Content | safeJS }}| |
| 53 |
Copy1: {{ $copy1.RelPermalink}}|{{ $copy1.MediaType }}|{{ $copy1.Content | safeJS }}| |
| 54 |
Copy2: {{ $copy2.RelPermalink}}|{{ $copy2.MediaType }}|{{ $copy2.Content | safeJS }}| |
| 55 |
Copy3: {{ $copy3.RelPermalink}}|{{ $copy3.MediaType }}|{{ $copy3.Content | safeJS }}| |
| 56 |
|
| 57 |
` |
| 58 |
|
| 59 |
b := hugolib.Test(t, files) |
| 60 |
|
| 61 |
b.AssertFileContent("public/index.html", ` |
| 62 |
Image Orig: /blog/images/pixel.png|image/png|1|1| |
| 63 |
Image Copy1: /blog/images/copy_hu_e592d810de530dee.png|image/png|3|4| |
| 64 |
Image Copy2: /blog/images/copy2.png|image/png|3|4| |
| 65 |
Image Copy3: image/png|3|4| |
| 66 |
Orig: /blog/js/foo.js|text/javascript|let foo;| |
| 67 |
Copy1: /blog/js/copies/bar.js|text/javascript|let foo;| |
| 68 |
Copy2: /blog/js/copies/baz.a677329fc6c4ad947e0c7116d91f37a2.js|text/javascript|let foo;| |
| 69 |
Copy3: /blog/js/copies/moo.a677329fc6c4ad947e0c7116d91f37a2.min.js|text/javascript|let foo| |
| 70 |
`) |
| 71 |
|
| 72 |
b.AssertFileExists("public/images/copy2.png", true) |
| 73 |
// No permalink used. |
| 74 |
b.AssertFileExists("public/images/copy3.png", false) |
| 75 |
} |
| 76 |
|
| 77 |
func TestCopyPageShouldFail(t *testing.T) { |
| 78 |
t.Parallel() |
| 79 |
|
| 80 |
files := ` |
| 81 |
-- hugo.toml -- |
| 82 |
-- layouts/home.html -- |
| 83 |
{{/* This is currently not supported. */}} |
| 84 |
{{ $copy := .Copy "copy.md" }} |
| 85 |
|
| 86 |
` |
| 87 |
|
| 88 |
b, err := hugolib.TestE(t, files) |
| 89 |
|
| 90 |
b.Assert(err, qt.IsNotNil) |
| 91 |
} |
| 92 |
|
| 93 |
func TestGet(t *testing.T) { |
| 94 |
t.Parallel() |
| 95 |
|
| 96 |
files := ` |
| 97 |
-- hugo.toml -- |
| 98 |
baseURL = "http://example.com/blog" |
| 99 |
-- assets/images/pixel.png -- |
| 100 |
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== |
| 101 |
-- layouts/home.html -- |
| 102 |
{{ with resources.Get "images/pixel.png" }}Image OK{{ else }}Image not found{{ end }} |
| 103 |
{{ with resources.Get "" }}Failed{{ else }}Empty string not found{{ end }} |
| 104 |
|
| 105 |
|
| 106 |
` |
| 107 |
|
| 108 |
b := hugolib.Test(t, files) |
| 109 |
|
| 110 |
b.AssertFileContent("public/index.html", ` |
| 111 |
Image OK |
| 112 |
Empty string not found |
| 113 |
|
| 114 |
`) |
| 115 |
} |
| 116 |
|
| 117 |
func TestResourcesGettersShouldNotNormalizePermalinks(t *testing.T) { |
| 118 |
t.Parallel() |
| 119 |
|
| 120 |
files := ` |
| 121 |
-- hugo.toml -- |
| 122 |
baseURL = "http://example.com/" |
| 123 |
-- assets/401K Prospectus.txt -- |
| 124 |
Prospectus. |
| 125 |
-- layouts/home.html -- |
| 126 |
{{ $name := "401K Prospectus.txt" }} |
| 127 |
Get: {{ with resources.Get $name }}{{ .RelPermalink }}|{{ .Permalink }}|{{ end }} |
| 128 |
GetMatch: {{ with resources.GetMatch $name }}{{ .RelPermalink }}|{{ .Permalink }}|{{ end }} |
| 129 |
Match: {{ with (index (resources.Match $name) 0) }}{{ .RelPermalink }}|{{ .Permalink }}|{{ end }} |
| 130 |
ByType: {{ with (index (resources.ByType "text") 0) }}{{ .RelPermalink }}|{{ .Permalink }}|{{ end }} |
| 131 |
` |
| 132 |
|
| 133 |
b := hugolib.Test(t, files) |
| 134 |
|
| 135 |
b.AssertFileContent("public/index.html", ` |
| 136 |
Get: /401K%20Prospectus.txt|http://example.com/401K%20Prospectus.txt| |
| 137 |
GetMatch: /401K%20Prospectus.txt|http://example.com/401K%20Prospectus.txt| |
| 138 |
Match: /401K%20Prospectus.txt|http://example.com/401K%20Prospectus.txt| |
| 139 |
ByType: /401K%20Prospectus.txt|http://example.com/401K%20Prospectus.txt| |
| 140 |
|
| 141 |
`) |
| 142 |
} |
| 143 |
|
| 144 |
func TestGlobalResourcesNotPublishedRegressionIssue12190(t *testing.T) { |
| 145 |
t.Parallel() |
| 146 |
|
| 147 |
files := ` |
| 148 |
-- hugo.toml -- |
| 149 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 150 |
-- assets/a.txt -- |
| 151 |
I am a.txt |
| 152 |
-- assets/b.txt -- |
| 153 |
I am b.txt |
| 154 |
-- layouts/home.html -- |
| 155 |
Home. |
| 156 |
{{ with resources.ByType "text" }} |
| 157 |
{{ with .Get "a.txt" }} |
| 158 |
{{ .Publish }} |
| 159 |
{{ end }} |
| 160 |
{{ with .GetMatch "*b*" }} |
| 161 |
{{ .Publish }} |
| 162 |
{{ end }} |
| 163 |
{{ end }} |
| 164 |
` |
| 165 |
|
| 166 |
b := hugolib.Test(t, files) |
| 167 |
|
| 168 |
b.AssertFileExists("public/a.txt", true) // failing test |
| 169 |
b.AssertFileExists("public/b.txt", true) // failing test |
| 170 |
} |
| 171 |
|
| 172 |
func TestGlobalResourcesNotPublishedRegressionIssue12214(t *testing.T) { |
| 173 |
t.Parallel() |
| 174 |
|
| 175 |
files := ` |
| 176 |
-- hugo.toml -- |
| 177 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 178 |
-- assets/files/a.txt -- |
| 179 |
I am a.txt |
| 180 |
-- assets/files/b.txt -- |
| 181 |
I am b.txt |
| 182 |
-- assets/files/c.txt -- |
| 183 |
I am c.txt |
| 184 |
-- assets/files/C.txt -- |
| 185 |
I am C.txt |
| 186 |
-- layouts/home.html -- |
| 187 |
Home. |
| 188 |
{{ with resources.ByType "text" }} |
| 189 |
{{ with .Get "files/a.txt" }} |
| 190 |
{{ .Publish }} |
| 191 |
files/a.txt: {{ .Name }} |
| 192 |
{{ end }} |
| 193 |
{{ with .Get "/files/a.txt" }} |
| 194 |
/files/a.txt: {{ .Name }} |
| 195 |
{{ end }} |
| 196 |
{{ with .GetMatch "files/*b*" }} |
| 197 |
{{ .Publish }} |
| 198 |
files/*b*: {{ .Name }} |
| 199 |
{{ end }} |
| 200 |
{{ with .GetMatch "files/C*" }} |
| 201 |
{{ .Publish }} |
| 202 |
files/C*: {{ .Name }} |
| 203 |
{{ end }} |
| 204 |
{{ with .GetMatch "files/c*" }} |
| 205 |
{{ .Publish }} |
| 206 |
files/c*: {{ .Name }} |
| 207 |
{{ end }} |
| 208 |
{{ with .GetMatch "/files/c*" }} |
| 209 |
/files/c*: {{ .Name }} |
| 210 |
{{ end }} |
| 211 |
{{ with .Match "files/C*" }} |
| 212 |
match files/C*: {{ len . }}| |
| 213 |
{{ end }} |
| 214 |
{{ with .Match "/files/C*" }} |
| 215 |
match /files/C*: {{ len . }}| |
| 216 |
{{ end }} |
| 217 |
{{ end }} |
| 218 |
` |
| 219 |
|
| 220 |
b := hugolib.Test(t, files) |
| 221 |
|
| 222 |
b.AssertFileContent("public/index.html", ` |
| 223 |
files/a.txt: /files/a.txt |
| 224 |
# There are both C.txt and c.txt in the assets, but the Glob matching is case insensitive, so GetMatch returns the first. |
| 225 |
files/C*: /files/C.txt |
| 226 |
files/c*: /files/C.txt |
| 227 |
files/*b*: /files/b.txt |
| 228 |
/files/c*: /files/C.txt |
| 229 |
/files/a.txt: /files/a.txt |
| 230 |
match files/C*: 2| |
| 231 |
match /files/C*: 2| |
| 232 |
`) |
| 233 |
|
| 234 |
b.AssertFileContent("public/files/a.txt", "I am a.txt") |
| 235 |
b.AssertFileContent("public/files/b.txt", "I am b.txt") |
| 236 |
b.AssertFileContent("public/files/C.txt", "I am C.txt") |
| 237 |
} |
| 238 |
|
| 239 |
// Issue #12961 |
| 240 |
func TestDartSassVars(t *testing.T) { |
| 241 |
t.Parallel() |
| 242 |
|
| 243 |
if !scss.Supports() || !dartsass.Supports() { |
| 244 |
t.Skip() |
| 245 |
} |
| 246 |
|
| 247 |
files := ` |
| 248 |
-- hugo.toml -- |
| 249 |
disableKinds = ['page','section','rss','sitemap','taxonomy','term'] |
| 250 |
-- layouts/home.html -- |
| 251 |
{{ $opts := dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" (dict "color" "red") }} |
| 252 |
{{ with resources.Get "dartsass.scss" | css.Sass $opts }} |
| 253 |
{{ .Content }} |
| 254 |
{{ end }} |
| 255 |
|
| 256 |
{{ $opts := dict "transpiler" "libsass" "outputStyle" "compressed" "vars" (dict "color" "blue") }} |
| 257 |
{{ with resources.Get "libsass.scss" | css.Sass $opts }} |
| 258 |
{{ .Content }} |
| 259 |
{{ end }} |
| 260 |
-- assets/dartsass.scss -- |
| 261 |
@use "hugo:vars" as v; |
| 262 |
.dartsass { |
| 263 |
color: v.$color; |
| 264 |
} |
| 265 |
-- assets/libsass.scss -- |
| 266 |
@import "hugo:vars"; |
| 267 |
.libsass { |
| 268 |
color: $color; |
| 269 |
} |
| 270 |
` |
| 271 |
|
| 272 |
b := hugolib.Test(t, files, hugolib.TestOptWarn()) |
| 273 |
|
| 274 |
b.AssertFileContent( |
| 275 |
"public/index.html", |
| 276 |
".dartsass{color:red}", |
| 277 |
".libsass{color:blue}", |
| 278 |
) |
| 279 |
b.AssertLogContains("! WARN Dart Sass: hugo:vars") |
| 280 |
} |
| 281 |
|
| 282 |
// See issue 15208. |
| 283 |
func TestPublish(t *testing.T) { |
| 284 |
t.Parallel() |
| 285 |
|
| 286 |
files := ` |
| 287 |
-- hugo.toml -- |
| 288 |
disableKinds = ["taxonomy", "term", "section", "RSS", "sitemap", "robotsTXT", "404"] |
| 289 |
-- assets/js/main.js -- |
| 290 |
let foo; |
| 291 |
-- layouts/home.html -- |
| 292 |
{{ $r := resources.Get "js/main.js" | minify | resources.Publish }} |
| 293 |
Name: {{ $r.Name }}| |
| 294 |
` |
| 295 |
|
| 296 |
b := hugolib.Test(t, files) |
| 297 |
|
| 298 |
b.AssertFileContent("public/index.html", "Name: /js/main.js|") |
| 299 |
b.AssertFileExists("public/js/main.min.js", true) |
| 300 |
} |
| 301 |
|
| 302 |
// See issue 15086. |
| 303 |
func TestPostProcessDeprecated(t *testing.T) { |
| 304 |
t.Parallel() |
| 305 |
|
| 306 |
files := ` |
| 307 |
-- hugo.toml -- |
| 308 |
disableKinds = ["taxonomy", "term", "section", "RSS", "sitemap", "robotsTXT", "404"] |
| 309 |
-- assets/css/main.css -- |
| 310 |
body { color: red; } |
| 311 |
-- layouts/home.html -- |
| 312 |
{{ $r := resources.Get "css/main.css" | minify | resources.PostProcess }} |
| 313 |
{{ $r.RelPermalink }} |
| 314 |
` |
| 315 |
|
| 316 |
b := hugolib.Test(t, files, hugolib.TestOptInfo()) |
| 317 |
|
| 318 |
b.AssertLogContains("resources.PostProcess was deprecated in Hugo v0.164.0") |
| 319 |
} |