forked from sass/dart-sass
/
shared.dart
569 lines (479 loc) · 17.4 KB
/
shared.dart
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
import 'package:test_process/test_process.dart';
/// Defines test that are shared between the Dart and Node.js CLI test suites.
void sharedTests(
Future<TestProcess> runSass(Iterable<String> arguments,
{Map<String, String>? environment})) {
/// Runs the executable on [arguments] plus an output file, then verifies that
/// the contents of the output file match [expected].
Future<void> expectCompiles(List<String> arguments, Object expected,
{Map<String, String>? environment}) async {
var sass = await runSass([...arguments, "out.css", "--no-source-map"],
environment: environment);
await sass.shouldExit(0);
await d.file("out.css", expected).validate();
}
test("--help prints the usage documentation", () async {
// Checking the entire output is brittle, so just do a sanity check to make
// sure it's not totally busted.
var sass = await runSass(["--help"]);
expect(sass.stdout, emits("Compile Sass to CSS."));
expect(
sass.stdout, emitsThrough(contains("Print this usage information.")));
await sass.shouldExit(64);
});
test("compiles a Sass file to CSS", () async {
await d.file("test.scss", "a {b: 1 + 2}").create();
var sass = await runSass(["test.scss"]);
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: 3;",
"}",
]));
await sass.shouldExit(0);
});
// Regression test for #437.
test("compiles a Sass file with a BOM to CSS", () async {
await d.file("test.scss", "\uFEFF\$color: red;").create();
var sass = await runSass(["test.scss"]);
expect(sass.stdout, emitsDone);
await sass.shouldExit(0);
});
// On Windows, this verifies that we don't consider the colon after a drive
// letter to be an `input:output` separator.
test("compiles an absolute Sass file to CSS", () async {
await d.file("test.scss", "a {b: 1 + 2}").create();
var sass = await runSass([p.absolute(d.path("test.scss"))]);
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: 3;",
"}",
]));
await sass.shouldExit(0);
});
test("writes a CSS file to disk", () async {
await d.file("test.scss", "a {b: 1 + 2}").create();
var sass = await runSass(["--no-source-map", "test.scss", "out.css"]);
expect(sass.stdout, emitsDone);
await sass.shouldExit(0);
await d.file("out.css", equalsIgnoringWhitespace("a { b: 3; }")).validate();
});
test("creates directories if necessary", () async {
await d.file("test.scss", "a {b: 1 + 2}").create();
var sass =
await runSass(["--no-source-map", "test.scss", "some/new/dir/out.css"]);
expect(sass.stdout, emitsDone);
await sass.shouldExit(0);
await d
.file("some/new/dir/out.css", equalsIgnoringWhitespace("a { b: 3; }"))
.validate();
});
test("compiles from stdin with the magic path -", () async {
var sass = await runSass(["-"]);
sass.stdin.writeln("a {b: 1 + 2}");
sass.stdin.close();
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: 3;",
"}",
]));
await sass.shouldExit(0);
});
group("can import files", () {
test("relative to the entrypoint", () async {
await d.file("test.scss", "@import 'dir/test'").create();
await d.dir("dir", [d.file("test.scss", "a {b: 1 + 2}")]).create();
await expectCompiles(
["test.scss"], equalsIgnoringWhitespace("a { b: 3; }"));
});
test("from the load path", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.dir("dir", [d.file("test2.scss", "a {b: c}")]).create();
await expectCompiles(["--load-path", "dir", "test.scss"],
equalsIgnoringWhitespace("a { b: c; }"));
});
test("from SASS_PATH", () async {
await d.file("test.scss", """
@import 'test2';
@import 'test3';
""").create();
await d.dir("dir2", [d.file("test2.scss", "a {b: c}")]).create();
await d.dir("dir3", [d.file("test3.scss", "x {y: z}")]).create();
var separator = Platform.isWindows ? ';' : ':';
await expectCompiles(
["test.scss"], equalsIgnoringWhitespace("a { b: c; } x { y: z; }"),
environment: {"SASS_PATH": "dir2${separator}dir3"});
});
// Regression test for #369
test("from within a directory, relative to a file on the load path",
() async {
await d.dir(
"dir1", [d.file("test.scss", "@import 'subdir/test2'")]).create();
await d.dir("dir2", [
d.dir("subdir", [
d.file("test2.scss", "@import 'test3'"),
d.file("test3.scss", "a {b: c}")
])
]).create();
await expectCompiles(["--load-path", "dir2", "dir1/test.scss"],
equalsIgnoringWhitespace("a { b: c; }"));
});
test("relative in preference to from the load path", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.file("test2.scss", "x {y: z}").create();
await d.dir("dir", [d.file("test2.scss", "a {b: c}")]).create();
await expectCompiles(["--load-path", "dir", "test.scss"],
equalsIgnoringWhitespace("x { y: z; }"));
});
test("in load path order", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.dir("dir1", [d.file("test2.scss", "a {b: c}")]).create();
await d.dir("dir2", [d.file("test2.scss", "x {y: z}")]).create();
await expectCompiles(
["--load-path", "dir2", "--load-path", "dir1", "test.scss"],
equalsIgnoringWhitespace("x { y: z; }"));
});
test("from the load path in preference to from SASS_PATH", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.dir("dir1", [d.file("test2.scss", "a {b: c}")]).create();
await d.dir("dir2", [d.file("test2.scss", "x {y: z}")]).create();
await expectCompiles(["--load-path", "dir2", "test.scss"],
equalsIgnoringWhitespace("x { y: z; }"),
environment: {"SASS_PATH": "dir1"});
});
test("in SASS_PATH order", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.dir("dir1", [d.file("test2.scss", "a {b: c}")]).create();
await d.dir("dir2", [d.file("test2.scss", "x {y: z}")]).create();
var separator = Platform.isWindows ? ';' : ':';
await expectCompiles(
["test.scss"], equalsIgnoringWhitespace("x { y: z; }"),
environment: {"SASS_PATH": "dir2${separator}dir3"});
});
// Regression test for an internal Google issue.
test("multiple times from different load paths", () async {
await d.file("test.scss", """
@import 'parent/child/test2';
@import 'child/test2';
""").create();
await d.dir("grandparent", [
d.dir("parent", [
d.dir("child", [
d.file("test2.scss", "@import 'test3';"),
d.file("test3.scss", "a {b: c};")
])
])
]).create();
await expectCompiles([
"--load-path",
"grandparent",
"--load-path",
"grandparent/parent",
"test.scss"
], equalsIgnoringWhitespace("a { b: c; } a { b: c; }"));
});
// Regression test for sass/dart-sass#899
test("with both @use and @import", () async {
await d.file("test.scss", """
@use 'library';
@import 'library';
""").create();
await d.dir("load-path", [
d.file("_library.scss", "a { b: regular }"),
d.file("_library.import.scss", "a { b: import-only }")
]).create();
await expectCompiles(["--load-path", "load-path", "test.scss"],
equalsIgnoringWhitespace("a { b: regular; } a { b: import-only; }"));
});
});
group("with --stdin", () {
test("compiles from stdin", () async {
var sass = await runSass(["--stdin"]);
sass.stdin.writeln("a {b: 1 + 2}");
sass.stdin.close();
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: 3;",
"}",
]));
await sass.shouldExit(0);
});
test("writes a CSS file to disk", () async {
var sass = await runSass(["--no-source-map", "--stdin", "out.css"]);
sass.stdin.writeln("a {b: 1 + 2}");
sass.stdin.close();
expect(sass.stdout, emitsDone);
await sass.shouldExit(0);
await d
.file("out.css", equalsIgnoringWhitespace("a { b: 3; }"))
.validate();
});
test("uses the indented syntax with --indented", () async {
var sass = await runSass(["--no-source-map", "--stdin", "--indented"]);
sass.stdin.writeln("a\n b: 1 + 2");
sass.stdin.close();
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: 3;",
"}",
]));
await sass.shouldExit(0);
});
// Regression test.
test("supports @debug", () async {
var sass = await runSass(["--no-source-map", "--stdin"]);
sass.stdin.writeln("@debug foo");
sass.stdin.close();
expect(sass.stderr, emitsInOrder(["-:1 DEBUG: foo"]));
await sass.shouldExit(0);
});
});
test("gracefully reports errors from stdin", () async {
var sass = await runSass(["--no-unicode", "-"]);
sass.stdin.writeln("a {b: 1 + }");
sass.stdin.close();
expect(
sass.stderr,
emitsInOrder([
"Error: Expected expression.",
" ,",
"1 | a {b: 1 + }",
" | ^",
" '",
" - 1:11 root stylesheet",
]));
await sass.shouldExit(65);
});
// Regression test for an issue mentioned in sass/linter#15
test(
"gracefully reports errors for binary operations with parenthesized "
"operands", () async {
var sass = await runSass(["--no-unicode", "-"]);
sass.stdin.writeln("a {b: (#123) + (#456)}");
sass.stdin.close();
expect(
sass.stderr,
emitsInOrder([
'Error: Undefined operation "#123 + #456".',
" ,",
"1 | a {b: (#123) + (#456)}",
" | ^^^^^^^^^^^^^^^",
" '",
" - 1:7 root stylesheet",
]));
await sass.shouldExit(65);
});
test("gracefully handles a non-partial next to a partial", () async {
await d.file("test.scss", "a {b: c}").create();
await d.file("_test.scss", "x {y: z}").create();
var sass = await runSass(["test.scss"]);
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: c;",
"}",
]));
await sass.shouldExit(0);
});
test("emits warnings on standard error", () async {
await d.file("test.scss", "@warn 'aw beans'").create();
var sass = await runSass(["test.scss"]);
expect(sass.stdout, emitsDone);
expect(
sass.stderr,
emitsInOrder([
"WARNING: aw beans",
" test.scss 1:1 root stylesheet",
]));
await sass.shouldExit(0);
});
test("emits debug messages on standard error", () async {
await d.file("test.scss", "@debug 'what the heck'").create();
var sass = await runSass(["test.scss"]);
expect(sass.stdout, emitsDone);
expect(sass.stderr, emits("test.scss:1 DEBUG: what the heck"));
await sass.shouldExit(0);
});
group("with --quiet", () {
test("doesn't emit @warn", () async {
await d.file("test.scss", "@warn heck").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
test("doesn't emit @debug", () async {
await d.file("test.scss", "@debug heck").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
test("doesn't emit parser warnings", () async {
await d.file("test.scss", "a {b: c && d}").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
test("doesn't emit runner warnings", () async {
await d.file("test.scss", "#{blue} {x: y}").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
});
group("with --charset", () {
test("doesn't emit @charset for a pure-ASCII stylesheet", () async {
await d.file("test.scss", "a {b: c}").create();
var sass = await runSass(["test.scss"]);
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: c;",
"}",
]));
await sass.shouldExit(0);
});
test("emits @charset with expanded output", () async {
await d.file("test.scss", "a {b: 👭}").create();
var sass = await runSass(["test.scss"]);
expect(
sass.stdout,
emitsInOrder([
"@charset \"UTF-8\";",
"a {",
" b: 👭;",
"}",
]));
await sass.shouldExit(0);
});
test("emits a BOM with compressed output", () async {
await d.file("test.scss", "a {b: 👭}").create();
var sass = await runSass(
["--no-source-map", "--style=compressed", "test.scss", "test.css"]);
await sass.shouldExit(0);
// We can't verify this as a string because `dart:io` automatically trims
// the BOM.
var bomBytes = utf8.encode("\uFEFF");
expect(
File(p.join(d.sandbox, "test.css"))
.readAsBytesSync()
.sublist(0, bomBytes.length),
equals(bomBytes));
});
});
group("with --no-charset", () {
test("doesn't emit @charset with expanded output", () async {
await d.file("test.scss", "a {b: 👭}").create();
var sass = await runSass(["--no-charset", "test.scss"]);
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: 👭;",
"}",
]));
await sass.shouldExit(0);
});
test("doesn't emit a BOM with compressed output", () async {
await d.file("test.scss", "a {b: 👭}").create();
var sass = await runSass([
"--no-charset",
"--no-source-map",
"--style=compressed",
"test.scss",
"test.css"
]);
await sass.shouldExit(0);
// We can't verify this as a string because `dart:io` automatically trims
// the BOM.
var bomBytes = utf8.encode("\uFEFF");
expect(
File(p.join(d.sandbox, "test.css"))
.readAsBytesSync()
.sublist(0, bomBytes.length),
isNot(equals(bomBytes)));
});
});
group("with --error-css", () {
var message = "Error: Expected expression.";
setUp(() => d.file("test.scss", "a {b: 1 + }").create());
group("not explicitly set", () {
test("doesn't emit error CSS when compiling to stdout", () async {
var sass = await runSass(["test.scss"]);
expect(sass.stdout, emitsDone);
await sass.shouldExit(65);
});
test("emits error CSS when compiling to a file", () async {
var sass = await runSass(["test.scss", "test.css"]);
await sass.shouldExit(65);
await d.file("test.css", contains(message)).validate();
});
});
group("explicitly set", () {
test("emits error CSS when compiling to stdout", () async {
var sass = await runSass(["--error-css", "test.scss"]);
expect(sass.stdout, emitsThrough(contains(message)));
await sass.shouldExit(65);
});
test("emits error CSS when compiling to a file", () async {
var sass = await runSass(["--error-css", "test.scss", "test.css"]);
await sass.shouldExit(65);
await d.file("test.css", contains(message)).validate();
});
});
group("explicitly unset", () {
test("doesn't emit error CSS when compiling to stdout", () async {
var sass = await runSass(["--no-error-css", "test.scss"]);
expect(sass.stdout, emitsDone);
await sass.shouldExit(65);
});
test("emits error CSS when compiling to a file", () async {
var sass = await runSass(["--no-error-css", "test.scss", "test.css"]);
await sass.shouldExit(65);
await d.nothing("test.css").validate();
});
});
});
test("doesn't unassign variables", () async {
// This is a regression test for one of the strangest errors I've ever
// encountered. Every bit of what's going on was necessary to reproduce it,
// *including* running with source maps enabled, which is why it's here and
// not in sass-spec.
await d.file("input.scss", "a {@import 'downstream'}").create();
await d.file("_downstream.scss", r"""
@import 'midstream';
$b: $c;
@mixin d($_) {}
@include d($b);
e {f: $b}
""").create();
await d.file("_midstream.scss", "@forward 'upstream'").create();
await d.file("_upstream.scss", r"$c: g").create();
var sass = await runSass(["input.scss", "output.css"]);
await sass.shouldExit(0);
await d.file("output.css", equalsIgnoringWhitespace("""
a e {
f: g;
}
/*# sourceMappingURL=output.css.map */
""")).validate();
});
}