Fix running tests in node.js
This commit is contained in:
@@ -50,6 +50,7 @@ import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils.TestFileFactory
|
||||
import org.jetbrains.kotlin.test.KotlinTestWithEnvironment
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.Closeable
|
||||
@@ -137,7 +138,8 @@ abstract class BasicBoxTest(
|
||||
|
||||
if (!SKIP_NODE_JS.matcher(expectedText).find()) {
|
||||
val nodeRunnerName = mainModule.outputFileName(outputDir) + ".node.js"
|
||||
val nodeRunnerText = generateNodeRunner(allJsFiles, outputDir, mainModuleName, testFactory.testPackage)
|
||||
val ignored = InTextDirectivesUtils.isIgnoredTarget(TargetBackend.JS, file)
|
||||
val nodeRunnerText = generateNodeRunner(allJsFiles, outputDir, mainModuleName, ignored, testFactory.testPackage)
|
||||
FileUtil.writeToFile(File(nodeRunnerName), nodeRunnerText)
|
||||
}
|
||||
|
||||
@@ -146,25 +148,33 @@ abstract class BasicBoxTest(
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateNodeRunner(files: Collection<String>, dir: File, moduleName: String, testPackage: String?): String {
|
||||
val sb = StringBuilder("var text = \"\";\n")
|
||||
sb.append("var fs = require('fs');\n")
|
||||
|
||||
sb.append("module.exports = function(kotlin, requireFromString) {\n")
|
||||
sb.append("text += 'module.exports = function(kotlin) {\\n';\n")
|
||||
|
||||
for (file in files) {
|
||||
val fileName = FileUtil.getRelativePath(dir, File(file))!!
|
||||
sb.append("text += fs.readFileSync(__dirname + \"/$fileName\") + \"\\n\";\n")
|
||||
}
|
||||
sb.append("text += 'return $moduleName;';\n")
|
||||
sb.append("text += \"};\";\n")
|
||||
|
||||
private fun generateNodeRunner(
|
||||
files: Collection<String>,
|
||||
dir: File,
|
||||
moduleName: String,
|
||||
ignored: Boolean,
|
||||
testPackage: String?
|
||||
): String {
|
||||
val filesToLoad = files.map { FileUtil.getRelativePath(dir, File(it))!! }.map { "\"$it\"" }
|
||||
val fqn = testPackage?.let { ".$it" } ?: ""
|
||||
val loadAndRun = "load([${filesToLoad.joinToString(",")}], '$moduleName')$fqn.box()"
|
||||
|
||||
sb.append("var testModule = requireFromString(text)(kotlin);\n")
|
||||
sb.append("return testModule$fqn.box();\n")
|
||||
sb.append("};")
|
||||
val sb = StringBuilder()
|
||||
sb.append("module.exports = function(load) {\n")
|
||||
if (ignored) {
|
||||
sb.append(" try {\n")
|
||||
sb.append(" var result = $loadAndRun;\n")
|
||||
sb.append(" if (result != 'OK') return 'OK';")
|
||||
sb.append(" return 'fail: expected test failure';\n")
|
||||
sb.append(" }\n")
|
||||
sb.append(" catch (e) {\n")
|
||||
sb.append(" return 'OK';\n")
|
||||
sb.append("}\n")
|
||||
}
|
||||
else {
|
||||
sb.append(" return $loadAndRun;\n")
|
||||
}
|
||||
sb.append("};\n")
|
||||
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
Vendored
+57
-20
@@ -1,17 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
(function () {
|
||||
var kotlinJsLocation = process.env.KOTLIN_JS_LOCATION || __dirname + "/../../../dist/js/kotlin.js";
|
||||
var Module = require('module');
|
||||
var originalRequire = Module.prototype.require;
|
||||
|
||||
Module.prototype.require = function(id) {
|
||||
if (id === "kotlin") {
|
||||
id = kotlinJsLocation;
|
||||
}
|
||||
return originalRequire.call(this, id);
|
||||
};
|
||||
})();
|
||||
|
||||
var assert = require('assert');
|
||||
var fs = require('fs');
|
||||
var kotlinJsLocation = process.env.KOTLIN_JS_LOCATION;
|
||||
if (!kotlinJsLocation) {
|
||||
kotlinJsLocation = "../../../dist/js/kotlin.js";
|
||||
}
|
||||
var kotlin = require(kotlinJsLocation);
|
||||
supplyAsserter(kotlin);
|
||||
var path = require('path');
|
||||
var kotlinJsTestLocation = process.env.KOTLIN_JS_TEST_LOCATION || __dirname + "/../../../dist/js/kotlin-test.js";
|
||||
|
||||
var kotlin = require("kotlin");
|
||||
var kotlinTest = require(kotlinJsTestLocation);
|
||||
supplyAsserter(kotlin, kotlinTest);
|
||||
var requireFromString = require('require-from-string');
|
||||
|
||||
var model = generateModel("out");
|
||||
exposeModel(model, "./out");
|
||||
var baseDir = "out";
|
||||
var model = generateModel(baseDir);
|
||||
exposeModel(model, "./" + baseDir);
|
||||
|
||||
function exposeModel(model, path) {
|
||||
for (var property in model) {
|
||||
@@ -25,8 +39,8 @@ function exposeModel(model, path) {
|
||||
return function() {
|
||||
if (typeof item === "string") {
|
||||
it("", function () {
|
||||
var result = require(childPath);
|
||||
assert.equal("OK", result(kotlin, requireFromString));
|
||||
var result = runTest(require(childPath), childPath);
|
||||
assert.equal("OK", result);
|
||||
});
|
||||
}
|
||||
else if (typeof item === "object") {
|
||||
@@ -39,7 +53,7 @@ function exposeModel(model, path) {
|
||||
|
||||
/**
|
||||
* @param path String
|
||||
* @returns String|{*}
|
||||
* @returns String|{}
|
||||
*/
|
||||
function generateModel(path) {
|
||||
var stats = fs.statSync(path);
|
||||
@@ -56,42 +70,65 @@ function generateModel(path) {
|
||||
}
|
||||
}
|
||||
return !empty ? result : void 0;
|
||||
} else if (stats.isFile()) {
|
||||
}
|
||||
else if (stats.isFile()) {
|
||||
if (path.endsWith(".node.js")) {
|
||||
return path;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return void 0;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
|
||||
function supplyAsserter(kotlin) {
|
||||
function runTest(testRunner, location) {
|
||||
var text = "";
|
||||
var fs = require('fs');
|
||||
var basePath = path.dirname(location);
|
||||
return testRunner(function(fileNames, moduleName) {
|
||||
text += 'module.exports = function(kotlin) {\n';
|
||||
text += "var exports = void 0;";
|
||||
for (var i = 0; i < fileNames.length; ++i) {
|
||||
text += fs.readFileSync(basePath + "/" + fileNames[i]) + "\n";
|
||||
}
|
||||
text += 'var resultModule = typeof emulatedModules != "undefined" ? emulatedModules.' + moduleName + ' : null;\n';
|
||||
text += "resultModule = resultModule || this." + moduleName + ";\n";
|
||||
text += 'return resultModule || ' + moduleName + ';\n';
|
||||
text += "};";
|
||||
return requireFromString(text).call({ "kotlin-test": kotlinTest }, kotlin);
|
||||
});
|
||||
}
|
||||
|
||||
function supplyAsserter(kotlin, kotlinTest) {
|
||||
function AsserterClass() {
|
||||
}
|
||||
AsserterClass.prototype.assertTrue_o10pc4$ = function(lazyMessage, actual) {
|
||||
kotlin.kotlin.test.assertTrue_ifx8ge$(actual, lazyMessage());
|
||||
kotlinTest.kotlin.test.assertTrue_ifx8ge$(actual, lazyMessage());
|
||||
};
|
||||
AsserterClass.prototype.assertTrue_4mavae$ = function(message, actual) {
|
||||
if (!actual) {
|
||||
this.failWithMessage(message);
|
||||
}
|
||||
};
|
||||
AsserterClass.prototype.assertEquals_lzc6tz$ = kotlin.kotlin.test.Asserter.prototype.assertEquals_lzc6tz$;
|
||||
AsserterClass.prototype.assertNotNull_67rc9h$ = kotlinTest.kotlin.test.Asserter.prototype.assertNotNull_67rc9h$;
|
||||
AsserterClass.prototype.assertEquals_lzc6tz$ = kotlinTest.kotlin.test.Asserter.prototype.assertEquals_lzc6tz$;
|
||||
AsserterClass.prototype.fail_pdl1vj$ = function(message) {
|
||||
this.failWithMessage(message);
|
||||
};
|
||||
AsserterClass.prototype.failWithMessage = function(message) {
|
||||
if (message == null) {
|
||||
throw new Kotlin.AssertionError();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new Kotlin.AssertionError(message);
|
||||
}
|
||||
};
|
||||
AsserterClass.$metadata$ = {
|
||||
type: kotlin.Kind.CLASS,
|
||||
baseClasses: [kotlin.kotlin.test.Asserter]
|
||||
baseClasses: [kotlinTest.kotlin.test.Asserter]
|
||||
};
|
||||
kotlin.kotlin.test.asserter = new AsserterClass();
|
||||
kotlinTest.kotlin.test._asserter = new AsserterClass();
|
||||
}
|
||||
Reference in New Issue
Block a user