summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-copyfile.js
diff options
context:
space:
mode:
authorcjihrig <cjihrig@gmail.com>2017-09-06 12:54:29 -0400
committercjihrig <cjihrig@gmail.com>2017-09-08 00:08:04 -0400
commit11b7428832466dd6933e9c26deaf3a4ce1d33cef (patch)
tree2c6f16d6e32170bd6149b34ff21a939c5d236a1d /test/parallel/test-fs-copyfile.js
parent62813615049aba50ef99af052b23532f2fb35eef (diff)
downloadandroid-node-v8-11b7428832466dd6933e9c26deaf3a4ce1d33cef.tar.gz
android-node-v8-11b7428832466dd6933e9c26deaf3a4ce1d33cef.tar.bz2
android-node-v8-11b7428832466dd6933e9c26deaf3a4ce1d33cef.zip
fs: add fs.copyFile{Sync}
Fixes: https://github.com/nodejs/node/issues/14906 PR-URL: https://github.com/nodejs/node/pull/15034 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'test/parallel/test-fs-copyfile.js')
-rw-r--r--test/parallel/test-fs-copyfile.js104
1 files changed, 104 insertions, 0 deletions
diff --git a/test/parallel/test-fs-copyfile.js b/test/parallel/test-fs-copyfile.js
new file mode 100644
index 0000000000..28dbc605fc
--- /dev/null
+++ b/test/parallel/test-fs-copyfile.js
@@ -0,0 +1,104 @@
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const fs = require('fs');
+const path = require('path');
+const src = path.join(common.fixturesDir, 'a.js');
+const dest = path.join(common.tmpDir, 'copyfile.out');
+const { COPYFILE_EXCL, UV_FS_COPYFILE_EXCL } = fs.constants;
+
+function verify(src, dest) {
+ const srcData = fs.readFileSync(src, 'utf8');
+ const srcStat = fs.statSync(src);
+ const destData = fs.readFileSync(dest, 'utf8');
+ const destStat = fs.statSync(dest);
+
+ assert.strictEqual(srcData, destData);
+ assert.strictEqual(srcStat.mode, destStat.mode);
+ assert.strictEqual(srcStat.size, destStat.size);
+}
+
+common.refreshTmpDir();
+
+// Verify that flags are defined.
+assert.strictEqual(typeof COPYFILE_EXCL, 'number');
+assert.strictEqual(typeof UV_FS_COPYFILE_EXCL, 'number');
+assert.strictEqual(COPYFILE_EXCL, UV_FS_COPYFILE_EXCL);
+
+// Verify that files are overwritten when no flags are provided.
+fs.writeFileSync(dest, '', 'utf8');
+const result = fs.copyFileSync(src, dest);
+assert.strictEqual(result, undefined);
+verify(src, dest);
+
+// Verify that files are overwritten with default flags.
+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);
+fs.copyFile(src, dest, common.mustCall((err) => {
+ assert.ifError(err);
+ verify(src, dest);
+
+ // Copy asynchronously with flags.
+ fs.copyFile(src, dest, COPYFILE_EXCL, common.mustCall((err) => {
+ assert(
+ /^Error: EEXIST: file already exists, copyfile/.test(err.toString())
+ );
+ }));
+}));
+
+// Throws if callback is not a function.
+common.expectsError(() => {
+ fs.copyFile(src, dest, 0, 0);
+}, {
+ code: 'ERR_INVALID_ARG_TYPE',
+ type: TypeError,
+ message: 'The "callback" argument must be of type function'
+});
+
+// Throws if the source path is not a string.
+assert.throws(() => {
+ fs.copyFileSync(null, dest);
+}, /^TypeError: src must be a string$/);
+
+// Throws if the source path is an invalid path.
+common.expectsError(() => {
+ fs.copyFileSync('\u0000', dest);
+}, {
+ code: 'ERR_INVALID_ARG_TYPE',
+ type: Error,
+ message: 'The "path" argument must be of type string without null bytes.' +
+ ' Received type string'
+});
+
+// Throws if the destination path is not a string.
+assert.throws(() => {
+ fs.copyFileSync(src, null);
+}, /^TypeError: dest must be a string$/);
+
+// Throws if the destination path is an invalid path.
+common.expectsError(() => {
+ fs.copyFileSync(src, '\u0000');
+}, {
+ code: 'ERR_INVALID_ARG_TYPE',
+ type: Error,
+ message: 'The "path" argument must be of type string without null bytes.' +
+ ' Received type string'
+});
+
+// Errors if invalid flags are provided.
+assert.throws(() => {
+ fs.copyFileSync(src, dest, -1);
+}, /^Error: EINVAL: invalid argument, copyfile/);