feat(fs/abstract#list): ignore ENOENT error

This commit is contained in:
badrAZ 2021-04-30 15:47:05 +02:00 committed by Julien Fontanet
parent cfaf336597
commit 48af5c7ed6
3 changed files with 31 additions and 14 deletions

View File

@ -242,22 +242,29 @@ export default class RemoteHandlerAbstract {
return timeout.call(this._getSize(typeof file === 'string' ? normalizePath(file) : file), this._timeout) return timeout.call(this._getSize(typeof file === 'string' ? normalizePath(file) : file), this._timeout)
} }
async list(dir, { filter, prependDir = false } = {}) { async list(dir, { filter, ignoreEnoentError: ignoreMissing = false, prependDir = false } = {}) {
const virtualDir = normalizePath(dir) try {
dir = normalizePath(dir) const virtualDir = normalizePath(dir)
dir = normalizePath(dir)
let entries = await timeout.call(this._list(dir), this._timeout) let entries = await timeout.call(this._list(dir), this._timeout)
if (filter !== undefined) { if (filter !== undefined) {
entries = entries.filter(filter) 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) { async lock(path) {

View File

@ -133,6 +133,14 @@ handlers.forEach(url => {
await handler.outputFile('dir/file', '') await handler.outputFile('dir/file', '')
expect(await handler.list('dir', { prependDir: true })).toEqual(['/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()', () => { describe('#mkdir()', () => {

View File

@ -27,3 +27,5 @@
> - major: if the change breaks compatibility > - major: if the change breaks compatibility
> >
> In case of conflict, the highest (lowest in previous list) `$version` wins. > In case of conflict, the highest (lowest in previous list) `$version` wins.
- @xen-orchestra/fs minor