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