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
|
||||||
import org.jetbrains.kotlin.test.KotlinTestUtils.TestFileFactory
|
import org.jetbrains.kotlin.test.KotlinTestUtils.TestFileFactory
|
||||||
import org.jetbrains.kotlin.test.KotlinTestWithEnvironment
|
import org.jetbrains.kotlin.test.KotlinTestWithEnvironment
|
||||||
|
import org.jetbrains.kotlin.test.TargetBackend
|
||||||
import org.jetbrains.kotlin.utils.DFS
|
import org.jetbrains.kotlin.utils.DFS
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.Closeable
|
import java.io.Closeable
|
||||||
@@ -137,7 +138,8 @@ abstract class BasicBoxTest(
|
|||||||
|
|
||||||
if (!SKIP_NODE_JS.matcher(expectedText).find()) {
|
if (!SKIP_NODE_JS.matcher(expectedText).find()) {
|
||||||
val nodeRunnerName = mainModule.outputFileName(outputDir) + ".node.js"
|
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)
|
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 {
|
private fun generateNodeRunner(
|
||||||
val sb = StringBuilder("var text = \"\";\n")
|
files: Collection<String>,
|
||||||
sb.append("var fs = require('fs');\n")
|
dir: File,
|
||||||
|
moduleName: String,
|
||||||
sb.append("module.exports = function(kotlin, requireFromString) {\n")
|
ignored: Boolean,
|
||||||
sb.append("text += 'module.exports = function(kotlin) {\\n';\n")
|
testPackage: String?
|
||||||
|
): String {
|
||||||
for (file in files) {
|
val filesToLoad = files.map { FileUtil.getRelativePath(dir, File(it))!! }.map { "\"$it\"" }
|
||||||
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")
|
|
||||||
|
|
||||||
val fqn = testPackage?.let { ".$it" } ?: ""
|
val fqn = testPackage?.let { ".$it" } ?: ""
|
||||||
|
val loadAndRun = "load([${filesToLoad.joinToString(",")}], '$moduleName')$fqn.box()"
|
||||||
|
|
||||||
sb.append("var testModule = requireFromString(text)(kotlin);\n")
|
val sb = StringBuilder()
|
||||||
sb.append("return testModule$fqn.box();\n")
|
sb.append("module.exports = function(load) {\n")
|
||||||
sb.append("};")
|
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()
|
return sb.toString()
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+57
-20
@@ -1,17 +1,31 @@
|
|||||||
"use strict";
|
"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 assert = require('assert');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var kotlinJsLocation = process.env.KOTLIN_JS_LOCATION;
|
var path = require('path');
|
||||||
if (!kotlinJsLocation) {
|
var kotlinJsTestLocation = process.env.KOTLIN_JS_TEST_LOCATION || __dirname + "/../../../dist/js/kotlin-test.js";
|
||||||
kotlinJsLocation = "../../../dist/js/kotlin.js";
|
|
||||||
}
|
var kotlin = require("kotlin");
|
||||||
var kotlin = require(kotlinJsLocation);
|
var kotlinTest = require(kotlinJsTestLocation);
|
||||||
supplyAsserter(kotlin);
|
supplyAsserter(kotlin, kotlinTest);
|
||||||
var requireFromString = require('require-from-string');
|
var requireFromString = require('require-from-string');
|
||||||
|
|
||||||
var model = generateModel("out");
|
var baseDir = "out";
|
||||||
exposeModel(model, "./out");
|
var model = generateModel(baseDir);
|
||||||
|
exposeModel(model, "./" + baseDir);
|
||||||
|
|
||||||
function exposeModel(model, path) {
|
function exposeModel(model, path) {
|
||||||
for (var property in model) {
|
for (var property in model) {
|
||||||
@@ -25,8 +39,8 @@ function exposeModel(model, path) {
|
|||||||
return function() {
|
return function() {
|
||||||
if (typeof item === "string") {
|
if (typeof item === "string") {
|
||||||
it("", function () {
|
it("", function () {
|
||||||
var result = require(childPath);
|
var result = runTest(require(childPath), childPath);
|
||||||
assert.equal("OK", result(kotlin, requireFromString));
|
assert.equal("OK", result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (typeof item === "object") {
|
else if (typeof item === "object") {
|
||||||
@@ -39,7 +53,7 @@ function exposeModel(model, path) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param path String
|
* @param path String
|
||||||
* @returns String|{*}
|
* @returns String|{}
|
||||||
*/
|
*/
|
||||||
function generateModel(path) {
|
function generateModel(path) {
|
||||||
var stats = fs.statSync(path);
|
var stats = fs.statSync(path);
|
||||||
@@ -56,42 +70,65 @@ function generateModel(path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return !empty ? result : void 0;
|
return !empty ? result : void 0;
|
||||||
} else if (stats.isFile()) {
|
}
|
||||||
|
else if (stats.isFile()) {
|
||||||
if (path.endsWith(".node.js")) {
|
if (path.endsWith(".node.js")) {
|
||||||
return path;
|
return path;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return void 0;
|
return void 0;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return void 0;
|
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() {
|
function AsserterClass() {
|
||||||
}
|
}
|
||||||
AsserterClass.prototype.assertTrue_o10pc4$ = function(lazyMessage, actual) {
|
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) {
|
AsserterClass.prototype.assertTrue_4mavae$ = function(message, actual) {
|
||||||
if (!actual) {
|
if (!actual) {
|
||||||
this.failWithMessage(message);
|
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) {
|
AsserterClass.prototype.fail_pdl1vj$ = function(message) {
|
||||||
this.failWithMessage(message);
|
this.failWithMessage(message);
|
||||||
};
|
};
|
||||||
AsserterClass.prototype.failWithMessage = function(message) {
|
AsserterClass.prototype.failWithMessage = function(message) {
|
||||||
if (message == null) {
|
if (message == null) {
|
||||||
throw new Kotlin.AssertionError();
|
throw new Kotlin.AssertionError();
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw new Kotlin.AssertionError(message);
|
throw new Kotlin.AssertionError(message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
AsserterClass.$metadata$ = {
|
AsserterClass.$metadata$ = {
|
||||||
type: kotlin.Kind.CLASS,
|
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