Server start refactor, route tests use ghost app

closes #2442, issue #2182

- Server start refactored - messaging is just messaging, deferred resolves the httpserver so that the connection can be closed
- Updated travis config to set node env
- Updated example config to be less travis-specific
- Route tests updated to use this new functionality
- Grunt test-routes simplified
This commit is contained in:
Hannah Wolfe 2014-03-25 12:42:52 +00:00
parent 3ee6987aa9
commit e6abe9bab7
6 changed files with 118 additions and 109 deletions

View File

@ -2,20 +2,20 @@ language: node_js
node_js:
- "0.10"
env:
- DB=sqlite3
- DB=mysql
- DB=pg
- DB=sqlite3 NODE_ENV=testing
- DB=mysql NODE_ENV=testing-mysql
- DB=pg NODE_ENV=testing-pg
matrix:
allow_failures:
- env: DB=pg
- env: DB=pg NODE_ENV=testing-pg
before_install:
- git clone git://github.com/n1k0/casperjs.git ~/casperjs
- cd ~/casperjs
- git checkout tags/1.1-beta3
- export PATH=$PATH:`pwd`/bin
- cd -
- if [ $DB == "mysql" ]; then mysql -e 'create database ghost_travis'; fi
- if [ $DB == "pg" ]; then npm install pg; psql -c 'create database ghost_travis;' -U postgres; fi
- if [ $DB == "mysql" ]; then mysql -e 'create database ghost_testing'; fi
- if [ $DB == "pg" ]; then npm install pg; psql -c 'create database ghost_testing;' -U postgres; fi
before_script:
- phantomjs --version
- casperjs --version

View File

@ -576,7 +576,7 @@ var path = require('path'),
// ## Custom Tasks
grunt.registerTask('setTestEnv', 'Use "testing" Ghost config; unless we are running on travis (then show queries for debugging)', function () {
process.env.NODE_ENV = process.env.TRAVIS ? 'travis-' + process.env.DB : 'testing';
process.env.NODE_ENV = process.env.TRAVIS ? process.env.NODE_ENV : 'testing';
cfg.express.test.options.node_env = process.env.NODE_ENV;
});
@ -910,7 +910,7 @@ var path = require('path'),
grunt.registerTask('test-api', 'Run functional api tests (mocha)', ['clean:test', 'setTestEnv', 'loadConfig', 'express:test', 'mochacli:api', 'express:test:stop']);
grunt.registerTask('test-routes', 'Run functional route tests (mocha)', ['clean:test', 'setTestEnv', 'loadConfig', 'express:test', 'mochacli:routes', 'express:test:stop']);
grunt.registerTask('test-routes', 'Run functional route tests (mocha)', ['clean:test', 'setTestEnv', 'loadConfig', 'mochacli:routes']);
grunt.registerTask('validate', 'Run tests and lint code', ['jshint', 'test-routes', 'test-unit', 'test-api', 'test-integration', 'test-functional']);

View File

@ -85,34 +85,17 @@ config = {
logging: false
},
// ### Travis
// Automated testing run through GitHub
'travis-sqlite3': {
url: 'http://127.0.0.1:2369',
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost-travis.db')
}
},
server: {
host: '127.0.0.1',
port: '2369'
},
logging: false
},
// ### Travis
// Automated testing run through GitHub
'travis-mysql': {
// ### Testing MySQL
// Used by Travis - Automated testing run through GitHub
'testing-mysql': {
url: 'http://127.0.0.1:2369',
database: {
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'travis',
user : 'root',
password : '',
database : 'ghost_travis',
database : 'ghost_testing',
charset : 'utf8'
}
},
@ -123,9 +106,9 @@ config = {
logging: false
},
// ### Travis
// Automated testing run through GitHub
'travis-pg': {
// ### Testing pg
// Used by Travis - Automated testing run through GitHub
'testing-pg': {
url: 'http://127.0.0.1:2369',
database: {
client: 'pg',
@ -133,7 +116,7 @@ config = {
host : '127.0.0.1',
user : 'postgres',
password : '',
database : 'ghost_travis',
database : 'ghost_testing',
charset : 'utf8'
}
},

View File

@ -22,6 +22,7 @@ var crypto = require('crypto'),
packageInfo = require('../../package.json'),
// Variables
httpServer,
dbHash;
// If we're in development mode, require "when/console/monitor"
@ -105,63 +106,58 @@ function builtFilesExist() {
return when.all(deferreds);
}
function startGhost(deferred) {
function ghostStartMessages() {
// Tell users if their node version is not supported, and exit
if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) {
console.log(
"\nERROR: Unsupported version of Node".red,
"\nGhost needs Node version".red,
packageInfo.engines.node.yellow,
"you are using version".red,
process.versions.node.yellow,
"\nPlease go to http://nodejs.org to get a supported version".green
);
return function () {
// Tell users if their node version is not supported, and exit
if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) {
process.exit(0);
}
// Startup & Shutdown messages
if (process.env.NODE_ENV === 'production') {
console.log(
"Ghost is running...".green,
"\nYour blog is now available on",
config().url,
"\nCtrl+C to shut down".grey
);
// ensure that Ghost exits correctly on Ctrl+C
process.on('SIGINT', function () {
console.log(
"\nERROR: Unsupported version of Node".red,
"\nGhost needs Node version".red,
packageInfo.engines.node.yellow,
"you are using version".red,
process.versions.node.yellow,
"\nPlease go to http://nodejs.org to get a supported version".green
"\nGhost has shut down".red,
"\nYour blog is now offline"
);
process.exit(0);
}
// Startup & Shutdown messages
if (process.env.NODE_ENV === 'production') {
console.log(
"Ghost is running...".green,
"\nYour blog is now available on",
config().url,
"\nCtrl+C to shut down".grey
);
// ensure that Ghost exits correctly on Ctrl+C
process.on('SIGINT', function () {
console.log(
"\nGhost has shut down".red,
"\nYour blog is now offline"
);
process.exit(0);
});
} else {
console.log(
("Ghost is running in " + process.env.NODE_ENV + "...").green,
"\nListening on",
});
} else {
console.log(
("Ghost is running in " + process.env.NODE_ENV + "...").green,
"\nListening on",
config.getSocket() || config().server.host + ':' + config().server.port,
"\nUrl configured as:",
config().url,
"\nCtrl+C to shut down".grey
"\nUrl configured as:",
config().url,
"\nCtrl+C to shut down".grey
);
// ensure that Ghost exits correctly on Ctrl+C
process.on('SIGINT', function () {
console.log(
"\nGhost has shutdown".red,
"\nGhost was running for",
Math.round(process.uptime()),
"seconds"
);
// ensure that Ghost exits correctly on Ctrl+C
process.on('SIGINT', function () {
console.log(
"\nGhost has shutdown".red,
"\nGhost was running for",
Math.round(process.uptime()),
"seconds"
);
process.exit(0);
});
}
deferred.resolve();
};
process.exit(0);
});
}
}
// ## Initializes the ghost application.
@ -258,21 +254,25 @@ function init(server) {
// Make sure the socket is gone before trying to create another
fs.unlink(config.getSocket(), function (err) {
/*jshint unused:false*/
server.listen(
config.getSocket(),
startGhost(deferred)
httpServer = server.listen(
config.getSocket()
);
fs.chmod(config.getSocket(), '0660');
});
} else {
server.listen(
httpServer = server.listen(
config().server.port,
config().server.host,
startGhost(deferred)
config().server.host
);
}
httpServer.on('listening', function () {
ghostStartMessages();
deferred.resolve(httpServer);
});
return deferred.promise;
});
}

View File

@ -6,11 +6,13 @@
// But then again testing real code, rather than mock code, might be more useful...
var request = require('supertest'),
express = require('express'),
should = require('should'),
moment = require('moment'),
testUtils = require('../../utils'),
config = require('../../../server/config'),
ghost = require('../../../../core'),
httpServer,
ONE_HOUR_S = 60 * 60,
ONE_YEAR_S = 365 * 24 * ONE_HOUR_S,
@ -53,15 +55,26 @@ describe('Admin Routing', function () {
}
before(function (done) {
testUtils.clearData().then(function () {
// we initialise data, but not a user.
return testUtils.initData();
}).then(function () {
done();
}, done);
var app = express();
// Setup the request object with the correct URL
request = request(config().url);
ghost({app: app}).then(function (_httpServer) {
// Setup the request object with the ghost express app
httpServer = _httpServer;
request = request(app);
testUtils.clearData().then(function () {
// we initialise data, but not a user. No user should be required for navigating the frontend
return testUtils.initData();
}).then(function () {
done();
}, done);
}).otherwise(function (e) {
console.log('Ghost Error: ', e);
console.log(e.stack);
});
});
after(function () {
httpServer.close();
});
describe('Legacy Redirects', function () {

View File

@ -6,11 +6,13 @@
// But then again testing real code, rather than mock code, might be more useful...
var request = require('supertest'),
express = require('express'),
should = require('should'),
moment = require('moment'),
testUtils = require('../../utils'),
config = require('../../../server/config'),
ghost = require('../../../../core'),
httpServer,
ONE_HOUR_S = 60 * 60,
ONE_YEAR_S = 365 * 24 * ONE_HOUR_S,
@ -38,15 +40,26 @@ describe('Frontend Routing', function () {
}
before(function (done) {
testUtils.clearData().then(function () {
// we initialise data, but not a user. No user should be required for navigating the frontend
return testUtils.initData();
}).then(function () {
done();
}, done);
var app = express();
// Setup the request object with the correct URL
request = request(config().url);
ghost({app: app}).then(function (_httpServer) {
// Setup the request object with the ghost express app
httpServer = _httpServer;
request = request(app);
testUtils.clearData().then(function () {
// we initialise data, but not a user. No user should be required for navigating the frontend
return testUtils.initData();
}).then(function () {
done();
}, done);
}).otherwise(function (e) {
console.log('Ghost Error: ', e);
console.log(e.stack);
});
});
after(function () {
httpServer.close();
});
describe('Home', function () {