Skip to content

Commit

Permalink
Merge pull request #83 from CleverStack/feature/update-tests
Browse files Browse the repository at this point in the history
Fixing travis for unit tests
  • Loading branch information
pilsy committed Jun 13, 2014
2 parents 2050b54 + 32997f1 commit c3ec404
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 271 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ before_install:
- npm install -g grunt-cli
- npm install -g bower
- npm install -g cleverstack-cli
- npm install ncp
- npm install q

before_script:
- mysql -e 'create database test_db;'
- node tests/before_travis_test.js
- export NODE_ENV=test
- export NODE_PATH=./tests/unit/:./lib/:./modules/
- export NODE_PATH=./lib/:./modules/
- node tests/before_travis_test.js

script:
- "clever test unit"
- "npm test"

notifications:
email:
Expand All @@ -21,7 +19,7 @@ notifications:
- "chat.freenode.net#cleverstack"

env:
- NODE_ENV=prod NODE_PATH=./tests/unit/:./lib/:./modules/
- NODE_ENV=test NODE_PATH=./lib/:./modules/ DEBUG=ModuleLoader,Modules,Models,Services,Controllers,clever-controller

language: node_js

Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,23 @@
"underscore": "~1.5.2"
},
"devDependencies": {
"grunt-contrib-watch": "",
"chai": "",
"grunt-concurrent": "~0.3.0",
"grunt-contrib-clean": "",
"grunt-contrib-jshint": "",
"grunt-contrib-connect": "~0.3.0",
"grunt-contrib-jshint": "",
"grunt-contrib-watch": "",
"grunt-exec": "~0.4.2",
"grunt-mocha-test": "",
"grunt-nodemon": "",
"karma": "",
"matchdep": "",
"mocha": "",
"chai": "",
"ncp": "^0.5.1",
"rimraf": "^2.2.8",
"should": "~2.1.1",
"sinon": "",
"karma": "",
"supertest": "",
"grunt-concurrent": "~0.3.0",
"grunt-exec": "~0.4.2",
"should": "~2.1.1"
"supertest": ""
},
"bundledDependencies": []
}
216 changes: 90 additions & 126 deletions tests/before_travis_test.js
Original file line number Diff line number Diff line change
@@ -1,148 +1,112 @@
var Q = require( 'q' )
, spawn = require( 'child_process' ).spawn
, path = require( 'path' )
, fs = require( 'fs' )
, ncp = require('ncp').ncp;

