github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/resources/resource_transformers/tocss/scss/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 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/hugolib"
    24  	"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
    25  )
    26  
    27  func TestTransformIncludePaths(t *testing.T) {
    28  	t.Parallel()
    29  	if !scss.Supports() {
    30  		t.Skip()
    31  	}
    32  	c := qt.New(t)
    33  
    34  	files := `
    35  -- assets/scss/main.scss --
    36  @import "moo";
    37  -- node_modules/foo/_moo.scss --
    38  $moolor: #fff;
    39  
    40  moo {
    41    color: $moolor;
    42  }
    43  -- config.toml --
    44  -- layouts/index.html --
    45  {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") ) }}
    46  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts  | minify  }}
    47  T1: {{ $r.Content }}
    48  	`
    49  
    50  	b := hugolib.NewIntegrationTestBuilder(
    51  		hugolib.IntegrationTestConfig{
    52  			T:           c,
    53  			TxtarString: files,
    54  			NeedsOsFS:   true,
    55  		}).Build()
    56  
    57  	b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`)
    58  }
    59  
    60  func TestTransformImportRegularCSS(t *testing.T) {
    61  	t.Parallel()
    62  	if !scss.Supports() {
    63  		t.Skip()
    64  	}
    65  
    66  	c := qt.New(t)
    67  
    68  	files := `
    69  -- assets/scss/_moo.scss --
    70  $moolor: #fff;
    71  
    72  moo {
    73  	color: $moolor;
    74  }
    75  -- assets/scss/another.css --
    76  
    77  -- assets/scss/main.scss --
    78  @import "moo";
    79  @import "regular.css";
    80  @import "moo";
    81  @import "another.css";
    82  
    83  /* foo */
    84  -- assets/scss/regular.css --
    85  
    86  -- config.toml --
    87  -- layouts/index.html --
    88  {{ $r := resources.Get "scss/main.scss" |  toCSS }}
    89  T1: {{ $r.Content | safeHTML }}
    90  
    91  	`
    92  
    93  	b := hugolib.NewIntegrationTestBuilder(
    94  		hugolib.IntegrationTestConfig{
    95  			T:           c,
    96  			TxtarString: files,
    97  			NeedsOsFS:   true,
    98  		}).Build()
    99  
   100  	// LibSass does not support regular CSS imports. There
   101  	// is an open bug about it that probably will never be resolved.
   102  	// Hugo works around this by preserving them in place:
   103  	b.AssertFileContent("public/index.html", `
   104   T1: moo {
   105   color: #fff; }
   106  
   107  @import "regular.css";
   108  moo {
   109   color: #fff; }
   110  
   111  @import "another.css";
   112  /* foo */
   113          
   114  `)
   115  }
   116  
   117  func TestTransformThemeOverrides(t *testing.T) {
   118  	t.Parallel()
   119  	if !scss.Supports() {
   120  		t.Skip()
   121  	}
   122  
   123  	c := qt.New(t)
   124  
   125  	files := `
   126  -- assets/scss/components/_boo.scss --
   127  $boolor: green;
   128  
   129  boo {
   130  	color: $boolor;
   131  }
   132  -- assets/scss/components/_moo.scss --
   133  $moolor: #ccc;
   134  
   135  moo {
   136  	color: $moolor;
   137  }
   138  -- config.toml --
   139  theme = 'mytheme'
   140  -- layouts/index.html --
   141  {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) ) }}
   142  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts  | minify  }}
   143  T1: {{ $r.Content }}
   144  -- themes/mytheme/assets/scss/components/_boo.scss --
   145  $boolor: orange;
   146  
   147  boo {
   148  	color: $boolor;
   149  }
   150  -- themes/mytheme/assets/scss/components/_imports.scss --
   151  @import "moo";
   152  @import "_boo";
   153  @import "_zoo";
   154  -- themes/mytheme/assets/scss/components/_moo.scss --
   155  $moolor: #fff;
   156  
   157  moo {
   158  	color: $moolor;
   159  }
   160  -- themes/mytheme/assets/scss/components/_zoo.scss --
   161  $zoolor: pink;
   162  
   163  zoo {
   164  	color: $zoolor;
   165  }
   166  -- themes/mytheme/assets/scss/main.scss --
   167  @import "components/imports";
   168  	`
   169  
   170  	b := hugolib.NewIntegrationTestBuilder(
   171  		hugolib.IntegrationTestConfig{
   172  			T:           c,
   173  			TxtarString: files,
   174  			NeedsOsFS:   true,
   175  		}).Build()
   176  
   177  	b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`)
   178  }
   179  
   180  func TestTransformErrors(t *testing.T) {
   181  	t.Parallel()
   182  	if !scss.Supports() {
   183  		t.Skip()
   184  	}
   185  
   186  	c := qt.New(t)
   187  
   188  	const filesTemplate = `
   189  -- config.toml --
   190  theme = 'mytheme'
   191  -- assets/scss/components/_foo.scss --
   192  /* comment line 1 */
   193  $foocolor: #ccc;
   194  
   195  foo {
   196  	color: $foocolor;
   197  }
   198  -- themes/mytheme/assets/scss/main.scss --
   199  /* comment line 1 */
   200  /* comment line 2 */
   201  @import "components/foo";
   202  /* comment line 4 */
   203  
   204  $maincolor: #eee;
   205  
   206  body {
   207  	color: $maincolor;
   208  }
   209  
   210  -- layouts/index.html --
   211  {{ $cssOpts := dict }}
   212  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts  | minify  }}
   213  T1: {{ $r.Content }}
   214  
   215  	`
   216  
   217  	c.Run("error in main", func(c *qt.C) {
   218  		b, err := hugolib.NewIntegrationTestBuilder(
   219  			hugolib.IntegrationTestConfig{
   220  				T:           c,
   221  				TxtarString: strings.Replace(filesTemplate, "$maincolor: #eee;", "$maincolor #eee;", 1),
   222  				NeedsOsFS:   true,
   223  			}).BuildE()
   224  
   225  		b.Assert(err, qt.IsNotNil)
   226  		b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`themes/mytheme/assets/scss/main.scss:6:1": expected ':' after $maincolor in assignment statement`))
   227  		fe := b.AssertIsFileError(err)
   228  		b.Assert(fe.ErrorContext(), qt.IsNotNil)
   229  		b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 4 */", "", "$maincolor #eee;", "", "body {"})
   230  		b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss")
   231  
   232  	})
   233  
   234  	c.Run("error in import", func(c *qt.C) {
   235  		b, err := hugolib.NewIntegrationTestBuilder(
   236  			hugolib.IntegrationTestConfig{
   237  				T:           c,
   238  				TxtarString: strings.Replace(filesTemplate, "$foocolor: #ccc;", "$foocolor #ccc;", 1),
   239  				NeedsOsFS:   true,
   240  			}).BuildE()
   241  
   242  		b.Assert(err, qt.IsNotNil)
   243  		b.Assert(err.Error(), qt.Contains, `assets/scss/components/_foo.scss:2:1": expected ':' after $foocolor in assignment statement`)
   244  		fe := b.AssertIsFileError(err)
   245  		b.Assert(fe.ErrorContext(), qt.IsNotNil)
   246  		b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 1 */", "$foocolor #ccc;", "", "foo {"})
   247  		b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss")
   248  
   249  	})
   250  
   251  }
   252  
   253  func TestOptionVars(t *testing.T) {
   254  	t.Parallel()
   255  	if !scss.Supports() {
   256  		t.Skip()
   257  	}
   258  
   259  	files := `
   260  -- assets/scss/main.scss --
   261  @import "hugo:vars";
   262  
   263  body {
   264  	body {
   265  		background: url($image) no-repeat center/cover;
   266  		font-family: $font;
   267  	  }	  
   268  }
   269  
   270  p {
   271  	color: $color1;
   272  	font-size: var$font_size;
   273  }
   274  
   275  b {
   276  	color: $color2;
   277  }
   278  -- layouts/index.html --
   279  {{ $image := "images/hero.jpg" }}
   280  {{ $font := "Hugo's New Roman" }}
   281  {{ $vars := dict "$color1" "blue" "$color2" "green" "font_size" "24px" "image" $image "font" $font }}
   282  {{ $cssOpts := (dict "transpiler" "libsass" "outputStyle" "compressed" "vars" $vars ) }}
   283  {{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts }}
   284  T1: {{ $r.Content }}
   285  	`
   286  
   287  	b := hugolib.NewIntegrationTestBuilder(
   288  		hugolib.IntegrationTestConfig{
   289  			T:           t,
   290  			TxtarString: files,
   291  			NeedsOsFS:   true,
   292  		}).Build()
   293  
   294  	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}`)
   295  }