Skip to content

Feature/add basic tests #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"undef": true,
"boss": true,
"eqnull": true,
"node": true,
"es5": true
"node": true
}
86 changes: 64 additions & 22 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,104 @@
* grunt-deployments
* https://github.com/getdave/grunt-deployments
*
* Copyright (c) 2013 David Smith
* Copyright (c) 2014 David Smith
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

// Project configuration.
grunt.initConfig({
db_fixture: grunt.file.readJSON('test/fixtures/test_db.json'),

db_fixture: grunt.file.readJSON('test/fixtures/basic_config.json'),
vows: {
all: {
options: {
// String {spec|json|dot-matrix|xunit|tap}
// defaults to "dot-matrix"
reporter: "spec",
// String or RegExp which is
// matched against title to
// restrict which tests to run
// onlyRun: /helper/,
// Boolean, defaults to false
verbose: false,
// Boolean, defaults to false
silent: false,
// Colorize reporter output,
// boolean, defaults to true
colors: true,
// Run each test in its own
// vows process, defaults to
// false
isolate: false,
// String {plain|html|json|xml}
// defaults to none
coverage: "json"
},
// String or array of strings
// determining which files to include.
// This option is grunt's "full" file format.
src: ["test/**/*_test.js"]
}
},
jshint: {
all: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>',
],
gruntfile: {
src: 'Gruntfile.js'
},
lib: {
src: ['tasks/*.js']
},
test: {
src: ['test/**/*.js']
},
options: {
jshintrc: '.jshintrc',
},
},

watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
lib: {
files: '<%= jshint.lib.src %>',
tasks: ['jshint:lib', 'vows']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'vows']
}
},

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
backups: ['./backups'],
},

deployments: {
options: {
backups_dir: ''
},
local: '<%= db_fixture.local %>',
develop: '<%= db_fixture.develop %>'
},

// Unit tests.
nodeunit: {
tests: ['test/*_test.js'],
local: '<%= db_fixture.local %>', // make sure you've created valid fixture DB creds
develop: '<%= db_fixture.develop %>' // make sure you've created valid fixture DB creds
},

});


