2017-10-21 01:56:54 +02:00
|
|
|
// Copyright 2017 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';
|
2018-06-15 22:42:36 +02:00
|
|
|
import 'dart:convert';
|
2018-07-24 01:20:06 +02:00
|
|
|
import 'dart:js_util';
|
2017-10-21 01:56:54 +02:00
|
|
|
|
|
|
|
import 'package:js/js.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2018-04-21 02:55:13 +02:00
|
|
|
import 'package:sass/src/io.dart';
|
2018-07-24 01:20:06 +02:00
|
|
|
import 'package:sass/src/node/function.dart';
|
2018-04-21 02:55:13 +02:00
|
|
|
|
2017-10-21 01:56:54 +02:00
|
|
|
import '../hybrid.dart';
|
|
|
|
import 'api.dart';
|
|
|
|
|
2018-11-06 00:24:14 +01:00
|
|
|
@JS('process.env')
|
|
|
|
external Object get _environment;
|
|
|
|
|
2017-10-21 01:56:54 +02:00
|
|
|
String sandbox;
|
|
|
|
|
|
|
|
void useSandbox() {
|
|
|
|
setUp(() async {
|
|
|
|
sandbox = await createTempDir();
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
if (sandbox != null) await deleteDirectory(sandbox);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Validates that a [RenderError]'s `toString()` and `message` both equal
|
|
|
|
/// [text].
|
|
|
|
Matcher toStringAndMessageEqual(String text) => predicate((error) {
|
|
|
|
expect(error.toString(), equals("Error: $text"));
|
|
|
|
expect(error.message, equals(text));
|
2018-11-01 02:13:04 +01:00
|
|
|
expect(error.formatted, equals("Error: $text"));
|
2017-10-21 01:56:54 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Returns the result of rendering via [options] as a string.
|
|
|
|
Future<String> render(RenderOptions options) {
|
2018-11-16 00:16:24 +01:00
|
|
|
var completer = Completer<String>();
|
2017-11-30 03:13:48 +01:00
|
|
|
sass.render(options,
|
2018-01-21 01:45:17 +01:00
|
|
|
allowInterop(Zone.current.bindBinaryCallbackGuarded((error, result) {
|
2017-10-21 01:56:54 +02:00
|
|
|
expect(error, isNull);
|
2018-06-15 22:42:36 +02:00
|
|
|
completer.complete(utf8.decode(result.css));
|
2017-11-30 03:13:48 +01:00
|
|
|
})));
|
2017-10-21 01:56:54 +02:00
|
|
|
return completer.future;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asserts that rendering via [options] produces an error, and returns that
|
|
|
|
/// error.
|
|
|
|
Future<RenderError> renderError(RenderOptions options) {
|
2018-11-16 00:16:24 +01:00
|
|
|
var completer = Completer<RenderError>();
|
2017-11-30 03:13:48 +01:00
|
|
|
sass.render(options,
|
2018-01-21 01:45:17 +01:00
|
|
|
allowInterop(Zone.current.bindBinaryCallbackGuarded((error, result) {
|
2017-10-21 01:56:54 +02:00
|
|
|
expect(result, isNull);
|
|
|
|
completer.complete(error);
|
2017-11-30 03:13:48 +01:00
|
|
|
})));
|
2017-10-21 01:56:54 +02:00
|
|
|
return completer.future;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the result of rendering via [options] as a string.
|
|
|
|
String renderSync(RenderOptions options) =>
|
2018-06-15 22:42:36 +02:00
|
|
|
utf8.decode(sass.renderSync(options).css);
|
2017-10-21 01:56:54 +02:00
|
|
|
|
2018-07-24 01:20:06 +02:00
|
|
|
/// Like [renderSync], but goes through the untyped JS API.
|
|
|
|
///
|
|
|
|
/// This lets us test that we properly cast untyped collections without throwing
|
|
|
|
/// type errors.
|
|
|
|
String renderSyncJS(Map<String, Object> options) {
|
|
|
|
var result = _renderSyncJS.call(sass, jsify(options)) as RenderResult;
|
|
|
|
return utf8.decode(result.css);
|
|
|
|
}
|
|
|
|
|
2018-07-24 01:57:21 +02:00
|
|
|
final _renderSyncJS =
|
2018-11-16 00:16:24 +01:00
|
|
|
JSFunction("sass", "args", "return sass.renderSync(args);");
|
2018-07-24 01:20:06 +02:00
|
|
|
|
2017-10-21 01:56:54 +02:00
|
|
|
/// Asserts that rendering via [options] produces an error, and returns that
|
|
|
|
/// error.
|
|
|
|
RenderError renderSyncError(RenderOptions options) {
|
|
|
|
try {
|
|
|
|
sass.renderSync(options);
|
|
|
|
} catch (error) {
|
|
|
|
return error as RenderError;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw "Expected renderSync() to throw an error.";
|
|
|
|
}
|
2018-04-21 02:55:13 +02:00
|
|
|
|
|
|
|
/// Runs the rest of the test with the working directory in the sandbox
|
|
|
|
/// directory.
|
|
|
|
void runTestInSandbox() {
|
|
|
|
// Make sure the module is loaded before we change the working directory.
|
|
|
|
sass;
|
|
|
|
|
|
|
|
var oldWorkingDirectory = currentPath;
|
|
|
|
chdir(sandbox);
|
|
|
|
addTearDown(() => chdir(oldWorkingDirectory));
|
|
|
|
}
|
2018-11-06 00:24:14 +01:00
|
|
|
|
|
|
|
/// Sets the environment variable [name] to [value] within this process.
|
|
|
|
void setEnvironmentVariable(String name, String value) {
|
|
|
|
setProperty(_environment, name, value);
|
|
|
|
}
|
2019-05-21 00:34:57 +02:00
|
|
|
|
|
|
|
// Runs [callback] with the `SASS_PATH` environment variable set to [paths].
|
|
|
|
T withSassPath<T>(List<String> paths, T callback()) {
|
2019-05-31 16:18:35 +02:00
|
|
|
setEnvironmentVariable("SASS_PATH", paths.join(isWindows ? ';' : ':'));
|
2019-05-21 00:34:57 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
return callback();
|
|
|
|
} finally {
|
|
|
|
setEnvironmentVariable("SASS_PATH", null);
|
|
|
|
}
|
|
|
|
}
|