GeezerVison is showing github.com — test: increase fs promise coverage · nodejs/node@4086b23
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises-file-handle-readFile.js +72-2Lines changed: 72 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,9 +6,15 @@ const common = require('../common'); | 6 | 6 | // FileHandle.readFile method. | 7 | 7 |
| 8 | 8 | const fs = require('fs'); | 9 | | -const { open } = fs.promises; | | 9 | +const { | | 10 | + open, | | 11 | + readFile, | | 12 | + writeFile, | | 13 | + truncate | | 14 | +} = fs.promises; | 10 | 15 | const path = require('path'); | 11 | 16 | const tmpdir = require('../common/tmpdir'); | | 17 | +const tick = require('../common/tick'); | 12 | 18 | const assert = require('assert'); | 13 | 19 | const tmpDir = tmpdir.path; | 14 | 20 |
| @@ -45,6 +51,70 @@ async function validateReadFileProc() { | 45 | 51 | assert.ok(hostname.length > 0); | 46 | 52 | } | 47 | 53 |
| | 54 | +async function doReadAndCancel() { | | 55 | + // Signal aborted from the start | | 56 | + { | | 57 | + const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt'); | | 58 | + const fileHandle = await open(filePathForHandle, 'w+'); | | 59 | + const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8'); | | 60 | + fs.writeFileSync(filePathForHandle, buffer); | | 61 | + const controller = new AbortController(); | | 62 | + const { signal } = controller; | | 63 | + controller.abort(); | | 64 | + await assert.rejects(readFile(fileHandle, { signal }), { | | 65 | + name: 'AbortError' | | 66 | + }); | | 67 | + } | | 68 | + | | 69 | + // Signal aborted on first tick | | 70 | + { | | 71 | + const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt'); | | 72 | + const fileHandle = await open(filePathForHandle, 'w+'); | | 73 | + const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8'); | | 74 | + fs.writeFileSync(filePathForHandle, buffer); | | 75 | + const controller = new AbortController(); | | 76 | + const { signal } = controller; | | 77 | + tick(1, () => controller.abort()); | | 78 | + await assert.rejects(readFile(fileHandle, { signal }), { | | 79 | + name: 'AbortError' | | 80 | + }); | | 81 | + } | | 82 | + | | 83 | + // Signal aborted right before buffer read | | 84 | + { | | 85 | + const newFile = path.resolve(tmpDir, 'dogs-running2.txt'); | | 86 | + const buffer = Buffer.from('Dogs running'.repeat(1000), 'utf8'); | | 87 | + fs.writeFileSync(newFile, buffer); | | 88 | + | | 89 | + const fileHandle = await open(newFile, 'r'); | | 90 | + | | 91 | + const controller = new AbortController(); | | 92 | + const { signal } = controller; | | 93 | + tick(2, () => controller.abort()); | | 94 | + await assert.rejects(fileHandle.readFile({ signal, encoding: 'utf8' }), { | | 95 | + name: 'AbortError' | | 96 | + }); | | 97 | + } | | 98 | + | | 99 | + // Validate file size is within range for reading | | 100 | + { | | 101 | + // Variable taken from https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L5 | | 102 | + const kIoMaxLength = 2 ** 31 - 1; | | 103 | + | | 104 | + const newFile = path.resolve(tmpDir, 'dogs-running3.txt'); | | 105 | + await writeFile(newFile, Buffer.from('0')); | | 106 | + await truncate(newFile, kIoMaxLength + 1); | | 107 | + | | 108 | + const fileHandle = await open(newFile, 'r'); | | 109 | + | | 110 | + await assert.rejects(fileHandle.readFile(), { | | 111 | + name: 'RangeError', | | 112 | + code: 'ERR_FS_FILE_TOO_LARGE' | | 113 | + }); | | 114 | + } | | 115 | +} | | 116 | + | 48 | 117 | validateReadFile() | 49 | | - .then(() => validateReadFileProc()) | | 118 | + .then(validateReadFileProc) | | 119 | + .then(doReadAndCancel) | 50 | 120 | .then(common.mustCall()); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises-file-handle-writeFile.js +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const common = require('../common'); | 4 | 4 |
| 5 | 5 | // The following tests validate base functionality for the fs.promises | 6 | | -// FileHandle.readFile method. | | 6 | +// FileHandle.writeFile method. | 7 | 7 |
| 8 | 8 | const fs = require('fs'); | 9 | | -const { open } = fs.promises; | | 9 | +const { open, writeFile } = fs.promises; | 10 | 10 | const path = require('path'); | 11 | 11 | const tmpdir = require('../common/tmpdir'); | 12 | 12 | const assert = require('assert'); | @@ -26,5 +26,19 @@ async function validateWriteFile() { | 26 | 26 | await fileHandle.close(); | 27 | 27 | } | 28 | 28 |
| | 29 | +// Signal aborted while writing file | | 30 | +async function doWriteAndCancel() { | | 31 | + const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt'); | | 32 | + const fileHandle = await open(filePathForHandle, 'w+'); | | 33 | + const buffer = Buffer.from('dogs running'.repeat(10000), 'utf8'); | | 34 | + const controller = new AbortController(); | | 35 | + const { signal } = controller; | | 36 | + process.nextTick(() => controller.abort()); | | 37 | + await assert.rejects(writeFile(fileHandle, buffer, { signal }), { | | 38 | + name: 'AbortError' | | 39 | + }); | | 40 | +} | | 41 | + | 29 | 42 | validateWriteFile() | | 43 | + .then(doWriteAndCancel) | 30 | 44 | .then(common.mustCall()); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises-writefile.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,7 +31,7 @@ async function doWriteWithCancel() { | 31 | 31 | } | 32 | 32 |
| 33 | 33 | async function doAppend() { | 34 | | - await fsPromises.appendFile(dest, buffer2); | | 34 | + await fsPromises.appendFile(dest, buffer2, { flag: null }); | 35 | 35 | const data = fs.readFileSync(dest); | 36 | 36 | const buf = Buffer.concat([buffer, buffer2]); | 37 | 37 | assert.deepStrictEqual(buf, data); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises.js +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ const { | 16 | 16 | link, | 17 | 17 | lchmod, | 18 | 18 | lstat, | | 19 | + lutimes, | 19 | 20 | mkdir, | 20 | 21 | mkdtemp, | 21 | 22 | open, | @@ -140,6 +141,13 @@ async function getHandle(dest) { | 140 | 141 | await handle.close(); | 141 | 142 | } | 142 | 143 |
| | 144 | + // Use fallback buffer allocation when input not buffer | | 145 | + { | | 146 | + const handle = await getHandle(dest); | | 147 | + const ret = await handle.read(0, 0, 0, 0); | | 148 | + assert.strictEqual(ret.buffer.length, 16384); | | 149 | + } | | 150 | + | 143 | 151 | // Bytes written to file match buffer | 144 | 152 | { | 145 | 153 | const handle = await getHandle(dest); | @@ -226,6 +234,19 @@ async function getHandle(dest) { | 226 | 234 | await handle.close(); | 227 | 235 | } | 228 | 236 |
| | 237 | + // Set modification times with lutimes | | 238 | + { | | 239 | + const a_time = new Date(); | | 240 | + a_time.setMinutes(a_time.getMinutes() - 1); | | 241 | + const m_time = new Date(); | | 242 | + m_time.setHours(m_time.getHours() - 1); | | 243 | + await lutimes(dest, a_time, m_time); | | 244 | + const stats = await stat(dest); | | 245 | + | | 246 | + assert.strictEqual(a_time.toString(), stats.atime.toString()); | | 247 | + assert.strictEqual(m_time.toString(), stats.mtime.toString()); | | 248 | + } | | 249 | + | 229 | 250 | // create symlink | 230 | 251 | { | 231 | 252 | const newPath = path.resolve(tmpDir, 'baz2.js'); | @@ -270,6 +291,15 @@ async function getHandle(dest) { | 270 | 291 | } | 271 | 292 | } | 272 | 293 |
| | 294 | + // specify symlink type | | 295 | + { | | 296 | + const dir = path.join(tmpDir, nextdir()); | | 297 | + await symlink(tmpDir, dir, 'dir'); | | 298 | + const stats = await lstat(dir); | | 299 | + assert.strictEqual(stats.isSymbolicLink(), true); | | 300 | + await unlink(dir); | | 301 | + } | | 302 | + | 273 | 303 | // create hard link | 274 | 304 | { | 275 | 305 | const newPath = path.resolve(tmpDir, 'baz2.js'); | @@ -296,6 +326,14 @@ async function getHandle(dest) { | 296 | 326 | await unlink(newFile); | 297 | 327 | } | 298 | 328 |
| | 329 | + // Use fallback encoding when input is null | | 330 | + { | | 331 | + const newFile = path.resolve(tmpDir, 'dogs_running.js'); | | 332 | + await writeFile(newFile, 'dogs running', { encoding: null }); | | 333 | + const fileExists = fs.existsSync(newFile); | | 334 | + assert.strictEqual(fileExists, true); | | 335 | + } | | 336 | + | 299 | 337 | // `mkdir` when options is number. | 300 | 338 | { | 301 | 339 | const dir = path.join(tmpDir, nextdir()); | |
|