diff --git a/.gitignore b/.gitignore index 57f3a0a..aa239b0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules .migrate *.db migrations/ +test/fixtures/tmp diff --git a/test/integration.js b/test/integration.js new file mode 100644 index 0000000..e9fbf7b --- /dev/null +++ b/test/integration.js @@ -0,0 +1,57 @@ +/* global describe, it, beforeEach */ +const path = require('path') +const assert = require('assert') +const rimraf = require('rimraf') +const mkdirp = require('mkdirp') +const run = require('./util/run') + +// Paths +const TMP_DIR = path.join(__dirname, 'fixtures', 'tmp') + +function reset () { + rimraf.sync(TMP_DIR) + mkdirp.sync(TMP_DIR) +} + +describe('integration tests', function () { + beforeEach(reset) + + it('should warn when the migrations are run out of order', function (done) { + run.init(TMP_DIR, [], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + + run.create(TMP_DIR, ['1-one', '-d', 'W'], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + + run.create(TMP_DIR, ['3-three', '-d', 'W'], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + + run.up(TMP_DIR, [], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + + run.create(TMP_DIR, ['2-two', '-d', 'W'], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + + run.up(TMP_DIR, [], function (err, out, code) { + assert(!err) + assert.equal(code, 0) + + // A warning should log, and the process not exit with 0 + // because migration 2 should come before migration 3, + // but migration 3 was already run from the previous + // state + assert(out.indexOf('warn') !== -1) + done() + }) + }) + }) + }) + }) + }) + }) +}) diff --git a/test/util/run.js b/test/util/run.js index 86a8ed7..e7c1c73 100644 --- a/test/util/run.js +++ b/test/util/run.js @@ -1,7 +1,8 @@ 'use strict' +const path = require('path') const spawn = require('child_process').spawn -module.exports = function run (cmd, dir, args, done) { +var run = module.exports = function run (cmd, dir, args, done) { var p = spawn(cmd, ['-c', dir, ...args]) var out = '' p.stdout.on('data', function (d) { @@ -12,9 +13,13 @@ module.exports = function run (cmd, dir, args, done) { }) p.on('error', done) p.on('close', function (code) { - if (code !== 0) { - console.error(out) - } done(null, out, code) }) } + +// Run specific commands +module.exports.up = run.bind(null, path.join(__dirname, '..', '..', 'bin', 'migrate-up')) +module.exports.down = run.bind(null, path.join(__dirname, '..', '..', 'bin', 'migrate-down')) +module.exports.create = run.bind(null, path.join(__dirname, '..', '..', 'bin', 'migrate-create')) +module.exports.init = run.bind(null, path.join(__dirname, '..', '..', 'bin', 'migrate-init')) +module.exports.list = run.bind(null, path.join(__dirname, '..', '..', 'bin', 'migrate-list'))