| 1 |
test_that("throws on invalid output dir", { |
| 2 |
local_temp_cache() |
| 3 |
|
| 4 |
# Prime the cache |
| 5 |
sass("div { border: 1px solid black; }") |
| 6 |
|
| 7 |
# Cache |
| 8 |
expect_error( |
| 9 |
sass( |
| 10 |
"div { border: 1px solid black; }", |
| 11 |
output = file.path("not", "a", "path.css") |
| 12 |
) |
| 13 |
) |
| 14 |
}) |
| 15 |
|
| 16 |
test_that("reads from and writes to cache", { |
| 17 |
local_temp_cache() |
| 18 |
expected <- read_utf8("test-unicode-var-expected.css") |
| 19 |
|
| 20 |
# Wrap the sass_file up into a bundle to ensure it's attribute |
| 21 |
# doesn't get dropped when the bundle is collapsed (into a layer) and compiled |
| 22 |
input <- sass_bundle(sass_layer(rules = sass_file("test-unicode-var-input.scss"))) |
| 23 |
|
| 24 |
expect_equal(as.character(sass(input)), expected) |
| 25 |
|
| 26 |
expect_equal(as.character(sass(input)), expected) |
| 27 |
|
| 28 |
expect_equal(sass_cache_get()$size(), 1) |
| 29 |
|
| 30 |
# Modifying the file busts the cache (even it if has the same contents) |
| 31 |
Sys.setFileTime("test-unicode-var-input.scss", Sys.time() + 5) |
| 32 |
expect_equal(as.character(sass(input)), expected) |
| 33 |
expect_equal(sass_cache_get()$size(), 2) |
| 34 |
}) |
| 35 |
|
| 36 |
test_that("writes to cache", { |
| 37 |
|
| 38 |
expect_cached <- function(input, css) { |
| 39 |
local_temp_cache() |
| 40 |
cache <- sass_cache_get() |
| 41 |
# Allow input to be an expression, like layer(), that can generate |
| 42 |
# different input everytime it gets evaluated |
| 43 |
input_code <- substitute(input) |
| 44 |
# Did we get the right CSS result? |
| 45 |
expect_css(eval(input_code), css) |
| 46 |
# Did a cache entry get added? |
| 47 |
expect_equal(cache$size(), 1) |
| 48 |
# Compile again, now with an output file |
| 49 |
out_file <- tempfile(fileext = ".css") |
| 50 |
expect_css(eval(input_code), css, output = out_file) |
| 51 |
# If there was a cache hit, the size should be the same |
| 52 |
expect_equal(cache$size(), 1) |
| 53 |
# Now manipulate the cache directly to make sure it is actually being read |
| 54 |
# from. We'll change the value in the cache and see if sass() reads from it. |
| 55 |
cache$set_content(cache$keys(), "foo") |
| 56 |
expect_css(eval(input_code), "foo") |
| 57 |
} |
| 58 |
|
| 59 |
expect_cached( |
| 60 |
list( |
| 61 |
list(text_color = "#313131"), |
| 62 |
list("body { color: $text_color; }") |
| 63 |
), |
| 64 |
"body{color:#313131;}" |
| 65 |
) |
| 66 |
|
| 67 |
# Make sure cache key doesn't change with new/temporary HTML dependencies |
| 68 |
my_layer <- function() { |
| 69 |
src <- tempfile() |
| 70 |
dir.create(src) |
| 71 |
sass_layer( |
| 72 |
"@function fib($x) { |
| 73 |
@if $x <= 1 { |
| 74 |
@return $x |
| 75 |
} |
| 76 |
@return fib($x - 2) + fib($x - 1); |
| 77 |
} |
| 78 |
body { width: fib(27);}", |
| 79 |
html_deps = htmltools::htmlDependency("foo", "1.0", src) |
| 80 |
) |
| 81 |
} |
| 82 |
|
| 83 |
expect_cached( |
| 84 |
my_layer(), |
| 85 |
"body{width:196418;}" |
| 86 |
) |
| 87 |
|
| 88 |
}) |
| 89 |
|
| 90 |
test_that("unicode characters work OK after caching", { |
| 91 |
local_temp_cache() |
| 92 |
expected <- read_utf8("test-unicode-var-expected.css") |
| 93 |
class(expected) <- c("css", "html", "character") |
| 94 |
attr(expected, "html") <- TRUE |
| 95 |
|
| 96 |
css <- sass(sass_file("test-unicode-var-input.scss")) |
| 97 |
expect_equal(css, expected) |
| 98 |
|
| 99 |
css <- sass(sass_file("test-unicode-var-input.scss")) |
| 100 |
expect_equal(css, expected) |
| 101 |
|
| 102 |
expect_equal(sass_cache_get()$size(), 1) |
| 103 |
}) |
| 104 |
|
| 105 |
test_that("cache isn't written if a compilation error occurs", { |
| 106 |
local_temp_cache() |
| 107 |
|
| 108 |
input <- list( |
| 109 |
list(text_color = "#a0a0a0"), |
| 110 |
list("body { color is: $text_color; }") |
| 111 |
) |
| 112 |
options <- sass_options(output_style = "compact") |
| 113 |
|
| 114 |
expect_error(sass(input, options), "must be followed") |
| 115 |
|
| 116 |
expect_equal(sass_cache_get()$size(), 0) |
| 117 |
}) |
| 118 |
|
| 119 |
test_that("cache key components", { |
| 120 |
tmpfile <- tempfile(fileext = ".tmp") |
| 121 |
file.create(tmpfile) |
| 122 |
old_mtime <- file.mtime(tmpfile) |
| 123 |
|
| 124 |
input <- list(sass_file(tmpfile)) |
| 125 |
options <- sass_options() |
| 126 |
|
| 127 |
key1 <- sass_hash(list(input, options)) |
| 128 |
while (file.mtime(tmpfile) == old_mtime) { |
| 129 |
# Force timestamp to change |
| 130 |
Sys.sleep(0.1) |
| 131 |
file.remove(tmpfile) |
| 132 |
file.create(tmpfile) |
| 133 |
} |
| 134 |
|
| 135 |
# Files differing by mtime should have different keys |
| 136 |
key2 <- sass_hash(list(input, options)) |
| 137 |
expect_true(key1 != key2) |
| 138 |
|
| 139 |
# Options that are identical should have same key |
| 140 |
options3 <- sass_options() |
| 141 |
key3 <- sass_hash(list(input, options3)) |
| 142 |
expect_true(key2 == key3) |
| 143 |
|
| 144 |
# Options that are different should have different keys |
| 145 |
options4 <- sass_options(output_style = "compact") |
| 146 |
key4 <- sass_hash(list(input, options4)) |
| 147 |
expect_true(key3 != key4) |
| 148 |
}) |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
test_that("output_template() is cache and options aware", { |
| 153 |
local_temp_cache() |
| 154 |
|
| 155 |
input <- list( |
| 156 |
list(color = "red"), |
| 157 |
"body{color:red}" |
| 158 |
) |
| 159 |
opts <- sass_options(output_style = "compressed") |
| 160 |
expect_red <- function(file) { |
| 161 |
expect_equal( |
| 162 |
"body{color:red}", |
| 163 |
gsub("(\\s+)|(\t)|(;)", "", paste(readLines(file), collapse = "")) |
| 164 |
) |
| 165 |
} |
| 166 |
|
| 167 |
# File is exactly the same with the same input+options |
| 168 |
output1 <- sass(input, output = output_template(), options = opts) |
| 169 |
output2 <- sass(input, output = output_template(), options = opts) |
| 170 |
expect_true(output1 == output2) |
| 171 |
expect_match(output1, ".min.css$") |
| 172 |
expect_red(output1) |
| 173 |
expect_red(output2) |
| 174 |
|
| 175 |
# If cache is different, should get a different output file |
| 176 |
output3 <- sass(input, output = output_template(), options = opts, cache_key_extra = "foo") |
| 177 |
expect_true(output1 != output3) |
| 178 |
expect_red(output3) |
| 179 |
|
| 180 |
# Not minimized by default (because output_style = "expanded") |
| 181 |
output4 <- sass(input, output = output_template()) |
| 182 |
expect_false(grepl(".min.css$", output4)) |
| 183 |
expect_red(output4) |
| 184 |
|
| 185 |
# If no caching, should get a different output files |
| 186 |
output5 <- sass(input, output = output_template(), cache = FALSE) |
| 187 |
output6 <- sass(input, output = output_template(), cache = FALSE) |
| 188 |
expect_true(dirname(output5) != dirname(output6)) |
| 189 |
expect_red(output5) |
| 190 |
expect_red(output6) |
| 191 |
|
| 192 |
# File can be written in another dir and still be cache aware |
| 193 |
temp_dir <- withr::local_tempdir() |
| 194 |
output7 <- sass(input, output = output_template(path = temp_dir), options = opts) |
| 195 |
expect_true(basename(dirname(dirname(output7))) == basename(temp_dir)) |
| 196 |
expect_true(basename(dirname(output7)) == basename(dirname(output1))) |
| 197 |
}) |
| 198 |
|
| 199 |
test_that("Cache directory getting/setting", { |
| 200 |
cache_dir <- tempfile("sass-cache-test-") |
| 201 |
cache <- sass_file_cache(dir = cache_dir) |
| 202 |
|
| 203 |
# Can normalize path _after_ dir is created. |
| 204 |
cache_dir <- normalizePath(cache_dir) |
| 205 |
expect_identical(normalizePath(cache$dir()), cache_dir) |
| 206 |
|
| 207 |
# Setting the cache for a directory works |
| 208 |
sass_cache_set_dir(cache_dir, cache) |
| 209 |
expect_identical(sass_cache_get_dir(cache_dir), cache) |
| 210 |
|
| 211 |
# It checks that the specified dir and the cache's dir match |
| 212 |
expect_error(sass_cache_set_dir(file.path(cache_dir, "foo"), cache)) |
| 213 |
|
| 214 |
# Check that the path is normalized. Create another cache object and set |
| 215 |
# it as the cache for this path. Then fetching the cache for the path should |
| 216 |
# return the new one. |
| 217 |
cache2 <- sass_file_cache(dir = cache_dir) |
| 218 |
expect_false(identical(cache, cache2)) |
| 219 |
sass_cache_set_dir( |
| 220 |
file.path(cache_dir, "..", basename(cache_dir)), |
| 221 |
cache2 |
| 222 |
) |
| 223 |
expect_identical(sass_cache_get_dir(cache_dir), cache2) |
| 224 |
|
| 225 |
# Can unset cache for a given directory |
| 226 |
sass_cache_set_dir(cache_dir, NULL) |
| 227 |
expect_false(exists(cache_dir, envir = .caches)) |
| 228 |
|
| 229 |
# Calling sass_cache_get_dir() when it doesn't exist should error |
| 230 |
cache_dir <- tempfile("sass-cache-test-") |
| 231 |
expect_error(sass_cache_get_dir(cache_dir)) |
| 232 |
|
| 233 |
# Calling sass_cache_get_dir() when the dir exists but no cache object has |
| 234 |
# been registered returns NULL |
| 235 |
dir.create(cache_dir) |
| 236 |
expect_null(sass_cache_get_dir(cache_dir)) |
| 237 |
|
| 238 |
|
| 239 |
# Calling sass_cache_get_dir() when the dir doesn't exist (and cache object |
| 240 |
# isn't registered) with create=TRUE should create the dir and the cache |
| 241 |
# object. |
| 242 |
cache_dir <- tempfile("sass-cache-test-") |
| 243 |
cache_obj <- sass_cache_get_dir(cache_dir, create = TRUE) |
| 244 |
expect_true(dir.exists(cache_dir)) |
| 245 |
expect_true(inherits(cache_obj, "FileCache")) |
| 246 |
|
| 247 |
# Calling sass_cache_get_dir() when the dir exists but cache object does not |
| 248 |
# with create=TRUE should create the cache object. |
| 249 |
cache_dir <- tempfile("sass-cache-test-") |
| 250 |
dir.create(cache_dir) |
| 251 |
cache_obj <- sass_cache_get_dir(cache_dir, create = TRUE) |
| 252 |
expect_true(dir.exists(cache_dir)) |
| 253 |
expect_true(inherits(cache_obj, "FileCache")) |
| 254 |
}) |
| 255 |
|
| 256 |
|
| 257 |
test_that("Can pass a cache directory to sass()", { |
| 258 |
cache_dir <- tempfile() |
| 259 |
sass("body {color: red; }", cache = cache_dir) |
| 260 |
|
| 261 |
# Cache directory should have been created |
| 262 |
cache_obj <- sass_cache_get_dir(cache_dir, create = FALSE) |
| 263 |
expect_true(inherits(cache_obj, "FileCache")) |
| 264 |
}) |