Skip to content
Open
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
59 changes: 59 additions & 0 deletions lib/copy/__tests__/copy-symlink-overwrite.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'
/* eslint-env mocha */

const assert = require('assert')
const os = require('os')
const path = require('path')
const fs = require('../..')

for (const method of ['copy', 'copySync']) {
describe(`${method}() / symlink overwrite options`, () => {
const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', `${method}-symlink-overwrite`)
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
const srcTarget = path.join(TEST_DIR, 'source-target')
const destTarget = path.join(TEST_DIR, 'destination-target')

beforeEach(() => {
fs.emptyDirSync(TEST_DIR)
fs.writeFileSync(srcTarget, 'source content')
fs.symlinkSync(srcTarget, src, 'file')
})

afterEach(() => fs.removeSync(TEST_DIR))

for (const broken of [false, true]) {
describe(`with a ${broken ? 'broken' : 'valid'} destination symlink`, () => {
beforeEach(() => {
if (!broken) fs.writeFileSync(destTarget, 'destination content')
fs.symlinkSync(destTarget, dest, 'file')
})

for (const option of ['overwrite', 'clobber']) {
it(`does not replace the destination when ${option} is false`, async () => {
await fs[method](src, dest, { [option]: false })
assert.strictEqual(fs.readlinkSync(dest), destTarget)
})
}

it('reports an existing destination when errorOnExist is true', async () => {
await assert.rejects(async () => fs[method](src, dest, {
overwrite: false,
errorOnExist: true
}), { message: `'${dest}' already exists` })
assert.strictEqual(fs.readlinkSync(dest), destTarget)
})

it('still replaces the destination by default', async () => {
await fs[method](src, dest)
assert.strictEqual(fs.readlinkSync(dest), srcTarget)
})
})
}

it('copies to a missing destination when overwrite is false', async () => {
await fs[method](src, dest, { overwrite: false, errorOnExist: true })
assert.strictEqual(fs.readlinkSync(dest), srcTarget)
})
})
}
4 changes: 4 additions & 0 deletions lib/copy/copy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ function onLink (destStat, src, dest, opts) {
if (opts.dereference) {
resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
}
if (destStat && !opts.overwrite) {
if (opts.errorOnExist) throw new Error(`'${dest}' already exists`)
return
}

if (!destStat) {
return fs.symlinkSync(resolvedSrc, dest)
Expand Down
4 changes: 4 additions & 0 deletions lib/copy/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ async function onLink (destStat, src, dest, opts) {
if (opts.dereference) {
resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
}
if (destStat && !opts.overwrite) {
if (opts.errorOnExist) throw new Error(`'${dest}' already exists`)
return
}
if (!destStat) {
return fs.symlink(resolvedSrc, dest)
}
Expand Down
Loading