//install clever-orm module to test project
function installORM () {
var defered = Q.defer()
, proc = spawn ( 'clever', [ '-v', '-f', 'install', 'clever-orm' ], { cwd: path.resolve( path.join( __dirname, '..' ) ) } );

console.log( 'step #1 - install clever-orm module - begin\n' );

proc.stdout.on('data', function (data) {
var str = data.toString()
, objs = [
{ reg: /Database username/ , write: 'travis\n' },
{ reg: /Database password/ , write: '\n' },
{ reg: /Database name/ , write: 'test_db\n' },
{ reg: /Database dialect/ , write: '\n' },
{ reg: /Database port/ , write: '3306\n' },
{ reg: /Database host/ , write: '127.0.0.1\n' },
];

if ( str.match( /ing/ ) !== null ) {
console.log( str )
}

objs.forEach ( function ( obj ) {
if ( str.match( obj.reg ) !== null ) {
proc.stdin.write( obj.write );
}
var Promise = require( 'bluebird' )
, spawn = require( 'child_process' ).spawn
, path = require( 'path' )
, fs = require( 'fs' )
, rimraf = require( 'rimraf' )
, ncp = require( 'ncp' );

function installTestModule() {
return new Promise( function( resolve, reject ) {
var pkgJson = path.resolve( path.join( __dirname, '..', 'package.json' ) )
, packageJson = require( pkgJson )
, source = path.resolve( path.join( __dirname, 'unit', 'test-module' ) )
, dest = path.resolve( path.join( __dirname, '..', 'modules', 'test-module' ) );

console.log( 'step #2 - install test-module and add to bundledDependencies - begin' );

rimraf( dest, function( e ) {
if ( e === null ) {
ncp( source, dest, function( err ) {
if ( err !== null ) {
console.log( 'Error in step #2 - ' + err + '\n');
reject( e );
} else if ( packageJson.bundledDependencies.indexOf( 'test-module' ) === -1 ) {
packageJson.bundledDependencies.push( 'test-module' );
fs.writeFile( pkgJson, JSON.stringify( packageJson, null, ' ' ), function( e ) {
if ( !!e ) {
console.log( 'Error in step #2 - ' + e + '\n');
reject( e );
} else {
console.log( 'step #2 - completed' );
resolve();
}
});
} else {
console.log( 'step #2 - completed' );
resolve();
}
});
} else {
console.log( 'Error in step #2 - ' + e + '\n' );
reject();
}
});
});

proc.stderr.on('data', function (data) {
console.log( 'Error in step #1 - ' + data.toString() + '\n');
defered.reject ( data.toString() );
});

proc.on('close', function (code) {
console.log('step #1 process exited with code ' + code + '\n' );
defered.resolve();
});

return defered.promise;
}

//create and update config files
function configFiles( ) {
var deferred = Q.defer()
, ormFile = path.join( __dirname, '..', 'modules', 'clever-orm', 'config', 'default.json' )
, comFile = path.join( __dirname, '..', 'config', 'test.json' )
, ormData = {
"clever-orm": {
"db": {
"username": "travis",
"password": "",
"database": "test_db",
"options": {
"host": "127.0.0.1",
"dialect": "mysql",
"port": 3306
},
},
"modelAssociations": {}
}
}
, comData = {
"environmentName": "TEST",
"memcacheHost": "127.0.0.1:11211",
"clever-orm": {
"db": {
"username": "travis",
"password": "",
"database": "test_db",
"options": {
"dialect": "mysql",
"host": "127.0.0.1",
"port": "3306"
}
}
}
};

console.log( 'step #2 - create and update config files - begin\n' );

fs.writeFile ( ormFile, JSON.stringify ( ormData ), function ( err ) {

if ( err ) {
console.log( 'Error in step #2 - ' + err + '\n');
return deferred.reject ( err );
}
function rebaseDb() {
return new Promise( function( resolve, reject ) {
var proc = spawn( 'grunt', [ 'db' ], { stdio: 'inherit', cwd: path.resolve( path.join( __dirname, '..' ) ) } );

fs.writeFile ( comFile, JSON.stringify ( comData ), function ( err ) {
console.log( 'step #3 - rebase db' );

if ( err ) {
console.log( 'Error in step #2 - ' + err + '\n');
return deferred.reject ( err );
}
proc.stderr.on('data', function (data) {
console.log( 'Error in step #3 - ' + data.toString() + '\n');
reject ( data.toString() );
});

console.log('step #2 process exited with code 0\n' );
deferred.resolve();
proc.on('close', function (code) {
console.log('step #3 process exited with code ' + code + '\n' );
resolve();
});
});

return deferred.promise;
}

//added clever-orm module in bundledDependencies
function bundled( ) {
var deferred = Q.defer()
, file = path.join( __dirname, '..', 'package.json' );

console.log( 'step #3 - added clever-orm module in bundledDependencies\n' );

fs.readFile ( file, function ( err, data ) {

if ( err ) {
console.log( 'Error in step #3 - ' + err + '\n');
return deferred.reject ( err );
}

data = JSON.parse ( data );

data.bundledDependencies.push ( 'clever-orm' );
function installORM () {
return new Promise( function( resolve, reject ) {
var objs = [
{ reg: /Database username/ , write: 'travis\n' , done: false },
{ reg: /Database password/ , write: '\n' , done: false },
{ reg: /Database name/ , write: 'test_db\n' , done: false },
{ reg: /Database dialect/ , write: '\n' , done: false },
{ reg: /Database port/ , write: '3306\n' , done: false },
{ reg: /Database host/ , write: '127.0.0.1\n', done: false },
]
, proc = spawn ( 'clever', [ 'install', 'clever-orm' ], { cwd: path.resolve( path.join( __dirname, '..' ) ) } );

console.log( 'step #1 - install clever-orm module - begin\n' );

proc.stdout.on('data', function (data) {
var str = data.toString();

if ( str.match( /ing/ ) !== null ) {
console.log( str )
}

fs.writeFile ( file, JSON.stringify ( data ), function ( err ) {
objs.forEach ( function ( obj, i ) {
if ( obj.done !== true && str.match( obj.reg ) !== null ) {
objs[i].done = true;
proc.stdin.write( obj.write );
}
});
});

if ( err ) {
console.log( 'Error in step #3 - ' + err + '\n');
return deferred.reject ( err );
}
proc.stderr.on('data', function (data) {
console.log( 'Error in step #1 - ' + data.toString() + '\n');
reject ( data.toString() );
});

console.log('step #3 process exited with code 0\n' );
deferred.resolve();
proc.on('close', function (code) {
console.log('step #1 process exited with code ' + code + '\n' );
resolve();
});
});

return deferred.promise;
}

installORM()
.then ( configFiles )
.then ( bundled )
.fail ( function (err) {
.then( installTestModule )
.then( rebaseDb )
.catch( function (err) {
console.log('Error - ' + err );
});
Loading

0 comments on commit c3ec404

Please sign in to comment.