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