Enable `require.main.require` in tests (#6896)

v1.18.x
Peter Jaszkowiak 6 years ago committed by Barış Soner Uşaklı
parent 4caaa046d7
commit 9a32118418

@ -19,11 +19,7 @@
'use strict'; 'use strict';
if (require.main !== module) { require('./require-main');
require.main.require = function (path) {
return require(path);
};
}
var nconf = require('nconf'); var nconf = require('nconf');
nconf.argv().env({ nconf.argv().env({

@ -200,39 +200,40 @@ function killWorkers() {
} }
fs.open(pathToConfig, 'r', function (err) { fs.open(pathToConfig, 'r', function (err) {
if (!err) { if (err) {
if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) { // No config detected, kickstart web installer
if (file.existsSync(pidFilePath)) { fork('app');
try { return;
var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); }
process.kill(pid, 0);
process.exit();
} catch (e) {
fs.unlinkSync(pidFilePath);
}
}
require('daemon')({
stdout: process.stdout,
stderr: process.stderr,
cwd: process.cwd(),
});
fs.writeFileSync(pidFilePath, process.pid); if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) {
if (file.existsSync(pidFilePath)) {
try {
var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' });
process.kill(pid, 0);
process.exit();
} catch (e) {
fs.unlinkSync(pidFilePath);
}
} }
async.series([ require('daemon')({
Loader.init, stdout: process.stdout,
Loader.displayStartupMessages, stderr: process.stderr,
Loader.start, cwd: process.cwd(),
], function (err) {
if (err) {
console.error('[loader] Error during startup');
throw err;
}
}); });
} else {
// No config detected, kickstart web installer fs.writeFileSync(pidFilePath, process.pid);
fork('app');
} }
async.series([
Loader.init,
Loader.displayStartupMessages,
Loader.start,
], function (err) {
if (err) {
console.error('[loader] Error during startup');
throw err;
}
});
}); });

@ -0,0 +1,10 @@
'use strict';
// this forces `require.main.require` to always be relative to this directory
// this allows plugins to use `require.main.require` to reference NodeBB modules
// without worrying about multiple parent modules
if (require.main !== module) {
require.main.require = function (path) {
return require(path);
};
}

@ -5,6 +5,7 @@
* ATTENTION: testing db is flushed before every use! * ATTENTION: testing db is flushed before every use!
*/ */
require('../../require-main');
var async = require('async'); var async = require('async');
var path = require('path'); var path = require('path');
@ -13,7 +14,6 @@ var url = require('url');
global.env = process.env.TEST_ENV || 'production'; global.env = process.env.TEST_ENV || 'production';
var errorText;
var packageInfo = require('../../package'); var packageInfo = require('../../package');
var winston = require('winston'); var winston = require('winston');
@ -43,7 +43,7 @@ var testDbConfig = nconf.get('test_database');
var productionDbConfig = nconf.get(dbType); var productionDbConfig = nconf.get(dbType);
if (!testDbConfig) { if (!testDbConfig) {
errorText = 'test_database is not defined'; const errorText = 'test_database is not defined';
winston.info( winston.info(
'\n===========================================================\n' + '\n===========================================================\n' +
'Please, add parameters for test database in config.json\n' + 'Please, add parameters for test database in config.json\n' +
@ -86,7 +86,7 @@ if (!testDbConfig) {
if (testDbConfig.database === productionDbConfig.database && if (testDbConfig.database === productionDbConfig.database &&
testDbConfig.host === productionDbConfig.host && testDbConfig.host === productionDbConfig.host &&
testDbConfig.port === productionDbConfig.port) { testDbConfig.port === productionDbConfig.port) {
errorText = 'test_database has the same config as production db'; const errorText = 'test_database has the same config as production db';
winston.error(errorText); winston.error(errorText);
throw new Error(errorText); throw new Error(errorText);
} }

Loading…
Cancel
Save