Skip to content

Commit 981ba8e

Browse files
committed
Merge pull request #31 from getdave/feature/add-basic-tests
Feature/add basic tests
2 parents 6ec50d0 + b57a47b commit 981ba8e

14 files changed

+415
-258
lines changed

.jshintrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@
99
"undef": true,
1010
"boss": true,
1111
"eqnull": true,
12-
"node": true,
13-
"es5": true
12+
"node": true
1413
}

Gruntfile.js

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,104 @@
22
* grunt-deployments
33
* https://github.com/getdave/grunt-deployments
44
*
5-
* Copyright (c) 2013 David Smith
5+
* Copyright (c) 2014 David Smith
66
* Licensed under the MIT license.
77
*/
88

99
'use strict';
1010

1111
module.exports = function(grunt) {
1212

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

1416
// Project configuration.
1517
grunt.initConfig({
16-
db_fixture: grunt.file.readJSON('test/fixtures/test_db.json'),
17-
18+
db_fixture: grunt.file.readJSON('test/fixtures/basic_config.json'),
19+
vows: {
20+
all: {
21+
options: {
22+
// String {spec|json|dot-matrix|xunit|tap}
23+
// defaults to "dot-matrix"
24+
reporter: "spec",
25+
// String or RegExp which is
26+
// matched against title to
27+
// restrict which tests to run
28+
// onlyRun: /helper/,
29+
// Boolean, defaults to false
30+
verbose: false,
31+
// Boolean, defaults to false
32+
silent: false,
33+
// Colorize reporter output,
34+
// boolean, defaults to true
35+
colors: true,
36+
// Run each test in its own
37+
// vows process, defaults to
38+
// false
39+
isolate: false,
40+
// String {plain|html|json|xml}
41+
// defaults to none
42+
coverage: "json"
43+
},
44+
// String or array of strings
45+
// determining which files to include.
46+
// This option is grunt's "full" file format.
47+
src: ["test/**/*_test.js"]
48+
}
49+
},
1850
jshint: {
19-
all: [
20-
'Gruntfile.js',
21-
'tasks/*.js',
22-
'<%= nodeunit.tests %>',
23-
],
51+
gruntfile: {
52+
src: 'Gruntfile.js'
53+
},
54+
lib: {
55+
src: ['tasks/*.js']
56+
},
57+
test: {
58+
src: ['test/**/*.js']
59+
},
2460
options: {
2561
jshintrc: '.jshintrc',
2662
},
2763
},
2864

65+
watch: {
66+
gruntfile: {
67+
files: '<%= jshint.gruntfile.src %>',
68+
tasks: ['jshint:gruntfile']
69+
},
70+
lib: {
71+
files: '<%= jshint.lib.src %>',
72+
tasks: ['jshint:lib', 'vows']
73+
},
74+
test: {
75+
files: '<%= jshint.test.src %>',
76+
tasks: ['jshint:test', 'vows']
77+
}
78+
},
79+
2980
// Before generating any new files, remove any previously-created files.
3081
clean: {
3182
tests: ['tmp'],
83+
backups: ['./backups'],
3284
},
3385

3486
deployments: {
3587
options: {
3688
backups_dir: ''
3789
},
38-
local: '<%= db_fixture.local %>',
39-
develop: '<%= db_fixture.develop %>'
40-
},
41-
42-
// Unit tests.
43-
nodeunit: {
44-
tests: ['test/*_test.js'],
90+
local: '<%= db_fixture.local %>', // make sure you've created valid fixture DB creds
91+
develop: '<%= db_fixture.develop %>' // make sure you've created valid fixture DB creds
4592
},
4693

4794
});
4895

4996

50-
// Actually load this plugin's task(s).
97+
// Actually load this plugin's task(s).
5198
grunt.loadTasks('tasks');
5299

53-
// These plugins provide necessary tasks.
54-
grunt.loadNpmTasks('grunt-contrib-jshint');
55-
grunt.loadNpmTasks('grunt-contrib-clean');
56-
grunt.loadNpmTasks('grunt-contrib-nodeunit');
57-
58100
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
59101
// plugin's task(s), then test the result.
60-
grunt.registerTask('test', ['clean', 'deployments', 'nodeunit']);
102+
grunt.registerTask('test', ['clean', 'vows']);
61103

62104
// By default, lint and run all tests.
63105
grunt.registerTask('default', ['jshint', 'test']);

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,12 @@ A string value that represents the default target for the tasks. You can easily
254254

255255
## Contributing
256256

257-
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.
257+
Contributions to this plugin are most welcome. Pull requests are preferred but input on open Issues is also most agreeable!
258+
259+
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.
260+
261+
### Branches and merge strategy
262+
All pull requests should merged into the `develop` branch. __Please do not merge into the `master` branch__.
258263

259264
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/).
260265

