From e05c685e5a02ebb8ebf18049da4c8af4d9a7fb9c Mon Sep 17 00:00:00 2001 From: bartland Date: Fri, 30 Oct 2015 16:29:42 +1100 Subject: [PATCH] copySync updated to overwrite destination file if readonly and clobber set to true. Added test case. This commit fixes #183. --- lib/copy-sync/__tests__/copy-sync-file.test.js | 14 ++++++++++++++ lib/copy-sync/copy-file-sync.js | 8 ++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/copy-sync/__tests__/copy-sync-file.test.js b/lib/copy-sync/__tests__/copy-sync-file.test.js index 63a4889c..b6c869b3 100644 --- a/lib/copy-sync/__tests__/copy-sync-file.test.js +++ b/lib/copy-sync/__tests__/copy-sync-file.test.js @@ -183,6 +183,20 @@ describe('+ copySync()', function () { assert.strictEqual(destData, destDataNew) }) }) + + describe('> when clobber is true and dest is readonly', function () { + it('should copy the file and not throw an error', function () { + try { + fs.chmodSync(dest, parseInt('444', 8)) + fs.copySync(src, dest, {clobber: true}) + destData = fs.readFileSync(dest, 'utf8') + assert.strictEqual(srcData, destData) + } finally { + // destination file is readonly so just remove it so we don't affect other tests + fs.unlinkSync(dest) + } + }) + }) }) }) }) diff --git a/lib/copy-sync/copy-file-sync.js b/lib/copy-sync/copy-file-sync.js index 2dc31ef3..f331b3c2 100644 --- a/lib/copy-sync/copy-file-sync.js +++ b/lib/copy-sync/copy-file-sync.js @@ -7,8 +7,12 @@ function copyFileSync (srcFile, destFile, options) { var clobber = options.clobber var preserveTimestamps = options.preserveTimestamps - if (fs.existsSync(destFile) && !clobber) { - throw Error('EEXIST') + if (fs.existsSync(destFile)) { + if (clobber) { + fs.unlinkSync(destFile) + } else { + throw Error('EEXIST') + } } var fdr = fs.openSync(srcFile, 'r')