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

Always throw if unable to overwrite #38

Merged
merged 2 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
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
7 changes: 4 additions & 3 deletions test/async.js
Original file line number Diff line number Diff line change
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 cpFile from '..';
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

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
2 changes: 1 addition & 1 deletion test/sync.js
Original file line number Diff line number Diff line change
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 '..';
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

const THREE_HUNDRED_KILO = (100 * 3 * 1024) + 1;

Expand Down