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

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) {
localfilesystem.init({userDir:userDir}).then(function() {
localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {