Debian Codegraph @

tests/testthat/test-layers.R

raw
1 # Disable sass cache
2 local_disable_cache()
3
4 assets <- normalizePath(testthat::test_path("test-assets"), mustWork = TRUE)
5
6 blue <- list(color = "blue !default")
7 red <- list(color = "red !default")
8 green <- list(color = "green !default")
9 core <- sass_layer(
10 defaults = blue,
11 functions = "@function my_invert($color, $amount: 100%) {
12 $inverse: change-color($color, $hue: hue($color) + 180);
13 @return mix($inverse, $color, $amount);
14 }",
15 rules = "body { background-color: $color; color: my_invert($color); }"
16 )
17
18 test_that("sass_layer is equivalent to sass", {
19 expect_equal(
20 sass(core),
21 sass(list(blue, "body { background-color: $color; color: yellow; }")),
22 ignore_attr = TRUE
23 )
24 })
25
26 test_that("sass layer format", {
27 expect_snapshot(format(core))
28 expect_snapshot(core)
29
30 layer1 <- sass_layer(
31 file_attachments = c(
32 file.path(assets, "a.txt")
33 )
34 )
35
36 core_extra <- sass_bundle(core, layer1)
37 expect_output(
38 print(core_extra),
39 "Other Sass Bundle information:"
40 )
41 })
42
43 test_that("sass_bundle() works as intended", {
44 red_layer <- sass_layer(red, rules = ":root{ --color: #{$color}; }")
45 expect_equal(
46 sass(list(red, core, ":root{ --color: #{$color}; }")),
47 sass(sass_bundle(core, red_layer)),
48 ignore_attr = TRUE
49 )
50 })
51
52 test_that("sass_bundle_remove() will remove all layers", {
53
54 obj <-
55 sass_bundle(
56 sass_bundle(core, red = ":root{ --color: #{$color}; }"),
57 green = sass_layer(green)
58 )
59
60 obj_slim <- sass_bundle_remove(obj, "red")
61 expected <- sass_bundle(core, green = sass_layer(green))
62 expect_identical(obj_slim, expected)
63
64 just_core <- sass_bundle_remove(obj, c("green", "red"))
65 only_core <- sass_bundle(core)
66 expect_identical(just_core, only_core)
67
68 expect_equal(
69 names(obj$layers),
70 c("", "red", "green")
71 )
72 expect_equal(
73 names(expected$layers),
74 c("", "green")
75 )
76
77 expect_true(is_sass_bundle(obj_slim))
78 expect_true(is_sass_bundle(expected))
79 })
80
81 test_that("additional merging features", {
82 layer1 <- sass_layer(
83 file_attachments = c(
84 file.path(assets, "a.txt")
85 )
86 )
87
88 layer2 <- sass_layer(
89 file_attachments = c(
90 "b/b1.txt" = file.path(assets, "b/b1.txt")
91 )
92 )
93
94 layer_merged <- as_sass_layer(sass_bundle(layer1, layer2))
95 expect_identical(
96 layer_merged$file_attachments,
97 c(
98 file.path(assets, "a.txt"),
99 "b/b1.txt" = file.path(assets, "b/b1.txt")
100 )
101 )
102 })
103
104 test_that("file attachments are written", {
105 layer1 <- sass_layer(
106 rules = "body { background-color: $body-bg; }",
107 file_attachments = c(
108 "assets/txt/a.txt" = file.path(assets, "a.txt")
109 )
110 )
111
112 input <- list(
113 list("body-bg" = "silver"),
114 layer1
115 )
116
117 expect_error(
118 sass(input, write_attachments = TRUE),
119 "cannot be used when output=NULL"
120 )
121
122 expect_warning(sass(input), NA)
123
124 tmpdir <- tempfile()
125 stopifnot(nzchar(tmpdir)) # Precaution because we'll `rm -rf` this later
126 on.exit(unlink(tmpdir, recursive = TRUE), add = TRUE)
127
128 dir.create(tmpdir)
129
130 sass(input,
131 output = file.path(tmpdir, "styles.css"),
132 write_attachments = TRUE)
133
134 expect_true(
135 file.exists(file.path(tmpdir, "assets/txt/a.txt"))
136 )
137
138 # the default for write_attachments warns if file attachments are present
139 expect_warning(
140 sass(input, output = file.path(tmpdir, "styles1.css")),
141 "file attachments that are being ignored"
142 )
143 # when write_attachments = FALSE, the warning is suppressed
144 expect_warning(
145 sass(input, output = file.path(tmpdir, "styles1.css"), write_attachments = FALSE),
146 NA
147 )
148
149 })
150
151 test_that("write_file_attachments edge cases", {
152 tmpdir <- tempfile()
153 stopifnot(nzchar(tmpdir)) # Precaution because we'll `rm -rf` this later
154
155 dir.create(tmpdir)
156 oldwd <- getwd()
157 setwd(tmpdir)
158 on.exit({
159 setwd(oldwd)
160 unlink(tmpdir, recursive = TRUE)
161 }, add = TRUE)
162
163 # === Failures ===========
164
165 dir.create("test0")
166
167 # Relative dest path points upward
168 expect_error(write_file_attachments(
169 c("foo/../../bar" = file.path(assets, "a.txt")),
170 output_path = "test0"
171 ), "Illegal file attachment destination")
172
173 # Relative source path not allowed
174 expect_error(write_file_attachments(
175 c("test0"),
176 output_path = "test0"
177 ), "must be absolute")
178
179 # File attachment must exist
180 expect_error(write_file_attachments(
181 c("a.txt" = "/weflkjwkefjeg.txt"),
182 output_path = "test0"
183 ), "must exist")
184
185 # === Successes ===========
186
187 dir.create("test1")
188 write_file_attachments(
189 c(
190 "files/A" = file.path(assets, "a.txt"),
191 "files/B" = file.path(assets, "b/b1.txt"),
192 "C" = file.path(assets, "c")
193 ),
194 "test1"
195 )
196 expect_identical(
197 sort(list.files("test1", all.files = TRUE, recursive = TRUE)),
198 c("C/c1.txt", "C/d/d1.txt", "files/A", "files/B")
199 )
200
201 dir.create("test2")
202 write_file_attachments(
203 c(
204 "a1" = file.path(assets, "a.txt"),
205 "a2/" = file.path(assets, "a.txt"),
206 file.path(assets, "b"),
207 "c1/" = file.path(assets, "c"),
208 "c2" = file.path(assets, "c"),
209 "c3/" = file.path(assets, "c/"),
210 "c4" = file.path(assets, "c/")
211 ),
212 "test2"
213 )
214 expect_identical(
215 sort(list.files("test2", all.files = TRUE, recursive = TRUE)),
216 c(
217 "a1",
218 "a2/a.txt",
219 "b1.txt",
220 "c1/c1.txt", "c1/d/d1.txt",
221 "c2/c1.txt", "c2/d/d1.txt",
222 "c3/c1.txt", "c3/d/d1.txt",
223 "c4/c1.txt", "c4/d/d1.txt"
224 )
225 )
226 })
227
228
229
230 test_that("sass_layer_file() basically works", {
231 f <- file.path(assets, "test-layer-file.scss")
232 expect_snapshot(str(sass_layer_file(f)))
233 })