Skip to content

Commit a4417ff

Browse files
authored
Merge pull request #82 from Perryvw/compiler-error-reporting-tests
Tests for compiler error reporting
2 parents 645aa90 + f3a007d commit a4417ff

10 files changed

Lines changed: 59 additions & 10 deletions

src/Compiler.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { CompilerOptions, parseCommandLine } from "./CommandLineParser";
88
import { LuaTranspiler, TranspileError } from "./Transpiler";
99
import { TSHelper as tsEx } from "./TSHelper";
1010

11-
function compile(fileNames: string[], options: CompilerOptions): void {
11+
export function compile(fileNames: string[], options: CompilerOptions): void {
1212
const program = ts.createProgram(fileNames, options);
1313
const checker = program.getTypeChecker();
1414

@@ -60,10 +60,9 @@ function compile(fileNames: string[], options: CompilerOptions): void {
6060
if (exception.node) {
6161
const pos = ts.getLineAndCharacterOfPosition(sourceFile, exception.node.pos);
6262
// Graciously handle transpilation errors
63+
console.error("Encountered error parsing file: " + exception.message);
6364
console.error(
64-
"Encountered error parsing file: " + exception.message + "\n" +
65-
sourceFile.fileName +
66-
" line: " + (1 + pos.line) + " column: " + pos.character + "\n" +
65+
sourceFile.fileName + " line: " + (1 + pos.line) + " column: " + pos.character + "\n" +
6766
exception.stack
6867
);
6968
process.exit(1);

test/compiler/errorreport.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Any, Expect, Setup, SpyOn, Teardown, Test, TestCase } from "alsatian";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
import * as ts from "typescript";
5+
import { compile } from "../../src/Compiler";
6+
7+
export class CompilerErrorReportTests {
8+
private originalStdErr: any;
9+
private originalStdOut: any;
10+
private originalProcessExit: any;
11+
12+
@TestCase("Encountered error parsing file: Default Imports are not supported, please use named imports instead!\n",
13+
"default_import.ts")
14+
@TestCase("Encountered error parsing file: Unsupported expression kind: Block\n", "invalid_syntax.ts")
15+
@Test("Compile project")
16+
public compileProject(errorMsg: string, ...fileNames: string[]) {
17+
fileNames = fileNames.map((file) => path.resolve(__dirname, "testfiles", file));
18+
compile(fileNames, {outDir: ".", rootDir: "."});
19+
20+
Expect(process.stderr.write).toHaveBeenCalledWith(errorMsg, Any);
21+
22+
Expect(process.exit).toHaveBeenCalledWith(1);
23+
}
24+
25+
@Setup
26+
private _spyProcess() {
27+
this.originalProcessExit = process.exit;
28+
this.originalStdOut = process.stdout.write;
29+
this.originalStdErr = process.stderr.write;
30+
31+
SpyOn(process, "exit").andStub();
32+
SpyOn(process.stderr, "write").andStub();
33+
SpyOn(process.stdout, "write").andStub();
34+
}
35+
36+
@Teardown
37+
private _resetProcess() {
38+
process.exit = this.originalProcessExit;
39+
process.stdout.write = this.originalStdOut;
40+
process.stderr.write = this.originalStdErr;
41+
}
42+
43+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function getAllFiles(dir: string): string[] {
1717
);
1818
}
1919

20-
export class CompilerTests {
20+
export class CompilerProjectTests {
2121

2222
private existingFiles: string[];
2323
private filesAfterCompile: string[];
@@ -28,7 +28,11 @@ export class CompilerTests {
2828
this.filesAfterCompile = [];
2929
}
3030

31-
@TestCase("tsconfig.default.json",
31+
@TestCase("tsconfig.json",
32+
"typescript_lualib.lua",
33+
"test_src/test_lib/file.lua",
34+
"test_src/main.lua")
35+
@TestCase("test_src/main.ts",
3236
"typescript_lualib.lua",
3337
"test_src/test_lib/file.lua",
3438
"test_src/main.lua")

test/compiler/project/tsconfig.bothDirOptions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./tsconfig.default.json",
2+
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"outDir": "out_dir",
55
"rootDir": "test_src"
File renamed without changes.

test/compiler/project/tsconfig.outDir.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./tsconfig.default.json",
2+
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"outDir": "out_dir"
55
}

test/compiler/project/tsconfig.rootDir.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./tsconfig.default.json",
2+
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"rootDir": "test_src"
55
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import Test from "./default_export";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const variable = () => {} => {};

test/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"exclude": [
77
"translation/ts/*",
8-
"compiler/project/*"
8+
"compiler/project/*",
9+
"compiler/testfiles/*"
910
]
1011
}

0 commit comments

Comments
 (0)