Skip to content

Commit

Permalink
handle more path types
Browse files Browse the repository at this point in the history
* Buffer
* Uint8Array
* URL
  • Loading branch information
shinnn committed Sep 18, 2018
1 parent 3c09d3e commit fef166c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const {promisify} = require('util');
const {types: {isUint8Array}, promisify} = require('util');
const {unlink} = require('fs');

const ARGLEN_ERROR = 'Expected 1 argument (<string>)';
const ERROR = 'Expected a path of a file or symbolic link (<string>)';
const PATH_TYPES = '(<string|Buffer|Uint8Array|URL>)';
const ARGLEN_ERROR = `Expected 1 argument ${PATH_TYPES}`;
const ERROR = `Expected a path of a file or symbolic link ${PATH_TYPES}`;
const promisifiedUnlink = promisify(unlink);

module.exports = async function rmf(...args) {
Expand All @@ -30,6 +31,16 @@ module.exports = async function rmf(...args) {
throw new Error(`${ERROR}, but got '' (empty string).`);
}

if (path.length === 0) {
if (Buffer.isBuffer(path)) {
throw new Error(`${ERROR}, but got an empty Buffer.`);
}

if (isUint8Array(path)) {
throw new Error(`${ERROR}, but got an empty Uint8Array.`);
}
}

try {
await promisifiedUnlink(path);
} catch (err) {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"@shinnn/eslint-config": "^6.7.1",
"eslint": "^5.6.0",
"file-url": "^2.0.2",
"nyc": "^13.0.1",
"nyc-config-common": "^1.0.0",
"tape": "^4.9.1"
Expand Down
33 changes: 26 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {join} = require('path');
const {access, writeFile} = require('fs').promises;

const fileUrl = require('file-url');
const rmf = require('.');
const test = require('tape');

Expand Down Expand Up @@ -37,32 +38,50 @@ test('rmf()', async t => {
}

t.equal(
await rmf(join(__dirname, '__this_file_does_not_exist__')),
await rmf(Buffer.from(join(__dirname, '__this_file_does_not_exist__'))),
false,
'should return false when the target does not exist.'
);

t.equal(
(await getError(join(__filename, 'file'))).code,
(await getError(new URL(fileUrl(join(__filename, 'file'))))).code,
'ENOTDIR',
'should return false when it tries to remove a directory.'
'should throw an error when it tries to remove a directory.'
);

t.equal(
(await getError('')).toString(),
'Error: Expected a path of a file or symbolic link (<string>), but got \'\' (empty string).',
'should return false when it takes an empty path.'
'Error: Expected a path of a file or symbolic link (<string|Buffer|Uint8Array|URL>), but got \'\' (empty string).',
'should return false when it takes an empty string.'
);

t.equal(
(await getError(Buffer.alloc(0))).toString(),
'Error: Expected a path of a file or symbolic link (<string|Buffer|Uint8Array|URL>), but got an empty Buffer.',
'should return false when it takes an empty Buffer.'
);

t.equal(
(await getError(new Uint8Array())).toString(),
'Error: Expected a path of a file or symbolic link (<string|Buffer|Uint8Array|URL>), but got an empty Uint8Array.',
'should return false when it takes an empty Uint8Array.'
);

t.equal(
(await getError(new Int8Array())).code,
'ERR_INVALID_ARG_TYPE',
'should return false when it takes an invalid path-type value.'
);

t.equal(
(await getError()).toString(),
'RangeError: Expected 1 argument (<string>), but got no arguments.',
'RangeError: Expected 1 argument (<string|Buffer|Uint8Array|URL>), but got no arguments.',
'should return false when it takes no arguments.'
);

t.equal(
(await getError('a', 'b')).toString(),
'RangeError: Expected 1 argument (<string>), but got 2 arguments.',
'RangeError: Expected 1 argument (<string|Buffer|Uint8Array|URL>), but got 2 arguments.',
'should return false when it takes too many arguments.'
);

Expand Down

0 comments on commit fef166c

Please sign in to comment.