Skip to content

Add support for --precompiled node tests #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.12.34-dev

* The `--precompiled` flag is now supported for the vm platform.
* The `--precompiled` flag is now supported for the vm platform and the node
platform.
* On browser platforms the `--precompiled` flag now serves all sources directly
from the precompiled directory, and will never attempt to do its own
compilation.
Expand Down
6 changes: 0 additions & 6 deletions lib/src/runner/loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ class Loader {

var name = (platform.runtime.isJS ? "compiling " : "loading ") + path;
yield new LoadSuite(name, platformConfig, platform, () async {
if (platformConfig.precompiledPath != null &&
(!platform.runtime.isBrowser && !platform.runtime.isDartVM)) {
warn("--precompiled is only supported for browser and vm platforms.",
print: true);
}

var memo = _platformPlugins[platform.runtime];

try {
Expand Down
90 changes: 67 additions & 23 deletions lib/src/runner/node/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,49 +141,93 @@ class NodePlatform extends PlatformPlugin
/// source map for the compiled suite.
Future<Pair<Process, StackTraceMapper>> _spawnProcess(String path,
Runtime runtime, SuiteConfiguration suiteConfig, int socketPort) async {
var dir = new Directory(_compiledDir).createTempSync('test_').path;
var jsPath = p.join(dir, p.basename(path) + ".node_test.dart.js");
if (_config.suiteDefaults.precompiledPath != null) {
return _spawnPrecompiledProcess(path, runtime, suiteConfig, socketPort,
_config.suiteDefaults.precompiledPath);
} else if (_config.pubServeUrl != null) {
return _spawnPubServeProcess(path, runtime, suiteConfig, socketPort);
} else {
return _spawnNormalProcess(path, runtime, suiteConfig, socketPort);
}
}

if (_config.pubServeUrl == null) {
await _compilers.compile('''
/// Compiles [testPath] with dart2js, adds the node preamble, and then spawns
/// a Node.js process that loads that Dart test suite.
Future<Pair<Process, StackTraceMapper>> _spawnNormalProcess(String testPath,
Runtime runtime, SuiteConfiguration suiteConfig, int socketPort) async {
var dir = new Directory(_compiledDir).createTempSync('test_').path;
var jsPath = p.join(dir, p.basename(testPath) + ".node_test.dart.js");
await _compilers.compile('''
import "package:test/src/bootstrap/node.dart";

import "${p.toUri(p.absolute(path))}" as test;
import "${p.toUri(p.absolute(testPath))}" as test;

void main() {
internalBootstrapNodeTest(() => test.main);
}
''', jsPath, suiteConfig);

// Add the Node.js preamble to ensure that the dart2js output is
// compatible. Use the minified version so the source map remains valid.
var jsFile = new File(jsPath);
await jsFile.writeAsString(
preamble.getPreamble(minified: true) + await jsFile.readAsString());

StackTraceMapper mapper;
if (!suiteConfig.jsTrace) {
var mapPath = jsPath + '.map';
mapper = new StackTraceMapper(await new File(mapPath).readAsString(),
mapUrl: p.toUri(mapPath),
packageResolver: await PackageResolver.current.asSync,
sdkRoot: p.toUri(sdkDir));
}
// Add the Node.js preamble to ensure that the dart2js output is
// compatible. Use the minified version so the source map remains valid.
var jsFile = new File(jsPath);
await jsFile.writeAsString(
preamble.getPreamble(minified: true) + await jsFile.readAsString());

return new Pair(await _startProcess(runtime, jsPath, socketPort), mapper);
StackTraceMapper mapper;
if (!suiteConfig.jsTrace) {
var mapPath = jsPath + '.map';
mapper = new StackTraceMapper(await new File(mapPath).readAsString(),
mapUrl: p.toUri(mapPath),
packageResolver: await PackageResolver.current.asSync,
sdkRoot: p.toUri(sdkDir));
}

return new Pair(await _startProcess(runtime, jsPath, socketPort), mapper);
}

/// Spawns a Node.js process that loads the Dart test suite at [testPath]
/// under [precompiledPath].
Future<Pair<Process, StackTraceMapper>> _spawnPrecompiledProcess(
String testPath,
Runtime runtime,
SuiteConfiguration suiteConfig,
int socketPort,
String precompiledPath) async {
StackTraceMapper mapper;
var jsPath =
p.join(precompiledPath, p.basename(testPath) + ".node_test.dart.js");

if (!suiteConfig.jsTrace) {
var mapPath = jsPath + '.map';
var resolver = await SyncPackageResolver
.loadConfig(p.toUri(p.join(precompiledPath, '.packages')));
mapper = new StackTraceMapper(await new File(mapPath).readAsString(),
mapUrl: p.toUri(mapPath),
packageResolver: resolver,
sdkRoot: p.toUri(sdkDir));
}

return new Pair(await _startProcess(runtime, jsPath, socketPort), mapper);
}

/// Requests the compiled js for [testPath] from the pub serve url, prepends
/// the node preamble, and then spawns a Node.js process that loads that Dart
/// test suite.
Future<Pair<Process, StackTraceMapper>> _spawnPubServeProcess(String testPath,
Runtime runtime, SuiteConfiguration suiteConfig, int socketPort) async {
var dir = new Directory(_compiledDir).createTempSync('test_').path;
var jsPath = p.join(dir, p.basename(testPath) + ".node_test.dart.js");
var url = _config.pubServeUrl.resolveUri(
p.toUri(p.relative(path, from: 'test') + '.node_test.dart.js'));
p.toUri(p.relative(testPath, from: 'test') + '.node_test.dart.js'));

var js = await _get(url, path);
var js = await _get(url, testPath);
await new File(jsPath)
.writeAsString(preamble.getPreamble(minified: true) + js);

StackTraceMapper mapper;
if (!suiteConfig.jsTrace) {
var mapUrl = url.replace(path: url.path + '.map');
mapper = new StackTraceMapper(await _get(mapUrl, path),
mapper = new StackTraceMapper(await _get(mapUrl, testPath),
mapUrl: mapUrl,
packageResolver: new SyncPackageResolver.root('packages'),
sdkRoot: p.toUri('packages/\$sdk'));
Expand Down
15 changes: 0 additions & 15 deletions test/runner/node/runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,6 @@ void main() {
await test.shouldExit(1);
});

test("prints a warning with --precompiled", () async {
await d.file("test.dart", _success).create();

var test = await runTest(["-p", "node", "--precompiled=.", "test.dart"]);
expect(
test.stdout,
containsInOrder([
'+0: compiling test.dart',
'Warning: --precompiled is only supported for browser and vm '
'platforms.',
'+1: All tests passed!'
]));
await test.shouldExit(0);
});

test("supports node_modules in the package directory", () async {
await d.dir("node_modules", [
d.dir("my_module", [d.file("index.js", "module.exports.value = 12;")])
Expand Down
60 changes: 53 additions & 7 deletions test/runner/precompiled_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// BSD-style license that can be found in the LICENSE file.

@TestOn("vm")
import 'dart:async';
import 'dart:io';

import 'package:node_preamble/preamble.dart' as preamble;
import 'package:package_resolver/package_resolver.dart';
import 'package:path/path.dart' as p;
import 'package:test_descriptor/test_descriptor.dart' as d;
Expand All @@ -16,7 +19,7 @@ import '../io.dart';

void main() {
group("browser tests", () {
test("runs a precompiled version of a test rather than recompiling",
test("run a precompiled version of a test rather than recompiling",
() async {
await d.file("to_precompile.dart", """
import "package:stream_channel/stream_channel.dart";
Expand Down Expand Up @@ -65,6 +68,45 @@ void main() {
});
}, tags: const ["chrome"]);

group("node tests", () {
test("run a precompiled version of a test rather than recompiling",
() async {
await d.file("test.dart", """
import "package:test/src/bootstrap/node.dart";
import "package:test/test.dart";

void main() {
internalBootstrapNodeTest(() => () => test("success", () {
expect(true, isTrue);
}));
}""").create();
await _writePackagesFile();

var jsPath = p.join(d.sandbox, "test.dart.node_test.dart.js");
var dart2js = await TestProcess.start(
p.join(sdkDir, "bin", "dart2js"),
[
await PackageResolver.current.processArgument,
"test.dart",
"--out=$jsPath",
],
workingDirectory: d.sandbox);
await dart2js.shouldExit(0);

var jsFile = new File(jsPath);
await jsFile.writeAsString(
preamble.getPreamble(minified: true) + await jsFile.readAsString());

await d.file("test.dart", "invalid dart}").create();

var test = await runTest(
["-p", "node", "--precompiled", d.sandbox, "test.dart"]);
expect(test.stdout,
containsInOrder(["+0: success", "+1: All tests passed!"]));
await test.shouldExit(0);
});
}, tags: const ["node"]);

group("vm tests", () {
test("run in the precompiled directory", () async {
await d.dir('test', [
Expand All @@ -85,12 +127,7 @@ void main() {
}
"""),
]).create();
var currentPackages = await PackageResolver.current.packageConfigMap;
var packagesFileContent = new StringBuffer();
currentPackages.forEach((package, location) {
packagesFileContent.writeln('$package:$location');
});
await d.file(".packages", packagesFileContent.toString()).create();
await _writePackagesFile();

var test = await runTest(
["-p", "vm", '--precompiled=${d.sandbox}', 'test/test.dart']);
Expand All @@ -100,3 +137,12 @@ void main() {
});
});
}

Future<Null> _writePackagesFile() async {
var currentPackages = await PackageResolver.current.packageConfigMap;
var packagesFileContent = new StringBuffer();
currentPackages.forEach((package, location) {
packagesFileContent.writeln('$package:$location');
});
await d.file(".packages", packagesFileContent.toString()).create();
}