-
Notifications
You must be signed in to change notification settings - Fork 596
/
Copy pathcompilerunner.ts
143 lines (122 loc) · 4.15 KB
/
compilerunner.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/// <reference path="../../built/pxtcompiler.d.ts"/>
/// <reference path="../../built/pxtsim.d.ts"/>
import * as fs from 'fs';
import * as path from 'path';
import "mocha";
import * as chai from "chai";
import { TestHost } from "../common/testHost";
import * as util from "../common/testUtils";
const casesDir = path.join(process.cwd(), "tests", "compile-test", "lang-test0");
const bareDir = path.relative(process.cwd(), path.join("tests", "compile-test", "bare"));
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);
// TODO(dz): merge with CompileHost in testUtils.ts
class CompileHost extends TestHost {
private basename: string;
private fileText: string;
static langTestText: string;
constructor(public filename: string) {
super("compile-test", { [pxt.MAIN_TS]: "" }, [], true);
this.basename = path.basename(filename);
this.fileText = fs.readFileSync(filename, "utf8");
if (!CompileHost.langTestText) {
CompileHost.langTestText = fs.readFileSync(path.join(casesDir, "lang-test0.ts"), "utf8");
}
}
readFile(module: pxt.Package, filename: string): string {
if (module.id === "this") {
if (filename === "pxt.json") {
return JSON.stringify({
"name": this.name,
"dependencies": { "bare": "file:" + bareDir },
"description": "",
"files": [
"lang-test0.ts",
pxt.MAIN_TS,
]
})
}
else if (filename === pxt.MAIN_TS) {
return this.fileText;
}
else if (filename === "lang-test0.ts") {
return CompileHost.langTestText;
}
}
return super.readFile(module, filename);
}
}
describe("ts compiler", () => {
before(() => {
pxsim.initCurrentRuntime = pxsim.initBareRuntime
})
const filenames: string[] = [];
for (const file of fs.readdirSync(casesDir)) {
if (file[0] == "." || file == "lang-test0.ts") {
continue;
}
const filename = path.join(casesDir, file)
if (file.substr(-3) === ".ts") {
filenames.push(filename)
}
};
describe("with floating point", () => {
filenames.forEach(filename => {
it("should compile and run " + path.basename(filename), function () {
this.timeout(10000)
return compileTestAsync(filename)
});
});
});
});
function fail(msg: string) {
chai.assert(false, msg);
}
function compileTestAsync(filename: string) {
const pkg = new pxt.MainPackage(new CompileHost(filename));
const target = pkg.getTargetOptions();
target.isNative = false;
return pkg.getCompileOptionsAsync(target)
.then(opts => {
opts.ast = true;
const compiled = pxtc.compile(opts);
if (compiled.success) {
return runCoreAsync(compiled)
}
else {
return Promise.reject("Could not compile " + filename + JSON.stringify(compiled.diagnostics, null, 4));
}
})
}
function runCoreAsync(res: pxtc.CompileResult) {
return new Promise<void>((resolve, reject) => {
let f = res.outfiles[pxtc.BINARY_JS]
if (f) {
let timeout = setTimeout(() => {
reject(new Error("Simulating code timed out"))
}, 5000);
let r = new pxsim.Runtime({ type: "run", code: f })
r.errorHandler = (e) => {
clearTimeout(timeout);
reject(e);
}
r.run(() => {
clearTimeout(timeout);
pxsim.dumpLivePointers();
resolve()
})
}
else {
reject(new Error("No compiled js"));
}
})
}