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