Debian Codegraph @

resources/resource_transformers/tocss/scss/scss_integration_test.go

raw
1 // Copyright 2024 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 scss_test
15
16 import (
17 "os"
18 "path/filepath"
19 "runtime"
20 "strings"
21 "testing"
22
23 qt "github.com/frankban/quicktest"
24
25 "github.com/gohugoio/hugo/common/herrors"
26 "github.com/gohugoio/hugo/htesting"
27 "github.com/gohugoio/hugo/hugolib"
28 "github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
29 )
30
31 func TestTransformIncludePaths(t *testing.T) {
32 t.Parallel()
33 if !scss.Supports() {
34 t.Skip()
35 }
36 c := qt.New(t)
37
38 files := `
39 -- assets/scss/main.scss --
40 @import "moo";
41 -- node_modules/foo/_moo.scss --
42 @import "sub/bar";
43 $moolor: $barcolor;
44
45 moo {
46 color: $moolor;
47 }
48 -- node_modules/foo/sub/_bar.scss --
49 $barcolor: #fff;
50 -- hugo.toml --
51 -- layouts/home.html --
52 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") ) }}
53 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
54 T1: {{ $r.Content }}
55 `
56
57 b := hugolib.Test(c, files, hugolib.TestOptOsFs())
58
59 b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`)
60 }
61
62 func TestTransformImportRegularCSS(t *testing.T) {
63 t.Parallel()
64 if !scss.Supports() {
65 t.Skip()
66 }
67
68 c := qt.New(t)
69
70 files := `
71 -- assets/scss/_moo.scss --
72 $moolor: #fff;
73
74 moo {
75 color: $moolor;
76 }
77 -- assets/scss/another.css --
78
79 -- assets/scss/main.scss --
80 @import "moo";
81 @import "regular.css";
82 @import "moo";
83 @import "another.css";
84
85 /* foo */
86 -- assets/scss/regular.css --
87
88 -- hugo.toml --
89 -- layouts/home.html --
90 {{ $r := resources.Get "scss/main.scss" | toCSS }}
91 T1: {{ $r.Content | safeHTML }}
92
93 `
94
95 b := hugolib.Test(c, files, hugolib.TestOptOsFs())
96
97 // LibSass does not support regular CSS imports. There
98 // is an open bug about it that probably will never be resolved.
99 // Hugo works around this by preserving them in place:
100 b.AssertFileContent("public/index.html", `
101 T1: moo {
102 color: #fff; }
103
104 @import "regular.css";
105 moo {
106 color: #fff; }
107
108 @import "another.css";
109 /* foo */
110
111 `)
112 }
113
114 func TestTransformThemeOverrides(t *testing.T) {
115 t.Parallel()
116 if !scss.Supports() {
117 t.Skip()
118 }
119
120 c := qt.New(t)
121
122 files := `
123 -- assets/scss/components/_boo.scss --
124 $boolor: green;
125
126 boo {
127 color: $boolor;
128 }
129 -- assets/scss/components/_moo.scss --
130 $moolor: #ccc;
131
132 moo {
133 color: $moolor;
134 }
135 -- hugo.toml --
136 theme = 'mytheme'
137 -- layouts/home.html --
138 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) ) }}
139 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
140 T1: {{ $r.Content }}
141 -- themes/mytheme/assets/scss/components/_boo.scss --
142 $boolor: orange;
143
144 boo {
145 color: $boolor;
146 }
147 -- themes/mytheme/assets/scss/components/_imports.scss --
148 @import "moo";
149 @import "_boo";
150 @import "_zoo";
151 -- themes/mytheme/assets/scss/components/_moo.scss --
152 $moolor: #fff;
153
154 moo {
155 color: $moolor;
156 }
157 -- themes/mytheme/assets/scss/components/_zoo.scss --
158 $zoolor: pink;
159
160 zoo {
161 color: $zoolor;
162 }
163 -- themes/mytheme/assets/scss/main.scss --
164 @import "components/imports";
165 `
166
167 b := hugolib.Test(c, files, hugolib.TestOptOsFs())
168
169 b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`)
170 }
171
172 func TestTransformErrors(t *testing.T) {
173 t.Parallel()
174 if !scss.Supports() {
175 t.Skip()
176 }
177
178 c := qt.New(t)
179
180 const filesTemplate = `
181 -- hugo.toml --
182 theme = 'mytheme'
183 -- assets/scss/components/_foo.scss --
184 /* comment line 1 */
185 $foocolor: #ccc;
186
187 foo {
188 color: $foocolor;
189 }
190 -- themes/mytheme/assets/scss/main.scss --
191 /* comment line 1 */
192 /* comment line 2 */
193 @import "components/foo";
194 /* comment line 4 */
195
196 $maincolor: #eee;
197
198 body {
199 color: $maincolor;
200 }
201
202 -- layouts/home.html --
203 {{ $cssOpts := dict }}
204 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
205 T1: {{ $r.Content }}
206
207 `
208
209 c.Run("error in main", func(c *qt.C) {
210 b, err := hugolib.TestE(c, strings.Replace(filesTemplate, "$maincolor: #eee;", "$maincolor #eee;", 1), hugolib.TestOptOsFs())
211
212 b.Assert(err, qt.IsNotNil)
213 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`themes/mytheme/assets/scss/main.scss:6:1": expected ':' after $maincolor in assignment statement`))
214 ferrs := herrors.UnwrapFileErrors(err)
215 c.Assert(len(ferrs), qt.Equals, 2)
216 fe := ferrs[1]
217 b.Assert(fe.ErrorContext(), qt.IsNotNil)
218 b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 4 */", "", "$maincolor #eee;", "", "body {"})
219 b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss")
220 })
221
222 c.Run("error in import", func(c *qt.C) {
223 b, err := hugolib.TestE(c, strings.Replace(filesTemplate, "$foocolor: #ccc;", "$foocolor #ccc;", 1), hugolib.TestOptOsFs())
224
225 b.Assert(err, qt.IsNotNil)
226 b.Assert(err.Error(), qt.Contains, `assets/scss/components/_foo.scss:2:1": expected ':' after $foocolor in assignment statement`)
227 ferrs := herrors.UnwrapFileErrors(err)
228 c.Assert(len(ferrs), qt.Equals, 2)
229 fe := ferrs[1]
230 b.Assert(fe.ErrorContext(), qt.IsNotNil)
231 b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 1 */", "$foocolor #ccc;", "", "foo {"})
232 b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss")
233 })
234 }
235
236 func TestOptionVars(t *testing.T) {
237 t.Parallel()
238 if !scss.Supports() {
239 t.Skip()
240 }
241
242 files := `
243 -- assets/scss/main.scss --
244 @import "hugo:vars";
245
246 body {
247 body {
248 background: url($image) no-repeat center/cover;
249 font-family: $font;
250 }
251 }
252
253 p {
254 color: $color1;
255 font-size: var$font_size;
256 }
257
258 b {
259 color: $color2;
260 }
261 -- layouts/home.html --
262 {{ $image := "images/hero.jpg" }}
263 {{ $font := "Hugo's New Roman" }}
264 {{ $vars := dict "$color1" "blue" "$color2" "green" "font_size" "24px" "image" $image "font" $font }}
265 {{ $cssOpts := (dict "transpiler" "libsass" "outputStyle" "compressed" "vars" $vars ) }}
266 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
267 T1: {{ $r.Content }}
268 `
269
270 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
271
272 b.AssertFileContent("public/index.html", `T1: body body{background:url(images/hero.jpg) no-repeat center/cover;font-family:Hugo's New Roman}p{color:blue;font-size:var 24px}b{color:green}`)
273 }
274
275 // Note: This test is more or less duplicated in both of the SCSS packages (libsass and dartsass).
276 func TestBootstrap(t *testing.T) {
277 t.Parallel()
278 if !scss.Supports() {
279 t.Skip()
280 }
281 if !htesting.IsCI() {
282 t.Skip("skip (slow) test in non-CI environment")
283 }
284
285 files := `
286 -- hugo.toml --
287 disableKinds = ["term", "taxonomy", "section", "page"]
288 [module]
289 [[module.imports]]
290 path="github.com/gohugoio/hugo-mod-bootstrap-scss/v5"
291 -- go.mod --
292 module github.com/gohugoio/tests/testHugoModules
293 -- assets/scss/main.scss --
294 @import "bootstrap/bootstrap";
295 -- layouts/home.html --
296 {{ $cssOpts := (dict "transpiler" "libsass" ) }}
297 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
298 Styles: {{ $r.RelPermalink }}
299 `
300
301 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
302
303 b.AssertFileContent("public/index.html", "Styles: /scss/main.css")
304 }
305
306 // Issue #1239.
307 func TestRebuildAssetGetMatch(t *testing.T) {
308 t.Parallel()
309 if !scss.Supports() {
310 t.Skip()
311 }
312
313 files := `
314 -- assets/scss/main.scss --
315 b {
316 color: red;
317 }
318 -- layouts/home.html --
319 {{ $r := resources.GetMatch "scss/main.scss" | toCSS }}
320 T1: {{ $r.Content }}
321 `
322
323 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptRunning())
324
325 b.AssertFileContent("public/index.html", `color: red`)
326
327 b.EditFiles("assets/scss/main.scss", `b { color: blue; }`).Build()
328
329 b.AssertFileContent("public/index.html", `color: blue`)
330 }
331
332 func TestRebuildAssetMatchIssue12456(t *testing.T) {
333 t.Parallel()
334 if !scss.Supports() {
335 t.Skip()
336 }
337
338 files := `
339 -- hugo.toml --
340 disableKinds = ["term", "taxonomy", "section", "page"]
341 disableLiveReload = true
342 -- assets/a.scss --
343 h1 {
344 color: red;
345 }
346 -- assets/dir/b.scss --
347 h2 {
348 color: blue;
349 }
350 -- assets/dir/c.scss --
351 h3 {
352 color: green;
353 }
354 -- layouts/home.html --
355 {{ $a := slice (resources.Get "a.scss") }}
356 {{ $b := resources.Match "dir/*.scss" }}
357
358 {{/* Add styles in a specific order. */}}
359 {{ $styles := slice $a $b }}
360
361 {{ $stylesheets := slice }}
362 {{ range $styles }}
363 {{ $stylesheets = $stylesheets | collections.Append . }}
364 {{ end }}
365
366
367 {{ range $stylesheets }}
368 {{ with . | css.Sass | fingerprint }}
369 <link as="style" href="{{ .RelPermalink }}" rel="preload stylesheet">
370 {{ end }}
371 {{ end }}
372 `
373
374 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptRunning())
375
376 b.AssertFileContent("public/index.html", `b.60a9f3bdc189ee8a857afd5b7e1b93ad1644de0873761a7c9bc84f781a821942.css`)
377
378 b.EditFiles("assets/dir/b.scss", `h2 { color: orange; }`).Build()
379
380 b.AssertFileContent("public/index.html", `b.46b2d77c7ffe37ee191678f72df991ecb1319f849957151654362f09b0ef467f.css`)
381 }
382
383 // Issue 12851
384 func TestDirectoryIndexes(t *testing.T) {
385 t.Parallel()
386 if !scss.Supports() {
387 t.Skip()
388 }
389
390 files := `
391 -- hugo.toml --
392 disableKinds = ['page','section','rss','sitemap','taxonomy','term']
393
394 [[module.mounts]]
395 source = 'assets'
396 target = 'assets'
397 [[module.mounts]]
398 source = "miscellaneous/sass"
399 target = "assets/sass"
400 -- layouts/home.html --
401 {{ $opts := dict "transpiler" "libsass" "outputStyle" "compressed" }}
402 {{ (resources.Get "sass/main.scss" | toCSS $opts).Content }}
403 -- assets/sass/main.scss --
404 @import "foo1"; // directory with _index file from OS file system
405 @import "bar1"; // directory with _index file from module mount
406 @import "foo2"; // directory with index file from OS file system
407 @import "bar2"; // directory with index file from module mount
408 -- assets/sass/foo1/_index.scss --
409 .foo1 {color: red;}
410 -- miscellaneous/sass/bar1/_index.scss --
411 .bar1 {color: blue;}
412 -- assets/sass/foo2/index.scss --
413 .foo2 {color: red;}
414 -- miscellaneous/sass/bar2/index.scss --
415 .bar2 {color: blue;}
416 `
417
418 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
419
420 b.AssertFileContent("public/index.html", ".foo1{color:red}.bar1{color:blue}.foo2{color:red}.bar2{color:blue}")
421 }
422
423 func TestLibsassDeprecatedIssue14261(t *testing.T) {
424 // This cannot be run in parallel because of global state in log deprecation handling.
425 if !scss.Supports() {
426 t.Skip()
427 }
428
429 files := `
430 -- assets/scss/main.scss --
431 body {
432 background-color: #fff;
433 }
434 -- hugo.toml --
435 -- layouts/home.html --
436 {{ $cssOpts := (dict "transpiler" "libsass") }}
437 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
438 {{ $r.RelPermalink }}
439 `
440
441 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptInfo())
442
443 b.AssertLogContains("deprecated: css.Sass: libsass was deprecated in Hugo v0.153.0")
444 }
445
446 // Imports not found in Hugo's file systems used to be resolved by the Sass compiler
447 // itself, following symlinks and with no notion of the project boundary.
448 func TestTransformImportOutsideProject(t *testing.T) {
449 if !scss.Supports() {
450 t.Skip()
451 }
452 c := qt.New(t)
453
454 base := t.TempDir()
455 secret := filepath.Join(base, "secret.scss")
456 c.Assert(os.WriteFile(secret, []byte(`.pwn { content: "PWN-SECRET"; }`), 0o644), qt.IsNil)
457
458 files := `
459 -- hugo.toml --
460 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "section", "page"]
461 theme = "mytheme"
462 SECURITY
463 -- themes/mytheme/hugo.toml --
464 -- themes/mytheme/assets/css/main.scss --
465 @import "IMPORT";
466 -- layouts/all.html --
467 {{ $css := resources.Get "css/main.scss" | css.Sass (dict "transpiler" "libsass") }}
468 RESULT:<pre>{{ $css.Content }}</pre>
469 `
470
471 build := func(c *qt.C, name, imp, security string) (*hugolib.IntegrationTestBuilder, error) {
472 workingDir := filepath.Join(base, name)
473 if runtime.GOOS != "windows" {
474 linkDir := filepath.Join(workingDir, "themes", "mytheme", "assets", "css")
475 c.Assert(os.MkdirAll(linkDir, 0o755), qt.IsNil)
476 c.Assert(os.Symlink(secret, filepath.Join(linkDir, "link.scss")), qt.IsNil)
477 }
478 f := strings.ReplaceAll(files, "IMPORT", imp)
479 f = strings.ReplaceAll(f, "SECURITY", security)
480 return hugolib.NewIntegrationTestBuilder(
481 hugolib.IntegrationTestConfig{
482 T: c,
483 TxtarString: f,
484 NeedsOsFS: true,
485 WorkingDir: workingDir,
486 },
487 ).BuildE()
488 }
489
490 const outsideErr = `.*"[^"]*\.scss" resolves to "[^"]*secret.scss" outside the project.*allowRead.*`
491
492 c.Run("Relative import", func(c *qt.C) {
493 _, err := build(c, "relative", "../../../../../secret", "")
494 c.Assert(err, qt.ErrorMatches, outsideErr)
495 })
496
497 c.Run("Symlink in theme", func(c *qt.C) {
498 if runtime.GOOS == "windows" {
499 c.Skip("symlinks")
500 }
501 _, err := build(c, "symlink", "link", "")
502 c.Assert(err, qt.ErrorMatches, outsideErr)
503 })
504
505 c.Run("Absolute import", func(c *qt.C) {
506 _, err := build(c, "absolute", filepath.ToSlash(filepath.Join(base, "secret")), "")
507 c.Assert(err, qt.ErrorMatches, outsideErr)
508 })
509
510 c.Run("Relative import, target in allowRead", func(c *qt.C) {
511 b, err := build(c, "allowed", "../../../../../secret", `
512 [security.node.permissions]
513 allowRead = [".", ".."]
514 `)
515 c.Assert(err, qt.IsNil)
516 b.AssertFileContent("public/index.html", "PWN-SECRET")
517 })
518 }