Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename clobber to overwrite #333

Merged
merged 1 commit into from
Dec 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ Methods
Copy a file or directory. The directory can have contents. Like `cp -r`.

Options:
- clobber (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
- errorOnExist (boolean): when `clobber` is `false` and the destination exists, throw an error. Default is `false`.
- overwrite (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
- errorOnExist (boolean): when `overwrite` is `false` and the destination exists, throw an error. Default is `false`.
- dereference (boolean): dereference symlinks, default is `false`.
- preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`.
- filter: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background). _Warning: `copySync` currently applies the filter only to files (see [#180](https://github.com/jprichardson/node-fs-extra/issues/180)). This will be fixed in a future release._
Expand Down Expand Up @@ -284,7 +284,7 @@ fs.mkdirsSync('/tmp/another/path')
Moves a file or directory, even across devices.

Options:
- clobber (boolean): overwrite existing file or directory
- overwrite (boolean): overwrite existing file or directory, default is `false`

Example:

Expand Down
44 changes: 32 additions & 12 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('+ copySync()', function () {
})
})

describe('> when clobber option is passed', function () {
describe('> when overwrite option is passed', function () {
var src, dest
var srcData = 'some src data'

Expand All @@ -134,17 +134,17 @@ describe('+ copySync()', function () {
})

describe('> when destination file does NOT exist', function () {
describe('> when clobber is true', function () {
describe('> when overwrite is true', function () {
it('should copy the file and not throw an error', function () {
fs.copySync(src, dest, {clobber: true})
fs.copySync(src, dest, {overwrite: true})
var destData = fs.readFileSync(dest, 'utf8')
assert.strictEqual(srcData, destData)
})
})

describe('> when clobber is false', function () {
describe('> when overwrite is false', function () {
it('should copy the file and not throw an error', function () {
fs.copySync(src, dest, {clobber: false})
fs.copySync(src, dest, {overwrite: false})
var destData = fs.readFileSync(dest, 'utf8')
assert.strictEqual(srcData, destData)
})
Expand All @@ -158,25 +158,25 @@ describe('+ copySync()', function () {
fs.writeFileSync(dest, destData)
})

describe('> when clobber is true', function () {
describe('> when overwrite is true', function () {
it('should copy the file and not throw an error', function () {
fs.copySync(src, dest, {clobber: true})
fs.copySync(src, dest, {overwrite: true})
destData = fs.readFileSync(dest, 'utf8')
assert.strictEqual(srcData, destData)
})
})

describe('> when clobber is false', function () {
describe('> when overwrite is false', function () {
it('should not throw an error', function () {
fs.copySync(src, dest, {clobber: false})
fs.copySync(src, dest, {overwrite: false})

// copy never happened
var destDataNew = fs.readFileSync(dest, 'utf8')
assert.strictEqual(destData, destDataNew)
})
it('should throw an error when errorOnExist is true', function () {
assert.throws(function () {
fs.copySync(src, dest, {clobber: false, errorOnExist: true})
fs.copySync(src, dest, {overwrite: false, errorOnExist: true})
})

// copy never happened
Expand All @@ -185,11 +185,11 @@ describe('+ copySync()', function () {
})
})

describe('> when clobber is true and dest is readonly', function () {
describe('> when overwrite 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})
fs.copySync(src, dest, {overwrite: true})
destData = fs.readFileSync(dest, 'utf8')
assert.strictEqual(srcData, destData)
} finally {
Expand All @@ -200,5 +200,25 @@ describe('+ copySync()', function () {
})
})
})
describe('clobber', function () {
var src, dest, srcData, destData

beforeEach(function () {
src = path.join(TEST_DIR, 'src-file')
dest = path.join(TEST_DIR, 'des-file')
srcData = 'some src data'
destData = 'some dest data'
fs.writeFileSync(src, srcData)
fs.writeFileSync(dest, destData)
})

it('is an alias for overwrite', function () {
fs.copySync(src, dest, {clobber: false})

// copy never happened
var destDataNew = fs.readFileSync(dest, 'utf8')
assert.strictEqual(destData, destDataNew)
})
})
})
})
4 changes: 2 additions & 2 deletions lib/copy-sync/copy-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ var BUF_LENGTH = 64 * 1024
var _buff = new Buffer(BUF_LENGTH)

function copyFileSync (srcFile, destFile, options) {
var clobber = options.clobber
var overwrite = options.overwrite
var errorOnExist = options.errorOnExist
var preserveTimestamps = options.preserveTimestamps

if (fs.existsSync(destFile)) {
if (clobber) {
if (overwrite) {
fs.unlinkSync(destFile)
} else if (errorOnExist) {
throw new Error(destFile + ' already exists')
Expand Down
4 changes: 3 additions & 1 deletion lib/copy-sync/copy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function copySync (src, dest, options) {

// default to true for now
options.clobber = 'clobber' in options ? !!options.clobber : true
// overwrite falls back to clobber
options.overwrite = 'overwrite' in options ? !!options.overwrite : options.clobber
options.dereference = 'dereference' in options ? !!options.dereference : false
options.preserveTimestamps = 'preserveTimestamps' in options ? !!options.preserveTimestamps : false

Expand All @@ -37,7 +39,7 @@ function copySync (src, dest, options) {
if (stats.isFile() && performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest, {
clobber: options.clobber,
overwrite: options.overwrite,
errorOnExist: options.errorOnExist,
preserveTimestamps: options.preserveTimestamps
})
Expand Down
28 changes: 23 additions & 5 deletions lib/copy/__tests__/ncp/ncp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('ncp', function () {
})
})

describe('when using clobber=true', function () {
describe('when using overwrite=true', function () {
before(function () {
this.originalCreateReadStream = fs.createReadStream
})
Expand All @@ -77,7 +77,7 @@ describe('ncp', function () {
})

it('the copy is complete after callback', function (done) {
ncp(src, out, {clobber: true}, function (err) {
ncp(src, out, {overwrite: true}, function (err) {
fs.createReadStream = function () {
done(new Error('createReadStream after callback'))
}
Expand All @@ -87,25 +87,43 @@ describe('ncp', function () {
})
})

describe('when using clobber=false', function () {
describe('when using overwrite=false', function () {
beforeEach(function (done) {
rimraf(out, done)
})
it('works', function (cb) {
ncp(src, out, {clobber: false}, function (err) {
ncp(src, out, {overwrite: false}, function (err) {
assert.ifError(err)
cb()
})
})
it('should not error if files exist', function (cb) {
ncp(src, out, function () {
ncp(src, out, {clobber: false}, function (err) {
ncp(src, out, {overwrite: false}, function (err) {
assert.ifError(err)
cb()
})
})
})
it('should error if errorOnExist and file exists', function (cb) {
ncp(src, out, function () {
ncp(src, out, {
overwrite: false,
errorOnExist: true
}, function (err) {
assert(err)
cb()
})
})
})
})

describe('clobber', function () {
beforeEach(function (done) {
rimraf(out, done)
})

it('is an alias for overwrite', function (cb) {
ncp(src, out, function () {
ncp(src, out, {
clobber: false,
Expand Down
7 changes: 5 additions & 2 deletions lib/copy/ncp.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ function ncp (source, dest, options, callback) {

var filter = options.filter
var transform = options.transform
var clobber = options.clobber !== false // default true
var overwrite = options.overwrite
// If overwrite is undefined, use clobber, otherwise default to true:
if (overwrite === undefined) overwrite = options.clobber
if (overwrite === undefined) overwrite = true
var errorOnExist = options.errorOnExist
var dereference = options.dereference
var preserveTimestamps = options.preserveTimestamps === true
Expand Down Expand Up @@ -78,7 +81,7 @@ function ncp (source, dest, options, callback) {
if (writable) {
copyFile(file, target)
} else {
if (clobber) {
if (overwrite) {
rmFile(target, function () {
copyFile(file, target)
})
Expand Down
46 changes: 42 additions & 4 deletions lib/move/__tests__/move-clobber.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var fse = require(process.cwd())

/* global afterEach, beforeEach, describe, it */

describe('move / clobber', function () {
describe('move / overwrite', function () {
var TEST_DIR, FIXTURES_DIR

beforeEach(function (done) {
Expand All @@ -26,9 +26,9 @@ describe('move / clobber', function () {
fse.remove(TEST_DIR, done)
})

describe('> when clobber = true', function () {
describe('> when overwrite = true', function () {
describe('> when dest is a directory', function () {
it('should clobber the destination', function (done) {
it('should overwrite the destination', function (done) {
// Tests fail on appveyor/Windows due to
// https://github.com/isaacs/node-graceful-fs/issues/98.
// Workaround by increasing the timeout by a minute (because
Expand All @@ -54,7 +54,7 @@ describe('move / clobber', function () {
assert(paths.indexOf('some-file') >= 0)
assert(paths.indexOf('some-folder') >= 0)

fse.move(src, dest, {clobber: true}, function (err) {
fse.move(src, dest, {overwrite: true}, function (err) {
if (err) return done(err)

// verify dest does not have old stuff
Expand All @@ -71,4 +71,42 @@ describe('move / clobber', function () {
})
})
})
describe('> clobber', function () {
it('is an alias for overwrite', function (done) {
// Tests fail on appveyor/Windows due to
// https://github.com/isaacs/node-graceful-fs/issues/98.
// Workaround by increasing the timeout by a minute (because
// graceful times out after a minute).
this.timeout(90000)

// use fixtures dir as dest since it has stuff
var dest = FIXTURES_DIR
var paths = fs.readdirSync(dest)

// verify dest has stuff
assert(paths.indexOf('a-file') >= 0)

// create new source dir
var src = path.join(TEST_DIR, 'src')
fse.ensureDirSync(src)
fs.writeFileSync(path.join(src, 'some-file'), 'hi')

// verify source has stuff
paths = fs.readdirSync(src)
assert(paths.indexOf('some-file') >= 0)

fse.move(src, dest, {clobber: true}, function (err) {
if (err) return done(err)

// verify dest does not have old stuff
var paths = fs.readdirSync(dest)
assert.strictEqual(paths.indexOf('a-file'), -1)

// verify dest has new stuff
assert(paths.indexOf('some-file') >= 0)

done()
})
})
})
})
8 changes: 4 additions & 4 deletions lib/move/__tests__/move.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ describe('move', function () {
})
})

it('should not overwrite if clobber = false', function (done) {
it('should not overwrite if overwrite = false', function (done) {
var src = FIXTURES_DIR + '/a-file'
var dest = FIXTURES_DIR + '/a-folder/another-file'

// verify file exists already
assert(fs.existsSync(dest))

fse.move(src, dest, {clobber: false}, function (err) {
fse.move(src, dest, {overwrite: false}, function (err) {
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
done()
})
Expand Down Expand Up @@ -173,15 +173,15 @@ describe('move', function () {
})
})

it('should clobber folders across devices', function (done) {
it('should overwrite folders across devices', function (done) {
var src = FIXTURES_DIR + '/a-folder'
var dest = FIXTURES_DIR + '/a-folder-dest'

fs.mkdirSync(dest)

setUpMockFs('EXDEV')

fse.move(src, dest, {clobber: true}, function (err) {
fse.move(src, dest, {overwrite: true}, function (err) {
assert.ifError(err)
assert.strictEqual(fs.rename.callCount, 1)

Expand Down
Loading