// Actually load this plugin's task(s).
// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'deployments', 'nodeunit']);
grunt.registerTask('test', ['clean', 'vows']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ A string value that represents the default target for the tasks. You can easily

## Contributing

Contributions to this plugin are most welcome. This is very much a Alpha release and so if you find a problem please consider raising a pull request or creating a Issue which describes the problem you are having and proposes a solution.
Contributions to this plugin are most welcome. Pull requests are preferred but input on open Issues is also most agreeable!

This is very much a Alpha release and so if you find a problem please consider raising a pull request or creating a Issue which describes the problem you are having and proposes a solution.

### Branches and merge strategy
All pull requests should merged into the `develop` branch. __Please do not merge into the `master` branch__.

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

Expand Down
81 changes: 81 additions & 0 deletions lib/dbDump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

var grunt = require('grunt');
var shell = require('shelljs');
var tpls = require('../lib/tpls');


/**
* Dumps a MYSQL database to a suitable backup location
*/
function dbDump(config, output_paths, noExec) {

var cmd;
var ignoreTables;
var rtn;

grunt.file.mkdir(output_paths.dir);

// 1) Get array of tables to ignore, as defined in the config, and format it correctly
if( config.ignoreTables ) {
ignoreTables = '--ignore-table=' + config.database + "." + config.ignoreTables.join(' --ignore-table='+config.database+'.');
}

// 2) Compile MYSQL cmd via Lo-Dash template string
var tpl_mysqldump = grunt.template.process(tpls.mysqldump, {
data: {
user: config.user,
pass: config.pass,
database: config.database,
host: config.host,
port: config.port || 3306,
ignoreTables: ignoreTables || ''
}
});

// 3) Test whether MYSQL DB is local or whether requires remote access via SSH

if (typeof config.ssh_host === "undefined") { // it's a local connection
grunt.log.writeln("Creating dump of local database");
cmd = tpl_mysqldump;

} else { // it's a remote connection
var tpl_ssh = grunt.template.process(tpls.ssh, {
data: {
host: config.ssh_host
}
});
grunt.log.writeln("Creating dump of remote database");

cmd = tpl_ssh + " \\ " + tpl_mysqldump;
}

if (!noExec) {
// Capture output...
var output = shell.exec(cmd, {silent: true}).output;
// TODO: Add test here to check whether we were able to connect

// Write output to file using native Grunt methods
grunt.file.write( output_paths.file, output );
}

if ( grunt.file.exists(output_paths.file) ) {

grunt.log.oklns("Database dump succesfully exported to: " + output_paths.file);
} else if (noExec) {
grunt.log.oklns("Running with 'noExec' option. Database dump would have otherwise been succesfully exported to: " + output_paths.file);
} else {
grunt.fail.warn("Unable to locate database dump .sql file at " + output_paths.file, 6);
}

rtn = {
cmd: cmd,
output_file: output_paths.file
};

// Return for reference and test suite purposes
return rtn;

}

module.exports = dbDump;
58 changes: 58 additions & 0 deletions lib/dbImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

var grunt = require('grunt');
var shell = require('shelljs');
var tpls = require('../lib/tpls');

/**
* Imports a .sql file into the DB provided
*/
function dbImport(config, src) {

var cmd;
var rtn;

// 1) Create cmd string from Lo-Dash template
var tpl_mysql = grunt.template.process(tpls.mysql, {
data: {
host: config.host,
user: config.user,
pass: config.pass,
database: config.database,
path: src,
port: config.port || 3306
}
});


// 2) Test whether target MYSQL DB is local or whether requires remote access via SSH
if (typeof config.ssh_host === "undefined") { // it's a local connection
grunt.log.writeln("Importing into local database");
cmd = tpl_mysql + " < " + src;
} else { // it's a remote connection
var tpl_ssh = grunt.template.process(tpls.ssh, {
data: {
host: config.ssh_host
}
});

grunt.log.writeln("Importing DUMP into remote database");

cmd = tpl_ssh + " '" + tpl_mysql + "' < " + src;
}

// Execute cmd
shell.exec(cmd);

grunt.log.oklns("Database imported succesfully");


rtn = {
cmd: cmd
};

// Return for reference and test suite purposes
return rtn;
}

module.exports = dbImport;
30 changes: 30 additions & 0 deletions lib/dbReplace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var grunt = require('grunt');
var shell = require('shelljs');
var tpls = require('../lib/tpls');

function dbReplace(search,replace,output_file, noExec) {

var cmd = grunt.template.process( tpls.search_replace, {
data: {
search: search,
replace: replace,
path: output_file
}
});

grunt.log.writeln("Replacing '" + search + "' with '" + replace + "' in the database.");

// Execute cmd
if (!noExec) {
shell.exec(cmd);
}

grunt.log.oklns("Database references succesfully updated.");

// Return for reference and test suite purposes
return cmd;
}

module.exports = dbReplace;
30 changes: 30 additions & 0 deletions lib/generateBackupPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var grunt = require('grunt');
var shell = require('shelljs');
var tpls = require('../lib/tpls');


function generateBackupPaths(target, task_options) {

var rtn = [];

var backups_dir = task_options['backups_dir'] || "backups";

// Create suitable backup directory paths
rtn['dir'] = grunt.template.process(tpls.backup_path, {
data: {
backups_dir: backups_dir,
env: target,
date: grunt.template.today('yyyymmdd'),
time: grunt.template.today('HH-MM-ss'),
}
});


rtn['file'] = rtn['dir'] + '/db_backup.sql';

return rtn;
}

module.exports = generateBackupPaths;
20 changes: 20 additions & 0 deletions lib/tpls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Lo-Dash Template Helpers
* http://lodash.com/docs/#template
* https://github.com/gruntjs/grunt/wiki/grunt.template
*/
var tpls = {

search_replace: "sed -i '' 's#<%= search %>#<%= replace %>#g' <%= path %>",

backup_path: "<%= backups_dir %>/<%= env %>/<%= date %>/<%= time %>",

mysql: "mysql -h <%= host %> -u <%= user %> -p<%= pass %> -P<%= port %> <%= database %>",

ssh: "ssh <%= host %>",

mysqldump: "mysqldump -h <%= host %> -u<%= user %> -p<%= pass %> -P<%= port %> <%= database %> <%= ignoreTables %>"
};


module.exports = tpls;
Loading