-
Notifications
You must be signed in to change notification settings - Fork 596
/
Copy patherrorrunner.ts
116 lines (96 loc) · 3.35 KB
/
errorrunner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/// <reference path="../../built/pxtcompiler.d.ts"/>
import * as fs from 'fs';
import * as path from 'path';
/* eslint-disable import/no-unassigned-import mocha-no-side-effect-code */
import "mocha";
import * as chai from "chai";
import { TestHost } from "../common/testHost";
import * as util from "../common/testUtils";
interface TestInfo {
filename: string;
base: string;
text: string;
}
const casesDir = path.join(process.cwd(), "tests", "errors-test", "cases");
function initGlobals() {
let g = global as any
g.pxt = pxt;
g.ts = ts;
g.pxtc = pxtc;
g.btoa = (str: string) => Buffer.from(str, "binary").toString("base64");
g.atob = (str: string) => Buffer.from(str, "base64").toString("binary");
}
initGlobals();
// Just needs to exist
pxt.setAppTarget(util.testAppTarget);
const tsFileNames: string[] = [];
const pyFileNames: string[] = [];
for (const file of fs.readdirSync(casesDir)) {
if (file[0] == ".") {
continue;
}
const filename = path.join(casesDir, file);
if (/\.ts$/.test(file)) {
tsFileNames.push(filename);
} else if (/\.py$/.test(file)) {
pyFileNames.push(filename);
}
};
describe("ts compiler errors", () => {
tsFileNames.forEach(filename => {
it(
"should correctly check " + path.basename(filename),
() => stsErrorTestAsync(filename)
);
});
});
describe("py compiler errors", () => {
pyFileNames.forEach(filename => {
it(
"should correctly check " + path.basename(filename),
() => pyErrorTestAsync(filename)
);
});
});
async function stsErrorTestAsync(filename: string) {
const basename = path.basename(filename);
const text = fs.readFileSync(filename, "utf8");
const pkg = new pxt.MainPackage(new TestHost(basename, { [pxt.MAIN_TS]: text }, [], true));
const target = pkg.getTargetOptions();
const opts = await pkg.getCompileOptionsAsync(target);
opts.testMode = true;
const res = pxtc.compile(opts);
checkDiagnostics(res.diagnostics, basename, text);
}
async function pyErrorTestAsync(filename: string) {
const basename = path.basename(filename);
const text = fs.readFileSync(filename, "utf8");
const res = await util.py2tsAsync(text, /* dependency */null, /* common package */false, /** allowErrors **/ true, filename);
checkDiagnostics(res.diagnostics, basename, text);
}
function checkDiagnostics(diagnostics: pxtc.KsDiagnostic[], baseName: string, fileText: string) {
const lines = fileText.split(/\r?\n/);
for (let diag of diagnostics) {
chai.assert(
errCode(lines[diag.line]) !== 0,
`Error code on line ${diag.line} is not valid ${JSON.stringify(diagnostics, undefined, 4)}`
);
}
lines.forEach((line: string, lineNo: number) => {
const code = errCode(line);
const errorsInLine = diagnostics.filter(d => d.line == lineNo);
chai.assert(
!code || errorsInLine.filter(d => d.code == code).length != 0,
`${baseName}(${lineNo + 1}): expecting error TS${code} but got ${JSON.stringify(errorsInLine, undefined, 4)}`
);
});
function errCode(s: string) {
if (!s)
return 0;
let m = /(\/\/|#)\s*TS(\d\d\d\d\d?)/.exec(s);
if (m)
return parseInt(m[1]);
else
return 0;
}
}