1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Fix .config.json unit tests

This commit is contained in:
Nick O'Leary 2020-09-24 15:42:52 +01:00
parent fb2da0ee9e
commit 4d0c572c2e
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 105 additions and 65 deletions

View File

@ -15,7 +15,6 @@
**/
var fs = require('fs-extra');
var when = require('when');
var fspath = require("path");
var log = require("@node-red/util").log; // TODO: separate module
@ -29,6 +28,11 @@ var projects = require("./projects");
var initialFlowLoadComplete = false;
var settings;
function checkForConfigFile(dir) {
return fs.existsSync(fspath.join(dir,".config.json")) ||
fs.existsSync(fspath.join(dir,".config.nodes.json"))
}
var localfilesystem = {
init: function(_settings, runtime) {
settings = _settings;
@ -36,34 +40,24 @@ var localfilesystem = {
var promises = [];
if (!settings.userDir) {
try {
fs.statSync(fspath.join(process.env.NODE_RED_HOME,".config.json"));
settings.userDir = process.env.NODE_RED_HOME;
} catch(err) {
try {
// Consider compatibility for older versions
if (process.env.HOMEPATH) {
fs.statSync(fspath.join(process.env.HOMEPATH,".node-red",".config.json"));
settings.userDir = fspath.join(process.env.HOMEPATH,".node-red");
}
} catch(err) {
}
if (!settings.userDir) {
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
if (!settings.readOnly) {
promises.push(fs.ensureDir(fspath.join(settings.userDir,"node_modules")));
}
}
if (checkForConfigFile(process.env.NODE_RED_HOME)) {
settings.userDir = process.env.NODE_RED_HOME
} else if (process.env.HOMEPATH && checkForConfigFile(fspath.join(process.env.HOMEPATH,".node-red"))) {
settings.userDir = fspath.join(process.env.HOMEPATH,".node-red");
} else {
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
}
}
if (!settings.readOnly) {
promises.push(fs.ensureDir(fspath.join(settings.userDir,"node_modules")));
}
sessions.init(settings);
runtimeSettings.init(settings);
promises.push(runtimeSettings.init(settings));
promises.push(library.init(settings));
promises.push(projects.init(settings, runtime));
var packageFile = fspath.join(settings.userDir,"package.json");
var packagePromise = when.resolve();
var packagePromise = Promise.resolve();
if (!settings.readOnly) {
packagePromise = function() {
@ -81,7 +75,7 @@ var localfilesystem = {
return true;
}
}
return when.all(promises).then(packagePromise);
return Promise.all(promises).then(packagePromise);
},

View File

@ -21,7 +21,6 @@ const log = require("@node-red/util").log;
const util = require("./util");
const configSections = ['nodes','users','projects'];
let initialisePromise;
const settingsCache = {};
@ -78,7 +77,9 @@ async function readSettings() {
configSections.forEach(key => {
const sectionFilename = getSettingsFilename(key);
readPromises.push(util.readFile(sectionFilename,sectionFilename+".backup",{}).then(sectionData => {
result[key] = sectionData;
if (Object.keys(sectionData).length > 0) {
result[key] = sectionData;
}
}))
});
return Promise.all(readPromises).then(() => result);
@ -95,18 +96,18 @@ module.exports = {
globalSettingsBackup = fspath.join(settings.userDir,".config.json.backup");
if (fs.existsSync(globalSettingsFile) && !settings.readOnly) {
initialisePromise = migrateToMultipleConfigFiles();
return migrateToMultipleConfigFiles();
} else {
initialisePromise = Promise.resolve();
return Promise.resolve();
}
},
getSettings: function() {
return initialisePromise.then(readSettings)
return readSettings()
},
saveSettings: function(newSettings) {
if (settings.readOnly) {
return Promise.resolve();
}
return initialisePromise.then(() => writeSettings(newSettings));
return writeSettings(newSettings);
}
}

View File

@ -14,9 +14,9 @@
* limitations under the License.
**/
var should = require("should");
var fs = require('fs-extra');
var path = require('path');
const should = require("should");
const fs = require('fs-extra');
const path = require('path');
var NR_TEST_UTILS = require("nr-test-utils");
@ -34,49 +34,94 @@ describe('storage/localfilesystem/settings', function() {
});
it('should handle non-existent settings', function(done) {
var settingsFile = path.join(userDir,".settings.json");
localfilesystemSettings.init({userDir:userDir});
fs.existsSync(settingsFile).should.be.false();
localfilesystemSettings.getSettings().then(function(settings) {
var settingsFile = path.join(userDir,".config.json");
localfilesystemSettings.init({userDir:userDir}).then(function() {
fs.existsSync(settingsFile).should.be.false();
return localfilesystemSettings.getSettings();
}).then(function(settings) {
settings.should.eql({});
done();
}).catch(function(err) {
done(err);
});
}).catch(err => { done(err)});
});
it('should handle corrupt settings', function(done) {
it('should migrate single config.json to multiple files', function(done) {
var settingsFile = path.join(userDir,".config.json");
fs.writeFileSync(settingsFile,"[This is not json","utf8");
localfilesystemSettings.init({userDir:userDir});
fs.existsSync(settingsFile).should.be.true();
localfilesystemSettings.getSettings().then(function(settings) {
settings.should.eql({});
fs.writeFileSync(settingsFile,JSON.stringify({
nodes:{a:1},
_credentialSecret: "foo",
users:{b:2},
projects: {c:3}
}),"utf8");
async function checkFile(sectionName, expectedContents) {
const file = path.join(userDir,".config."+sectionName+".json");
fs.existsSync(file).should.be.true();
var contents = await fs.readFile(file,'utf8');
var data = JSON.parse(contents);
data.should.eql(expectedContents)
}
localfilesystemSettings.init({userDir:userDir}).then(async function() {
fs.existsSync(settingsFile).should.be.false();
await checkFile("nodes",{a:1})
await checkFile("users",{b:2})
await checkFile("projects",{c:3})
await checkFile("runtime",{_credentialSecret:"foo"})
done();
}).catch(function(err) {
done(err);
});
}).catch(err => { done(err)});
});
it('should handle settings', function(done) {
var settingsFile = path.join(userDir,".config.json");
it('should load separate settings file', async function() {
await fs.writeFile( path.join(userDir,".config.nodes.json"),JSON.stringify({a:1}),"utf8");
await fs.writeFile( path.join(userDir,".config.users.json"),JSON.stringify({b:2}),"utf8");
await fs.writeFile( path.join(userDir,".config.projects.json"),JSON.stringify({c:3}),"utf8");
await fs.writeFile( path.join(userDir,".config.runtime.json"),JSON.stringify({_credentialSecret:"foo"}),"utf8");
localfilesystemSettings.init({userDir:userDir});
fs.existsSync(settingsFile).should.be.false();
return localfilesystemSettings.init({userDir:userDir})
.then(localfilesystemSettings.getSettings)
.then(settings => {
settings.should.eql({
nodes:{a:1},
_credentialSecret: "foo",
users:{b:2},
projects: {c:3}
})
})
});
var settings = {"abc":{"type":"creds"}};
it('should write only the files that need writing', async function() {
await fs.writeFile( path.join(userDir,".config.nodes.json"),JSON.stringify({a:1}),"utf8");
await fs.writeFile( path.join(userDir,".config.users.json"),JSON.stringify({b:2}),"utf8");
await fs.writeFile( path.join(userDir,".config.projects.json"),JSON.stringify({c:3}),"utf8");
await fs.writeFile( path.join(userDir,".config.runtime.json"),JSON.stringify({_credentialSecret:"foo"}),"utf8");
localfilesystemSettings.saveSettings(settings).then(function() {
fs.existsSync(settingsFile).should.be.true();
localfilesystemSettings.getSettings().then(function(_settings) {
_settings.should.eql(settings);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
const fsStatNodes = await fs.stat(path.join(userDir,".config.nodes.json"))
const fsStatUsers = await fs.stat(path.join(userDir,".config.users.json"))
const fsStatProjects = await fs.stat(path.join(userDir,".config.projects.json"))
const fsStatRuntime = await fs.stat(path.join(userDir,".config.runtime.json"))
return localfilesystemSettings.init({userDir:userDir}).then(() => {
return localfilesystemSettings.saveSettings({
nodes:{d:4},
_credentialSecret: "bar",
users:{b:2},
projects: {c:3}
})
}).then(async function() {
const newFsStatNodes = await fs.stat(path.join(userDir,".config.nodes.json"))
const newFsStatUsers = await fs.stat(path.join(userDir,".config.users.json"))
const newFsStatProjects = await fs.stat(path.join(userDir,".config.projects.json"))
const newFsStatRuntime = await fs.stat(path.join(userDir,".config.runtime.json"))
// Not changed
newFsStatUsers.mtimeMs.should.eql(fsStatUsers.mtimeMs);
newFsStatProjects.mtimeMs.should.eql(fsStatProjects.mtimeMs);
// Changed
newFsStatNodes.mtimeMs.should.not.eql(fsStatNodes.mtimeMs);
newFsStatRuntime.mtimeMs.should.not.eql(fsStatRuntime.mtimeMs);
})
});
});