Skip to content

Commit

Permalink
Always throw if unable to overwrite (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Feb 24, 2020
1 parent 4e1367f commit 4668c5a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
4 changes: 1 addition & 3 deletions index.js
Expand Up @@ -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) {
Expand Down
13 changes: 7 additions & 6 deletions test/async.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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});
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/_fs-errors.js
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion test/sync.js
Expand Up @@ -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;

Expand Down

0 comments on commit 4668c5a

Please sign in to comment.