mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 21:27:23 +01:00
fix(12842) - Remove duplication in gulp-tests.js
This commit is contained in:
@@ -3,9 +3,7 @@ import { exec } from 'child_process';
|
|||||||
import gulp from 'gulp';
|
import gulp from 'gulp';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import nconf from 'nconf';
|
import nconf from 'nconf';
|
||||||
import {
|
import { pipe } from './taskHelper';
|
||||||
pipe,
|
|
||||||
} from './taskHelper';
|
|
||||||
import {
|
import {
|
||||||
getDevelopmentConnectionUrl,
|
getDevelopmentConnectionUrl,
|
||||||
getDefaultConnectionOptions,
|
getDefaultConnectionOptions,
|
||||||
@@ -21,15 +19,16 @@ const TEST_DB_URI = nconf.get('TEST_DB_URI');
|
|||||||
const SANITY_TEST_COMMAND = 'npm run test:sanity';
|
const SANITY_TEST_COMMAND = 'npm run test:sanity';
|
||||||
const COMMON_TEST_COMMAND = 'npm run test:common';
|
const COMMON_TEST_COMMAND = 'npm run test:common';
|
||||||
const CONTENT_TEST_COMMAND = 'npm run test:content';
|
const CONTENT_TEST_COMMAND = 'npm run test:content';
|
||||||
const CONTENT_OPTIONS = { maxBuffer: 1024 * 500 };
|
const LIMIT_MAX_BUFFER_OPTIONS = { maxBuffer: 1024 * 500 };
|
||||||
|
|
||||||
/* Helper methods for reporting test summary */
|
/* Helper method for reporting test summary */
|
||||||
const testResults = [];
|
const testResults = [];
|
||||||
const testCount = (stdout, regexp) => {
|
const testCount = (stdout, regexp) => {
|
||||||
const match = stdout.match(regexp);
|
const match = stdout.match(regexp);
|
||||||
return parseInt(match && (match[1] || 0), 10);
|
return parseInt(match && (match[1] || 0), 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Helper methods to correctly run child test processes */
|
||||||
const testBin = (string, additionalEnvVariables = '') => {
|
const testBin = (string, additionalEnvVariables = '') => {
|
||||||
if (os.platform() === 'win32') {
|
if (os.platform() === 'win32') {
|
||||||
if (additionalEnvVariables !== '') {
|
if (additionalEnvVariables !== '') {
|
||||||
@@ -41,6 +40,15 @@ const testBin = (string, additionalEnvVariables = '') => {
|
|||||||
return `NODE_ENV=test ${additionalEnvVariables} ${string}`;
|
return `NODE_ENV=test ${additionalEnvVariables} ${string}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function runInChildProcess (command, options = {}, envVariables = '') {
|
||||||
|
return done => pipe(exec(testBin(command, envVariables), options, done));
|
||||||
|
}
|
||||||
|
|
||||||
|
function integrationTestCommand (testDir, coverageDir) {
|
||||||
|
return `istanbul cover --dir coverage/${coverageDir} --report lcovonly node_modules/mocha/bin/_mocha -- ${testDir} --recursive --require ./test/helpers/start-server`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test task definitions */
|
||||||
gulp.task('test:nodemon', gulp.series(done => {
|
gulp.task('test:nodemon', gulp.series(done => {
|
||||||
process.env.PORT = TEST_SERVER_PORT; // eslint-disable-line no-process-env
|
process.env.PORT = TEST_SERVER_PORT; // eslint-disable-line no-process-env
|
||||||
process.env.NODE_DB_URI = TEST_DB_URI; // eslint-disable-line no-process-env
|
process.env.NODE_DB_URI = TEST_DB_URI; // eslint-disable-line no-process-env
|
||||||
@@ -82,31 +90,9 @@ gulp.task('test:prepare', gulp.series(
|
|||||||
done => done(),
|
done => done(),
|
||||||
));
|
));
|
||||||
|
|
||||||
gulp.task('test:sanity', cb => {
|
gulp.task('test:sanity', runInChildProcess(SANITY_TEST_COMMAND));
|
||||||
const runner = exec(
|
|
||||||
testBin(SANITY_TEST_COMMAND),
|
|
||||||
err => {
|
|
||||||
if (err) {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
pipe(runner);
|
|
||||||
});
|
|
||||||
|
|
||||||
gulp.task('test:common', gulp.series('test:prepare:build', cb => {
|
gulp.task('test:common', gulp.series('test:prepare:build', runInChildProcess(COMMON_TEST_COMMAND)));
|
||||||
const runner = exec(
|
|
||||||
testBin(COMMON_TEST_COMMAND),
|
|
||||||
err => {
|
|
||||||
if (err) {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
pipe(runner);
|
|
||||||
}));
|
|
||||||
|
|
||||||
gulp.task('test:common:clean', cb => {
|
gulp.task('test:common:clean', cb => {
|
||||||
pipe(exec(testBin(COMMON_TEST_COMMAND), () => cb()));
|
pipe(exec(testBin(COMMON_TEST_COMMAND), () => cb()));
|
||||||
@@ -130,22 +116,11 @@ gulp.task('test:common:safe', gulp.series('test:prepare:build', cb => {
|
|||||||
pipe(runner);
|
pipe(runner);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
gulp.task('test:content', gulp.series('test:prepare:build', cb => {
|
gulp.task('test:content', gulp.series('test:prepare:build',
|
||||||
const runner = exec(
|
runInChildProcess(CONTENT_TEST_COMMAND, LIMIT_MAX_BUFFER_OPTIONS)));
|
||||||
testBin(CONTENT_TEST_COMMAND),
|
|
||||||
CONTENT_OPTIONS,
|
|
||||||
err => {
|
|
||||||
if (err) {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
pipe(runner);
|
|
||||||
}));
|
|
||||||
|
|
||||||
gulp.task('test:content:clean', cb => {
|
gulp.task('test:content:clean', cb => {
|
||||||
pipe(exec(testBin(CONTENT_TEST_COMMAND), CONTENT_OPTIONS, () => cb()));
|
pipe(exec(testBin(CONTENT_TEST_COMMAND), LIMIT_MAX_BUFFER_OPTIONS, () => cb()));
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('test:content:watch', gulp.series('test:content:clean', () => gulp.watch(['common/script/content/**', 'test/**'], gulp.series('test:content:clean', done => done()))));
|
gulp.task('test:content:watch', gulp.series('test:content:clean', () => gulp.watch(['common/script/content/**', 'test/**'], gulp.series('test:content:clean', done => done()))));
|
||||||
@@ -153,7 +128,7 @@ gulp.task('test:content:watch', gulp.series('test:content:clean', () => gulp.wat
|
|||||||
gulp.task('test:content:safe', gulp.series('test:prepare:build', cb => {
|
gulp.task('test:content:safe', gulp.series('test:prepare:build', cb => {
|
||||||
const runner = exec(
|
const runner = exec(
|
||||||
testBin(CONTENT_TEST_COMMAND),
|
testBin(CONTENT_TEST_COMMAND),
|
||||||
CONTENT_OPTIONS,
|
LIMIT_MAX_BUFFER_OPTIONS,
|
||||||
(err, stdout) => { // eslint-disable-line handle-callback-err
|
(err, stdout) => { // eslint-disable-line handle-callback-err
|
||||||
testResults.push({
|
testResults.push({
|
||||||
suite: 'Content Specs\t',
|
suite: 'Content Specs\t',
|
||||||
@@ -167,76 +142,39 @@ gulp.task('test:content:safe', gulp.series('test:prepare:build', cb => {
|
|||||||
pipe(runner);
|
pipe(runner);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
gulp.task('test:api:unit:run', done => {
|
gulp.task('test:api:unit:run',
|
||||||
const runner = exec(
|
runInChildProcess(integrationTestCommand('test/api/unit', 'coverage/api-unit')));
|
||||||
testBin('istanbul cover --dir coverage/api-unit node_modules/mocha/bin/_mocha -- test/api/unit --recursive --require ./test/helpers/start-server'),
|
|
||||||
err => {
|
|
||||||
if (err) {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
pipe(runner);
|
|
||||||
});
|
|
||||||
|
|
||||||
gulp.task('test:api:unit:watch', () => gulp.watch(['website/server/libs/*', 'test/api/unit/**/*', 'website/server/controllers/**/*'], gulp.series('test:api:unit:run', done => done())));
|
gulp.task('test:api:unit:watch', () => gulp.watch(['website/server/libs/*', 'test/api/unit/**/*', 'website/server/controllers/**/*'], gulp.series('test:api:unit:run', done => done())));
|
||||||
|
|
||||||
gulp.task('test:api-v3:integration', gulp.series('test:prepare:mongo', done => {
|
gulp.task('test:api-v3:integration', gulp.series('test:prepare:mongo',
|
||||||
const runner = exec(
|
runInChildProcess(
|
||||||
testBin('istanbul cover --dir coverage/api-v3-integration --report lcovonly node_modules/mocha/bin/_mocha -- test/api/v3/integration --recursive --require ./test/helpers/start-server'),
|
integrationTestCommand('test/api/v3/integration', 'coverage/api-v3-integration'),
|
||||||
{ maxBuffer: 500 * 1024 },
|
LIMIT_MAX_BUFFER_OPTIONS,
|
||||||
err => {
|
)));
|
||||||
if (err) {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
pipe(runner);
|
|
||||||
}));
|
|
||||||
|
|
||||||
gulp.task('test:api-v3:integration:watch', () => gulp.watch([
|
gulp.task('test:api-v3:integration:watch', () => gulp.watch([
|
||||||
'website/server/controllers/api-v3/**/*', 'common/script/ops/*', 'website/server/libs/*.js',
|
'website/server/controllers/api-v3/**/*', 'common/script/ops/*', 'website/server/libs/*.js',
|
||||||
'test/api/v3/integration/**/*',
|
'test/api/v3/integration/**/*',
|
||||||
], gulp.series('test:api-v3:integration', done => done())));
|
], gulp.series('test:api-v3:integration', done => done())));
|
||||||
|
|
||||||
gulp.task('test:api-v3:integration:separate-server', done => {
|
gulp.task('test:api-v3:integration:separate-server', runInChildProcess(
|
||||||
const runner = exec(
|
'mocha test/api/v3/integration --recursive --require ./test/helpers/start-server',
|
||||||
testBin('mocha test/api/v3/integration --recursive --require ./test/helpers/start-server', 'LOAD_SERVER=0'),
|
LIMIT_MAX_BUFFER_OPTIONS,
|
||||||
{ maxBuffer: 500 * 1024 },
|
'LOAD_SERVER=0',
|
||||||
err => done(err),
|
));
|
||||||
);
|
|
||||||
|
|
||||||
pipe(runner);
|
gulp.task('test:api-v4:integration', gulp.series('test:prepare:mongo',
|
||||||
});
|
runInChildProcess(
|
||||||
|
integrationTestCommand('test/api/v4', 'api-v4-integration'),
|
||||||
|
LIMIT_MAX_BUFFER_OPTIONS,
|
||||||
|
)));
|
||||||
|
|
||||||
gulp.task('test:api-v4:integration', gulp.series('test:prepare:mongo', done => {
|
gulp.task('test:api-v4:integration:separate-server', runInChildProcess(
|
||||||
const runner = exec(
|
'mocha test/api/v4 --recursive --require ./test/helpers/start-server',
|
||||||
testBin('istanbul cover --dir coverage/api-v4-integration --report lcovonly node_modules/mocha/bin/_mocha -- test/api/v4 --recursive --require ./test/helpers/start-server'),
|
LIMIT_MAX_BUFFER_OPTIONS,
|
||||||
{ maxBuffer: 500 * 1024 },
|
'LOAD_SERVER=0',
|
||||||
err => {
|
));
|
||||||
if (err) {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
pipe(runner);
|
|
||||||
}));
|
|
||||||
|
|
||||||
gulp.task('test:api-v4:integration:separate-server', done => {
|
|
||||||
const runner = exec(
|
|
||||||
testBin('mocha test/api/v4 --recursive --require ./test/helpers/start-server', 'LOAD_SERVER=0'),
|
|
||||||
{ maxBuffer: 500 * 1024 },
|
|
||||||
err => done(err),
|
|
||||||
);
|
|
||||||
|
|
||||||
pipe(runner);
|
|
||||||
});
|
|
||||||
|
|
||||||
gulp.task('test:api:unit', gulp.series(
|
gulp.task('test:api:unit', gulp.series(
|
||||||
'test:prepare:mongo',
|
'test:prepare:mongo',
|
||||||
|
|||||||
Reference in New Issue
Block a user