github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/resource_transformers/tocss/dartsass/integration_test.go (about)

     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  	"github.com/bep/logg"
    21  	qt "github.com/frankban/quicktest"
    22  	"github.com/gohugoio/hugo/hugolib"
    23  	"github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
    24  )
    25  
    26  func TestTransformIncludePaths(t *testing.T) {
    27  	t.Parallel()
    28  	if !dartsass.Supports() {
    29  		t.Skip()
    30  	}
    31  
    32  	files := `
    33  -- assets/scss/main.scss --
    34  @import "moo";
    35  -- node_modules/foo/_moo.scss --
    36  $moolor: #fff;
    37  
    38  moo {
    39    color: $moolor;
    40  }
    41  -- config.toml --
    42  -- layouts/index.html --
    43  {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") "transpiler" "dartsass" ) }}
    44  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts  | minify  }}
    45  T1: {{ $r.Content }}
    46  	`
    47  
    48  	b := hugolib.NewIntegrationTestBuilder(
    49  		hugolib.IntegrationTestConfig{
    50  			T:           t,
    51  			TxtarString: files,
    52  			NeedsOsFS:   true,
    53  		}).Build()
    54  
    55  	b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`)
    56  }
    57  
    58  func TestTransformImportRegularCSS(t *testing.T) {
    59  	t.Parallel()
    60  	if !dartsass.Supports() {
    61  		t.Skip()
    62  	}
    63  
    64  	files := `
    65  -- assets/scss/_moo.scss --
    66  $moolor: #fff;
    67  
    68  moo {
    69  	color: $moolor;
    70  }
    71  -- assets/scss/another.css --
    72  
    73  -- assets/scss/main.scss --
    74  @import "moo";
    75  @import "regular.css";
    76  @import "moo";
    77  @import "another.css";
    78  
    79  /* foo */
    80  -- assets/scss/regular.css --
    81  
    82  -- config.toml --
    83  -- layouts/index.html --
    84  {{ $r := resources.Get "scss/main.scss" |  toCSS (dict "transpiler" "dartsass")  }}
    85  T1: {{ $r.Content | safeHTML }}
    86  
    87  	`
    88  
    89  	b := hugolib.NewIntegrationTestBuilder(
    90  		hugolib.IntegrationTestConfig{
    91  			T:           t,
    92  			TxtarString: files,
    93  			NeedsOsFS:   true,
    94  		},
    95  	).Build()
    96  
    97  	// Dart Sass does not follow regular CSS import, but they
    98  	// get pulled to the top.
    99  	b.AssertFileContent("public/index.html", `T1: @import "regular.css";
   100  		@import "another.css";
   101  		moo {
   102  		  color: #fff;
   103  		}
   104  
   105  		moo {
   106  		  color: #fff;
   107  		}
   108  
   109  		/* foo */`)
   110  }
   111  
   112  func TestTransformImportIndentedSASS(t *testing.T) {
   113  	t.Parallel()
   114  	if !dartsass.Supports() {
   115  		t.Skip()
   116  	}
   117  
   118  	files := `
   119  -- assets/scss/_moo.sass --
   120  #main
   121  	color: blue
   122  -- assets/scss/main.scss --
   123  @import "moo";
   124  
   125  /* foo */
   126  -- config.toml --
   127  -- layouts/index.html --
   128  {{ $r := resources.Get "scss/main.scss" |  toCSS (dict "transpiler" "dartsass")  }}
   129  T1: {{ $r.Content | safeHTML }}
   130  
   131  	`
   132  
   133  	b := hugolib.NewIntegrationTestBuilder(
   134  		hugolib.IntegrationTestConfig{
   135  			T:           t,
   136  			TxtarString: files,
   137  			NeedsOsFS:   true,
   138  		},
   139  	).Build()
   140  
   141  	b.AssertFileContent("public/index.html", "T1: #main {\n  color: blue;\n}\n\n/* foo */")
   142  }
   143  
   144  // Issue 10592
   145  func TestTransformImportMountedCSS(t *testing.T) {
   146  	t.Parallel()
   147  	if !dartsass.Supports() {
   148  		t.Skip()
   149  	}
   150  
   151  	files := `
   152  -- assets/main.scss --
   153  @import "import-this-file.css";
   154  @import "foo/import-this-mounted-file.css";
   155  @import "compile-this-file";
   156  @import "foo/compile-this-mounted-file";
   157  a {color: main-scss;}
   158  -- assets/_compile-this-file.css --
   159  a {color: compile-this-file-css;}
   160  -- assets/_import-this-file.css --
   161  a {color: import-this-file-css;}
   162  -- foo/_compile-this-mounted-file.css --
   163  a {color: compile-this-mounted-file-css;}
   164  -- foo/_import-this-mounted-file.css --
   165  a {color: import-this-mounted-file-css;}
   166  -- layouts/index.html --
   167  {{- $opts := dict "transpiler" "dartsass" }}
   168  {{- with resources.Get "main.scss" | toCSS $opts }}{{ .Content | safeHTML }}{{ end }}
   169  -- config.toml --
   170  disableKinds = ['RSS','sitemap','taxonomy','term','page','section']
   171  
   172  [[module.mounts]]
   173  source = 'assets'
   174  target = 'assets'
   175  
   176  [[module.mounts]]
   177  source = 'foo'
   178  target = 'assets/foo'
   179  	`
   180  	b := hugolib.NewIntegrationTestBuilder(
   181  		hugolib.IntegrationTestConfig{
   182  			T:           t,
   183  			TxtarString: files,
   184  			NeedsOsFS:   true,
   185  		},
   186  	).Build()
   187  
   188  	b.AssertFileContent("public/index.html", `
   189  		@import "import-this-file.css";
   190  		@import "foo/import-this-mounted-file.css";
   191  		a {
   192  			color: compile-this-file-css;
   193  		}
   194  
   195  		a {
   196  			color: compile-this-mounted-file-css;
   197  		}
   198  
   199  		a {
   200  			color: main-scss;
   201  		}
   202  	`)
   203  }
   204  
   205  func TestTransformThemeOverrides(t *testing.T) {
   206  	t.Parallel()
   207  	if !dartsass.Supports() {
   208  		t.Skip()
   209  	}
   210  
   211  	files := `
   212  -- assets/scss/components/_boo.scss --
   213  $boolor: green;
   214  
   215  boo {
   216  	color: $boolor;
   217  }
   218  -- assets/scss/components/_moo.scss --
   219  $moolor: #ccc;
   220  
   221  moo {
   222  	color: $moolor;
   223  }
   224  -- config.toml --
   225  theme = 'mytheme'
   226  -- layouts/index.html --
   227  {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) "transpiler" "dartsass" ) }}
   228  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts  | minify  }}
   229  T1: {{ $r.Content }}
   230  -- themes/mytheme/assets/scss/components/_boo.scss --
   231  $boolor: orange;
   232  
   233  boo {
   234  	color: $boolor;
   235  }
   236  -- themes/mytheme/assets/scss/components/_imports.scss --
   237  @import "moo";
   238  @import "_boo";
   239  @import "_zoo";
   240  -- themes/mytheme/assets/scss/components/_moo.scss --
   241  $moolor: #fff;
   242  
   243  moo {
   244  	color: $moolor;
   245  }
   246  -- themes/mytheme/assets/scss/components/_zoo.scss --
   247  $zoolor: pink;
   248  
   249  zoo {
   250  	color: $zoolor;
   251  }
   252  -- themes/mytheme/assets/scss/main.scss --
   253  @import "components/imports";
   254  	`
   255  
   256  	b := hugolib.NewIntegrationTestBuilder(
   257  		hugolib.IntegrationTestConfig{
   258  			T:           t,
   259  			TxtarString: files,
   260  			NeedsOsFS:   true,
   261  		},
   262  	).Build()
   263  
   264  	b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`)
   265  }
   266  
   267  func TestTransformLogging(t *testing.T) {
   268  	t.Parallel()
   269  	if !dartsass.Supports() {
   270  		t.Skip()
   271  	}
   272  
   273  	files := `
   274  -- assets/scss/main.scss --
   275  @warn "foo";
   276  @debug "bar";
   277  
   278  -- config.toml --
   279  disableKinds = ["term", "taxonomy", "section", "page"]
   280  -- layouts/index.html --
   281  {{ $cssOpts := (dict  "transpiler" "dartsass" ) }}
   282  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts   }}
   283  T1: {{ $r.Content }}
   284  	`
   285  
   286  	b := hugolib.NewIntegrationTestBuilder(
   287  		hugolib.IntegrationTestConfig{
   288  			T:           t,
   289  			TxtarString: files,
   290  			NeedsOsFS:   true,
   291  			LogLevel:    logg.LevelInfo,
   292  		}).Build()
   293  
   294  	b.AssertLogMatches(`Dart Sass: foo`)
   295  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:1:0: bar`)
   296  
   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  -- config.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/index.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.NewIntegrationTestBuilder(
   337  			hugolib.IntegrationTestConfig{
   338  				T:           c,
   339  				TxtarString: strings.Replace(filesTemplate, "$maincolor: #eee;", "$maincolor #eee;", 1),
   340  				NeedsOsFS:   true,
   341  			}).BuildE()
   342  
   343  		b.Assert(err, qt.IsNotNil)
   344  		b.Assert(err.Error(), qt.Contains, `main.scss:8:13":`)
   345  		b.Assert(err.Error(), qt.Contains, `: expected ":".`)
   346  		fe := b.AssertIsFileError(err)
   347  		b.Assert(fe.ErrorContext(), qt.IsNotNil)
   348  		b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"  $maincolor #eee;", "", "body {", "\tcolor: $maincolor;", "}"})
   349  		b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss")
   350  
   351  	})
   352  
   353  	c.Run("error in import", func(c *qt.C) {
   354  		b, err := hugolib.NewIntegrationTestBuilder(
   355  			hugolib.IntegrationTestConfig{
   356  				T:           c,
   357  				TxtarString: strings.Replace(filesTemplate, "$foocolor: #ccc;", "$foocolor #ccc;", 1),
   358  				NeedsOsFS:   true,
   359  			}).BuildE()
   360  
   361  		b.Assert(err, qt.IsNotNil)
   362  		b.Assert(err.Error(), qt.Contains, `_foo.scss:2:10":`)
   363  		b.Assert(err.Error(), qt.Contains, `: expected ":".`)
   364  		fe := b.AssertIsFileError(err)
   365  		b.Assert(fe.ErrorContext(), qt.IsNotNil)
   366  		b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 1 */", "$foocolor #ccc;", "", "foo {"})
   367  		b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss")
   368  
   369  	})
   370  
   371  }
   372  
   373  func TestOptionVars(t *testing.T) {
   374  	t.Parallel()
   375  	if !dartsass.Supports() {
   376  		t.Skip()
   377  	}
   378  
   379  	files := `
   380  -- assets/scss/main.scss --
   381  @use "hugo:vars";
   382  
   383  body {
   384  	body {
   385  		background: url(vars.$image) no-repeat center/cover;
   386  		font-family: vars.$font;
   387  	  }
   388  }
   389  
   390  p {
   391  	color: vars.$color1;
   392  	font-size: vars.$font_size;
   393  }
   394  
   395  b {
   396  	color: vars.$color2;
   397  }
   398  -- layouts/index.html --
   399  {{ $image := "images/hero.jpg" }}
   400  {{ $font := "Hugo's New Roman" }}
   401  {{ $vars := dict "$color1" "blue" "$color2" "green" "font_size" "24px" "image" $image "font" $font }}
   402  {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
   403  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts }}
   404  T1: {{ $r.Content }}
   405  	`
   406  
   407  	b := hugolib.NewIntegrationTestBuilder(
   408  		hugolib.IntegrationTestConfig{
   409  			T:           t,
   410  			TxtarString: files,
   411  			NeedsOsFS:   true,
   412  		}).Build()
   413  
   414  	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}`)
   415  }
   416  
   417  func TestOptionVarsParams(t *testing.T) {
   418  	t.Parallel()
   419  	if !dartsass.Supports() {
   420  		t.Skip()
   421  	}
   422  
   423  	files := `
   424  -- config.toml --
   425  [params]
   426  [params.sassvars]
   427  color1 = "blue"
   428  color2 = "green"
   429  font_size = "24px"
   430  image = "images/hero.jpg"
   431  -- assets/scss/main.scss --
   432  @use "hugo:vars";
   433  
   434  body {
   435  	body {
   436  		background: url(vars.$image) no-repeat center/cover;
   437  	  }
   438  }
   439  
   440  p {
   441  	color: vars.$color1;
   442  	font-size: vars.$font_size;
   443  }
   444  
   445  b {
   446  	color: vars.$color2;
   447  }
   448  -- layouts/index.html --
   449  {{ $vars := site.Params.sassvars}}
   450  {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
   451  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts }}
   452  T1: {{ $r.Content }}
   453  	`
   454  
   455  	b := hugolib.NewIntegrationTestBuilder(
   456  		hugolib.IntegrationTestConfig{
   457  			T:           t,
   458  			TxtarString: files,
   459  			NeedsOsFS:   true,
   460  		}).Build()
   461  
   462  	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}`)
   463  }
   464  
   465  func TestVarsCasting(t *testing.T) {
   466  	t.Parallel()
   467  	if !dartsass.Supports() {
   468  		t.Skip()
   469  	}
   470  
   471  	files := `
   472  -- config.toml --
   473  disableKinds = ["term", "taxonomy", "section", "page"]
   474  
   475  [params]
   476  [params.sassvars]
   477  color_hex = "#fff"
   478  color_rgb = "rgb(255, 255, 255)"
   479  color_hsl = "hsl(0, 0%, 100%)"
   480  dimension = "24px"
   481  percentage = "10%"
   482  flex = "5fr"
   483  name = "Hugo"
   484  url = "https://gohugo.io"
   485  integer = 32
   486  float = 3.14
   487  -- assets/scss/main.scss --
   488  @use "hugo:vars";
   489  @use "sass:meta";
   490  
   491  @debug meta.type-of(vars.$color_hex);
   492  @debug meta.type-of(vars.$color_rgb);
   493  @debug meta.type-of(vars.$color_hsl);
   494  @debug meta.type-of(vars.$dimension);
   495  @debug meta.type-of(vars.$percentage);
   496  @debug meta.type-of(vars.$flex);
   497  @debug meta.type-of(vars.$name);
   498  @debug meta.type-of(vars.$url);
   499  @debug meta.type-of(vars.$not_a_number);
   500  @debug meta.type-of(vars.$integer);
   501  @debug meta.type-of(vars.$float);
   502  @debug meta.type-of(vars.$a_number);
   503  -- layouts/index.html --
   504  {{ $vars := site.Params.sassvars}}
   505  {{ $vars = merge $vars (dict "not_a_number" ("32xxx" | css.Quoted) "a_number" ("234" | css.Unquoted) )}}
   506  {{ $cssOpts := (dict "transpiler" "dartsass" "vars" $vars ) }}
   507  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts }}
   508  T1: {{ $r.Content }}
   509  		`
   510  
   511  	b := hugolib.NewIntegrationTestBuilder(
   512  		hugolib.IntegrationTestConfig{
   513  			T:           t,
   514  			TxtarString: files,
   515  			NeedsOsFS:   true,
   516  			LogLevel:    logg.LevelInfo,
   517  		}).Build()
   518  
   519  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:3:0: color`)
   520  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:4:0: color`)
   521  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:5:0: color`)
   522  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:6:0: number`)
   523  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:7:0: number`)
   524  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:8:0: number`)
   525  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:9:0: string`)
   526  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:10:0: string`)
   527  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:11:0: string`)
   528  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:12:0: number`)
   529  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:13:0: number`)
   530  	b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:14:0: number`)
   531  
   532  }