Skip to content

Commit 5971e3a

Browse files
committed
Removed ts-node from test runner and fixed threaded runner
1 parent 971a178 commit 5971e3a

File tree

5 files changed

+76
-44
lines changed

5 files changed

+76
-44
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
"scripts": {
1515
"build": "tsc -p tsconfig.json && npm run build-lualib",
1616
"build-lualib": "ts-node ./build_lualib.ts",
17-
"test": "npm run style-check && tslint -c ./tslint.json src/lualib/*.ts && npm run build-lualib && ts-node ./test/runner.ts",
18-
"coverage": "nyc npm test && nyc report --reporter=text-lcov > coverage.lcov",
19-
"coverage-html": "nyc npm test && nyc report --reporter=html",
20-
"test-threaded": "style-check && npm run build && ts-node ./test/threaded_runner.ts",
17+
"test": "npm run style-check && npm run build-lualib && tsc -p ./test/tsconfig.json && node ./test/runner.js",
18+
"coverage": "nyc --source-map=true npm test && nyc report --reporter=text-lcov > coverage.lcov",
19+
"coverage-html": "nyc --source-map=true npm test && nyc report --reporter=html",
20+
"test-threaded": "npm run style-check && npm run build && node ./test/threaded_runner.js",
2121
"release-patch": "npm version patch",
2222
"release-minor": "npm version minor",
2323
"release-major": "npm version major",
2424
"preversion": "npm run build && npm test",
2525
"postversion": "git push && git push --tags",
26-
"style-check": "tslint -p .",
26+
"style-check": "tslint -p . && tslint -c ./tslint.json src/lualib/*.ts",
2727
"style-fix": "gts fix && tslint -p . --fix"
2828
},
2929
"bin": {

test/compiler/watchmode.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class CompilerWatchModeTest {
1515
fileToChange = fileToChange;
1616
const fileToChangeOut = fileToChange.replace(".ts", ".lua");
1717

18-
const child = fork(path.join(__dirname, "watcher_proccess.ts"));
18+
const child = fork(path.join(__dirname, "watcher_proccess.js"));
1919
child.send(args);
2020

2121
await this.waitForFileExists(fileToChangeOut, 9000)

test/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from "path";
77
const testSet = TestSet.create();
88

99
// add your tests
10-
testSet.addTestsFromFiles("./test/**/*.spec.ts");
10+
testSet.addTestsFromFiles("./test/**/*.spec.js");
1111

1212
// create a test runner
1313
const testRunner = new TestRunner();

test/test_thread.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { MatchError, TestRunner, TestSet, TestOutcome } from "alsatian";
22
import * as JSON from "circular-json";
33

4-
module.exports = function(input, done) {
4+
module.exports = (input, done) => {
55
const testSet = TestSet.create();
66
testSet.addTestsFromFiles(input.files);
77
const testRunner = new TestRunner();
88

9-
testRunner.onTestComplete((result) => {
9+
let testCount = 0;
10+
let failedTestCount = 0;
11+
12+
testRunner.onTestComplete(result => {
1013
if (result.outcome === TestOutcome.Fail) {
1114
if (result.error instanceof MatchError) {
12-
console.log(`Test ${result.testFixture.description}, ${result.test.key}(${JSON.stringify(result.testCase.caseArguments)}) Failed!`)
15+
console.log(`Test ${result.testFixture.description}, ${result.test.key}(${JSON.stringify(result.testCase.caseArguments)}) Failed!`);
1316
console.log(" ---\n" +
1417
' message: "' +
1518
result.error.message +
@@ -23,9 +26,11 @@ module.exports = function(input, done) {
2326
result.error.expected +
2427
"\n");
2528
}
29+
failedTestCount++;
2630
}
27-
})
31+
testCount++;
32+
});
2833

2934
testRunner.run(testSet)
30-
.then((results) => done(results, input))
35+
.then(() => done(testCount, failedTestCount));
3136
};

test/threaded_runner.ts

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import * as os from "os";
44
import * as path from "path";
55
import {config, Pool} from "threads";
66

7+
function fileArrToString(fileArr: string[]): string {
8+
return fileArr.map(val => path.basename(val).replace(".spec.js", "")).join(", ");
9+
}
10+
11+
function printTestStats(testCount: number, failedTestCount: number, header: string, footer: string): void {
12+
console.log("-----------------");
13+
console.log(header);
14+
console.log(`Total: ${testCount}`);
15+
console.log(`Passed: ${testCount - failedTestCount}`);
16+
console.log(`Failed: ${failedTestCount}`);
17+
console.log(footer);
18+
console.log("-----------------");
19+
}
20+
721
config.set({
822
basepath: {
923
node: __dirname,
@@ -12,45 +26,58 @@ config.set({
1226

1327
let cpuCount = os.cpus().length + 1;
1428
if ("TRAVIS" in process.env && "CI" in process.env) {
15-
// fixed thread count for CI
16-
cpuCount = 8;
29+
// fixed thread count for CI
30+
cpuCount = 8;
1731
}
18-
const testFiles: string[] = glob.sync("./test/**/*.spec.ts");
32+
const testFiles: string[] = glob.sync("./test/**/*.spec.js");
1933
const pool = new Pool(cpuCount);
2034
let jobCounter = 0;
21-
22-
const fileArrToString = (fileArr: string[]) =>
23-
fileArr.map(val => path.basename(val).replace(".spec.ts", "")).join(", ");
35+
const testStartTime = new Date();
36+
const fileCount = testFiles.length;
37+
let exitWithError = false;
38+
let totalTestCount = 0;
39+
let totalFailedTestCount = 0;
2440

2541
console.log(
2642
`Running tests: ${fileArrToString(testFiles)} with ${cpuCount} threads`);
2743

28-
const filesPerThread = Math.floor(testFiles.length / cpuCount);
29-
const threadsWithMoreWork = testFiles.length % cpuCount;
30-
31-
for (let i = 1; i <= cpuCount; i++) {
32-
let files: string[] = [];
33-
if (i <= threadsWithMoreWork) {
34-
files = testFiles.splice(0, filesPerThread + 1);
35-
} else {
36-
files = testFiles.splice(0, filesPerThread);
37-
}
38-
console.log(`Running tests: ${fileArrToString(files)} in thread ${i}`);
39-
40-
pool.run("./test_thread")
41-
.send({files: files})
42-
.on("done",
43-
(results, input) => {
44-
jobCounter++;
45-
console.log(`Tests ${fileArrToString(files)} ${jobCounter}/${
46-
cpuCount} done.`);
47-
})
48-
.on("error", error => {
49-
console.log("Exception in test:", files, error);
50-
});
51-
}
44+
testFiles.forEach(file => {
45+
pool.run("./test_thread")
46+
.send({files: [file]})
47+
.on("done",
48+
(testCount, failedTestCount) => {
49+
if (failedTestCount !== 0) {
50+
exitWithError = true;
51+
}
52+
totalTestCount += testCount;
53+
totalFailedTestCount += failedTestCount;
54+
jobCounter++;
55+
printTestStats(
56+
testCount,
57+
failedTestCount,
58+
`Tests ${file} results:`,
59+
`Thread: ${jobCounter}/${fileCount} done.`);
60+
})
61+
.on("error", error => {
62+
console.log("Fatal non test related Exception in test file:", file, error);
63+
});
64+
});
5265

5366
pool.on("finished", () => {
54-
console.log("Everything done, shutting down the thread pool.");
55-
pool.killAll();
67+
let footer = "All tests passed!";
68+
if (exitWithError) {
69+
footer = "Exiting with Error: One or more tests failed!";
70+
}
71+
printTestStats(totalTestCount, totalFailedTestCount, "Final Results:", footer);
72+
73+
console.log("Everything done, shutting down the thread pool.");
74+
const timeInMs = (new Date().valueOf() - testStartTime.valueOf());
75+
console.log(`Tests took: ${Math.floor(timeInMs / 1000 / 60)}:${Math.floor(timeInMs / 1000) % 60}`);
76+
77+
pool.killAll();
78+
79+
if (exitWithError) {
80+
process.exit(1);
81+
}
5682
});
83+

0 commit comments

Comments
 (0)