Skip to content

Commit 424dc11

Browse files
committed
Add unit test that tests arch separation during the compilation
Re-factoring: add new logger, make some methods for the input files to be mix-ins Urigo/angular2-meteor#102
1 parent 80c0425 commit 424dc11

File tree

8 files changed

+102
-50
lines changed

8 files changed

+102
-50
lines changed

debug.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

file-mixin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ FileMixin = {
55

66
isBare() {
77
let fileOptions = this.getFileOptions();
8-
return fileOptions.bare;
8+
return fileOptions && fileOptions.bare;
9+
},
10+
11+
isConfig() {
12+
return this.getBasename() === 'tsconfig.json';
13+
},
14+
15+
isDeclaration() {
16+
return TypeScript.isDeclarationFile(this.getBasename());
917
}
1018
};

logger.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const util = Npm.require('util');
2+
3+
class Logger_ {
4+
constructor() {
5+
this.llevel = process.env.TYPESCRIPT_LOG;
6+
}
7+
8+
newDebug(name) {
9+
let debug = new Debug(name);
10+
if (this.isDebug) debug.start();
11+
return debug;
12+
}
13+
14+
get isDebug() {
15+
return this.llevel >= 2;
16+
}
17+
18+
log(msg) {
19+
if (this.llevel >= 1) {
20+
console.log(msg);
21+
}
22+
}
23+
24+
debug(msg) {
25+
if (this.isDebug) {
26+
console.log(msg);
27+
}
28+
}
29+
};
30+
31+
Logger = new Logger_();
32+
33+
class Debug {
34+
constructor(name) {
35+
this.name = name;
36+
}
37+
38+
start() {
39+
console.log('%s started', this.name);
40+
console.time(util.format('%s time', this.name));
41+
this._started = true;
42+
}
43+
44+
log(msg, ...args) {
45+
if (this._started) {
46+
console.log.apply(null, [msg].concat(args));
47+
}
48+
}
49+
50+
end() {
51+
if (this._started) {
52+
console.timeEnd(util.format('%s time', this.name));
53+
}
54+
}
55+
}

package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Package.describe({
77
});
88

99
Npm.depends({
10-
'meteor-typescript': 'https://github.com/barbatus/meteor-typescript/tarball/52fc6c7f3b5b5df9483a927bc96e7a901b61c4ab',
10+
'meteor-typescript': 'https://github.com/barbatus/meteor-typescript/tarball/c4b62ae8a5cd7ecdd634cbffa287d53a9dbe6220',
1111
'async': '1.4.0'
1212
});
1313

@@ -19,7 +19,7 @@ Package.onUse(function(api) {
1919
], 'server');
2020

2121
api.addFiles([
22-
'debug.js',
22+
'logger.js',
2323
'file-mixin.js',
2424
'typescript-compiler.js',
2525
'typescript.js'

run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/sh
2-
VELOCITY_TEST_PACKAGES=1 meteor test-packages --driver-package=velocity:html-reporter ./
2+
VELOCITY_TEST_PACKAGES=1 TYPESCRIPT_LOG=1 meteor test-packages --driver-package=velocity:html-reporter ./

tests/server/unit/compiler-tests_spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,26 @@ describe('typescript-compiler', () => {
7474
expect(inputFile.result.bare).toBe(true);
7575
});
7676
});
77+
78+
describe('testing architecture separation', () => {
79+
it('should render diagnostics for some arch files using typings of the same arch', () => {
80+
let compiler = new TypeScriptCompiler();
81+
let clientCode = 'var client: API.Client';
82+
let clientTypings = 'declare module API { interface Client {} };';
83+
let clientFile = new InputFile(clientCode, 'client.ts', 'web');
84+
clientFile.warn = jasmine.createSpy();
85+
let typingsFile1 = new InputFile(clientTypings, 'client/client.d.ts', 'web');
86+
87+
let serverCode = 'var server: API.Client1';
88+
let serverTypings = 'declare module API { interface Server {} };';
89+
let serverFile = new InputFile(serverCode, 'server.ts', 'os');
90+
serverFile.warn = jasmine.createSpy();
91+
let typingsFile2 = new InputFile(serverTypings, 'server/server.d.ts', 'os');
92+
compiler.processFilesForTarget([clientFile, typingsFile1, typingsFile2, serverFile]);
93+
94+
expect(clientFile.warn).not.toHaveBeenCalled();
95+
expect(serverFile.warn).toHaveBeenCalled();
96+
expect(serverFile.warn.calls.first().args[0].message).toContain('Client1');
97+
});
98+
});
7799
});

tests/server/unit/input-file.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ function sha1(content) {
77
}
88

99
InputFile = class InputFile {
10-
constructor(source, fileName, options) {
10+
constructor(source, fileName, arch) {
1111
this.source = source;
1212
this.fileName = fileName;
13-
this.options = options || {};
13+
this.arch = arch || 'os';
1414
}
1515

1616
getContentsAsString() {
@@ -42,7 +42,7 @@ InputFile = class InputFile {
4242
}
4343

4444
getArch() {
45-
return 'os';
45+
return this.arch;
4646
}
4747

4848
warn(error) {

typescript-compiler.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TypeScriptCompiler = class TypeScriptCompiler {
2121
let archMap = {};
2222
let filesMap = {};
2323
inputFiles.forEach((inputFile, index) => {
24-
if (this.isConfigFile(inputFile)) return;
24+
if (inputFile.isConfig()) return;
2525

2626
let arch = inputFile.getArch();
2727
let archFiles = archMap[arch];
@@ -46,19 +46,19 @@ TypeScriptCompiler = class TypeScriptCompiler {
4646
compilerOptions, this.extraOptions);
4747
let buildOptions = { compilerOptions, typings };
4848

49-
let compileDebug = new DebugLog('compilation');
49+
let dcompile = Logger.newDebug('compilation');
5050
const future = new Future;
5151
async.each(_.keys(archMap), (arch, cb) => {
5252
let archFiles = archMap[arch];
5353
let filePaths = archFiles.map(inputFile => this.getExtendedPath(inputFile));
54-
compileDebug.log('process files: %s', filePaths);
54+
dcompile.log('process files: %s', filePaths);
5555
buildOptions.arch = arch;
5656

57-
let buildDebug = new DebugLog('tsBuild');
57+
let dbuild = Logger.newDebug('tsBuild');
5858
let tsBuild = new TSBuild(filePaths, getFileContent, buildOptions);
5959

6060
archFiles.forEach(inputFile => {
61-
if (this.isDeclarationFile(inputFile)) return;
61+
if (inputFile.isDeclaration()) return;
6262

6363
let co = compilerOptions;
6464
let source = inputFile.getContentsAsString();
@@ -76,10 +76,10 @@ TypeScriptCompiler = class TypeScriptCompiler {
7676
let filePath = this.getExtendedPath(inputFile);
7777
let moduleName = this.getFileModuleName(inputFile, co);
7878

79-
let emitDebug = new DebugLog('tsEmit');
79+
let demit = Logger.newDebug('tsEmit');
8080
let result = tsBuild.emit(filePath, moduleName);
8181
this.processDiagnostics(inputFile, result.diagnostics, co);
82-
emitDebug.end();
82+
demit.end();
8383

8484
toBeAdded.data = result.code;
8585
toBeAdded.bare = toBeAdded.bare || ! result.isExternal;
@@ -91,12 +91,12 @@ TypeScriptCompiler = class TypeScriptCompiler {
9191

9292
cb();
9393

94-
buildDebug.end();
94+
dbuild.end();
9595
}, future.resolver());
9696

9797
future.wait();
9898

99-
compileDebug.end();
99+
dcompile.end();
100100
}
101101

102102
extendFiles(inputFiles) {
@@ -149,13 +149,9 @@ TypeScriptCompiler = class TypeScriptCompiler {
149149
return TypeScript.isDeclarationFile(inputFile.getBasename());
150150
}
151151

152-
isConfigFile(inputFile) {
153-
return inputFile.getBasename() === 'tsconfig.json';
154-
}
155-
156152
processConfig(inputFiles) {
157153
let cfgFile = inputFiles.filter(
158-
inputFile => this.isConfigFile(inputFile))[0];
154+
inputFile => inputFile.isConfig())[0];
159155
if (cfgFile) {
160156
let source = cfgFile.getContentsAsString();
161157
let hash = cfgFile.getSourceHash();

0 commit comments

Comments
 (0)