Skip to content

Commit

Permalink
copySync updated to overwrite destination file if readonly and clobber
Browse files Browse the repository at this point in the history
set to true. Added test case. This commit fixes #183.
  • Loading branch information
bartland committed Oct 31, 2015
1 parent d3b2e03 commit e05c685
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})
})
})
})
Expand Down
8 changes: 6 additions & 2 deletions lib/copy-sync/copy-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit e05c685

Please sign in to comment.