config.js tests
This commit is contained in:
Generated
-5
@@ -7024,11 +7024,6 @@
|
||||
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
||||
"dev": true
|
||||
},
|
||||
"ncp": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz",
|
||||
"integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M="
|
||||
},
|
||||
"negotiator": {
|
||||
"version": "0.6.2",
|
||||
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
"main": "src/server.js",
|
||||
"dependencies": {
|
||||
"express": "^4.17.1",
|
||||
"ncp": "^2.0.0",
|
||||
"showdown": "^1.9.0",
|
||||
"shx": "^0.3.2",
|
||||
"xml": "^1.0.1"
|
||||
|
||||
+13
-2
@@ -1,16 +1,27 @@
|
||||
const refConfig = require('./default_config.json');
|
||||
const srcConfig = require('../config.json');
|
||||
const fs = require('fs');
|
||||
|
||||
const merge = function (ref, src) {
|
||||
if (typeof ref !== typeof src) {
|
||||
console.log(ref, src, ref);
|
||||
return ref;
|
||||
} else if (typeof ref === 'object') {
|
||||
const out = {};
|
||||
Object.keys(ref).forEach(key => out[key] = merge(ref[key], src[key]));
|
||||
return out;
|
||||
} else {
|
||||
console.log(ref, src, src);
|
||||
return src;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = merge(refConfig, srcConfig);
|
||||
module.exports = function () {
|
||||
try {
|
||||
let configData = fs.readFileSync('./config.json');
|
||||
let config = JSON.parse(configData);
|
||||
return merge(refConfig, config);
|
||||
} catch (error) {
|
||||
console.error('Failed to load config.json');
|
||||
return refConfig;
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"nodePort": 3000,
|
||||
"dataDir": "data",
|
||||
"node_port": 3000,
|
||||
"data_dir": "data",
|
||||
"modules" : {
|
||||
"plantuml" : false,
|
||||
"rss": true,
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
const config = require('./config');
|
||||
const config = require('./config')();
|
||||
const app = require('./app')(config);
|
||||
|
||||
app.listen(config.nodePort, () => {
|
||||
console.log(`gitblog.md server listening on port ${config.nodePort}`);
|
||||
app.listen(config['node_port'], () => {
|
||||
console.log(`gitblog.md server listening on port ${config['node_port']}`);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/* jshint -W117 */
|
||||
const fs = require('fs');
|
||||
|
||||
const configFile = './config.json';
|
||||
const tmpConfigFile = './config.temp.json';
|
||||
|
||||
beforeAll(() => {
|
||||
if (fs.existsSync(configFile)) {
|
||||
fs.renameSync(configFile, tmpConfigFile);
|
||||
}
|
||||
expect(fs.existsSync(configFile)).toBeFalsy();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
if (fs.existsSync(tmpConfigFile)) {
|
||||
fs.renameSync(tmpConfigFile, configFile);
|
||||
} else if (fs.existsSync(configFile)) {
|
||||
fs.unlinkSync(configFile); //remove config file if remaining
|
||||
}
|
||||
});
|
||||
|
||||
test('no config', () => {
|
||||
if (fs.existsSync(configFile))
|
||||
fs.unlinkSync(configFile);
|
||||
expect(fs.existsSync(configFile)).toBeFalsy();
|
||||
const config = require('../src/config')();
|
||||
expect(config).toBeDefined();
|
||||
expect(config['node_port']).toBe(3000);
|
||||
expect(config['data_dir']).toBe('data');
|
||||
});
|
||||
|
||||
test('invalid config ignored', () => {
|
||||
fs.writeFileSync(configFile, 'invalid JSON');
|
||||
const config = require('../src/config')();
|
||||
expect(config).toBeDefined();
|
||||
expect(config['node_port']).toBe(3000);
|
||||
expect(config['data_dir']).toBe('data');
|
||||
});
|
||||
|
||||
test('good config merged', () => {
|
||||
fs.writeFileSync(configFile, '{"node_port":5000}');
|
||||
const config = require('../src/config')();
|
||||
expect(config).toBeDefined();
|
||||
expect(config['node_port']).toBe(5000);
|
||||
expect(config['data_dir']).toBe('data');
|
||||
});
|
||||
|
||||
test('wrong config fixed', () => {
|
||||
fs.writeFileSync(configFile, '{"node_port":"hello","data_dir":"data2"}');
|
||||
const config = require('../src/config')();
|
||||
expect(config).toBeDefined();
|
||||
expect(config['node_port']).toBe(3000);
|
||||
expect(config['data_dir']).toBe('data2');
|
||||
});
|
||||
Reference in New Issue
Block a user