spec/sprockets-sass_spec.rb in sprockets-sass-1.3.1 vs spec/sprockets-sass_spec.rb in sprockets-sass-2.0.0.beta1

- old
+ new

@@ -2,28 +2,36 @@ describe Sprockets::Sass do before :each do @root = create_construct @assets = @root.directory 'assets' + @public_dir = @root.directory 'public' @env = Sprockets::Environment.new @root.to_s @env.append_path @assets.to_s - @env.register_postprocessor 'text/css', :fail_postprocessor do |context, data| - data.gsub /@import/, 'fail engine' - end + @env.register_postprocessor 'text/css', FailPostProcessor end after :each do @root.destroy! end it 'processes scss files normally' do - @assets.file 'main.css.scss', '//= require dep' - @assets.file 'dep.css.scss', 'body { color: blue; }' + @assets.file 'main.css.scss', '//= require "dep"' + @assets.file 'dep.css.scss', 'body{ color: blue; }' asset = @env['main.css'] expect(asset.to_s).to eql("body {\n color: blue; }\n") end + if Sprockets::Sass::Utils.version_of_sprockets < 4 + it 'processes scss files normally without the .css extension' do + @assets.file 'main.scss', '//= require dep' + @assets.file 'dep.scss', 'body { color: blue; }' + asset = @env['main'] + expect(asset.to_s).to eql("body {\n color: blue; }\n") + end + end + it 'processes sass files normally' do @assets.file 'main.css.sass', '//= require dep' @assets.file 'dep.css.sass', "body\n color: blue" asset = @env['main.css'] expect(asset.to_s).to eql("body {\n color: blue; }\n") @@ -43,11 +51,11 @@ expect(asset.to_s).to eql("body {\n color: blue; }\n") end it 'imports other syntax' do @assets.file 'main.css.scss', %(@import "dep";\nbody { color: $color; }) - @assets.file 'dep.sass', "$color: blue\nhtml\n height: 100%" + @assets.file 'dep.css.sass', "$color: blue\nhtml\n height: 100%" asset = @env['main.css'] expect(asset.to_s).to eql("html {\n height: 100%; }\n\nbody {\n color: blue; }\n") end it 'imports files with the correct content type' do @@ -67,11 +75,11 @@ end it 'imports files with additional processors' do @assets.file 'main.css.scss', %(@import "dep";\nbody { color: $color; }) @assets.file 'dep.css.scss.erb', "$color: <%= 'blue' %>;" - asset = @env['main.css'] + asset = @env['main.css.scss'] expect(asset.to_s).to eql("body {\n color: blue; }\n") end it 'imports relative files' do @assets.file 'folder/main.css.scss', %(@import "./dep-1";\n@import "./subfolder/dep-2";\nbody { background-color: $background-color; color: $color; }) @@ -88,14 +96,14 @@ asset = @env['folder/main.css'] expect(asset.to_s).to eql("body {\n background-color: red;\n color: blue; }\n") end it 'imports deeply nested relative partials' do - @assets.file 'package-prime/stylesheets/main.scss', %(@import "package-dep/src/stylesheets/variables";\nbody { background-color: $background-color; color: $color; }) - @assets.file 'package-dep/src/stylesheets/_variables.scss', %(@import "./colors";\n$background-color: red;) - @assets.file 'package-dep/src/stylesheets/_colors.scss', '$color: blue;' - asset = @env['package-prime/stylesheets/main.scss'] + @assets.file 'package-prime/stylesheets/main.css.scss', %(@import "package-dep/src/stylesheets/variables";\nbody { background-color: $background-color; color: $color; }) + @assets.file 'package-dep/src/stylesheets/_variables.css.scss', %(@import "./colors";\n$background-color: red;) + @assets.file 'package-dep/src/stylesheets/_colors.css.scss', '$color: blue;' + asset = @env['package-prime/stylesheets/main.css.scss'] expect(asset.to_s).to eql("body {\n background-color: red;\n color: blue; }\n") end it 'imports relative files without preceding ./' do @assets.file 'folder/main.css.scss', %(@import "dep-1";\n@import "subfolder/dep-2";\nbody { background-color: $background-color; color: $color; }) @@ -127,12 +135,12 @@ expect(asset.to_s).to eql("body {\n color: blue; }\n") end it 'shares Sass environment with other imports' do @assets.file 'main.css.scss', %(@import "dep-1";\n@import "dep-2";) - @assets.file '_dep-1.scss', '$color: blue;' - @assets.file '_dep-2.scss', 'body { color: $color; }' + @assets.file '_dep-1.css.scss', '$color: blue;' + @assets.file '_dep-2.css.scss', 'body { color: $color; }' asset = @env['main.css'] expect(asset.to_s).to eql("body {\n color: blue; }\n") end it 'imports files from the assets load path' do @@ -149,24 +157,24 @@ vendor = @root.directory 'vendor' @env.append_path vendor.to_s @assets.file 'folder/main.css.scss', %(@import "dep";\nbody { color: $color; }) vendor.file 'dep.css.scss', '@import "folder1/dep1";' - vendor.file 'folder1/_dep1.scss', '@import "folder2/dep2";' - vendor.file 'folder1/folder2/_dep2.scss', '$color: blue;' + vendor.file 'folder1/_dep1.css.scss', '@import "folder2/dep2";' + vendor.file 'folder1/folder2/_dep2.css.scss', '$color: blue;' asset = @env['folder/main.css'] expect(asset.to_s).to eql("body {\n color: blue; }\n") end it 'imports nested partials with relative path and glob from the assets load path' do vendor = @root.directory 'vendor' @env.append_path vendor.to_s @assets.file 'folder/main.css.scss', %(@import "dep";\nbody { color: $color; }) vendor.file 'dep.css.scss', '@import "folder1/dep1";' - vendor.file 'folder1/_dep1.scss', '@import "folder2/*";' - vendor.file 'folder1/folder2/_dep2.scss', '$color: blue;' + vendor.file 'folder1/_dep1.css.scss', '@import "folder2/*";' + vendor.file 'folder1/folder2/_dep2.css.scss', '$color: blue;' asset = @env['folder/main.css'] expect(asset.to_s).to eql("body {\n color: blue; }\n") end it 'allows global Sass configuration' do @@ -207,60 +215,65 @@ it 'adds dependencies when imported' do @assets.file 'main.css.scss', %(@import "dep";\nbody { color: $color; }) dep = @assets.file 'dep.css.scss', '$color: blue;' asset = @env['main.css'] - expect(asset).to be_fresh(@env) + old_asset = asset.dup + expect(asset).to be_fresh(@env, old_asset) - mtime = Time.now + 1 - dep.open('w') { |f| f.write '$color: red;' } - dep.utime mtime, mtime + write_asset(dep, '$color: red;') - expect(asset).to_not be_fresh(@env) + asset = Sprockets::Sass::Utils.version_of_sprockets >= 3 ? @env['main.css'] : asset + expect(asset).to_not be_fresh(@env, old_asset) end it 'adds dependencies from assets when imported' do @assets.file 'main.css.scss', %(@import "dep-1";\nbody { color: $color; }) @assets.file 'dep-1.css.scss', %(@import "dep-2";\n) dep = @assets.file 'dep-2.css.scss', '$color: blue;' asset = @env['main.css'] - expect(asset).to be_fresh(@env) + old_asset = asset.dup + expect(asset).to be_fresh(@env, old_asset) - mtime = Time.now + 1 - dep.open('w') { |f| f.write '$color: red;' } - dep.utime mtime, mtime + write_asset(dep, '$color: red;') - expect(asset).to_not be_fresh(@env) + asset = Sprockets::Sass::Utils.version_of_sprockets >= 3 ? @env['main.css'] : asset + expect(asset).to_not be_fresh(@env, old_asset) end it 'adds dependencies when imported from a glob' do @assets.file 'main.css.scss', %(@import "folder/*";\nbody { color: $color; background: $bg-color; }) - @assets.file 'folder/_dep-1.scss', '$color: blue;' - dep = @assets.file 'folder/_dep-2.scss', '$bg-color: red;' + @assets.file 'folder/_dep-1.css.scss', '$color: blue;' + dep = @assets.file 'folder/_dep-2.css.scss', '$bg-color: red;' asset = @env['main.css'] - expect(asset).to be_fresh(@env) + old_asset = asset.dup + expect(asset).to be_fresh(@env, old_asset) - mtime = Time.now + 1 - dep.open('w') { |f| f.write "$bg-color: white;" } - dep.utime mtime, mtime + write_asset(dep, "$bg-color: white;" ) - expect(asset).to_not be_fresh(@env) + asset = Sprockets::Sass::Utils.version_of_sprockets >= 3 ? @env['main.css'] : asset + + expect(asset).to_not be_fresh(@env, old_asset) end it "uses the environment's cache" do cache = {} @env.cache = cache @assets.file 'main.css.scss', %($color: blue;\nbody { color: $color; }) @env['main.css'].to_s - if Sass.version[:minor] > 2 - sass_cache = cache.detect.detect { |key, value| value['pathname'] =~ /main\.css\.scss/ } + if Sprockets::Sass::Utils.version_of_sprockets < 3 + if Sass.version[:minor] > 2 + sass_cache = cache.detect { |key, value| value['pathname'] =~ /main\.css\.scss/ } + else + sass_cache = cache.keys.detect { |key| key =~ /main\.css\.scss/ } + end else - sass_cache = cache.keys.detect { |key| key =~ /main\.css\.scss/ } + sass_cache = cache.detect { |key, value| value =~ /main\.css\.scss/ } end expect(sass_cache).to_not be_nil end it 'adds the #asset_path helper' do @@ -343,45 +356,72 @@ @assets.file 'bullet.gif' expect(@env['bullets.css'].to_s).to match(%r[background: url\("/assets/bullet\.gif"\)]) end - it 'compresses css' do - css = "div {\n color: red;\n}\n" - compressed_css = Sprockets::Sass::Compressor.new.compress(css) - expect(compressed_css).to eql("div{color:red}\n") + if Sprockets::Sass::Utils.version_of_sprockets < 3 + it 'compresses css from string' do + compressor = Sprockets::Sass::V2::Compressor.new + compressed_css = compressor.compress("div {\n color: red;\n}\n") + expect(compressed_css).to eql("div{color:red}\n") + end end + if Sprockets::Sass::Utils.version_of_sprockets >= 3 + it 'compresses css from filename' do + file_path = @assets.file 'asset_path.css.scss', %(div {\n color: red;\n}\n) + compressor = Sprockets::Sass::V3::Compressor.new + compressed_css = compressor.run(file_path) + expect(compressed_css).to eql("div{color:red}\n") + end + + it 'compresses css from string' do + compressor = Sprockets::Sass::V3::Compressor.new + compressed_css = compressor.run("div {\n color: red;\n}\n") + expect(compressed_css).to eql("div{color:red}\n") + end + end + + if Sprockets::Sass::Utils.version_of_sprockets < 3 + it 'compresses css using the environment compressor' do + @env.css_compressor = Sprockets::Sass::V2::Compressor + @assets.file 'asset_path.css.scss', %(div {\n color: red;\n}\n) + res = compile_asset_and_return_compilation(@env, @public_dir, "asset_path.css") + expect(res).to eql("div{color:red}\n") + end + else + it 'compresses css using the environment compressor' do + @env.css_compressor = :sprockets_sass + @assets.file 'asset_path.css.scss', %(div {\n color: red;\n}\n) + res = compile_asset_and_return_compilation(@env, @public_dir, "asset_path.css") + expect(res).to eql("div{color:red}\n") + end + end + + describe Sprockets::Sass::SassTemplate do - describe 'initialize_engine' do - it 'initializes super if super is uninitinalized' do - Tilt::SassTemplate.stub(:engine_initialized?).and_return false - template = Sprockets::Sass::SassTemplate.new {} - template.should_receive(:require_template_library) # called from Tilt::SassTemplate.initialize - template.initialize_engine - end - it 'does not initializes super if super is initinalized to silence warnings' do - Tilt::SassTemplate.stub(:engine_initialized?).and_return true - template = Sprockets::Sass::SassTemplate.new {} - template.should_not_receive(:require_template_library) # called from Tilt::SassTemplate.initialize - template.initialize_engine + let(:template) do + Sprockets::Sass::SassTemplate.new(@assets.file 'bullet.gif') do + # nothing end + end + describe 'initialize_engine' do - it 'does not add Sass functions if sprockets-helpers is not available' do - template = Sprockets::Sass::SassTemplate.new {} - template.should_receive(:require).with('sprockets/helpers').and_raise LoadError - template.should_not_receive(:require).with 'sprockets/sass/functions' - template.initialize_engine - expect(Sprockets::Sass::SassTemplate.engine_initialized?).to be_truthy + it 'does add Sass functions if sprockets-helpers is not available' do + Sprockets::Sass::SassTemplate.sass_functions_initialized = false + Sprockets::Sass.add_sass_functions = true + Sprockets::Sass::SassTemplate.any_instance.should_receive(:require).with('sprockets/helpers').and_raise LoadError + Sprockets::Sass::SassTemplate.any_instance.should_not_receive(:require).with 'sprockets/sass/functions' + template + expect(Sprockets::Sass::SassTemplate.engine_initialized?).to be_falsy end it 'does not add Sass functions if add_sass_functions is false' do Sprockets::Sass.add_sass_functions = false - template = Sprockets::Sass::SassTemplate.new {} template.should_not_receive(:require).with 'sprockets/sass/functions' template.initialize_engine - expect(Sprockets::Sass::SassTemplate.engine_initialized?).to be_truthy + expect(Sprockets::Sass::SassTemplate.engine_initialized?).to be_falsy Sprockets::Sass.add_sass_functions = true end end end end