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:
parent
fb2da0ee9e
commit
4d0c572c2e
@ -15,7 +15,6 @@
|
|||||||
**/
|
**/
|
||||||
|
|
||||||
var fs = require('fs-extra');
|
var fs = require('fs-extra');
|
||||||
var when = require('when');
|
|
||||||
var fspath = require("path");
|
var fspath = require("path");
|
||||||
|
|
||||||
var log = require("@node-red/util").log; // TODO: separate module
|
var log = require("@node-red/util").log; // TODO: separate module
|
||||||
@ -29,6 +28,11 @@ var projects = require("./projects");
|
|||||||
var initialFlowLoadComplete = false;
|
var initialFlowLoadComplete = false;
|
||||||
var settings;
|
var settings;
|
||||||
|
|
||||||
|
function checkForConfigFile(dir) {
|
||||||
|
return fs.existsSync(fspath.join(dir,".config.json")) ||
|
||||||
|
fs.existsSync(fspath.join(dir,".config.nodes.json"))
|
||||||
|
}
|
||||||
|
|
||||||
var localfilesystem = {
|
var localfilesystem = {
|
||||||
init: function(_settings, runtime) {
|
init: function(_settings, runtime) {
|
||||||
settings = _settings;
|
settings = _settings;
|
||||||
@ -36,34 +40,24 @@ var localfilesystem = {
|
|||||||
var promises = [];
|
var promises = [];
|
||||||
|
|
||||||
if (!settings.userDir) {
|
if (!settings.userDir) {
|
||||||
try {
|
if (checkForConfigFile(process.env.NODE_RED_HOME)) {
|
||||||
fs.statSync(fspath.join(process.env.NODE_RED_HOME,".config.json"));
|
settings.userDir = 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"))) {
|
||||||
} 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");
|
settings.userDir = fspath.join(process.env.HOMEPATH,".node-red");
|
||||||
}
|
} else {
|
||||||
} 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");
|
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!settings.readOnly) {
|
if (!settings.readOnly) {
|
||||||
promises.push(fs.ensureDir(fspath.join(settings.userDir,"node_modules")));
|
promises.push(fs.ensureDir(fspath.join(settings.userDir,"node_modules")));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sessions.init(settings);
|
sessions.init(settings);
|
||||||
runtimeSettings.init(settings);
|
promises.push(runtimeSettings.init(settings));
|
||||||
promises.push(library.init(settings));
|
promises.push(library.init(settings));
|
||||||
promises.push(projects.init(settings, runtime));
|
promises.push(projects.init(settings, runtime));
|
||||||
|
|
||||||
var packageFile = fspath.join(settings.userDir,"package.json");
|
var packageFile = fspath.join(settings.userDir,"package.json");
|
||||||
var packagePromise = when.resolve();
|
var packagePromise = Promise.resolve();
|
||||||
|
|
||||||
if (!settings.readOnly) {
|
if (!settings.readOnly) {
|
||||||
packagePromise = function() {
|
packagePromise = function() {
|
||||||
@ -81,7 +75,7 @@ var localfilesystem = {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return when.all(promises).then(packagePromise);
|
return Promise.all(promises).then(packagePromise);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ const log = require("@node-red/util").log;
|
|||||||
const util = require("./util");
|
const util = require("./util");
|
||||||
|
|
||||||
const configSections = ['nodes','users','projects'];
|
const configSections = ['nodes','users','projects'];
|
||||||
let initialisePromise;
|
|
||||||
|
|
||||||
const settingsCache = {};
|
const settingsCache = {};
|
||||||
|
|
||||||
@ -78,7 +77,9 @@ async function readSettings() {
|
|||||||
configSections.forEach(key => {
|
configSections.forEach(key => {
|
||||||
const sectionFilename = getSettingsFilename(key);
|
const sectionFilename = getSettingsFilename(key);
|
||||||
readPromises.push(util.readFile(sectionFilename,sectionFilename+".backup",{}).then(sectionData => {
|
readPromises.push(util.readFile(sectionFilename,sectionFilename+".backup",{}).then(sectionData => {
|
||||||
|
if (Object.keys(sectionData).length > 0) {
|
||||||
result[key] = sectionData;
|
result[key] = sectionData;
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
return Promise.all(readPromises).then(() => result);
|
return Promise.all(readPromises).then(() => result);
|
||||||
@ -95,18 +96,18 @@ module.exports = {
|
|||||||
globalSettingsBackup = fspath.join(settings.userDir,".config.json.backup");
|
globalSettingsBackup = fspath.join(settings.userDir,".config.json.backup");
|
||||||
|
|
||||||
if (fs.existsSync(globalSettingsFile) && !settings.readOnly) {
|
if (fs.existsSync(globalSettingsFile) && !settings.readOnly) {
|
||||||
initialisePromise = migrateToMultipleConfigFiles();
|
return migrateToMultipleConfigFiles();
|
||||||
} else {
|
} else {
|
||||||
initialisePromise = Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getSettings: function() {
|
getSettings: function() {
|
||||||
return initialisePromise.then(readSettings)
|
return readSettings()
|
||||||
},
|
},
|
||||||
saveSettings: function(newSettings) {
|
saveSettings: function(newSettings) {
|
||||||
if (settings.readOnly) {
|
if (settings.readOnly) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
return initialisePromise.then(() => writeSettings(newSettings));
|
return writeSettings(newSettings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
var should = require("should");
|
const should = require("should");
|
||||||
var fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
var NR_TEST_UTILS = require("nr-test-utils");
|
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) {
|
it('should handle non-existent settings', function(done) {
|
||||||
var settingsFile = path.join(userDir,".settings.json");
|
var settingsFile = path.join(userDir,".config.json");
|
||||||
|
localfilesystemSettings.init({userDir:userDir}).then(function() {
|
||||||
localfilesystemSettings.init({userDir:userDir});
|
|
||||||
fs.existsSync(settingsFile).should.be.false();
|
fs.existsSync(settingsFile).should.be.false();
|
||||||
localfilesystemSettings.getSettings().then(function(settings) {
|
return localfilesystemSettings.getSettings();
|
||||||
|
}).then(function(settings) {
|
||||||
settings.should.eql({});
|
settings.should.eql({});
|
||||||
done();
|
done();
|
||||||
}).catch(function(err) {
|
}).catch(err => { done(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");
|
var settingsFile = path.join(userDir,".config.json");
|
||||||
fs.writeFileSync(settingsFile,"[This is not json","utf8");
|
fs.writeFileSync(settingsFile,JSON.stringify({
|
||||||
localfilesystemSettings.init({userDir:userDir});
|
nodes:{a:1},
|
||||||
fs.existsSync(settingsFile).should.be.true();
|
_credentialSecret: "foo",
|
||||||
localfilesystemSettings.getSettings().then(function(settings) {
|
users:{b:2},
|
||||||
settings.should.eql({});
|
projects: {c:3}
|
||||||
done();
|
}),"utf8");
|
||||||
}).catch(function(err) {
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle settings', function(done) {
|
async function checkFile(sectionName, expectedContents) {
|
||||||
var settingsFile = path.join(userDir,".config.json");
|
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});
|
localfilesystemSettings.init({userDir:userDir}).then(async function() {
|
||||||
fs.existsSync(settingsFile).should.be.false();
|
fs.existsSync(settingsFile).should.be.false();
|
||||||
|
await checkFile("nodes",{a:1})
|
||||||
var settings = {"abc":{"type":"creds"}};
|
await checkFile("users",{b:2})
|
||||||
|
await checkFile("projects",{c:3})
|
||||||
localfilesystemSettings.saveSettings(settings).then(function() {
|
await checkFile("runtime",{_credentialSecret:"foo"})
|
||||||
fs.existsSync(settingsFile).should.be.true();
|
|
||||||
localfilesystemSettings.getSettings().then(function(_settings) {
|
|
||||||
_settings.should.eql(settings);
|
|
||||||
done();
|
done();
|
||||||
}).catch(function(err) {
|
}).catch(err => { done(err)});
|
||||||
done(err);
|
|
||||||
});
|
});
|
||||||
}).catch(function(err) {
|
|
||||||
done(err);
|
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");
|
||||||
|
|
||||||
|
return localfilesystemSettings.init({userDir:userDir})
|
||||||
|
.then(localfilesystemSettings.getSettings)
|
||||||
|
.then(settings => {
|
||||||
|
settings.should.eql({
|
||||||
|
nodes:{a:1},
|
||||||
|
_credentialSecret: "foo",
|
||||||
|
users:{b:2},
|
||||||
|
projects: {c:3}
|
||||||
|
})
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
})
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user