diff --git a/index.js b/index.js index b049482..f0613df 100644 --- a/index.js +++ b/index.js @@ -30,9 +30,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => { progressEmitter.written = progressEmitter.size; updateStats = true; } catch (error) { - if (options.overwrite || error.code !== 'EEXIST') { - throw new CpFileError(`Cannot write to \`${destination}\`: ${error.message}`, error); - } + throw new CpFileError(`Cannot write to \`${destination}\`: ${error.message}`, error); } if (readError) { diff --git a/test/async.js b/test/async.js index b8064e3..2c87715 100644 --- a/test/async.js +++ b/test/async.js @@ -7,9 +7,9 @@ import del from 'del'; import test from 'ava'; import uuid from 'uuid'; import sinon from 'sinon'; -import cpFile from '..'; import assertDateEqual from './helpers/_assert'; -import {buildEACCES, buildEIO, buildENOSPC, buildENOENT, buildEPERM} from './helpers/_fs-errors'; +import {buildEACCES, buildEIO, buildENOSPC, buildENOENT, buildEPERM, buildERRSTREAMWRITEAFTEREND} from './helpers/_fs-errors'; +import cpFile from '..'; const THREE_HUNDRED_KILO = (100 * 3 * 1024) + 1; @@ -75,8 +75,9 @@ test('overwrite when options are undefined', async t => { test('do not overwrite when disabled', async t => { fs.writeFileSync(t.context.destination, ''); - await cpFile('license', t.context.destination, {overwrite: false}); - t.is(fs.readFileSync(t.context.destination, 'utf8'), ''); + const error = await t.throwsAsync(cpFile('license', t.context.destination, {overwrite: false})); + t.is(error.name, 'CpFileError', error.message); + t.is(error.code, 'EEXIST', error.message); }); test('do not create `destination` on unreadable `source`', async t => { @@ -245,7 +246,7 @@ test.serial('rethrow read after open errors', async t => { const {createWriteStream, createReadStream} = fs; let calledWriteEnd = 0; let readStream; - const readError = buildEIO(); + const readError = process.release.lts === 'Erbium' || parseInt(process.versions.node.slice(0, 2), 10) > 12 ? buildERRSTREAMWRITEAFTEREND() : buildEIO(); fs.createWriteStream = (...args) => { const stream = createWriteStream(...args); @@ -275,8 +276,8 @@ test.serial('rethrow read after open errors', async t => { const uncached = importFresh('..'); const error = await t.throwsAsync(uncached('license', t.context.destination)); t.is(error.name, 'CpFileError', error.message); - t.is(error.errno, readError.errno, error.message); t.is(error.code, readError.code, error.message); + t.is(error.errno, readError.errno, error.message); t.is(calledWriteEnd, 1); Object.assign(fs, {createWriteStream, createReadStream}); diff --git a/test/helpers/_fs-errors.js b/test/helpers/_fs-errors.js index 9636b11..a9f80a0 100644 --- a/test/helpers/_fs-errors.js +++ b/test/helpers/_fs-errors.js @@ -22,6 +22,10 @@ exports.buildEIO = () => Object.assign(new Error('EIO: i/o error, read errno: -5 code: 'EIO' }); +exports.buildERRSTREAMWRITEAFTEREND = () => Object.assign(new Error('ERR_STREAM_WRITE_AFTER_END'), { + code: 'ERR_STREAM_WRITE_AFTER_END' +}); + exports.buildEBADF = () => Object.assign(new Error('EBADF: bad file descriptor'), { errno: -9, code: 'EBADF' diff --git a/test/sync.js b/test/sync.js index 21c02a6..83048af 100644 --- a/test/sync.js +++ b/test/sync.js @@ -5,9 +5,9 @@ import del from 'del'; import test from 'ava'; import uuid from 'uuid'; import sinon from 'sinon'; -import cpFile from '..'; import assertDateEqual from './helpers/_assert'; import {buildEACCES, buildENOSPC, buildEBADF, buildEPERM} from './helpers/_fs-errors'; +import cpFile from '..'; const THREE_HUNDRED_KILO = (100 * 3 * 1024) + 1;