dart-sass/test/cli/shared.dart
Natalie Weizenbaum a25bbb3fc7
Add a ParenthesizedExpression class (#503)
This allows us to accurately track the source spans for parenthesized
expressions, which in turn allows us to print accurate error
indications.

Adding a new class for this more accurately represents the structure
of the expression, but it also involves an extra allocation during
parsing and an extra level of nesting during evaluation which could
have a small but real performance impact.

We could alternatively add a package-internal setter for
Expression.span, and update the source span for parenthesized
expressions after they're initially parsed. However, this has its own
downsides: it adds complexity and mutability to the object model; and
many expression classes currently use lazily-generated spans, so
making them settable would require adding extra slots on those
classes.

I decided to go with the extra class because it only adds overhead
when parentheses are actually used in practice, as opposed to adding
overhead to every list/color/etc. The runtime overhead is also likely
to be mitigated if at any point we add a constant-folding step.
2018-10-17 16:52:32 -07:00

346 lines
10 KiB
Dart

// 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:async';
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)) {
/// Runs the executable on [arguments] plus an output file, then verifies that
/// the contents of the output file match [expected].
Future expectCompiles(List<String> arguments, expected) async {
var sass = await runSass(
arguments.toList()..add("out.css")..add("--no-source-map"));
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; }"));
});
// 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; }"));
});
// 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; }"));
});
});
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(["-"]);
sass.stdin.writeln("a {b: 1 + }");
sass.stdin.close();
expect(
sass.stderr,
emitsInOrder([
"Error: Expected expression.",
"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 parentheized "
"operands", () async {
var sass = await runSass(["-"]);
sass.stdin.writeln("a {b: (#123) + (#456)}");
sass.stdin.close();
expect(
sass.stderr,
emitsInOrder([
'Error: Undefined operation "#123 + #456".',
"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);
});
});
}