fix bug where savesettings did not honor local settings variables (#1073)

* fix bug where savesettings did not honor local settings variables

* don't create lib/flows on read;  It's already created in localfilesystem.init and in saveLibraryEntry -
so removed call to promiseDir, and added a return of [] if accessing a folder which did not exist.
This is important because else when settings.readOnly is true, it still creates folders.

* Fix a CI failure where path passed to getLibraryEntry is empty;
treat this case as meaning it was wanting a folder, and return empty if the folder dioes not exist

* Add a test for getLibraryEntry( type, '/' ) as called by node-red

* change newsettings to camelCase newSettings
This commit is contained in:
btsimonh 2017-01-08 23:00:27 +00:00 committed by Nick O'Leary
parent 81bbdfe413
commit c8d6693fba
2 changed files with 66 additions and 40 deletions

View File

@ -302,11 +302,11 @@ var localfilesystem = {
}) })
}) })
}, },
saveSettings: function(settings) { saveSettings: function(newSettings) {
if (settings.readOnly) { if (settings.readOnly) {
return when.resolve(); return when.resolve();
} }
return writeFile(globalSettingsFile,JSON.stringify(settings,null,1)); return writeFile(globalSettingsFile,JSON.stringify(newSettings,null,1));
}, },
getSessions: function() { getSessions: function() {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
@ -332,44 +332,57 @@ var localfilesystem = {
getLibraryEntry: function(type,path) { getLibraryEntry: function(type,path) {
var root = fspath.join(libDir,type); var root = fspath.join(libDir,type);
var rootPath = fspath.join(libDir,type,path); var rootPath = fspath.join(libDir,type,path);
return promiseDir(root).then(function () {
return nodeFn.call(fs.lstat, rootPath).then(function(stats) { // don't create the folder if it does not exist - we are only reading....
if (stats.isFile()) { return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
return getFileBody(root,path); if (stats.isFile()) {
} return getFileBody(root,path);
if (path.substr(-1) == '/') { }
path = path.substr(0,path.length-1); if (path.substr(-1) == '/') {
} path = path.substr(0,path.length-1);
return nodeFn.call(fs.readdir, rootPath).then(function(fns) { }
var dirs = []; return nodeFn.call(fs.readdir, rootPath).then(function(fns) {
var files = []; var dirs = [];
fns.sort().filter(function(fn) { var files = [];
var fullPath = fspath.join(path,fn); fns.sort().filter(function(fn) {
var absoluteFullPath = fspath.join(root,fullPath); var fullPath = fspath.join(path,fn);
if (fn[0] != ".") { var absoluteFullPath = fspath.join(root,fullPath);
var stats = fs.lstatSync(absoluteFullPath); if (fn[0] != ".") {
if (stats.isDirectory()) { var stats = fs.lstatSync(absoluteFullPath);
dirs.push(fn); if (stats.isDirectory()) {
} else { dirs.push(fn);
var meta = getFileMeta(root,fullPath); } else {
meta.fn = fn; var meta = getFileMeta(root,fullPath);
files.push(meta); meta.fn = fn;
} files.push(meta);
} }
}); }
return dirs.concat(files); });
}); return dirs.concat(files);
}).otherwise(function(err) { });
if (type === "flows" && !/\.json$/.test(path)) { }).otherwise(function(err) {
return localfilesystem.getLibraryEntry(type,path+".json") // if path is empty, then assume it was a folder, return empty
.otherwise(function(e) { if (path === ""){
throw err; return [];
}); }
} else {
throw err; // if path ends with slash, it was a folder
} // so return empty
}); if (path.substr(-1) == '/') {
}); return [];
}
// else path was specified, but did not exist,
// check for path.json as an alternative if flows
if (type === "flows" && !/\.json$/.test(path)) {
return localfilesystem.getLibraryEntry(type,path+".json")
.otherwise(function(e) {
throw err;
});
} else {
throw err;
}
});
}, },
saveLibraryEntry: function(type,path,meta,body) { saveLibraryEntry: function(type,path,meta,body) {

View File

@ -489,6 +489,19 @@ describe('LocalFileSystem', function() {
}); });
}); });
it('should return an empty list of library objects (path=/)',function(done) {
localfilesystem.init({userDir:userDir}).then(function() {
localfilesystem.getLibraryEntry('object','/').then(function(flows) {
flows.should.eql([]);
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should return an error for a non-existent library object',function(done) { it('should return an error for a non-existent library object',function(done) {
localfilesystem.init({userDir:userDir}).then(function() { localfilesystem.init({userDir:userDir}).then(function() {
localfilesystem.getLibraryEntry('object','A/B').then(function(flows) { localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {