Skip to content

Commit

Permalink
Add unit test that tests arch separation during the compilation
Browse files Browse the repository at this point in the history
Re-factoring: add new logger, make some methods for the input files to be mix-ins

Urigo/angular2-meteor#102
  • Loading branch information
barbatus committed Mar 9, 2016
1 parent 80c0425 commit 424dc11
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 50 deletions.
29 changes: 0 additions & 29 deletions debug.js

This file was deleted.

10 changes: 9 additions & 1 deletion file-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ FileMixin = {

isBare() {
let fileOptions = this.getFileOptions();
return fileOptions.bare;
return fileOptions && fileOptions.bare;
},

isConfig() {
return this.getBasename() === 'tsconfig.json';
},

isDeclaration() {
return TypeScript.isDeclarationFile(this.getBasename());
}
};
55 changes: 55 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const util = Npm.require('util');

class Logger_ {
constructor() {
this.llevel = process.env.TYPESCRIPT_LOG;
}

newDebug(name) {
let debug = new Debug(name);
if (this.isDebug) debug.start();
return debug;
}

get isDebug() {
return this.llevel >= 2;
}

log(msg) {
if (this.llevel >= 1) {
console.log(msg);
}
}

debug(msg) {
if (this.isDebug) {
console.log(msg);
}
}
};

Logger = new Logger_();

class Debug {
constructor(name) {
this.name = name;
}

start() {
console.log('%s started', this.name);
console.time(util.format('%s time', this.name));
this._started = true;
}

log(msg, ...args) {
if (this._started) {
console.log.apply(null, [msg].concat(args));
}
}

end() {
if (this._started) {
console.timeEnd(util.format('%s time', this.name));
}
}
}
4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Package.describe({
});

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

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

api.addFiles([
'debug.js',
'logger.js',
'file-mixin.js',
'typescript-compiler.js',
'typescript.js'
Expand Down
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
VELOCITY_TEST_PACKAGES=1 meteor test-packages --driver-package=velocity:html-reporter ./
VELOCITY_TEST_PACKAGES=1 TYPESCRIPT_LOG=1 meteor test-packages --driver-package=velocity:html-reporter ./
22 changes: 22 additions & 0 deletions tests/server/unit/compiler-tests_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,26 @@ describe('typescript-compiler', () => {
expect(inputFile.result.bare).toBe(true);
});
});

describe('testing architecture separation', () => {
it('should render diagnostics for some arch files using typings of the same arch', () => {
let compiler = new TypeScriptCompiler();
let clientCode = 'var client: API.Client';
let clientTypings = 'declare module API { interface Client {} };';
let clientFile = new InputFile(clientCode, 'client.ts', 'web');
clientFile.warn = jasmine.createSpy();
let typingsFile1 = new InputFile(clientTypings, 'client/client.d.ts', 'web');

let serverCode = 'var server: API.Client1';
let serverTypings = 'declare module API { interface Server {} };';
let serverFile = new InputFile(serverCode, 'server.ts', 'os');
serverFile.warn = jasmine.createSpy();
let typingsFile2 = new InputFile(serverTypings, 'server/server.d.ts', 'os');
compiler.processFilesForTarget([clientFile, typingsFile1, typingsFile2, serverFile]);

expect(clientFile.warn).not.toHaveBeenCalled();
expect(serverFile.warn).toHaveBeenCalled();
expect(serverFile.warn.calls.first().args[0].message).toContain('Client1');
});
});
});
6 changes: 3 additions & 3 deletions tests/server/unit/input-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ function sha1(content) {
}

InputFile = class InputFile {
constructor(source, fileName, options) {
constructor(source, fileName, arch) {
this.source = source;
this.fileName = fileName;
this.options = options || {};
this.arch = arch || 'os';
}

getContentsAsString() {
Expand Down Expand Up @@ -42,7 +42,7 @@ InputFile = class InputFile {
}

getArch() {
return 'os';
return this.arch;
}

warn(error) {
Expand Down
24 changes: 10 additions & 14 deletions typescript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TypeScriptCompiler = class TypeScriptCompiler {
let archMap = {};
let filesMap = {};
inputFiles.forEach((inputFile, index) => {
if (this.isConfigFile(inputFile)) return;
if (inputFile.isConfig()) return;

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

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

let buildDebug = new DebugLog('tsBuild');
let dbuild = Logger.newDebug('tsBuild');
let tsBuild = new TSBuild(filePaths, getFileContent, buildOptions);

archFiles.forEach(inputFile => {
if (this.isDeclarationFile(inputFile)) return;
if (inputFile.isDeclaration()) return;

let co = compilerOptions;
let source = inputFile.getContentsAsString();
Expand All @@ -76,10 +76,10 @@ TypeScriptCompiler = class TypeScriptCompiler {
let filePath = this.getExtendedPath(inputFile);
let moduleName = this.getFileModuleName(inputFile, co);

let emitDebug = new DebugLog('tsEmit');
let demit = Logger.newDebug('tsEmit');
let result = tsBuild.emit(filePath, moduleName);
this.processDiagnostics(inputFile, result.diagnostics, co);
emitDebug.end();
demit.end();

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

cb();

buildDebug.end();
dbuild.end();
}, future.resolver());

future.wait();

compileDebug.end();
dcompile.end();
}

extendFiles(inputFiles) {
Expand Down Expand Up @@ -149,13 +149,9 @@ TypeScriptCompiler = class TypeScriptCompiler {
return TypeScript.isDeclarationFile(inputFile.getBasename());
}

isConfigFile(inputFile) {
return inputFile.getBasename() === 'tsconfig.json';
}

processConfig(inputFiles) {
let cfgFile = inputFiles.filter(
inputFile => this.isConfigFile(inputFile))[0];
inputFile => inputFile.isConfig())[0];
if (cfgFile) {
let source = cfgFile.getContentsAsString();
let hash = cfgFile.getSourceHash();
Expand Down

0 comments on commit 424dc11

Please sign in to comment.