|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +// The following tests validate base functionality for the fs/promises |
| 6 | +// FileHandle.read method. |
| 7 | + |
| 8 | +const fs = require('fs'); |
| 9 | +const { open } = require('fs/promises'); |
| 10 | +const path = require('path'); |
| 11 | +const tmpdir = require('../common/tmpdir'); |
| 12 | +const assert = require('assert'); |
| 13 | +const tmpDir = tmpdir.path; |
| 14 | + |
| 15 | +tmpdir.refresh(); |
| 16 | +common.crashOnUnhandledRejection(); |
| 17 | + |
| 18 | +async function validateRead() { |
| 19 | + const filePath = path.resolve(tmpDir, 'tmp-read-file.txt'); |
| 20 | + const fileHandle = await open(filePath, 'w+'); |
| 21 | + const buffer = Buffer.from('Hello world', 'utf8'); |
| 22 | + |
| 23 | + const fd = fs.openSync(filePath, 'w+'); |
| 24 | + fs.writeSync(fd, buffer, 0, buffer.length); |
| 25 | + fs.closeSync(fd); |
| 26 | + const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0); |
| 27 | + assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead); |
| 28 | + assert.deepStrictEqual(buffer, readAsyncHandle.buffer); |
| 29 | +} |
| 30 | + |
| 31 | +async function validateEmptyRead() { |
| 32 | + const filePath = path.resolve(tmpDir, 'tmp-read-empty-file.txt'); |
| 33 | + const fileHandle = await open(filePath, 'w+'); |
| 34 | + const buffer = Buffer.from('', 'utf8'); |
| 35 | + |
| 36 | + const fd = fs.openSync(filePath, 'w+'); |
| 37 | + fs.writeSync(fd, buffer, 0, buffer.length); |
| 38 | + fs.closeSync(fd); |
| 39 | + const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0); |
| 40 | + assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead); |
| 41 | +} |
| 42 | + |
| 43 | +validateRead() |
| 44 | + .then(validateEmptyRead) |
| 45 | + .then(common.mustCall()); |
0 commit comments