Skip to content

Commit

Permalink
test(fs-copy-file) node v10 support
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 2, 2018
1 parent 8424800 commit 5283397
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 80 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
"rules": {
"indent": ["error", 4]
},
"extends": ["eslint:recommended"]
"extends": [
"eslint:recommended",
"plugin:node/recommended"
],
"plugins": [
"node"
]
}
8 changes: 8 additions & 0 deletions .eslintrc.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"node/no-unsupported-features": 0
},
"extends": [
".eslintrc",
],
}
27 changes: 6 additions & 21 deletions lib/fs-copy-file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const fs = require('fs');
const pipe = require('pipe-io');
const _copyFile = require('@cloudcmd/copy-file');

const COPYFILE_EXCL = 1;

Expand All @@ -20,7 +20,7 @@ function copyFile(src, dest, flags, callback) {
check(src, dest, flags, callback);

if (flags !== COPYFILE_EXCL)
return copyFiles(src, dest, callback);
return _copyFile(src, dest, callback);

fs.stat(dest, (error) => {
if (!error)
Expand All @@ -29,37 +29,22 @@ function copyFile(src, dest, flags, callback) {
if (error.code !== 'ENOENT')
return callback(error);

copyFiles(src, dest, callback);
});
}

function copyFiles(src, dest, callback) {
fs.stat(src, (error, stat) => {
if (error)
return callback(error);

const mode = stat.mode;
const read = fs.createReadStream(src);
const write = fs.createWriteStream(dest, {
mode
});

pipe([read, write], callback);
_copyFile(src, dest, callback);
});
}

function check(src, dest, flags, callback) {
if (typeof callback !== 'function') {
const error = TypeError('The "callback" argument must be of type function');
error.code = 'ERR_INVALID_ARG_TYPE';
error.code = 'ERR_INVALID_CALLBACK';
throw error;
}

if (typeof dest !== 'string')
throw TypeError('dest must be a string');
throw TypeError('The "dest" argument must be one of type string, Buffer, or URL. Received type number');

if (typeof src !== 'string')
throw TypeError('src must be a string');
throw TypeError('The "src" argument must be one of type string, Buffer, or URL. Received type number');

if (typeof flags === 'number' && flags && flags !== COPYFILE_EXCL)
throw Error(`EINVAL: invalid argument, copyfile -> '${dest}'`);
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
"scripts": {
"test": "tape test/*.js",
"lint": "redrun lint:*",
"lint:lib": "eslint lib test",
"lint:lib": "eslint lib",
"lint:test": "eslint test -c .eslintrc.test",
"watch:test": "npm run watcher -- \"npm test\"",
"watcher": "nodemon -w lib -w test -x",
"coverage": "nyc npm test",
"report": "nyc report --reporter=text-lcov | coveralls"
},
"dependencies": {
"pipe-io": "^3.0.0"
"@cloudcmd/copy-file": "^1.0.0"
},
"license": "MIT",
"engines": {
Expand All @@ -35,12 +36,13 @@
"devDependencies": {
"coveralls": "^3.0.0",
"eslint": "^4.0.0",
"eslint-plugin-node": "^6.0.1",
"nodemon": "^1.11.0",
"nyc": "^11.0.2",
"pullout": "^1.0.1",
"redrun": "^6.0.0",
"rimraf": "^2.6.2",
"tape": "^4.2.0",
"tar-fs": "^1.14.0"
"try-to-catch": "^1.0.2"
}
}
102 changes: 47 additions & 55 deletions test/fs-copy-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,80 @@
const path = require('path');
const fs = require('fs');
const test = require('tape');
const tryToCatch = require('try-to-catch');
const {promisify} = require('util');
const copyFile = require('../lib/fs-copy-file');

const copyFile = require('..');

const {COPYFILE_EXCL} = fs.constants;

const fixture = path.join(__dirname, 'fixture');
const noop = () => {};

test('fs.copyFile: no args: message', (t) => {
promisify(fs.copyFile)().catch((e) => {
promisify(copyFile)().catch((error) => {
t.equal(e.message, error.message, 'should throw when no callback');
t.end();
});
});
});

test('copyFile: no args: message', (t) => {
const original = fs.copyFile;
test('fs.copyFile: no args: message', async (t) => {
const copyFileOriginal = fs.copyFile;
fs.copyFile = null;
const copyFile = rerequire('../lib/fs-copy-file');
const fn = promisify(copyFile);

promisify(original)().catch((e) => {
fn().catch((error) => {
fs.copyFile = original;
t.equal(e.message, error.message, 'should throw when no callback');
t.end();
});
});

const copyFile = promisify(rerequire('..'));
const _copyFile = promisify(copyFile);

const msg = 'The "callback" argument must be of type function';
const [e] = await tryToCatch(_copyFile);

fs.copyFile = copyFileOriginal;

t.equal(e.message, msg, 'should throw when no callback');
t.end();
});

test('copyFile: no args: code', (t) => {
const original = fs.copyFile;
test('copyFile: no args: code', async (t) => {
const copyFileOriginal = fs.copyFile;
fs.copyFile = null;
const copyFile = rerequire('../lib/fs-copy-file');
const fn = promisify(copyFile);

promisify(original)().catch((e) => {
fn().catch((error) => {
fs.copyFile = original
t.equal(e.code, error.code, 'should code be equal');
t.end();
});
});

const copyFile = promisify(rerequire('..'));
const _copyFile = promisify(copyFile);

const code = 'ERR_INVALID_CALLBACK';
const [e] = await tryToCatch(_copyFile);

fs.copyFile = copyFileOriginal;

t.equal(e.code, code, 'should throw when no callback');
t.end();
});

test('copyFile: no dest', (t) => {
test('copyFile: no dest', async (t) => {
const src = '1';
const dest = 0;

const original = fs.copyFile;
fs.copyFile = null;
const copyFile = rerequire('../lib/fs-copy-file');
const fn = promisify(copyFile);

promisify(original)(src, dest, noop).catch((e) => {
fn(src, dest, noop).catch((error) => {
fs.copyFile = original;
t.equal(e.message, error.message, 'should throw when no dest');
t.end();
});
});
const _copyFile = promisify(copyFile);

const [e] = await tryToCatch(_copyFile, src, dest);
const msg = 'The "dest" argument must be one of type string, Buffer, or URL. Received type number';
fs.copyFile = original;

t.equal(e.message, msg, 'should throw when dest not string');
t.end();
});

test('copyFile: no src', (t) => {
test('copyFile: no src', async (t) => {
const src = 1;
const dest = '0';

const original = fs.copyFile;
fs.copyFile = null;
const copyFile = rerequire('../lib/fs-copy-file');
const fn = promisify(copyFile);

promisify(original)(src, dest, noop).catch((e) => {
fn(src, dest, noop).catch((error) => {
fs.copyFile = original;
t.equal(e.message, error.message, 'should throw when no dest');
t.end();
});
});
const _copyFile = promisify(copyFile);

const [e] = await tryToCatch(_copyFile, src, dest);
const msg = 'The "src" argument must be one of type string, Buffer, or URL. Received type number';
fs.copyFile = original;

t.equal(e.message, msg, 'should throw when src not string');
t.end();
});

test('copyFile: no src', (t) => {
Expand Down

0 comments on commit 5283397

Please sign in to comment.