Skip to content

Commit

Permalink
Test for each pie working.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralt committed Aug 18, 2012
1 parent c8beb9e commit 8749292
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
40 changes: 36 additions & 4 deletions cli/test.js
@@ -1,13 +1,45 @@
var fs = require( 'fs' ),
path = require( 'path' ),
mocha = require( 'mocha' );
path = require( 'path' );

module.exports = {
getEngine: function( engine ) {
// This function is necessary to only require once
// even when a function is called several times
if ( !this.engine ) {
this.engine = require(
path.join( __dirname, 'tests', engine + '.js' )
);
}
return this.engine;
},

test: function( pie ) {
// First, get the pie if it's not an object
if ( typeof pie === 'string' ) {
pie = getPie( pie );
}

// Get the config to know which test engine to use
var config = JSON.parse(
fs.readFileSync(
path.join( process.cwd(), 'config.json' )
)
);

// Set the list of supported test engines
var supported = [
'mocha'
];

// If the test engine isn't part of the supported ones,
// stop the program immediately.
if ( !~supported.indexOf( config[ 'test engine' ] ) ) {
console.error( 'Test engine not supported.' );
process.exit( -1 );
}

// Run the test engine
module.exports.getEngine( config[ 'test engine' ] ).test( pie );
},

testAll: function() {
Expand All @@ -16,8 +48,8 @@ module.exports = {

// For each pie, run mocha
Object.keys( pies ).forEach( function( pie ) {
this.test( pies[ pie ] );
}, this );
module.exports.test( pies[ pie ] );
});
}
};

Expand Down
44 changes: 44 additions & 0 deletions cli/tests/mocha.js
@@ -0,0 +1,44 @@
var Mocha = require( 'mocha' ),
mocha = new Mocha,
fs = require( 'fs' ),
path = require( 'path' );

module.exports = {
// Run the test using mocha on a pie
test: function( pie ) {
// Get all the test files of this pie
var files = fs.readdirSync(
path.join( process.cwd(), 'pies', pie.path, 'test' )

).filter( function( file ) {
// Keep only the .js files
return file.substr( -3 ) === '.js';

}).forEach( function( file ) {
// Add each file to mocha
mocha.addFile(
path.join(
process.cwd(),
'pies',
pie.path,
'test',
file
)
);
});

// If there is no file to test in this pie, just return
if ( mocha.files.length === 0 ) {
return;
}

// Get the pie name by splitting the path
var pieName = pie.path.split( '/' ).reverse()[ 0 ];

console.log( '\nRunning tests for ' + pieName );

// And run mocha tests while setting the reporter to "list"
mocha.reporter( 'list' ).run();
}
};

0 comments on commit 8749292

Please sign in to comment.