integration_test.go (5706B)
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 if !scss.Supports() { 29 t.Skip() 30 } 31 c := qt.New(t) 32 33 files := ` 34 -- assets/scss/main.scss -- 35 @import "moo"; 36 -- node_modules/foo/_moo.scss -- 37 $moolor: #fff; 38 39 moo { 40 color: $moolor; 41 } 42 -- config.toml -- 43 -- layouts/index.html -- 44 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") ) }} 45 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }} 46 T1: {{ $r.Content }} 47 ` 48 49 b := hugolib.NewIntegrationTestBuilder( 50 hugolib.IntegrationTestConfig{ 51 T: c, 52 TxtarString: files, 53 NeedsOsFS: true, 54 }).Build() 55 56 b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`) 57 } 58 59 func TestTransformImportRegularCSS(t *testing.T) { 60 if !scss.Supports() { 61 t.Skip() 62 } 63 64 c := qt.New(t) 65 66 files := ` 67 -- assets/scss/_moo.scss -- 68 $moolor: #fff; 69 70 moo { 71 color: $moolor; 72 } 73 -- assets/scss/another.css -- 74 75 -- assets/scss/main.scss -- 76 @import "moo"; 77 @import "regular.css"; 78 @import "moo"; 79 @import "another.css"; 80 81 /* foo */ 82 -- assets/scss/regular.css -- 83 84 -- config.toml -- 85 -- layouts/index.html -- 86 {{ $r := resources.Get "scss/main.scss" | toCSS }} 87 T1: {{ $r.Content | safeHTML }} 88 89 ` 90 91 b := hugolib.NewIntegrationTestBuilder( 92 hugolib.IntegrationTestConfig{ 93 T: c, 94 TxtarString: files, 95 NeedsOsFS: true, 96 }).Build() 97 98 // LibSass does not support regular CSS imports. There 99 // is an open bug about it that probably will never be resolved. 100 // Hugo works around this by preserving them in place: 101 b.AssertFileContent("public/index.html", ` 102 T1: moo { 103 color: #fff; } 104 105 @import "regular.css"; 106 moo { 107 color: #fff; } 108 109 @import "another.css"; 110 /* foo */ 111 112 `) 113 } 114 115 func TestTransformThemeOverrides(t *testing.T) { 116 if !scss.Supports() { 117 t.Skip() 118 } 119 120 c := qt.New(t) 121 122 files := ` 123 -- assets/scss/components/_boo.scss -- 124 $boolor: green; 125 126 boo { 127 color: $boolor; 128 } 129 -- assets/scss/components/_moo.scss -- 130 $moolor: #ccc; 131 132 moo { 133 color: $moolor; 134 } 135 -- config.toml -- 136 theme = 'mytheme' 137 -- layouts/index.html -- 138 {{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) ) }} 139 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }} 140 T1: {{ $r.Content }} 141 -- themes/mytheme/assets/scss/components/_boo.scss -- 142 $boolor: orange; 143 144 boo { 145 color: $boolor; 146 } 147 -- themes/mytheme/assets/scss/components/_imports.scss -- 148 @import "moo"; 149 @import "_boo"; 150 @import "_zoo"; 151 -- themes/mytheme/assets/scss/components/_moo.scss -- 152 $moolor: #fff; 153 154 moo { 155 color: $moolor; 156 } 157 -- themes/mytheme/assets/scss/components/_zoo.scss -- 158 $zoolor: pink; 159 160 zoo { 161 color: $zoolor; 162 } 163 -- themes/mytheme/assets/scss/main.scss -- 164 @import "components/imports"; 165 ` 166 167 b := hugolib.NewIntegrationTestBuilder( 168 hugolib.IntegrationTestConfig{ 169 T: c, 170 TxtarString: files, 171 NeedsOsFS: true, 172 }).Build() 173 174 b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`) 175 } 176 177 func TestTransformErrors(t *testing.T) { 178 if !scss.Supports() { 179 t.Skip() 180 } 181 182 c := qt.New(t) 183 184 const filesTemplate = ` 185 -- config.toml -- 186 theme = 'mytheme' 187 -- assets/scss/components/_foo.scss -- 188 /* comment line 1 */ 189 $foocolor: #ccc; 190 191 foo { 192 color: $foocolor; 193 } 194 -- themes/mytheme/assets/scss/main.scss -- 195 /* comment line 1 */ 196 /* comment line 2 */ 197 @import "components/foo"; 198 /* comment line 4 */ 199 200 $maincolor: #eee; 201 202 body { 203 color: $maincolor; 204 } 205 206 -- layouts/index.html -- 207 {{ $cssOpts := dict }} 208 {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }} 209 T1: {{ $r.Content }} 210 211 ` 212 213 c.Run("error in main", func(c *qt.C) { 214 b, err := hugolib.NewIntegrationTestBuilder( 215 hugolib.IntegrationTestConfig{ 216 T: c, 217 TxtarString: strings.Replace(filesTemplate, "$maincolor: #eee;", "$maincolor #eee;", 1), 218 NeedsOsFS: true, 219 }).BuildE() 220 221 b.Assert(err, qt.IsNotNil) 222 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`themes/mytheme/assets/scss/main.scss:6:1": expected ':' after $maincolor in assignment statement`)) 223 fe := b.AssertIsFileError(err) 224 b.Assert(fe.ErrorContext(), qt.IsNotNil) 225 b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 4 */", "", "$maincolor #eee;", "", "body {"}) 226 b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss") 227 228 }) 229 230 c.Run("error in import", func(c *qt.C) { 231 b, err := hugolib.NewIntegrationTestBuilder( 232 hugolib.IntegrationTestConfig{ 233 T: c, 234 TxtarString: strings.Replace(filesTemplate, "$foocolor: #ccc;", "$foocolor #ccc;", 1), 235 NeedsOsFS: true, 236 }).BuildE() 237 238 b.Assert(err, qt.IsNotNil) 239 b.Assert(err.Error(), qt.Contains, `assets/scss/components/_foo.scss:2:1": expected ':' after $foocolor in assignment statement`) 240 fe := b.AssertIsFileError(err) 241 b.Assert(fe.ErrorContext(), qt.IsNotNil) 242 b.Assert(fe.ErrorContext().Lines, qt.DeepEquals, []string{"/* comment line 1 */", "$foocolor #ccc;", "", "foo {"}) 243 b.Assert(fe.ErrorContext().ChromaLexer, qt.Equals, "scss") 244 245 }) 246 247 }