From 48af5c7ed6198d2483b86e5db2dba88b510bea50 Mon Sep 17 00:00:00 2001 From: badrAZ Date: Fri, 30 Apr 2021 15:47:05 +0200 Subject: [PATCH] feat(fs/abstract#list): ignore ENOENT error --- @xen-orchestra/fs/src/abstract.js | 35 ++++++++++++++++++------------- @xen-orchestra/fs/src/fs.spec.js | 8 +++++++ CHANGELOG.unreleased.md | 2 ++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/@xen-orchestra/fs/src/abstract.js b/@xen-orchestra/fs/src/abstract.js index 156b99050..72dc64c1f 100644 --- a/@xen-orchestra/fs/src/abstract.js +++ b/@xen-orchestra/fs/src/abstract.js @@ -242,22 +242,29 @@ export default class RemoteHandlerAbstract { return timeout.call(this._getSize(typeof file === 'string' ? normalizePath(file) : file), this._timeout) } - async list(dir, { filter, prependDir = false } = {}) { - const virtualDir = normalizePath(dir) - dir = normalizePath(dir) + async list(dir, { filter, ignoreEnoentError: ignoreMissing = false, prependDir = false } = {}) { + try { + const virtualDir = normalizePath(dir) + dir = normalizePath(dir) - let entries = await timeout.call(this._list(dir), this._timeout) - if (filter !== undefined) { - entries = entries.filter(filter) + let entries = await timeout.call(this._list(dir), this._timeout) + if (filter !== undefined) { + entries = entries.filter(filter) + } + + if (prependDir) { + entries.forEach((entry, i) => { + entries[i] = virtualDir + '/' + entry + }) + } + + return entries + } catch (error) { + if (ignoreMissing && error?.code === 'ENOENT') { + return [] + } + throw error } - - if (prependDir) { - entries.forEach((entry, i) => { - entries[i] = virtualDir + '/' + entry - }) - } - - return entries } async lock(path) { diff --git a/@xen-orchestra/fs/src/fs.spec.js b/@xen-orchestra/fs/src/fs.spec.js index 3a25e4cd0..7d12869dc 100644 --- a/@xen-orchestra/fs/src/fs.spec.js +++ b/@xen-orchestra/fs/src/fs.spec.js @@ -133,6 +133,14 @@ handlers.forEach(url => { await handler.outputFile('dir/file', '') expect(await handler.list('dir', { prependDir: true })).toEqual(['/dir/file']) }) + + it.only('throws ENOENT if no such directory', async () => { + expect((await rejectionOf(handler.list('dir'))).code).toBe('ENOENT') + }) + + it.only('can returns empty for missing directory', async () => { + expect(await handler.list('dir', { ignoreMissing: true })).toEqual([]) + }) }) describe('#mkdir()', () => { diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 154c5e87a..4d6a4ff21 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -27,3 +27,5 @@ > - major: if the change breaks compatibility > > In case of conflict, the highest (lowest in previous list) `$version` wins. + +- @xen-orchestra/fs minor