habitica-self-host/tasks/gulp-tests.js

390 lines
10 KiB
JavaScript
Raw Normal View History

2015-11-03 14:41:16 +00:00
import {
pipe,
awaitPort,
kill,
runMochaTests,
2015-11-03 14:41:16 +00:00
} from './taskHelper';
2015-06-06 14:48:38 +00:00
import { server as karma } from 'karma';
import mongoose from 'mongoose';
import { exec } from 'child_process';
2015-06-06 14:48:38 +00:00
import psTree from 'ps-tree';
import gulp from 'gulp';
import Q from 'q';
2015-11-09 18:47:56 +00:00
import runSequence from 'run-sequence';
import os from 'os';
2016-02-24 03:14:39 +00:00
import nconf from 'nconf';
const TEST_SERVER_PORT = 3003
let server;
2016-02-24 03:14:39 +00:00
const TEST_DB_URI = nconf.get('TEST_DB_URI');
const API_V2_TEST_COMMAND = 'npm run test:api-v2:integration';
const LEGACY_API_TEST_COMMAND = 'npm run test:api-legacy';
const COMMON_TEST_COMMAND = 'npm run test:common';
const CONTENT_TEST_COMMAND = 'npm run test:content';
2015-10-03 15:47:26 +00:00
const CONTENT_OPTIONS = {maxBuffer: 1024 * 500};
const KARMA_TEST_COMMAND = 'npm run test:karma';
const SERVER_SIDE_TEST_COMMAND = 'npm run test:api-v2:unit';
const ISTANBUL_TEST_COMMAND = 'npm run test:api-legacy';
2015-09-28 13:54:53 +00:00
/* Helper methods for reporting test summary */
let testResults = [];
let testCount = (stdout, regexp) => {
let match = stdout.match(regexp);
return parseInt(match && match[1] || 0);
}
let testBin = (string, additionalEnvVariables = '') => {
if(os.platform() === "win32") {
if(additionalEnvVariables != '') {
additionalEnvVariables = additionalEnvVariables.split(' ').join('&&set ');
additionalEnvVariables = 'set ' + additionalEnvVariables + '&&';
}
return `set NODE_ENV=testing&&${additionalEnvVariables}${string}`;
} else {
return `NODE_ENV=testing ${additionalEnvVariables} ${string}`;
}
};
gulp.task('test:nodemon', (done) => {
process.env.PORT = TEST_SERVER_PORT;
process.env.NODE_DB_URI=TEST_DB_URI;
2016-02-24 03:14:39 +00:00
runSequence('nodemon');
});
gulp.task('test:prepare:mongo', (cb) => {
mongoose.connect(TEST_DB_URI, (err) => {
if (err) return cb(`Unable to connect to mongo database. Are you sure it's running? \n\n${err}`);
mongoose.connection.db.dropDatabase();
mongoose.connection.close();
cb();
});
});
gulp.task('test:prepare:server', ['test:prepare:mongo'], () => {
if (!server) {
server = exec(testBin('node ./website/src/server.js', `NODE_DB_URI=${TEST_DB_URI} PORT=${TEST_SERVER_PORT} `), (error, stdout, stderr) => {
if (error) { throw `Problem with the server: ${error}`; }
if (stderr) { console.error(stderr); }
});
}
});
gulp.task('test:prepare:build', (cb) => {
exec(testBin('grunt build:test'), cb);
});
gulp.task('test:prepare:webdriver', (cb) => {
exec('npm run test:prepare:webdriver', cb);
});
gulp.task('test:prepare', [
'test:prepare:build',
'test:prepare:mongo',
'test:prepare:webdriver'
]);
gulp.task('test:common', ['test:prepare:build'], (cb) => {
let runner = exec(
2015-09-28 13:54:53 +00:00
testBin(COMMON_TEST_COMMAND),
(err, stdout, stderr) => {
cb(err);
}
);
pipe(runner);
});
2015-08-05 13:15:53 +00:00
gulp.task('test:common:clean', (cb) => {
2015-09-28 13:54:53 +00:00
pipe(exec(testBin(COMMON_TEST_COMMAND), () => cb()));
2015-08-05 13:15:53 +00:00
});
2015-08-05 23:12:28 +00:00
gulp.task('test:common:watch', ['test:common:clean'], () => {
2015-08-05 13:15:53 +00:00
gulp.watch(['common/script/**', 'test/common/**'], ['test:common:clean']);
});
gulp.task('test:common:safe', ['test:prepare:build'], (cb) => {
let runner = exec(
2015-09-28 13:54:53 +00:00
testBin(COMMON_TEST_COMMAND),
(err, stdout, stderr) => {
testResults.push({
suite: 'Common Specs\t',
pass: testCount(stdout, /(\d+) passing/),
2015-11-16 18:04:09 +00:00
fail: testCount(stdout, /(\d+) failing/),
pend: testCount(stdout, /(\d+) pending/)
});
cb();
}
);
pipe(runner);
});
2015-09-29 12:55:13 +00:00
gulp.task('test:content', ['test:prepare:build'], (cb) => {
let runner = exec(
testBin(CONTENT_TEST_COMMAND),
2015-10-03 15:47:26 +00:00
CONTENT_OPTIONS,
2015-09-29 12:55:13 +00:00
(err, stdout, stderr) => {
cb(err);
}
);
pipe(runner);
});
gulp.task('test:content:clean', (cb) => {
2015-10-03 15:47:26 +00:00
pipe(exec(testBin(CONTENT_TEST_COMMAND), CONTENT_OPTIONS, () => cb()));
2015-09-29 12:55:13 +00:00
});
gulp.task('test:content:watch', ['test:content:clean'], () => {
gulp.watch(['common/script/src/content/**', 'test/**'], ['test:content:clean']);
});
gulp.task('test:content:safe', ['test:prepare:build'], (cb) => {
let runner = exec(
testBin(CONTENT_TEST_COMMAND),
2015-10-03 15:47:26 +00:00
CONTENT_OPTIONS,
2015-09-29 12:55:13 +00:00
(err, stdout, stderr) => {
testResults.push({
suite: 'Content Specs\t',
pass: testCount(stdout, /(\d+) passing/),
2015-11-16 18:04:09 +00:00
fail: testCount(stdout, /(\d+) failing/),
2015-09-29 12:55:13 +00:00
pend: testCount(stdout, /(\d+) pending/)
});
cb();
}
);
pipe(runner);
});
2015-07-12 13:49:57 +00:00
gulp.task('test:server_side', ['test:prepare:build'], (cb) => {
let runner = exec(
2015-09-28 13:54:53 +00:00
testBin(SERVER_SIDE_TEST_COMMAND),
2015-07-12 13:49:57 +00:00
(err, stdout, stderr) => {
cb(err);
}
);
pipe(runner);
});
gulp.task('test:server_side:safe', ['test:prepare:build'], (cb) => {
let runner = exec(
2015-09-28 13:54:53 +00:00
testBin(SERVER_SIDE_TEST_COMMAND),
2015-07-12 13:49:57 +00:00
(err, stdout, stderr) => {
testResults.push({
suite: 'Server Side Specs',
2015-07-12 13:49:57 +00:00
pass: testCount(stdout, /(\d+) passing/),
2015-11-16 18:04:09 +00:00
fail: testCount(stdout, /(\d+) failing/),
2015-07-12 13:49:57 +00:00
pend: testCount(stdout, /(\d+) pending/)
});
cb();
}
);
pipe(runner);
});
gulp.task('test:api-legacy', ['test:prepare:mongo'], (cb) => {
let runner = exec(
testBin(ISTANBUL_TEST_COMMAND),
(err, stdout, stderr) => {
cb(err);
}
);
pipe(runner);
});
gulp.task('test:api-legacy:safe', ['test:prepare:mongo'], (cb) => {
let runner = exec(
testBin(ISTANBUL_TEST_COMMAND),
(err, stdout, stderr) => {
testResults.push({
suite: 'API (legacy) Specs',
pass: testCount(stdout, /(\d+) passing/),
2015-11-16 18:04:09 +00:00
fail: testCount(stdout, /(\d+) failing/),
pend: testCount(stdout, /(\d+) pending/)
});
cb();
}
);
pipe(runner);
});
gulp.task('test:api-legacy:clean', (cb) => {
2015-10-17 02:43:43 +00:00
pipe(exec(testBin(LEGACY_API_TEST_COMMAND), () => cb()));
2015-06-24 01:33:05 +00:00
});
gulp.task('test:api-legacy:watch', [
2015-06-24 01:33:05 +00:00
'test:prepare:mongo',
'test:api-legacy:clean'
2015-06-24 01:33:05 +00:00
], () => {
gulp.watch(['website/src/**', 'test/api-legacy/**'], ['test:api-legacy:clean']);
2015-06-24 01:33:05 +00:00
});
2015-08-23 00:25:34 +00:00
gulp.task('test:karma', ['test:prepare:build'], (cb) => {
2015-08-19 13:56:20 +00:00
let runner = exec(
testBin(KARMA_TEST_COMMAND),
2015-08-19 13:56:20 +00:00
(err, stdout) => {
cb(err);
}
);
pipe(runner);
});
2015-08-23 00:25:34 +00:00
gulp.task('test:karma:watch', ['test:prepare:build'], (cb) => {
let runner = exec(
testBin(`${KARMA_TEST_COMMAND}:watch`),
(err, stdout) => {
cb(err);
}
);
pipe(runner);
});
gulp.task('test:karma:safe', ['test:prepare:build'], (cb) => {
let runner = exec(
testBin(KARMA_TEST_COMMAND),
(err, stdout) => {
testResults.push({
suite: 'Karma Specs\t',
2015-08-23 02:38:40 +00:00
pass: testCount(stdout, /(\d+) tests? completed/),
fail: testCount(stdout, /(\d+) tests? failed/),
pend: testCount(stdout, /(\d+) tests? skipped/)
});
cb();
}
);
pipe(runner);
});
gulp.task('test:e2e', ['test:prepare', 'test:prepare:server'], (cb) => {
let support = [
'Xvfb :99 -screen 0 1024x768x24 -extension RANDR',
testBin('npm run test:e2e:webdriver', 'DISPLAY=:99'),
].map(exec);
support.push(server);
Q.all([
awaitPort(TEST_SERVER_PORT),
awaitPort(4444)
]).then(() => {
let runner = exec(
'npm run test:e2e',
(err, stdout, stderr) => {
/*
* Note: As it stands, protractor wont report pending specs
*/
support.forEach(kill);
cb(err);
}
);
pipe(runner);
});
});
gulp.task('test:e2e:safe', ['test:prepare', 'test:prepare:server'], (cb) => {
let support = [
'Xvfb :99 -screen 0 1024x768x24 -extension RANDR',
'npm run test:e2e:webdriver',
].map(exec);
Q.all([
awaitPort(TEST_SERVER_PORT),
awaitPort(4444)
]).then(() => {
let runner = exec(
'npm run test:e2e',
(err, stdout, stderr) => {
/*
* Note: As it stands, protractor wont report pending specs
*/
let match = stdout.match(/(\d+) tests?.*(\d) failures?/);
testResults.push({
suite: 'End-to-End Specs',
pass: parseInt(match[1]) - parseInt(match[2]),
fail: parseInt(match[2]),
pend: 0
});
support.forEach(kill);
cb();
}
);
pipe(runner);
});
});
2015-11-03 14:31:20 +00:00
gulp.task('test:api-v2', ['test:prepare:server'], (done) => {
awaitPort(TEST_SERVER_PORT).then(() => {
runMochaTests('./test/api/v2/**/*.js', server, done)
});
2015-11-03 14:31:20 +00:00
});
gulp.task('test:api-v2:watch', ['test:prepare:server'], () => {
2015-10-18 00:17:23 +00:00
process.env.RUN_INTEGRATION_TEST_FOREVER = true;
2015-11-03 14:49:10 +00:00
gulp.watch(['website/src/**', 'test/api/v2/**'], ['test:api-v2']);
2015-10-18 00:17:23 +00:00
});
2015-11-03 14:31:20 +00:00
gulp.task('test:api-v2:safe', ['test:prepare:server'], (done) => {
awaitPort(TEST_SERVER_PORT).then(() => {
2015-10-17 23:04:32 +00:00
let runner = exec(
2015-11-03 14:31:20 +00:00
testBin(API_V2_TEST_COMMAND),
2015-10-17 23:04:32 +00:00
(err, stdout, stderr) => {
testResults.push({
suite: 'API Specs\t',
pass: testCount(stdout, /(\d+) passing/),
2015-11-16 18:04:09 +00:00
fail: testCount(stdout, /(\d+) failing/),
2015-10-17 23:04:32 +00:00
pend: testCount(stdout, /(\d+) pending/)
});
done();
}
);
pipe(runner);
});
2015-10-16 20:00:33 +00:00
});
2015-11-09 18:47:56 +00:00
gulp.task('test:all', (done) => {
runSequence(
2015-11-15 14:28:16 +00:00
'lint',
2015-10-23 17:42:47 +00:00
'test:e2e:safe',
'test:common:safe',
// 'test:content:safe',
// 'test:server_side:safe',
'test:karma:safe',
2015-10-21 01:17:10 +00:00
'test:api-legacy:safe',
2015-11-03 14:31:20 +00:00
'test:api-v2:safe',
2015-11-09 18:47:56 +00:00
done);
});
gulp.task('test', ['test:all'], () => {
let totals = [0,0,0];
console.log('\n\x1b[36m\x1b[4mHabitica Test Summary\x1b[0m\n');
testResults.forEach((s) => {
totals[0] = totals[0] + s.pass;
totals[1] = totals[1] + s.fail;
totals[2] = totals[2] + s.pend;
console.log(
`\x1b[33m\x1b[4m${s.suite}\x1b[0m\t`,
`\x1b[32mPassing: ${s.pass},\t`,
`\x1b[31mFailed: ${s.fail},\t`,
`\x1b[36mPending: ${s.pend}\t`
);
});
console.log(
'\n\x1b[33m\x1b[4mTotal:\x1b[0m\t\t\t',
`\x1b[32mPassing: ${totals[0]},\t`,
`\x1b[31mFailed: ${totals[1]},\t`,
`\x1b[36mPending: ${totals[2]}\t`
);
2015-11-16 18:23:09 +00:00
kill(server);
if (totals[1] > 0) {
console.error('ERROR: There are failing tests!');
process.exit(1);
} else {
console.log('\n\x1b[36mThanks for helping keep Habitica clean!\x1b[0m');
2015-10-17 02:43:43 +00:00
process.exit();
}
});