lib/dbDump.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
var grunt = require('grunt');
4+
var shell = require('shelljs');
5+
var tpls = require('../lib/tpls');
6+
7+
8+
/**
9+
* Dumps a MYSQL database to a suitable backup location
10+
*/
11+
function dbDump(config, output_paths, noExec) {
12+
13+
var cmd;
14+
var ignoreTables;
15+
var rtn;
16+
17+
grunt.file.mkdir(output_paths.dir);
18+
19+
// 1) Get array of tables to ignore, as defined in the config, and format it correctly
20+
if( config.ignoreTables ) {
21+
ignoreTables = '--ignore-table=' + config.database + "." + config.ignoreTables.join(' --ignore-table='+config.database+'.');
22+
}
23+
24+
// 2) Compile MYSQL cmd via Lo-Dash template string
25+
var tpl_mysqldump = grunt.template.process(tpls.mysqldump, {
26+
data: {
27+
user: config.user,
28+
pass: config.pass,
29+
database: config.database,
30+
host: config.host,
31+
port: config.port || 3306,
32+
ignoreTables: ignoreTables || ''
33+
}
34+
});
35+
36+
// 3) Test whether MYSQL DB is local or whether requires remote access via SSH
37+
38+
if (typeof config.ssh_host === "undefined") { // it's a local connection
39+
grunt.log.writeln("Creating dump of local database");
40+
cmd = tpl_mysqldump;
41+
42+
} else { // it's a remote connection
43+
var tpl_ssh = grunt.template.process(tpls.ssh, {
44+
data: {
45+
host: config.ssh_host
46+
}
47+
});
48+
grunt.log.writeln("Creating dump of remote database");
49+
50+
cmd = tpl_ssh + " \\ " + tpl_mysqldump;
51+
}
52+
53+
if (!noExec) {
54+
// Capture output...
55+
var output = shell.exec(cmd, {silent: true}).output;
56+
// TODO: Add test here to check whether we were able to connect
57+
58+
// Write output to file using native Grunt methods
59+
grunt.file.write( output_paths.file, output );
60+
}
61+
62+
if ( grunt.file.exists(output_paths.file) ) {
63+
64+
grunt.log.oklns("Database dump succesfully exported to: " + output_paths.file);
65+
} else if (noExec) {
66+
grunt.log.oklns("Running with 'noExec' option. Database dump would have otherwise been succesfully exported to: " + output_paths.file);
67+
} else {
68+
grunt.fail.warn("Unable to locate database dump .sql file at " + output_paths.file, 6);
69+
}
70+
71+
rtn = {
72+
cmd: cmd,
73+
output_file: output_paths.file
74+
};
75+
76+
// Return for reference and test suite purposes
77+
return rtn;
78+
79+
}
80+
81+
module.exports = dbDump;

