feat(fs/{rmdir,unlink}): dont throw on missing entries

This commit is contained in:
Julien Fontanet
2018-12-05 17:17:40 +01:00
parent 1b48c626f4
commit 629931782e
5 changed files with 25 additions and 13 deletions

View File

@@ -20,7 +20,7 @@
"node": ">=6"
},
"dependencies": {
"@marsaud/smb2": "^0.11.0",
"@marsaud/smb2": "^0.12.0",
"@xen-orchestra/async-map": "^0.0.0",
"execa": "^1.0.0",
"fs-extra": "^7.0.0",

View File

@@ -33,6 +33,12 @@ const kResolve = Symbol('resolve')
const DEFAULT_TIMEOUT = 6e5 // 10 min
const ignoreEnoent = error => {
if (error == null || error.code !== 'ENOENT') {
throw error
}
}
export default class RemoteHandlerAbstract {
_remote: Object
_timeout: number
@@ -274,7 +280,10 @@ export default class RemoteHandlerAbstract {
}
async rmdir(dir: string): Promise<void> {
await timeout.call(this._rmdir(this[kResolve](dir)), this._timeout)
await timeout.call(
this._rmdir(this[kResolve](dir)).catch(ignoreEnoent),
this._timeout
)
}
async rmtree(dir: string): Promise<void> {
@@ -320,7 +329,7 @@ export default class RemoteHandlerAbstract {
ignoreErrors.call(this._unlink(checksumFile(file)))
}
await timeout.call(this._unlink(file), this._timeout)
await timeout.call(this._unlink(file).catch(ignoreEnoent), this._timeout)
}
// Methods that can be implemented by inheriting classes

View File

@@ -192,6 +192,10 @@ handlers.forEach(url => {
const error = await rejectionOf(handler.rmdir('.'))
await expect(error.code).toEqual('ENOTEMPTY')
})
it('does not throw on missing directory', async () => {
await handler.rmdir('dir')
})
})
describe('#rmtree', () => {
@@ -218,6 +222,10 @@ handlers.forEach(url => {
await expect(await handler.list('.')).toEqual([])
})
it('does not throw on missing file', async () => {
await handler.unlink('file')
})
})
})
})

View File

@@ -101,11 +101,6 @@ export default class LocalHandler extends RemoteHandlerAbstract {
}
async _unlink(file) {
return fs.unlink(this._getFilePath(file)).catch(error => {
// do not throw if the file did not exist
if (error == null || error.code !== 'ENOENT') {
throw error
}
})
return fs.unlink(this._getFilePath(file))
}
}

View File

@@ -799,10 +799,10 @@
normalize-path "^2.0.1"
through2 "^2.0.3"
"@marsaud/smb2@^0.11.0":
version "0.11.0"
resolved "https://registry.yarnpkg.com/@marsaud/smb2/-/smb2-0.11.0.tgz#6e7d7b71342790e7486cf9ece48f7a7c4eeb4904"
integrity sha512-SQOPUHz0JRFMJ+plKg6JKYkYSsEDf2LlXuntRyNzlg3kUDNmBHKN5erE2R6nsxv3B7h1nlCcJZSi0HsGfHMTQQ==
"@marsaud/smb2@^0.12.0":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@marsaud/smb2/-/smb2-0.12.0.tgz#8f02d73d8396fa318add926ec3dec8036effa6a9"
integrity sha512-8bK3bSiIlWAZHMe3dow9DeggjW89uZFIDaE1j0EVpyNQ1cPkQu81MADdxYcMnpPvO++ZMlF0hWVGsnZugBKtgA==
dependencies:
babel-runtime "^5.8.34"
bluebird "^3.5.3"