|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const src = path.join(common.fixturesDir, 'a.js'); |
| 7 | +const dest = path.join(common.tmpDir, 'copyfile.out'); |
| 8 | +const { COPYFILE_EXCL, UV_FS_COPYFILE_EXCL } = fs.constants; |
| 9 | + |
| 10 | +function verify(src, dest) { |
| 11 | + const srcData = fs.readFileSync(src, 'utf8'); |
| 12 | + const srcStat = fs.statSync(src); |
| 13 | + const destData = fs.readFileSync(dest, 'utf8'); |
| 14 | + const destStat = fs.statSync(dest); |
| 15 | + |
| 16 | + assert.strictEqual(srcData, destData); |
| 17 | + assert.strictEqual(srcStat.mode, destStat.mode); |
| 18 | + assert.strictEqual(srcStat.size, destStat.size); |
| 19 | +} |
| 20 | + |
| 21 | +common.refreshTmpDir(); |
| 22 | + |
| 23 | +// Verify that flags are defined. |
| 24 | +assert.strictEqual(typeof COPYFILE_EXCL, 'number'); |
| 25 | +assert.strictEqual(typeof UV_FS_COPYFILE_EXCL, 'number'); |
| 26 | +assert.strictEqual(COPYFILE_EXCL, UV_FS_COPYFILE_EXCL); |
| 27 | + |
| 28 | +// Verify that files are overwritten when no flags are provided. |
| 29 | +fs.writeFileSync(dest, '', 'utf8'); |
| 30 | +const result = fs.copyFileSync(src, dest); |
| 31 | +assert.strictEqual(result, undefined); |
| 32 | +verify(src, dest); |
| 33 | + |
| 34 | +// Verify that files are overwritten with default flags. |
| 35 | +fs.copyFileSync(src, dest, 0); |
| 36 | +verify(src, dest); |
| 37 | + |
| 38 | +// Throws if destination exists and the COPYFILE_EXCL flag is provided. |
| 39 | +assert.throws(() => { |
| 40 | + fs.copyFileSync(src, dest, COPYFILE_EXCL); |
| 41 | +}, /^Error: EEXIST|ENOENT:.+, copyfile/); |
| 42 | + |
| 43 | +// Throws if the source does not exist. |
| 44 | +assert.throws(() => { |
| 45 | + fs.copyFileSync(src + '__does_not_exist', dest, COPYFILE_EXCL); |
| 46 | +}, /^Error: ENOENT: no such file or directory, copyfile/); |
| 47 | + |
| 48 | +// Copies asynchronously. |
| 49 | +fs.unlinkSync(dest); |
| 50 | +fs.copyFile(src, dest, common.mustCall((err) => { |
| 51 | + assert.ifError(err); |
| 52 | + verify(src, dest); |
| 53 | + |
| 54 | + // Copy asynchronously with flags. |
| 55 | + fs.copyFile(src, dest, COPYFILE_EXCL, common.mustCall((err) => { |
| 56 | + assert( |
| 57 | + /^Error: EEXIST: file already exists, copyfile/.test(err.toString()) |
| 58 | + ); |
| 59 | + })); |
| 60 | +})); |
| 61 | + |
| 62 | +// Throws if callback is not a function. |
| 63 | +common.expectsError(() => { |
| 64 | + fs.copyFile(src, dest, 0, 0); |
| 65 | +}, { |
| 66 | + code: 'ERR_INVALID_ARG_TYPE', |
| 67 | + type: TypeError, |
| 68 | + message: 'The "callback" argument must be of type function' |
| 69 | +}); |
| 70 | + |
| 71 | +// Throws if the source path is not a string. |
| 72 | +assert.throws(() => { |
| 73 | + fs.copyFileSync(null, dest); |
| 74 | +}, /^TypeError: src must be a string$/); |
| 75 | + |
| 76 | +// Throws if the source path is an invalid path. |
| 77 | +assert.throws(() => { |
| 78 | + fs.copyFileSync('\u0000', dest); |
| 79 | +}, /^Error: Path must be a string without null bytes$/); |
| 80 | + |
| 81 | +// Throws if the destination path is not a string. |
| 82 | +assert.throws(() => { |
| 83 | + fs.copyFileSync(src, null); |
| 84 | +}, /^TypeError: dest must be a string$/); |
| 85 | + |
| 86 | +// Throws if the destination path is an invalid path. |
| 87 | +assert.throws(() => { |
| 88 | + fs.copyFileSync(src, '\u0000'); |
| 89 | +}, /^Error: Path must be a string without null bytes$/); |
| 90 | + |
| 91 | +// Errors if invalid flags are provided. |
| 92 | +assert.throws(() => { |
| 93 | + fs.copyFileSync(src, dest, -1); |
| 94 | +}, /^Error: EINVAL: invalid argument, copyfile/); |
0 commit comments