Debian Codegraph @

resources/resource_transformers/tocss/dartsass/dartsass_integration_test.go

raw
1 // Copyright 2021 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 dartsass_test
15
16 import (
17 "os"
18 "path/filepath"
19 "runtime"
20 "strings"
21 "testing"
22
23 qt "github.com/frankban/quicktest"
24 "github.com/gohugoio/hugo/common/herrors"
25 "github.com/gohugoio/hugo/htesting"
26 "github.com/gohugoio/hugo/hugolib"
27 "github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
28 )
29
30 func TestTransformIncludePaths(t *testing.T) {
31 t.Parallel()
32 if !dartsass.Supports() {
33 t.Skip()
34 }
35
36 files := `
37 -- assets/scss/main.scss --
38 @import "moo";
39 -- node_modules/foo/_moo.scss --
40 @import "sub/bar";
41 $moolor: $barcolor;
42
43 moo {
44 color: $moolor;
45 }
46 -- node_modules/foo/sub/_bar.scss --
47 $barcolor: #fff;
48 -- hugo.toml --
49 -- layouts/home.html --
50 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") "transpiler" "dartsass" ) }}
51 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
52 T1: {{ $r.Content }}
53 `
54
55 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
56
57 b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`)
58 }
59
60 // See issue 15103.
61 func TestTransformImportContext(t *testing.T) {
62 t.Parallel()
63 if !dartsass.Supports() {
64 t.Skip()
65 }
66
67 files := `
68 -- hugo.toml --
69 -- assets/scss/_foo.scss --
70 body { color: orange; }
71 -- assets/scss/main.scss --
72 @import "foo";
73 @import "bar";
74 -- layouts/home.html --
75 {{ $foo := resources.FromString "foo.scss" "body { color: blue; }" }}
76 {{ $bar := resources.FromString "bar.scss" "@import \"baz\";\nbody { color: green; }" }}
77 {{ $baz := resources.FromString "baz.scss" "p { color: red; }" }}
78 {{ $opts := dict "transpiler" "dartsass" "outputStyle" "compressed" "importContext" (slice $foo $bar $baz) }}
79 {{ $r := resources.Get "scss/main.scss" | css.Sass $opts }}
80 T1: {{ $r.Content }}
81 `
82
83 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
84
85 // foo resolves in the import context before the assets filesystem.
86 b.AssertFileContent("public/index.html", `T1: body{color:blue}p{color:red}body{color:green}`)
87 }
88
89 func TestTransformImportRegularCSS(t *testing.T) {
90 t.Parallel()
91 if !dartsass.Supports() {
92 t.Skip()
93 }
94
95 files := `
96 -- assets/scss/_moo.scss --
97 $moolor: #fff;
98
99 moo {
100 color: $moolor;
101 }
102 -- assets/scss/another.css --
103
104 -- assets/scss/main.scss --
105 @import "moo";
106 @import "regular.css";
107 @import "moo";
108 @import "another.css";
109
110 /* foo */
111 -- assets/scss/regular.css --
112
113 -- hugo.toml --
114 -- layouts/home.html --
115 {{ $r := resources.Get "scss/main.scss" | toCSS (dict "transpiler" "dartsass") }}
116 T1: {{ $r.Content | safeHTML }}
117
118 `
119
120 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
121
122 // Dart Sass does not follow regular CSS import, but they
123 // get pulled to the top.
124 b.AssertFileContent("public/index.html", `T1: @import "regular.css";
125 @import "another.css";
126 moo {
127 color: #fff;
128 }
129
130 moo {
131 color: #fff;
132 }
133
134 /* foo */`)
135 }
136
137 func TestTransformImportIndentedSASS(t *testing.T) {
138 t.Parallel()
139 if !dartsass.Supports() {
140 t.Skip()
141 }
142
143 files := `
144 -- assets/scss/_moo.sass --
145 #main
146 color: blue
147 -- assets/scss/main.scss --
148 @import "moo";
149
150 /* foo */
151 -- hugo.toml --
152 -- layouts/home.html --
153 {{ $r := resources.Get "scss/main.scss" | toCSS (dict "transpiler" "dartsass") }}
154 T1: {{ $r.Content | safeHTML }}
155
156 `
157
158 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
159
160 b.AssertFileContent("public/index.html", "T1: #main {\n color: blue;\n}\n\n/* foo */")
161 }
162
163 // Issue 10592
164 func TestTransformImportMountedCSS(t *testing.T) {
165 t.Parallel()
166 if !dartsass.Supports() {
167 t.Skip()
168 }
169
170 files := `
171 -- assets/main.scss --
172 @import "import-this-file.css";
173 @import "foo/import-this-mounted-file.css";
174 @import "compile-this-file";
175 @import "foo/compile-this-mounted-file";
176 a {color: main-scss;}
177 -- assets/_compile-this-file.css --
178 a {color: compile-this-file-css;}
179 -- assets/_import-this-file.css --
180 a {color: import-this-file-css;}
181 -- foo/_compile-this-mounted-file.css --
182 a {color: compile-this-mounted-file-css;}
183 -- foo/_import-this-mounted-file.css --
184 a {color: import-this-mounted-file-css;}
185 -- layouts/home.html --
186 {{- $opts := dict "transpiler" "dartsass" }}
187 {{- with resources.Get "main.scss" | toCSS $opts }}{{ .Content | safeHTML }}{{ end }}
188 -- hugo.toml --
189 disableKinds = ['RSS','sitemap','taxonomy','term','page','section']
190
191 [[module.mounts]]
192 source = 'assets'
193 target = 'assets'
194
195 [[module.mounts]]
196 source = 'foo'
197 target = 'assets/foo'
198 `
199 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
200
201 b.AssertFileContent("public/index.html", `
202 @import "import-this-file.css";
203 @import "foo/import-this-mounted-file.css";
204 a {
205 color: compile-this-file-css;
206 }
207
208 a {
209 color: compile-this-mounted-file-css;
210 }
211
212 a {
213 color: main-scss;
214 }
215 `)
216 }
217
218 func TestTransformThemeOverrides(t *testing.T) {
219 t.Parallel()
220 if !dartsass.Supports() {
221 t.Skip()
222 }
223
224 files := `
225 -- assets/scss/components/_boo.scss --
226 $boolor: green;
227
228 boo {
229 color: $boolor;
230 }
231 -- assets/scss/components/_moo.scss --
232 $moolor: #ccc;
233
234 moo {
235 color: $moolor;
236 }
237 -- hugo.toml --
238 theme = 'mytheme'
239 -- layouts/home.html --
240 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) "transpiler" "dartsass" ) }}
241 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
242 T1: {{ $r.Content }}
243 -- themes/mytheme/assets/scss/components/_boo.scss --
244 $boolor: orange;
245
246 boo {
247 color: $boolor;
248 }
249 -- themes/mytheme/assets/scss/components/_imports.scss --
250 @import "moo";
251 @import "_boo";
252 @import "_zoo";
253 -- themes/mytheme/assets/scss/components/_moo.scss --
254 $moolor: #fff;
255
256 moo {
257 color: $moolor;
258 }
259 -- themes/mytheme/assets/scss/components/_zoo.scss --
260 $zoolor: pink;
261
262 zoo {
263 color: $zoolor;
264 }
265 -- themes/mytheme/assets/scss/main.scss --
266 @import "components/imports";
267 `
268
269 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
270
271 b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`)
272 }
273
274 func TestTransformLogging(t *testing.T) {
275 t.Parallel()
276 if !dartsass.Supports() {
277 t.Skip()
278 }
279
280 files := `
281 -- assets/scss/main.scss --
282 @warn "foo";
283 @debug "bar";
284
285 -- hugo.toml --
286 disableKinds = ["term", "taxonomy", "section", "page"]
287 -- layouts/home.html --
288 {{ $cssOpts := (dict "transpiler" "dartsass" ) }}
289 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
290 T1: {{ $r.Content }}
291 `
292
293 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptInfo())
294
295 b.AssertLogMatches(`Dart Sass: foo`)
296 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:1:0: bar`)
297 }
298
299 func TestTransformErrors(t *testing.T) {
300 t.Parallel()
301 if !dartsass.Supports() {
302 t.Skip()
303 }
304
305 c := qt.New(t)
306
307 const filesTemplate = `
308 -- hugo.toml --
309 -- assets/scss/components/_foo.scss --
310 /* comment line 1 */
311 $foocolor: #ccc;
312
313 foo {
314 color: $foocolor;
315 }
316 -- assets/scss/main.scss --
317 /* comment line 1 */
318 /* comment line 2 */
319 @import "components/foo";
320 /* comment line 4 */
321
322 $maincolor: #eee;
323
324 body {
325 color: $maincolor;
326 }
327
328 -- layouts/home.html --
329 {{ $cssOpts := dict "transpiler" "dartsass" }}
330 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
331 T1: {{ $r.Content }}
332
333 `
334
335 c.Run("error in main", func(c *qt.C) {
336 b, err := hugolib.TestE(c, strings.Replace(filesTemplate, "$maincolor: #eee;", "$maincolor #eee;", 1), hugolib.TestOptOsFs())
337
338 b.Assert(err, qt.IsNotNil)
339 b.Assert(err.Error(), qt.Contains, `main.scss:8:13":`)
340 b.Assert(err.Error(), qt.Contains, `: expected ":".`)
341 fileErrs := herrors.UnwrapFileErrors(err)
342 b.Assert(len(fileErrs), qt.Equals, 2) // template + scss.
343
344 templErr := fileErrs[0]
345 b.Assert(templErr.ErrorContext(), qt.IsNotNil)
346 b.Assert(templErr.ErrorContext().Lines, qt.DeepEquals, []string{"{{ $cssOpts := dict \"transpiler\" \"dartsass\" }}", "{{ $r := resources.Get \"scss/main.scss\" | toCSS $cssOpts | minify }}", "T1: {{ $r.Content }}", "", "\t"})
347 b.Assert(templErr.ErrorContext().ChromaLexer, qt.Equals, "go-html-template")
348
349 scssErr := fileErrs[1]
350 b.Assert(scssErr.ErrorContext(), qt.IsNotNil)
351 b.Assert(scssErr.ErrorContext().Lines, qt.DeepEquals, []string{" $maincolor #eee;", "", "body {", "\tcolor: $maincolor;", "}"})
352 b.Assert(scssErr.ErrorContext().ChromaLexer, qt.Equals, "scss")
353 })
354
355 c.Run("error in import", func(c *qt.C) {
356 b, err := hugolib.TestE(c, strings.Replace(filesTemplate, "$foocolor: #ccc;", "$foocolor #ccc;", 1), hugolib.TestOptOsFs())
357
358 b.Assert(err, qt.IsNotNil)
359 b.Assert(err.Error(), qt.Contains, `_foo.scss:2:10":`)
360 b.Assert(err.Error(), qt.Contains, `: expected ":".`)
361 fileErrs := herrors.UnwrapFileErrors(err)
362 b.Assert(len(fileErrs), qt.Equals, 2) // template + scss.
363 scssErr := fileErrs[1]
364 b.Assert(scssErr.ErrorContext(), qt.IsNotNil)
365 b.Assert(scssErr.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 1 */", "$foocolor #ccc;", "", "foo {"})
366 b.Assert(scssErr.ErrorContext().ChromaLexer, qt.Equals, "scss")
367 })
368 }
369
370 func TestOptionVars(t *testing.T) {
371 t.Parallel()
372 if !dartsass.Supports() {
373 t.Skip()
374 }
375
376 files := `
377 -- assets/scss/main.scss --
378 @use "hugo:vars";
379
380 body {
381 body {
382 background: url(vars.$image) no-repeat center/cover;
383 font-family: vars.$font;
384 }
385 }
386
387 p {
388 color: vars.$color1;
389 font-size: vars.$font_size;
390 }
391
392 b {
393 color: vars.$color2;
394 }
395 -- layouts/home.html --
396 {{ $image := "images/hero.jpg" }}
397 {{ $font := "Hugo's New Roman" }}
398 {{ $vars := dict "$color1" "blue" "$color2" "green" "font_size" "24px" "image" $image "font" $font }}
399 {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
400 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
401 T1: {{ $r.Content }}
402 `
403
404 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
405
406 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:24px}b{color:green}`)
407 }
408
409 func TestOptionVarsNestedIssue14705(t *testing.T) {
410 t.Parallel()
411 if !dartsass.Supports() {
412 t.Skip()
413 }
414
415 files := `
416 -- assets/scss/main.scss --
417 @use "hugo:vars";
418 @use "hugo:vars/mobile" as mobile;
419
420 body {
421 color: vars.$color1;
422 font-size: vars.$font_size;
423 }
424
425 @media (max-width: 650px) {
426 body {
427 color: mobile.$color1;
428 font-size: mobile.$font_size;
429 }
430 }
431 -- layouts/home.html --
432 {{ $vars := dict
433 "color1" "blue"
434 "font_size" "16px"
435 "mobile" (dict "color1" "red" "font_size" "12px")
436 }}
437 {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
438 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
439 T1: {{ $r.Content }}
440 `
441
442 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
443
444 b.AssertFileContent("public/index.html", `T1: body{color:blue;font-size:16px}@media(max-width: 650px){body{color:red;font-size:12px}}`)
445 }
446
447 func TestOptionVarsNestedFromParamsIssue14705(t *testing.T) {
448 t.Parallel()
449 if !dartsass.Supports() {
450 t.Skip()
451 }
452
453 files := `
454 -- hugo.toml --
455 [params]
456 [params.sassvars]
457 color1 = "blue"
458 font_size = "16px"
459 [params.sassvars.mobile]
460 color1 = "red"
461 font_size = "12px"
462 -- assets/scss/main.scss --
463 @use "hugo:vars";
464 @use "hugo:vars/mobile" as mobile;
465
466 body {
467 color: vars.$color1;
468 font-size: vars.$font_size;
469 }
470
471 @media (max-width: 650px) {
472 body {
473 color: mobile.$color1;
474 font-size: mobile.$font_size;
475 }
476 }
477 -- layouts/home.html --
478 {{ $vars := site.Params.sassvars }}
479 {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
480 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
481 T1: {{ $r.Content }}
482 `
483
484 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
485
486 b.AssertFileContent("public/index.html", `T1: body{color:blue;font-size:16px}@media(max-width: 650px){body{color:red;font-size:12px}}`)
487 }
488
489 func TestOptionVarsParams(t *testing.T) {
490 t.Parallel()
491 if !dartsass.Supports() {
492 t.Skip()
493 }
494
495 files := `
496 -- hugo.toml --
497 [params]
498 [params.sassvars]
499 color1 = "blue"
500 color2 = "green"
501 font_size = "24px"
502 image = "images/hero.jpg"
503 -- assets/scss/main.scss --
504 @use "hugo:vars";
505
506 body {
507 body {
508 background: url(vars.$image) no-repeat center/cover;
509 }
510 }
511
512 p {
513 color: vars.$color1;
514 font-size: vars.$font_size;
515 }
516
517 b {
518 color: vars.$color2;
519 }
520 -- layouts/home.html --
521 {{ $vars := site.Params.sassvars}}
522 {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
523 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
524 T1: {{ $r.Content }}
525 `
526
527 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
528
529 b.AssertFileContent("public/index.html", `T1: body body{background:url(images/hero.jpg) no-repeat center/cover}p{color:blue;font-size:24px}b{color:green}`)
530 }
531
532 func TestVarsCasting(t *testing.T) {
533 t.Parallel()
534 if !dartsass.Supports() {
535 t.Skip()
536 }
537
538 files := `
539 -- hugo.toml --
540 disableKinds = ["term", "taxonomy", "section", "page"]
541
542 [params]
543 [params.sassvars]
544 color_hex = "#fff"
545 color_rgb = "rgb(255, 255, 255)"
546 color_hsl = "hsl(0, 0%, 100%)"
547 dimension = "24px"
548 percentage = "10%"
549 flex = "5fr"
550 name = "Hugo"
551 url = "https://gohugo.io"
552 integer = 32
553 float = 3.14
554 -- assets/scss/main.scss --
555 @use "hugo:vars";
556 @use "sass:meta";
557
558 @debug meta.type-of(vars.$color_hex);
559 @debug meta.type-of(vars.$color_rgb);
560 @debug meta.type-of(vars.$color_hsl);
561 @debug meta.type-of(vars.$dimension);
562 @debug meta.type-of(vars.$percentage);
563 @debug meta.type-of(vars.$flex);
564 @debug meta.type-of(vars.$name);
565 @debug meta.type-of(vars.$url);
566 @debug meta.type-of(vars.$not_a_number);
567 @debug meta.type-of(vars.$integer);
568 @debug meta.type-of(vars.$float);
569 @debug meta.type-of(vars.$a_number);
570 -- layouts/home.html --
571 {{ $vars := site.Params.sassvars}}
572 {{ $vars = merge $vars (dict "not_a_number" ("32xxx" | css.Quoted) "a_number" ("234" | css.Unquoted) )}}
573 {{ $cssOpts := (dict "transpiler" "dartsass" "vars" $vars ) }}
574 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
575 T1: {{ $r.Content }}
576 `
577
578 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptInfo())
579
580 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:3:0: color`)
581 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:4:0: color`)
582 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:5:0: color`)
583 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:6:0: number`)
584 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:7:0: number`)
585 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:8:0: number`)
586 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:9:0: string`)
587 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:10:0: string`)
588 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:11:0: string`)
589 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:12:0: number`)
590 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:13:0: number`)
591 b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:14:0: number`)
592 }
593
594 // Note: This test is more or less duplicated in both of the SCSS packages (libsass and dartsass).
595 func TestBootstrap(t *testing.T) {
596 t.Parallel()
597 if !dartsass.Supports() {
598 t.Skip()
599 }
600 if !htesting.IsCI() {
601 t.Skip("skip (slow) test in non-CI environment")
602 }
603
604 files := `
605 -- hugo.toml --
606 disableKinds = ["term", "taxonomy", "section", "page"]
607 [module]
608 [[module.imports]]
609 path="github.com/gohugoio/hugo-mod-bootstrap-scss/v5"
610 -- go.mod --
611 module github.com/gohugoio/tests/testHugoModules
612 -- assets/scss/main.scss --
613 @import "bootstrap/bootstrap";
614 -- layouts/home.html --
615 {{ $cssOpts := (dict "transpiler" "dartsass" ) }}
616 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
617 Styles: {{ $r.RelPermalink }}
618 `
619
620 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
621
622 b.AssertFileContent("public/index.html", "Styles: /scss/main.css")
623 }
624
625 // Issue 12849
626 func TestDirectoryIndexes(t *testing.T) {
627 t.Parallel()
628 if !dartsass.Supports() {
629 t.Skip()
630 }
631
632 files := `
633 -- hugo.toml --
634 disableKinds = ['page','section','rss','sitemap','taxonomy','term']
635
636 [[module.mounts]]
637 source = 'assets'
638 target = 'assets'
639 [[module.mounts]]
640 source = "miscellaneous/sass"
641 target = "assets/sass"
642 -- layouts/home.html --
643 {{ $opts := dict "transpiler" "dartsass" "outputStyle" "compressed" }}
644 {{ (resources.Get "sass/main.scss" | toCSS $opts).Content }}
645 -- assets/sass/main.scss --
646 @use "foo1"; // directory with _index file from OS file system
647 @use "bar1"; // directory with _index file from module mount
648 @use "foo2"; // directory with index file from OS file system
649 @use "bar2"; // directory with index file from module mount
650 -- assets/sass/foo1/_index.scss --
651 .foo1 {color: red;}
652 -- miscellaneous/sass/bar1/_index.scss --
653 .bar1 {color: blue;}
654 -- assets/sass/foo2/index.scss --
655 .foo2 {color: red;}
656 -- miscellaneous/sass/bar2/index.scss --
657 .bar2 {color: blue;}
658 `
659
660 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
661
662 b.AssertFileContent("public/index.html", ".foo1{color:red}.bar1{color:blue}.foo2{color:red}.bar2{color:blue}")
663 }
664
665 func TestIgnoreDeprecationWarnings(t *testing.T) {
666 t.Parallel()
667 if !dartsass.Supports() {
668 t.Skip()
669 }
670
671 files := `
672 -- hugo.toml --
673 disableKinds = ['page','section','rss','sitemap','taxonomy','term']
674 -- assets/scss/main.scss --
675 @import "moo";
676 -- node_modules/foo/_moo.scss --
677 $moolor: #fff;
678
679 moo {
680 color: $moolor;
681 }
682 -- hugo.toml --
683 -- layouts/home.html --
684 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") "transpiler" "dartsass" ) }}
685 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
686 T1: {{ $r.Content }}
687 `
688
689 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWarn())
690 b.AssertLogContains("Dart Sass: DEPRECATED [import]")
691 b.AssertFileContent("public/index.html", `moo{color:#fff}`)
692
693 files = strings.ReplaceAll(files, `"transpiler" "dartsass"`, `"transpiler" "dartsass" "silenceDeprecations" (slice "import")`)
694
695 b = hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWarn())
696 b.AssertLogContains("! Dart Sass: DEPRECATED [import]")
697 b.AssertFileContent("public/index.html", `moo{color:#fff}`)
698 }
699
700 func TestSilenceDependencyDeprecations(t *testing.T) {
701 t.Parallel()
702 if !dartsass.Supports() {
703 t.Skip()
704 }
705
706 files := `
707 -- hugo.toml --
708 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
709 -- layouts/home.html --
710 {{ $opts := dict
711 "transpiler" "dartsass"
712 "outputStyle" "compressed"
713 "includePaths" (slice "node_modules")
714 KVPAIR
715 }}
716 {{ (resources.Get "sass/main.scss" | css.Sass $opts).Content }}
717 -- assets/sass/main.scss --
718 @use "sass:color";
719 @use "foo/deprecated.scss";
720 h3 { color: rgb(color.channel(#ccc, "red", $space: rgb), 0, 0); }
721 // COMMENT
722 -- node_modules/foo/deprecated.scss --
723 @use "sass:color";
724 h1 { color: rgb(color.channel(#eee, "red", $space: rgb), 0, 0); }
725 h2 { color: rgb(color.red(#ddd), 0, 0); } // deprecated
726 `
727
728 expectedCSS := "h1{color:#e00}h2{color:#d00}h3{color:#c00}"
729
730 // Do not silence dependency deprecation warnings (default).
731 f := strings.ReplaceAll(files, "KVPAIR", "")
732 b := hugolib.Test(t, f, hugolib.TestOptWarn(), hugolib.TestOptOsFs())
733 b.AssertFileContent("public/index.html", expectedCSS)
734 b.AssertLogContains(
735 "WARN Dart Sass: DEPRECATED [color-functions]",
736 "color.red() is deprecated",
737 )
738
739 // Do not silence dependency deprecation warnings (explicit).
740 f = strings.ReplaceAll(files, "KVPAIR", `"silenceDependencyDeprecations" false`)
741 b = hugolib.Test(t, f, hugolib.TestOptWarn(), hugolib.TestOptOsFs())
742 b.AssertFileContent("public/index.html", expectedCSS)
743 b.AssertLogContains(
744 "WARN Dart Sass: DEPRECATED [color-functions]",
745 "color.red() is deprecated",
746 )
747
748 // Silence dependency deprecation warnings.
749 f = strings.ReplaceAll(files, "KVPAIR", `"silenceDependencyDeprecations" true`)
750 b = hugolib.Test(t, f, hugolib.TestOptWarn(), hugolib.TestOptOsFs())
751 b.AssertFileContent("public/index.html", expectedCSS)
752 b.AssertLogContains("! WARN")
753
754 // Make sure that we are not silencing non-dependency deprecation warnings.
755 f = strings.ReplaceAll(files, "KVPAIR", `"silenceDependencyDeprecations" true`)
756 f = strings.ReplaceAll(f, "// COMMENT", "h4 { color: rgb(0, color.green(#bbb), 0); }")
757 b = hugolib.Test(t, f, hugolib.TestOptWarn(), hugolib.TestOptOsFs())
758 b.AssertFileContent("public/index.html", expectedCSS+"h4{color:#0b0}")
759 b.AssertLogContains(
760 "WARN Dart Sass: DEPRECATED [color-functions]",
761 "color.green() is deprecated",
762 )
763 }
764
765 // Imports not found in Hugo's file systems used to be resolved by the Sass compiler
766 // itself, following symlinks and with no notion of the project boundary.
767 func TestTransformImportOutsideProject(t *testing.T) {
768 if !dartsass.Supports() {
769 t.Skip()
770 }
771 c := qt.New(t)
772
773 base := t.TempDir()
774 secret := filepath.Join(base, "secret.scss")
775 c.Assert(os.WriteFile(secret, []byte(`.pwn { content: "PWN-SECRET"; }`), 0o644), qt.IsNil)
776
777 files := `
778 -- hugo.toml --
779 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "section", "page"]
780 theme = "mytheme"
781 SECURITY
782 -- themes/mytheme/hugo.toml --
783 -- themes/mytheme/assets/css/main.scss --
784 @import "IMPORT";
785 -- layouts/all.html --
786 {{ $css := resources.Get "css/main.scss" | css.Sass (dict "transpiler" "dartsass") }}
787 RESULT:<pre>{{ $css.Content }}</pre>
788 `
789
790 build := func(c *qt.C, name, imp, security string) (*hugolib.IntegrationTestBuilder, error) {
791 workingDir := filepath.Join(base, name)
792 if runtime.GOOS != "windows" {
793 linkDir := filepath.Join(workingDir, "themes", "mytheme", "assets", "css")
794 c.Assert(os.MkdirAll(linkDir, 0o755), qt.IsNil)
795 c.Assert(os.Symlink(secret, filepath.Join(linkDir, "link.scss")), qt.IsNil)
796 }
797 f := strings.ReplaceAll(files, "IMPORT", imp)
798 f = strings.ReplaceAll(f, "SECURITY", security)
799 return hugolib.NewIntegrationTestBuilder(
800 hugolib.IntegrationTestConfig{
801 T: c,
802 TxtarString: f,
803 NeedsOsFS: true,
804 WorkingDir: workingDir,
805 },
806 ).BuildE()
807 }
808
809 const outsideErr = `.*"[^"]*\.scss" resolves to "[^"]*secret.scss" outside the project.*allowRead.*`
810
811 c.Run("Relative import", func(c *qt.C) {
812 _, err := build(c, "relative", "../../../../../secret", "")
813 c.Assert(err, qt.ErrorMatches, outsideErr)
814 })
815
816 c.Run("Symlink in theme", func(c *qt.C) {
817 if runtime.GOOS == "windows" {
818 c.Skip("symlinks")
819 }
820 _, err := build(c, "symlink", "link", "")
821 c.Assert(err, qt.ErrorMatches, outsideErr)
822 })
823
824 c.Run("Absolute import", func(c *qt.C) {
825 _, err := build(c, "absolute", filepath.ToSlash(filepath.Join(base, "secret")), "")
826 c.Assert(err, qt.ErrorMatches, outsideErr)
827 })
828
829 c.Run("Relative import, target in allowRead", func(c *qt.C) {
830 b, err := build(c, "allowed", "../../../../../secret", `
831 [security.node.permissions]
832 allowRead = [".", ".."]
833 `)
834 c.Assert(err, qt.IsNil)
835 b.AssertFileContent("public/index.html", "PWN-SECRET")
836 })
837 }