Add --snapshots and basic test

This commit is contained in:
Typicode
2015-10-14 08:28:02 +02:00
parent 94cfbabd41
commit f0e6c9ad0c
4 changed files with 36 additions and 6 deletions

View File

@ -1,5 +1,15 @@
# Change Log # Change Log
## [Unreleased][Unreleased]
### Added
* CLI option `-S/--snapshots` to set a custom snapshots directory.
### Fixed
* Fix plural resources: DELETE should return 404 if resource doesn't exist.
## [0.8.1][2015-10-06] ## [0.8.1][2015-10-06]
### Fixed ### Fixed

View File

@ -32,6 +32,11 @@ module.exports = function () {
alias: 's', alias: 's',
description: 'Set static files directory' description: 'Set static files directory'
}, },
snapshots: {
alias: 'S',
description: 'Set snapshots directory',
default: '.'
},
delay: { delay: {
alias: 'd', alias: 'd',
description: 'Add delay to responses (ms)' description: 'Add delay to responses (ms)'

View File

@ -117,7 +117,8 @@ module.exports = function (argv) {
process.stdin.setEncoding('utf8') process.stdin.setEncoding('utf8')
process.stdin.on('data', function (chunk) { process.stdin.on('data', function (chunk) {
if (chunk.trim().toLowerCase() === 's') { if (chunk.trim().toLowerCase() === 's') {
var file = 'db-' + Date.now() + '.json' var filename = 'db-' + Date.now() + '.json'
var file = path.join(argv.snapshots, filename)
app.db.saveSync(file) app.db.saveSync(file)
console.log(' Saved snapshot to ' + file + '\n') console.log(' Saved snapshot to ' + file + '\n')
} }

View File

@ -2,6 +2,7 @@ var os = require('os')
var fs = require('fs') var fs = require('fs')
var path = require('path') var path = require('path')
var cp = require('child_process') var cp = require('child_process')
var assert = require('assert')
var request = require('supertest') var request = require('supertest')
var rmrf = require('rimraf') var rmrf = require('rimraf')
var serverReady = require('server-ready') var serverReady = require('server-ready')
@ -18,8 +19,8 @@ var routesFile = path.join(tmpDir, 'routes.json')
function cli (args) { function cli (args) {
var bin = path.join(__dirname, '../..', pkg.bin) var bin = path.join(__dirname, '../..', pkg.bin)
return cp.spawn('node', [bin, '-p', PORT].concat(args), { return cp.spawn('node', [bin, '-p', PORT].concat(args), {
stdio: 'inherit', cwd: __dirname,
cwd: __dirname stdio: ['pipe', process.stdout, process.stderr]
}) })
} }
@ -30,6 +31,7 @@ describe('cli', function () {
var child var child
beforeEach(function () { beforeEach(function () {
rmrf.sync(tmpDir)
fs.mkdirSync(tmpDir) fs.mkdirSync(tmpDir)
fs.writeFileSync(dbFile, JSON.stringify({ posts: [{ 'id': 1, '_id': 2 }] })) fs.writeFileSync(dbFile, JSON.stringify({ posts: [{ 'id': 1, '_id': 2 }] }))
fs.writeFileSync(routesFile, JSON.stringify({ '/blog/': '/' })) fs.writeFileSync(routesFile, JSON.stringify({ '/blog/': '/' }))
@ -111,17 +113,29 @@ describe('cli', function () {
}) })
describe('db.json -s fixtures/public', function () { describe('db.json -s fixtures/public -S ../../tmp', function () {
var snapshotsDir = path.join(tmpDir, 'snapshots')
var publicDir = 'fixtures/public'
beforeEach(function (done) { beforeEach(function (done) {
child = cli([dbFile, '-s', 'fixtures/public']) fs.mkdirSync(snapshotsDir)
serverReady(PORT, done) child = cli([dbFile, '-s', publicDir, '-S', snapshotsDir])
serverReady(PORT, function () {
child.stdin.write('s\n')
setTimeout(done, 100)
})
}) })
it('should serve fixtures/public', function (done) { it('should serve fixtures/public', function (done) {
request.get('/').expect(/Hello/, done) request.get('/').expect(/Hello/, done)
}) })
it('should save a snapshot in ../../tmp', function () {
console.log(fs.readdirSync(snapshotsDir), fs.readdirSync(snapshotsDir).length)
assert.equal(fs.readdirSync(snapshotsDir).length, 1)
})
}) })
// FIXME test fails on OS X and maybe on Windows // FIXME test fails on OS X and maybe on Windows