diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-02-16 13:25:21 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-02-27 20:34:18 +0800 |
commit | 4eb45b884d9bd1f13979047750ad680275f4a348 (patch) | |
tree | 0bffb5e15f59ab58888f28e4e75dc663bf85bce2 /test | |
parent | d2dc2a50113492840e77e4f5d3df6ff75fa986a4 (diff) | |
download | android-node-v8-4eb45b884d9bd1f13979047750ad680275f4a348.tar.gz android-node-v8-4eb45b884d9bd1f13979047750ad680275f4a348.tar.bz2 android-node-v8-4eb45b884d9bd1f13979047750ad680275f4a348.zip |
fs: throw copyFileSync errors in JS
PR-URL: https://github.com/nodejs/node/pull/18871
Refs: https://github.com/nodejs/node/issues/18106
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-fs-copyfile.js | 38 | ||||
-rw-r--r-- | test/parallel/test-fs-error-messages.js | 74 |
2 files changed, 92 insertions, 20 deletions
diff --git a/test/parallel/test-fs-copyfile.js b/test/parallel/test-fs-copyfile.js index 8b910ba046..21e0838148 100644 --- a/test/parallel/test-fs-copyfile.js +++ b/test/parallel/test-fs-copyfile.js @@ -4,6 +4,7 @@ const fixtures = require('../common/fixtures'); const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const fs = require('fs'); +const uv = process.binding('uv'); const path = require('path'); const src = fixtures.path('a.js'); const dest = path.join(tmpdir.path, 'copyfile.out'); @@ -37,15 +38,6 @@ verify(src, dest); fs.copyFileSync(src, dest, 0); verify(src, dest); -// Throws if destination exists and the COPYFILE_EXCL flag is provided. -assert.throws(() => { - fs.copyFileSync(src, dest, COPYFILE_EXCL); -}, /^Error: EEXIST|ENOENT:.+, copyfile/); - -// Throws if the source does not exist. -assert.throws(() => { - fs.copyFileSync(`${src}__does_not_exist`, dest, COPYFILE_EXCL); -}, /^Error: ENOENT: no such file or directory, copyfile/); // Copies asynchronously. fs.unlinkSync(dest); @@ -55,9 +47,21 @@ fs.copyFile(src, dest, common.mustCall((err) => { // Copy asynchronously with flags. fs.copyFile(src, dest, COPYFILE_EXCL, common.mustCall((err) => { - assert( - /^Error: EEXIST: file already exists, copyfile/.test(err.toString()) - ); + if (err.code === 'ENOENT') { // Could be ENOENT or EEXIST + assert.strictEqual(err.message, + 'ENOENT: no such file or directory, copyfile ' + + `'${src}' -> '${dest}'`); + assert.strictEqual(err.errno, uv.UV_ENOENT); + assert.strictEqual(err.code, 'ENOENT'); + assert.strictEqual(err.syscall, 'copyfile'); + } else { + assert.strictEqual(err.message, + 'EEXIST: file already exists, copyfile ' + + `'${src}' -> '${dest}'`); + assert.strictEqual(err.errno, uv.UV_EEXIST); + assert.strictEqual(err.code, 'EEXIST'); + assert.strictEqual(err.syscall, 'copyfile'); + } })); })); @@ -65,9 +69,8 @@ fs.copyFile(src, dest, common.mustCall((err) => { common.expectsError(() => { fs.copyFile(src, dest, 0, 0); }, { - code: 'ERR_INVALID_ARG_TYPE', - type: TypeError, - message: 'The "callback" argument must be of type Function' + code: 'ERR_INVALID_CALLBACK', + type: TypeError }); // Throws if the source path is not a string. @@ -101,8 +104,3 @@ common.expectsError(() => { } ); }); - -// Errors if invalid flags are provided. -assert.throws(() => { - fs.copyFileSync(src, dest, -1); -}, /^Error: EINVAL: invalid argument, copyfile/); diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index 29056e8b66..6718e6fe66 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -30,6 +30,7 @@ const existingFile = fixtures.path('exit.js'); const existingFile2 = fixtures.path('create-file.js'); const existingDir = fixtures.path('empty'); const existingDir2 = fixtures.path('keys'); +const { COPYFILE_EXCL } = fs.constants; const uv = process.binding('uv'); // Template tag function for escaping special characters in strings so that: @@ -634,3 +635,76 @@ if (!common.isAIX) { validateError ); } + +// copyFile with invalid flags +{ + const validateError = (err) => { + assert.strictEqual(err.message, + 'EINVAL: invalid argument, copyfile ' + + `'${existingFile}' -> '${nonexistentFile}'`); + assert.strictEqual(err.errno, uv.UV_EINVAL); + assert.strictEqual(err.code, 'EINVAL'); + assert.strictEqual(err.syscall, 'copyfile'); + return true; + }; + + // TODO(joyeecheung): test fs.copyFile() when uv_fs_copyfile does not + // keep the loop open when the flags are invalid. + // See https://github.com/libuv/libuv/pull/1747 + + assert.throws( + () => fs.copyFileSync(existingFile, nonexistentFile, -1), + validateError + ); +} + +// copyFile: destination exists but the COPYFILE_EXCL flag is provided. +{ + const validateError = (err) => { + if (err.code === 'ENOENT') { // Could be ENOENT or EEXIST + assert.strictEqual(err.message, + 'ENOENT: no such file or directory, copyfile ' + + `'${existingFile}' -> '${existingFile2}'`); + assert.strictEqual(err.errno, uv.UV_ENOENT); + assert.strictEqual(err.code, 'ENOENT'); + assert.strictEqual(err.syscall, 'copyfile'); + } else { + assert.strictEqual(err.message, + 'EEXIST: file already exists, copyfile ' + + `'${existingFile}' -> '${existingFile2}'`); + assert.strictEqual(err.errno, uv.UV_EEXIST); + assert.strictEqual(err.code, 'EEXIST'); + assert.strictEqual(err.syscall, 'copyfile'); + } + return true; + }; + + fs.copyFile(existingFile, existingFile2, COPYFILE_EXCL, + common.mustCall(validateError)); + + assert.throws( + () => fs.copyFileSync(existingFile, existingFile2, COPYFILE_EXCL), + validateError + ); +} + +// copyFile: the source does not exist. +{ + const validateError = (err) => { + assert.strictEqual(err.message, + 'ENOENT: no such file or directory, copyfile ' + + `'${nonexistentFile}' -> '${existingFile2}'`); + assert.strictEqual(err.errno, uv.UV_ENOENT); + assert.strictEqual(err.code, 'ENOENT'); + assert.strictEqual(err.syscall, 'copyfile'); + return true; + }; + + fs.copyFile(nonexistentFile, existingFile2, COPYFILE_EXCL, + common.mustCall(validateError)); + + assert.throws( + () => fs.copyFileSync(nonexistentFile, existingFile2, COPYFILE_EXCL), + validateError + ); +} |