integration_test.go (6330B)
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/hugolib" 22 "github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass" 23 jww "github.com/spf13/jwalterweatherman" 24 ) 25 26 func TestTransformIncludePaths(t *testing.T) { 27 if !dartsass.Supports() { 28 t.Skip() 29 } 30 31 files := ` 32 -- assets/scss/main.scss -- 33 @import "moo"; 34 -- node_modules/foo/_moo.scss -- 35 $moolor: #fff; 36 37 moo { 38 color: $moolor; 39 } 40 -- config.toml -- 41 -- layouts/index.html -- 42 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") "transpiler" "dartsass" ) }} 43 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }} 44 T1: {{ $r.Content }} 45 ` 46 47 b := hugolib.NewIntegrationTestBuilder( 48 hugolib.IntegrationTestConfig{ 49 T: t, 50 TxtarString: files, 51 NeedsOsFS: true, 52 }).Build() 53 54 b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`) 55 } 56 57 func TestTransformImportRegularCSS(t *testing.T) { 58 if !dartsass.Supports() { 59 t.Skip() 60 } 61 62 files := ` 63 -- assets/scss/_moo.scss -- 64 $moolor: #fff; 65 66 moo { 67 color: $moolor; 68 } 69 -- assets/scss/another.css -- 70 71 -- assets/scss/main.scss -- 72 @import "moo"; 73 @import "regular.css"; 74 @import "moo"; 75 @import "another.css"; 76 77 /* foo */ 78 -- assets/scss/regular.css -- 79 80 -- config.toml -- 81 -- layouts/index.html -- 82 {{ $r := resources.Get "scss/main.scss" | toCSS (dict "transpiler" "dartsass") }} 83 T1: {{ $r.Content | safeHTML }} 84 85 ` 86 87 b := hugolib.NewIntegrationTestBuilder( 88 hugolib.IntegrationTestConfig{ 89 T: t, 90 TxtarString: files, 91 NeedsOsFS: true, 92 }, 93 ).Build() 94 95 // Dart Sass does not follow regular CSS import, but they 96 // get pulled to the top. 97 b.AssertFileContent("public/index.html", `T1: @import "regular.css"; 98 @import "another.css"; 99 moo { 100 color: #fff; 101 } 102 103 moo { 104 color: #fff; 105 } 106 107 /* foo */`) 108 } 109 110 func TestTransformThemeOverrides(t *testing.T) { 111 if !dartsass.Supports() { 112 t.Skip() 113 } 114 115 files := ` 116 -- assets/scss/components/_boo.scss -- 117 $boolor: green; 118 119 boo { 120 color: $boolor; 121 } 122 -- assets/scss/components/_moo.scss -- 123 $moolor: #ccc; 124 125 moo { 126 color: $moolor; 127 } 128 -- config.toml -- 129 theme = 'mytheme' 130 -- layouts/index.html -- 131 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) "transpiler" "dartsass" ) }} 132 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }} 133 T1: {{ $r.Content }} 134 -- themes/mytheme/assets/scss/components/_boo.scss -- 135 $boolor: orange; 136 137 boo { 138 color: $boolor; 139 } 140 -- themes/mytheme/assets/scss/components/_imports.scss -- 141 @import "moo"; 142 @import "_boo"; 143 @import "_zoo"; 144 -- themes/mytheme/assets/scss/components/_moo.scss -- 145 $moolor: #fff; 146 147 moo { 148 color: $moolor; 149 } 150 -- themes/mytheme/assets/scss/components/_zoo.scss -- 151 $zoolor: pink; 152 153 zoo { 154 color: $zoolor; 155 } 156 -- themes/mytheme/assets/scss/main.scss -- 157 @import "components/imports"; 158 ` 159 160 b := hugolib.NewIntegrationTestBuilder( 161 hugolib.IntegrationTestConfig{ 162 T: t, 163 TxtarString: files, 164 NeedsOsFS: true, 165 }, 166 ).Build() 167 168 b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`) 169 } 170 171 func TestTransformLogging(t *testing.T) { 172 if !dartsass.Supports() { 173 t.Skip() 174 } 175 176 files := ` 177 -- assets/scss/main.scss -- 178 @warn "foo"; 179 @debug "bar"; 180 181 -- config.toml -- 182 disableKinds = ["term", "taxonomy", "section", "page"] 183 -- layouts/index.html -- 184 {{ $cssOpts := (dict "transpiler" "dartsass" ) }} 185 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }} 186 T1: {{ $r.Content }} 187 ` 188 189 b := hugolib.NewIntegrationTestBuilder( 190 hugolib.IntegrationTestConfig{ 191 T: t, 192 TxtarString: files, 193 NeedsOsFS: true, 194 LogLevel: jww.LevelInfo, 195 }).Build() 196 197 b.AssertLogMatches(`WARN.*Dart Sass: foo`) 198 b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:1:0: bar`) 199 200 } 201 202 func TestTransformErrors(t *testing.T) { 203 if !dartsass.Supports() { 204 t.Skip() 205 } 206 207 c := qt.New(t) 208 209 const filesTemplate = ` 210 -- config.toml -- 211 -- assets/scss/components/_foo.scss -- 212 /* comment line 1 */ 213 $foocolor: #ccc; 214 215 foo { 216 color: $foocolor; 217 } 218 -- assets/scss/main.scss -- 219 /* comment line 1 */ 220 /* comment line 2 */ 221 @import "components/foo"; 222 /* comment line 4 */ 223 224 $maincolor: #eee; 225 226 body { 227 color: $maincolor; 228 } 229 230 -- layouts/index.html -- 231 {{ $cssOpts := dict "transpiler" "dartsass" }} 232 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }} 233 T1: {{ $r.Content }} 234 235 ` 236 237 c.Run("error in main", func(c *qt.C) { 238 b, err := hugolib.NewIntegrationTestBuilder( 239 hugolib.IntegrationTestConfig{ 240 T: c, 241 TxtarString: strings.Replace(filesTemplate, "$maincolor: #eee;", "$maincolor #eee;", 1), 242 NeedsOsFS: true, 243 }).BuildE() 244 245 b.Assert(err, qt.IsNotNil) 246 b.Assert(err.Error(), qt.Contains, `main.scss:8:13":`) 247 b.Assert(err.Error(), qt.Contains, `: expected ":".`) 248 fe := b.AssertIsFileError(err) 249 b.Assert(fe.ErrorContext(), qt.IsNotNil) 250 b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{" $maincolor #eee;", "", "body {", "\tcolor: $maincolor;", "}"}) 251 b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss") 252 253 }) 254 255 c.Run("error in import", func(c *qt.C) { 256 b, err := hugolib.NewIntegrationTestBuilder( 257 hugolib.IntegrationTestConfig{ 258 T: c, 259 TxtarString: strings.Replace(filesTemplate, "$foocolor: #ccc;", "$foocolor #ccc;", 1), 260 NeedsOsFS: true, 261 }).BuildE() 262 263 b.Assert(err, qt.IsNotNil) 264 b.Assert(err.Error(), qt.Contains, `_foo.scss:2:10":`) 265 b.Assert(err.Error(), qt.Contains, `: expected ":".`) 266 fe := b.AssertIsFileError(err) 267 b.Assert(fe.ErrorContext(), qt.IsNotNil) 268 b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 1 */", "$foocolor #ccc;", "", "foo {"}) 269 b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss") 270 271 }) 272 273 }