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) {
|
settings.userDir = fspath.join(process.env.HOMEPATH,".node-red");
|
||||||
try {
|
} else {
|
||||||
// Consider compatibility for older versions
|
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
|
||||||
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 (!settings.readOnly) {
|
||||||
|
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 => {
|
||||||
result[key] = sectionData;
|
if (Object.keys(sectionData).length > 0) {
|
||||||
|
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();
|
return localfilesystemSettings.getSettings();
|
||||||
localfilesystemSettings.getSettings().then(function(settings) {
|
}).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}
|
||||||
|
}),"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();
|
done();
|
||||||
}).catch(function(err) {
|
}).catch(err => { done(err)});
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle settings', function(done) {
|
it('should load separate settings file', async function() {
|
||||||
var settingsFile = path.join(userDir,".config.json");
|
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});
|
return localfilesystemSettings.init({userDir:userDir})
|
||||||
fs.existsSync(settingsFile).should.be.false();
|
.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() {
|
const fsStatNodes = await fs.stat(path.join(userDir,".config.nodes.json"))
|
||||||
fs.existsSync(settingsFile).should.be.true();
|
const fsStatUsers = await fs.stat(path.join(userDir,".config.users.json"))
|
||||||
localfilesystemSettings.getSettings().then(function(_settings) {
|
const fsStatProjects = await fs.stat(path.join(userDir,".config.projects.json"))
|
||||||
_settings.should.eql(settings);
|
const fsStatRuntime = await fs.stat(path.join(userDir,".config.runtime.json"))
|
||||||
done();
|
|
||||||
}).catch(function(err) {
|
return localfilesystemSettings.init({userDir:userDir}).then(() => {
|
||||||
done(err);
|
|
||||||
});
|
return localfilesystemSettings.saveSettings({
|
||||||
}).catch(function(err) {
|
nodes:{d:4},
|
||||||
done(err);
|
_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