lib/dbImport.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
var grunt = require('grunt');
4+
var shell = require('shelljs');
5+
var tpls = require('../lib/tpls');
6+
7+
/**
8+
* Imports a .sql file into the DB provided
9+
*/
10+
function dbImport(config, src) {
11+
12+
var cmd;
13+
var rtn;
14+
15+
// 1) Create cmd string from Lo-Dash template
16+
var tpl_mysql = grunt.template.process(tpls.mysql, {
17+
data: {
18+
host: config.host,
19+
user: config.user,
20+
pass: config.pass,
21+
database: config.database,
22+
path: src,
23+
port: config.port || 3306
24+
}
25+
});
26+
27+
28+
// 2) Test whether target MYSQL DB is local or whether requires remote access via SSH
29+
if (typeof config.ssh_host === "undefined") { // it's a local connection
30+
grunt.log.writeln("Importing into local database");
31+
cmd = tpl_mysql + " < " + src;
32+
} else { // it's a remote connection
33+
var tpl_ssh = grunt.template.process(tpls.ssh, {
34+
data: {
35+
host: config.ssh_host
36+
}
37+
});
38+
39+
grunt.log.writeln("Importing DUMP into remote database");
40+
41+
cmd = tpl_ssh + " '" + tpl_mysql + "' < " + src;
42+
}
43+
44+
// Execute cmd
45+
shell.exec(cmd);
46+
47+
grunt.log.oklns("Database imported succesfully");
48+
49+
50+
rtn = {
51+
cmd: cmd
52+
};
53+
54+
// Return for reference and test suite purposes
55+
return rtn;
56+
}
57+
58+
module.exports = dbImport;

lib/dbReplace.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var grunt = require('grunt');
4+
var shell = require('shelljs');
5+
var tpls = require('../lib/tpls');
6+
7+
function dbReplace(search,replace,output_file, noExec) {
8+
9+
var cmd = grunt.template.process( tpls.search_replace, {
10+
data: {
11+
search: search,
12+
replace: replace,
13+
path: output_file
14+
}
15+
});
16+
17+
grunt.log.writeln("Replacing '" + search + "' with '" + replace + "' in the database.");
18+
19+
// Execute cmd
20+
if (!noExec) {
21+
shell.exec(cmd);
22+
}
23+
24+
grunt.log.oklns("Database references succesfully updated.");
25+
26+
// Return for reference and test suite purposes
27+
return cmd;
28+
}
29+
30+
module.exports = dbReplace;

lib/generateBackupPaths.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var grunt = require('grunt');
4+
var shell = require('shelljs');
5+
var tpls = require('../lib/tpls');
6+
7+
8+
function generateBackupPaths(target, task_options) {
9+
10+
var rtn = [];
11+
12+
var backups_dir = task_options['backups_dir'] || "backups";
13+
14+
// Create suitable backup directory paths
15+
rtn['dir'] = grunt.template.process(tpls.backup_path, {
16+
data: {
17+
backups_dir: backups_dir,
18+
env: target,
19+
date: grunt.template.today('yyyymmdd'),
20+
time: grunt.template.today('HH-MM-ss'),
21+
}
22+
});
23+
24+
25+
rtn['file'] = rtn['dir'] + '/db_backup.sql';
26+
27+
return rtn;
28+
}
29+
30+
module.exports = generateBackupPaths;

lib/tpls.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Lo-Dash Template Helpers
3+
* http://lodash.com/docs/#template
4+
* https://github.com/gruntjs/grunt/wiki/grunt.template
5+
*/
6+
var tpls = {
7+
8+
search_replace: "sed -i '' 's#<%= search %>#<%= replace %>#g' <%= path %>",
9+
10+
backup_path: "<%= backups_dir %>/<%= env %>/<%= date %>/<%= time %>",
11+
12+
mysql: "mysql -h <%= host %> -u <%= user %> -p<%= pass %> -P<%= port %> <%= database %>",
13+
14+
ssh: "ssh <%= host %>",
15+
16+
mysqldump: "mysqldump -h <%= host %> -u<%= user %> -p<%= pass %> -P<%= port %> <%= database %> <%= ignoreTables %>"
17+
};
18+
19+
20+
module.exports = tpls;

0 commit comments

Comments
 (0)