config.js tests
This commit is contained in:
Generated
-5
@@ -7024,11 +7024,6 @@
|
|||||||
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"ncp": {
|
|
||||||
"version": "2.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz",
|
|
||||||
"integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M="
|
|
||||||
},
|
|
||||||
"negotiator": {
|
"negotiator": {
|
||||||
"version": "0.6.2",
|
"version": "0.6.2",
|
||||||
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
"main": "src/server.js",
|
"main": "src/server.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"ncp": "^2.0.0",
|
|
||||||
"showdown": "^1.9.0",
|
"showdown": "^1.9.0",
|
||||||
"shx": "^0.3.2",
|
"shx": "^0.3.2",
|
||||||
"xml": "^1.0.1"
|
"xml": "^1.0.1"
|
||||||
|
|||||||
+13
-2
@@ -1,16 +1,27 @@
|
|||||||
const refConfig = require('./default_config.json');
|
const refConfig = require('./default_config.json');
|
||||||
const srcConfig = require('../config.json');
|
const fs = require('fs');
|
||||||
|
|
||||||
const merge = function (ref, src) {
|
const merge = function (ref, src) {
|
||||||
if (typeof ref !== typeof src) {
|
if (typeof ref !== typeof src) {
|
||||||
|
console.log(ref, src, ref);
|
||||||
return ref;
|
return ref;
|
||||||
} else if (typeof ref === 'object') {
|
} else if (typeof ref === 'object') {
|
||||||
const out = {};
|
const out = {};
|
||||||
Object.keys(ref).forEach(key => out[key] = merge(ref[key], src[key]));
|
Object.keys(ref).forEach(key => out[key] = merge(ref[key], src[key]));
|
||||||
return out;
|
return out;
|
||||||
} else {
|
} else {
|
||||||
|
console.log(ref, src, src);
|
||||||
return 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,
|
"node_port": 3000,
|
||||||
"dataDir": "data",
|
"data_dir": "data",
|
||||||
"modules" : {
|
"modules" : {
|
||||||
"plantuml" : false,
|
"plantuml" : false,
|
||||||
"rss": true,
|
"rss": true,
|
||||||
|
|||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
const config = require('./config');
|
const config = require('./config')();
|
||||||
const app = require('./app')(config);
|
const app = require('./app')(config);
|
||||||
|
|
||||||
app.listen(config.nodePort, () => {
|
app.listen(config['node_port'], () => {
|
||||||
console.log(`gitblog.md server listening on port ${config.nodePort}`);
|
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