Skip to content

Commit 0eb1cb4

Browse files
committed
Resolve issues with unit tests
1 parent 44bb37c commit 0eb1cb4

File tree

5 files changed

+49
-38
lines changed

5 files changed

+49
-38
lines changed

source/application/compiler/compiler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ export class Compiler {
102102

103103
const templatedProgram = this.createProgram(parsedCommandLine.fileNames, suboptions, compilerVmHost, program);
104104

105-
templatedProgram.emit(undefined, writer, null, false);
105+
const emitResult = templatedProgram.emit(undefined, writer, null, false);
106+
if (emitResult) {
107+
this.conditionalException(emitResult.diagnostics);
108+
}
109+
else {
110+
throw new CompilerException('Complete compilation failure for unknown reason');
111+
}
106112
}
107113

108114
private createProgram(files: Array<string>, compilerOptions: CompilerOptions, compilerHost: CompilerHost, previousProgram?: Program): Program {

source/application/compiler/tests/compiler.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {NgModuleFactory} from '@angular/core';
2-
31
import {getApplicationProject} from 'test-fixtures';
42

53
import {Compiler} from '../compiler';
@@ -15,7 +13,6 @@ describe('Compiler', () => {
1513
const module = await compiler.compile();
1614
expect(module).not.toBeNull();
1715
expect(typeof module).toBe('object');
18-
expect(module instanceof NgModuleFactory).toBe(true);
19-
expect((<{name?}>module).name).toBe('BasicInlineApplicationNgFactory');
16+
expect(module.constructor.name).toBe('NgModuleFactory');
2017
});
2118
});

source/application/compiler/vm/tests/vm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('VirtualMachine', () => {
1515
vmexec(vm => {
1616
vm.define('/foo.js', 'foo', 'return 0');
1717

18-
const result = vm.require('./foo');
18+
const result = vm.require('foo');
1919
expect(result).not.toBeNull();
2020
expect(result).toBe(0);
2121
});
@@ -40,7 +40,7 @@ describe('VirtualMachine', () => {
4040
vmexec(vm => {
4141
vm.define('/foo.js', 'foo', 'return require("@angular/core")');
4242

43-
const core = vm.require('./foo');
43+
const core = vm.require('foo');
4444
expect(core).not.toBeNull();
4545
expect(core.createPlatform).not.toBeNull();
4646
});

source/application/compiler/vm/vm.ts

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {Context, Script, createContext} from 'vm';
1+
import 'reflect-metadata';
2+
3+
import {Script, createContext} from 'vm';
24

35
import {dirname, join, normalize} from 'path';
46

@@ -9,19 +11,15 @@ import {VirtualMachineException} from 'exception';
911
import {transpile} from './transpile';
1012

1113
export class VirtualMachine implements Disposable {
12-
private scripts = new Map<string, Script>();
14+
private scripts = new Map<string, [Script, string]>();
1315
private modules = new Map<string, any>();
1416

1517
private paths = new Set<string>();
1618
private files = new Set<string>();
1719

1820
private fileContents = new Map<string, string>();
1921

20-
private context: Context;
21-
22-
constructor() {
23-
this.context = createContext({global, require: mid => this.require(mid)});
24-
}
22+
private global = {Reflect};
2523

2624
read(filename): string {
2725
return this.fileContents.get(filename);
@@ -39,18 +37,9 @@ export class VirtualMachine implements Disposable {
3937
throw new VirtualMachineException(`Cannot overwrite existing module '${normalizedModuleId}'`);
4038
}
4139

42-
const preamble = {exports: {}, filename, id: normalizedModuleId};
43-
44-
const wrappedCode = `(function() {
45-
const module = ${JSON.stringify(preamble)};
46-
const exports = module.exports;
47-
${code};
48-
return module.exports;
49-
})()`;
50-
51-
const script = new Script(wrappedCode, {filename, displayErrors: true});
40+
const script = new Script(code, {filename, displayErrors: true});
5241

53-
this.scripts.set(moduleId, script);
42+
this.scripts.set(moduleId, [script, filename]);
5443
}
5544

5645
private requireStack = new Array<string>();
@@ -68,17 +57,26 @@ export class VirtualMachine implements Disposable {
6857
if (script != null) {
6958
this.requireStack.push(moduleId);
7059
try {
71-
this.modules.set(normalizedModuleId, this.executeScript(script, moduleId));
60+
const [executable, filename] = script;
61+
moduleResult = this.executeScript(executable, filename, normalizedModuleId);
62+
63+
this.modules.set(normalizedModuleId, moduleResult);
7264
}
7365
finally {
7466
this.requireStack.pop();
7567
}
7668
}
7769
else {
7870
moduleResult = this.conditionalTranspile(normalizedModuleId);
71+
7972
this.modules.set(moduleId, moduleResult);
8073
}
8174
}
75+
76+
if (moduleResult == null) {
77+
throw new VirtualMachineException(`Require of ${moduleId} (normalized: ${normalizedModuleId}) returned null`);
78+
}
79+
8280
return moduleResult;
8381
}
8482

@@ -99,13 +97,26 @@ export class VirtualMachine implements Disposable {
9997
this.modules.clear();
10098
}
10199

102-
private executeScript(script: Script, moduleId: string) {
100+
private executeScript(script: Script, filename: string, moduleId: string) {
103101
try {
104-
return script.runInContext(this.context);
102+
const exports = {};
103+
104+
const context = createContext({
105+
require: mid => this.require(mid),
106+
exports,
107+
global: this.global,
108+
module: {exports, filename, id: moduleId},
109+
__filename: filename,
110+
__dirname: dirname(filename),
111+
});
112+
113+
script.runInContext(context);
114+
115+
return exports;
105116
}
106117
catch (exception) {
107118
throw new VirtualMachineException(
108-
`Exception in ${moduleId} in sandboxed virtual machine: ${exception.stack}`, exception);
119+
`Exception in ${moduleId} in sandboxed virtual machine: ${exception.toString()}`, exception);
109120
}
110121
}
111122

@@ -114,26 +125,23 @@ export class VirtualMachine implements Disposable {
114125
}
115126

116127
normalizeModuleId(to: string): string {
117-
const stack = this.requireStack;
118-
119128
if (/^\./.test(to)) {
120-
if (this.requireStack.length > 0) {
129+
const stack = this.requireStack;
130+
if (stack.length > 0) {
121131
return join(dirname(stack[stack.length - 1]), to);
122132
}
123-
else {
124-
throw new VirtualMachineException(
125-
`Cannot determine relative path to ${to} (require stack: ${stack.join(' -> ')})`);
126-
}
127133
}
134+
128135
return to;
129136
}
130137

131138
private conditionalTranspile(moduleId: string) {
132-
if (/^\@angular/.test(moduleId)) {
139+
if (/^@angular/.test(moduleId)) {
133140
const [path, code] = transpile(moduleId);
134141
this.define(path, moduleId, code);
135142
return this.require(moduleId);
136143
}
144+
137145
return require(moduleId);
138146
}
139147
}

source/exception.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class Exception extends Error {
33

44
constructor(msg: string, innerException?: Error) {
55
if (innerException) {
6-
super(`${msg} -> ${innerException.stack}`);
6+
super(`${msg} -> ${innerException.toString()}`);
77
}
88
else {
99
super(msg);

0 commit comments

Comments
 (0)