Skip to content

Commit 4668c5a

Browse files
authoredFeb 24, 2020
Always throw if unable to overwrite (#38)
1 parent 4e1367f commit 4668c5a

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed
 

‎index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
3030
progressEmitter.written = progressEmitter.size;
3131
updateStats = true;
3232
} catch (error) {
33-
if (options.overwrite || error.code !== 'EEXIST') {
34-
throw new CpFileError(`Cannot write to \`${destination}\`: ${error.message}`, error);
35-
}
33+
throw new CpFileError(`Cannot write to \`${destination}\`: ${error.message}`, error);
3634
}
3735

3836
if (readError) {

‎test/async.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import del from 'del';
77
import test from 'ava';
88
import uuid from 'uuid';
99
import sinon from 'sinon';
10-
import cpFile from '..';
1110
import assertDateEqual from './helpers/_assert';
12-
import {buildEACCES, buildEIO, buildENOSPC, buildENOENT, buildEPERM} from './helpers/_fs-errors';
11+
import {buildEACCES, buildEIO, buildENOSPC, buildENOENT, buildEPERM, buildERRSTREAMWRITEAFTEREND} from './helpers/_fs-errors';
12+
import cpFile from '..';
1313

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

@@ -75,8 +75,9 @@ test('overwrite when options are undefined', async t => {
7575

7676
test('do not overwrite when disabled', async t => {
7777
fs.writeFileSync(t.context.destination, '');
78-
await cpFile('license', t.context.destination, {overwrite: false});
79-
t.is(fs.readFileSync(t.context.destination, 'utf8'), '');
78+
const error = await t.throwsAsync(cpFile('license', t.context.destination, {overwrite: false}));
79+
t.is(error.name, 'CpFileError', error.message);
80+
t.is(error.code, 'EEXIST', error.message);
8081
});
8182

8283
test('do not create `destination` on unreadable `source`', async t => {
@@ -245,7 +246,7 @@ test.serial('rethrow read after open errors', async t => {
245246
const {createWriteStream, createReadStream} = fs;
246247
let calledWriteEnd = 0;
247248
let readStream;
248-
const readError = buildEIO();
249+
const readError = process.release.lts === 'Erbium' || parseInt(process.versions.node.slice(0, 2), 10) > 12 ? buildERRSTREAMWRITEAFTEREND() : buildEIO();
249250

250251
fs.createWriteStream = (...args) => {
251252
const stream = createWriteStream(...args);
@@ -275,8 +276,8 @@ test.serial('rethrow read after open errors', async t => {
275276
const uncached = importFresh('..');
276277
const error = await t.throwsAsync(uncached('license', t.context.destination));
277278
t.is(error.name, 'CpFileError', error.message);
278-
t.is(error.errno, readError.errno, error.message);
279279
t.is(error.code, readError.code, error.message);
280+
t.is(error.errno, readError.errno, error.message);
280281
t.is(calledWriteEnd, 1);
281282

282283
Object.assign(fs, {createWriteStream, createReadStream});

‎test/helpers/_fs-errors.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ exports.buildEIO = () => Object.assign(new Error('EIO: i/o error, read errno: -5
2222
code: 'EIO'
2323
});
2424

25+
exports.buildERRSTREAMWRITEAFTEREND = () => Object.assign(new Error('ERR_STREAM_WRITE_AFTER_END'), {
26+
code: 'ERR_STREAM_WRITE_AFTER_END'
27+
});
28+
2529
exports.buildEBADF = () => Object.assign(new Error('EBADF: bad file descriptor'), {
2630
errno: -9,
2731
code: 'EBADF'

‎test/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import del from 'del';
55
import test from 'ava';
66
import uuid from 'uuid';
77
import sinon from 'sinon';
8-
import cpFile from '..';
98
import assertDateEqual from './helpers/_assert';
109
import {buildEACCES, buildENOSPC, buildEBADF, buildEPERM} from './helpers/_fs-errors';
10+
import cpFile from '..';
1111

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

0 commit comments

Comments
 (0)
Please sign in to comment.