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
|
require File.dirname(__FILE__) + '/../test_helper'
require File.dirname(__FILE__) + '/test_helper'
require 'sass/engine'
class CacheTest < Minitest::Test
@@cache_dir = "tmp/file_cache"
def setup
FileUtils.mkdir_p @@cache_dir
end
def teardown
FileUtils.rm_rf @@cache_dir
clean_up_sassc
end
def test_file_cache_writes_a_file
file_store = Sass::CacheStores::Filesystem.new(@@cache_dir)
file_store.store("asdf/foo.scssc", "fakesha1", root_node)
assert File.exist?("#{@@cache_dir}/asdf/foo.scssc")
end
def test_file_cache_reads_a_file
file_store = Sass::CacheStores::Filesystem.new(@@cache_dir)
assert !File.exist?("#{@@cache_dir}/asdf/foo.scssc")
file_store.store("asdf/foo.scssc", "fakesha1", root_node)
assert File.exist?("#{@@cache_dir}/asdf/foo.scssc")
assert_kind_of Sass::Tree::RootNode, file_store.retrieve("asdf/foo.scssc", "fakesha1")
end
def test_file_cache_miss_returns_nil
file_store = Sass::CacheStores::Filesystem.new(@@cache_dir)
assert !File.exist?("#{@@cache_dir}/asdf/foo.scssc")
assert_nil file_store.retrieve("asdf/foo.scssc", "fakesha1")
end
def test_sha_change_invalidates_cache_and_cleans_up
file_store = Sass::CacheStores::Filesystem.new(@@cache_dir)
assert !File.exist?("#{@@cache_dir}/asdf/foo.scssc")
file_store.store("asdf/foo.scssc", "fakesha1", root_node)
assert File.exist?("#{@@cache_dir}/asdf/foo.scssc")
assert_nil file_store.retrieve("asdf/foo.scssc", "differentsha1")
assert !File.exist?("#{@@cache_dir}/asdf/foo.scssc")
end
def test_version_change_invalidates_cache_and_cleans_up
file_store = Sass::CacheStores::Filesystem.new(@@cache_dir)
assert !File.exist?("#{@@cache_dir}/asdf/foo.scssc")
file_store.store("asdf/foo.scssc", "fakesha1", root_node)
assert File.exist?("#{@@cache_dir}/asdf/foo.scssc")
real_version = Sass::VERSION
begin
Sass::VERSION.replace("a different version")
assert_nil file_store.retrieve("asdf/foo.scssc", "fakesha1")
assert !File.exist?("#{@@cache_dir}/asdf/foo.scssc")
ensure
Sass::VERSION.replace(real_version)
end
end
def test_arbitrary_objects_can_go_into_cache
cache = Sass::CacheStores::Memory.new
an_object = {:foo => :bar}
cache.store("an_object", "", an_object)
assert_equal an_object, cache.retrieve("an_object", "")
end
def test_cache_node_with_unmarshalable_option
engine_with_unmarshalable_options("foo {a: b + c}").to_tree
end
# Regression tests
def test_cache_mixin_def_splat_sass_node_with_unmarshalable_option
engine_with_unmarshalable_options(<<SASS, :syntax => :sass).to_tree
=color($args...)
color: red
SASS
end
def test_cache_mixin_def_splat_scss_node_with_unmarshalable_option
engine_with_unmarshalable_options(<<SCSS, :syntax => :scss).to_tree
@mixin color($args...) {
color: red;
}
SCSS
end
def test_cache_function_splat_sass_node_with_unmarshalable_option
engine_with_unmarshalable_options(<<SASS, :syntax => :sass).to_tree
@function color($args...)
@return red
SASS
end
def test_cache_function_splat_scss_node_with_unmarshalable_option
engine_with_unmarshalable_options(<<SCSS, :syntax => :scss).to_tree
@function color($args...) {
@return red;
}
SCSS
end
def test_cache_include_splat_sass_node_with_unmarshalable_option
engine_with_unmarshalable_options(<<SASS, :syntax => :sass).to_tree
@include color($args..., $kwargs...)
SASS
end
def test_cache_include_splat_scss_node_with_unmarshalable_option
engine_with_unmarshalable_options(<<SCSS, :syntax => :scss).to_tree
@include color($args..., $kwargs...);
SCSS
end
private
def root_node
Sass::Engine.new(<<-SCSS, :syntax => :scss).to_tree
@mixin color($c) { color: $c}
div { @include color(red); }
SCSS
end
def engine_with_unmarshalable_options(src, options={})
Sass::Engine.new(src, {
:syntax => :scss, :object => Class.new.new, :filename => 'file.scss',
:importer => Sass::Importers::Filesystem.new(absolutize('templates'))
}.merge(options))
end
end
|