1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
# frozen_string_literal: true
require "spec_helper"
require "tmpdir"
describe(Jekyll::Converters::Scss) do
let(:site) do
Jekyll::Site.new(site_configuration)
end
let(:scss_converter) do
scss_converter_instance(site)
end
let(:content) do
<<~SCSS
$font-stack: Helvetica, sans-serif;
body {
font-family: $font-stack;
font-color: fuschia;
}
SCSS
end
let(:expanded_css_output) do
<<~CSS
body {
font-family: Helvetica, sans-serif;
font-color: fuschia;
}
CSS
end
let(:compact_css_output) do
<<~CSS
body { font-family: Helvetica, sans-serif; font-color: fuschia; }
CSS
end
let(:invalid_content) do
<<~SCSS
$font-stack: Helvetica
body {
font-family: $font-stack;
SCSS
end
def converter(overrides = {})
scss_converter_instance(site).dup.tap do |obj|
obj.instance_variable_get(:@config)["sass"] = overrides
end
end
context "matching file extensions" do
it "matches .scss files" do
expect(converter.matches(".scss")).to be_truthy
end
it "does not match .sass files" do
expect(converter.matches(".sass")).to be_falsey
end
end
context "determining the output file extension" do
it "always outputs the .css file extension" do
expect(converter.output_ext(".always-css")).to eql(".css")
end
end
context "when building configurations" do
# Caching is no more a feature with sassC
# it "allow caching in unsafe mode" do
# expect(converter.sass_configs[:cache]).to be_truthy
# end
it "set the load paths to the _sass dir relative to site source" do
expect(converter.sass_configs[:load_paths]).to eql([source_dir("_sass")])
end
it "allow for other styles" do
expect(converter("style" => :compressed).sass_configs[:style]).to eql(:compressed)
end
context "when specifying sass dirs" do
context "when the sass dir exists" do
it "allow the user to specify a different sass dir" do
create_directory(source_dir("_scss"))
override = { "sass_dir" => "_scss" }
expect(converter(override).sass_configs[:load_paths]).to eql([source_dir("_scss")])
remove_directory(source_dir("_scss"))
end
it "not allow sass_dirs outside of site source" do
expect(
converter("sass_dir" => "/etc/passwd").sass_dir_relative_to_site_source
).to eql("etc/passwd")
end
end
end
context "in safe mode" do
let(:verter) do
Jekyll::Converters::Scss.new(
site.config.merge(
"sass" => {},
"safe" => true
)
)
end
it "does not allow caching" do
expect(verter.sass_configs[:cache]).to be_falsey
end
it "forces load_paths to be just the local load path" do
expect(verter.sass_configs[:load_paths]).to eql([source_dir("_sass")])
end
it "allows the user to specify the style" do
allow(verter).to receive(:sass_style).and_return(:compressed)
expect(verter.sass_configs[:style]).to eql(:compressed)
end
it "defaults style to :expanded for sass-embedded or :compact for sassc" do
expected = sass_embedded? ? :expanded : :compact
expect(verter.sass_configs[:style]).to eql(expected)
end
it "at least contains :syntax and :load_paths keys" do
expect(verter.sass_configs.keys).to include(:load_paths, :syntax)
end
end
end
context "converting SCSS" do
it "produces CSS" do
expected = sass_embedded? ? expanded_css_output : compact_css_output
expect(converter.convert(content)).to eql(expected)
end
it "includes the syntax error line in the syntax error message" do
expected = if sass_embedded?
%r!expected ";"!i
else
error_message = 'Error: Invalid CSS after "body": expected 1 selector or at-rule'
%r!\A#{error_message}, was "{"\s+on line 2!
end
expect { scss_converter.convert(invalid_content) }.to(
raise_error(Jekyll::Converters::Scss::SyntaxError, expected)
)
end
it "removes byte order mark from compressed SCSS" do
result = converter("style" => :compressed).convert("a{content:\"\uF015\"}")
expect(result).to eql(%(a{content:"\uF015"}\n))
expect(result.bytes.to_a[0..2]).not_to eql([0xEF, 0xBB, 0xBF])
end
it "does not include the charset unless asked to" do
overrides = { "style" => :compressed, "add_charset" => true }
result = converter(overrides).convert(%(a{content:"\uF015"}))
expect(result).to eql(%(@charset "UTF-8";a{content:"\uF015"}\n))
expect(result.bytes.to_a[0..2]).not_to eql([0xEF, 0xBB, 0xBF])
end
end
context "importing partials" do
let(:test_css_file) { dest_dir("css/main.css") }
before(:each) { site.process }
it "outputs the CSS file" do
expect(File.exist?(test_css_file)).to be_truthy
end
it "imports SCSS partial" do
expect(File.read(test_css_file)).to eql(
".half{width:50%}\n\n/*# sourceMappingURL=main.css.map */"
)
end
it "uses a compressed style" do
instance = scss_converter_instance(site)
expect(instance.jekyll_sass_configuration).to eql("style" => :compressed)
expect(instance.sass_configs[:style]).to eql(:compressed)
end
end
context "importing from external libraries" do
let(:external_library) { source_dir("bower_components/jquery") }
let(:test_css_file) { dest_dir("css", "main.css") }
context "in unsafe mode" do
let(:site) do
make_site(
"source" => sass_lib,
"sass" => {
"load_paths" => external_library,
}
)
end
before(:each) { create_directory external_library }
after(:each) { remove_directory external_library }
it "recognizes the new load path" do
expect(scss_converter.sass_load_paths).to include(external_library)
end
it "ensures the sass_dir is still in the load path" do
expect(scss_converter.sass_load_paths).to include(sass_lib("_sass"))
end
it "brings in the grid partial" do
site.process
expected = if sass_embedded?
"a {\n color: #999999;\n}\n\n/*# sourceMappingURL=main.css.map */"
else
"a { color: #999999; }\n\n/*# sourceMappingURL=main.css.map */"
end
expect(File.read(test_css_file)).to eql(expected)
end
context "with the sass_dir specified twice" do
let(:site) do
make_site(
"source" => sass_lib,
"sass" => {
"load_paths" => [
external_library,
sass_lib("_sass"),
],
}
)
end
it "ensures the sass_dir only occurrs once in the load path" do
expect(scss_converter.sass_load_paths).to eql([external_library, sass_lib("_sass")])
end
end
end
context "in safe mode" do
let(:site) do
make_site(
"safe" => true,
"source" => sass_lib,
"sass" => {
"load_paths" => external_library,
}
)
end
it "ignores the new load path" do
expect(scss_converter.sass_load_paths).not_to include(external_library)
end
it "ensures the sass_dir is the entire load path" do
expect(scss_converter.sass_load_paths).to eql([sass_lib("_sass")])
end
end
end
context "importing from internal libraries" do
let(:internal_library) { source_dir("bower_components/jquery") }
before(:each) { create_directory internal_library }
after(:each) { remove_directory internal_library }
context "in unsafe mode" do
let(:site) do
make_site(
"sass" => {
"load_paths" => ["bower_components/*"],
}
)
end
it "expands globs" do
expect(scss_converter.sass_load_paths).to include(internal_library)
end
end
context "in safe mode" do
let(:site) do
make_site(
"safe" => true,
"sass" => {
"load_paths" => [
Dir.tmpdir,
"bower_components/*",
"../..",
],
}
)
end
it "allows local load paths" do
expect(scss_converter.sass_load_paths).to include(internal_library)
end
it "ignores external load paths" do
expect(scss_converter.sass_load_paths).not_to include(Dir.tmpdir)
end
it "does not allow traversing outside source directory" do
scss_converter.sass_load_paths.each do |path|
expect(path).to include(source_dir)
expect(path).not_to include("..")
end
end
end
end
context "with valid sass paths in a theme" do
context "in unsafe mode" do
let(:site) do
make_site("theme" => "minima")
end
it "includes the theme's sass directory" do
expect(site.theme.sass_path).to be_truthy
expect(scss_converter.sass_load_paths).to include(site.theme.sass_path)
end
end
context "in safe mode" do
let(:site) do
make_site(
"theme" => "minima",
"safe" => true
)
end
it "includes the theme's sass directory" do
expect(site.safe).to be true
expect(site.theme.sass_path).to be_truthy
expect(converter.sass_load_paths).to include(site.theme.sass_path)
end
end
end
context "in a site with a collection labelled 'pages'" do
let(:site) do
make_site(
"source" => File.expand_path("pages-collection", __dir__),
"sass" => {
"style" => :expanded,
},
"collections" => {
"pages" => {
"output" => true,
},
}
)
end
it "produces CSS without raising errors" do
expect { site.process }.not_to raise_error
expect(scss_converter.convert(content)).to eql(expanded_css_output)
end
end
context "in a site nested inside directory with square brackets" do
let(:site) do
make_site(
"source" => File.expand_path("[alpha]beta", __dir__),
"sass" => {
"style" => :expanded,
}
)
end
it "produces CSS without raising errors" do
expect { site.process }.not_to raise_error
expect(scss_converter.convert(content)).to eql(expanded_css_output)
end
end
context "generating sourcemap" do
let(:sourcemap_file) { dest_dir("css/app.css.map") }
let(:sourcemap_contents) { File.binread(sourcemap_file) }
before { site.process }
it "outputs the sourcemap file" do
expect(File.exist?(sourcemap_file)).to be true
end
it "should not have Liquid expressions rendered" do
expect(sourcemap_contents).to include("{{ site.mytheme.skin }}")
end
context "in a site with source not equal to its default value of `Dir.pwd`" do
let(:site) do
make_site(
"source" => File.expand_path("nested_source/src", __dir__)
)
end
let(:test_sourcemap_file) { dest_dir("css/main.css.map") }
let(:sourcemap_data) { JSON.parse(File.binread(test_sourcemap_file)) }
before(:each) { site.process }
it "outputs the sourcemap file" do
expect(File.exist?(test_sourcemap_file)).to be_truthy
end
it "contains relevant sass sources" do
sources = sourcemap_data["sources"]
# sass-embedded (dart-sass) does not inlcude main.scss in sources
# because main.scss only contains @import statements
# thus there is no actual scss code to be mapped
expect(sources).to include("main.scss") unless sass_embedded?
expect(sources).to include("_sass/_grid.scss")
expect(sources).to_not include("_sass/_color.scss") # not imported into "main.scss"
end
it "does not leak directory structure outside of `site.source`" do
site_source_relative_from_pwd = \
Pathname.new(site.source)
.relative_path_from(Pathname.new(Dir.pwd))
.to_s
relative_path_parts = site_source_relative_from_pwd.split(File::SEPARATOR)
expect(site_source_relative_from_pwd).to eql("spec/nested_source/src")
expect(relative_path_parts).to eql(%w(spec nested_source src))
relative_path_parts.each do |dirname|
sourcemap_data["sources"].each do |fpath|
expect(fpath).to_not include(dirname)
end
end
end
end
end
end
|