Last active
November 23, 2017 12:07
-
-
Save tohagan/d4dc8acf54f44cd2a843b03fb54a8f5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BootScript } from '@mean-expert/boot-script'; | |
import * as Mocha from 'mocha'; | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import { FsDirectory } from '../../common/util/fs-directory'; | |
import { stat } from 'fs'; | |
@BootScript() | |
class TestSuite { | |
private mocha: Mocha; | |
private rootDir: string = path.join(__dirname, '..', '..', 'tests'); | |
constructor(app: any) { | |
if (process.env.NODE_ENV !== 'testing') return; | |
app.on('started', () => { | |
this.setup(); | |
this.run(); | |
}); | |
} | |
setup() { | |
console.log('Setting up Mocha Environment.'); | |
this.mocha = new Mocha(); | |
let dirs = new FsDirectory(this.filterTests, this.mocha.addFile.bind(this.mocha)) | |
.readSyncRecursive(this.rootDir); | |
} | |
filterTests(name: string, stats: fs.Stats): boolean { | |
return stats.isDirectory() || name.substr(-3) === '.ts'; | |
} | |
run(): void { | |
console.log('Running Mocha Tests.'); | |
this.mocha.run((failures: any) => { | |
console.log('Running Mocha Done'); | |
process.exit(failures); | |
}); | |
} | |
} | |
module.exports = TestSuite; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import * as path from 'path'; | |
import * as fs from 'fs'; | |
import { stat } from 'fs'; | |
/** | |
* Filesystem directory wrapper to support recursive/filtered traversal. | |
*/ | |
export class FsDirectory { | |
private files: string[]; | |
constructor( | |
private filter?: (name: string, stats?: fs.Stats, index?: number, dir?: string) => boolean, | |
private cb?: (fileName: string, stats?: fs.Stats) => void | |
) { | |
this.cb = cb || (file => this.files.push(file)); | |
this.filter = filter || (name => name[0] !== '.'); | |
} | |
public readSyncRecursive(rootDir: string) { | |
this.visit(rootDir); | |
} | |
private visit(filename: string) { | |
var me = this; | |
var stats = fs.statSync(filename); | |
if (!fs.existsSync(filename)) return; | |
if (stats.isFile()) { | |
me.cb(filename, stats); | |
} else if (stats.isDirectory()) { | |
let dir = filename; | |
fs.readdirSync(dir) | |
.filter(function (name, index) { | |
return me.filter(name, stats, index, dir); | |
}) | |
.forEach(function (name) { | |
me.readSyncRecursive(path.join(dir, name)); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment