From c8d6693fbad7b984ffa7e56a87096cda7601ddf3 Mon Sep 17 00:00:00 2001 From: btsimonh Date: Sun, 8 Jan 2017 23:00:27 +0000 Subject: [PATCH] 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 --- red/runtime/storage/localfilesystem.js | 93 +++++++++++-------- .../runtime/storage/localfilesystem_spec.js | 13 +++ 2 files changed, 66 insertions(+), 40 deletions(-) diff --git a/red/runtime/storage/localfilesystem.js b/red/runtime/storage/localfilesystem.js index 8e024d661..cc93dfe21 100644 --- a/red/runtime/storage/localfilesystem.js +++ b/red/runtime/storage/localfilesystem.js @@ -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) { diff --git a/test/red/runtime/storage/localfilesystem_spec.js b/test/red/runtime/storage/localfilesystem_spec.js index 1c7d75e52..35faf1ccf 100644 --- a/test/red/runtime/storage/localfilesystem_spec.js +++ b/test/red/runtime/storage/localfilesystem_spec.js @@ -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) {