|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const tmpdir = require('../common/tmpdir'); |
| 7 | +const assert = require('assert'); |
| 8 | + |
| 9 | +tmpdir.refresh(); |
| 10 | + |
| 11 | +const dest = path.resolve(tmpdir.path, 'tmp.txt'); |
| 12 | +const buffer = Buffer.from('zyx'); |
| 13 | + |
| 14 | +{ |
| 15 | + let fd = fs.openSync(dest, 'w+'); |
| 16 | + |
| 17 | + assert.throws(() => { |
| 18 | + fs.writeSync(fd, {}); // Missing buffer, {} is second argument |
| 19 | + }, { |
| 20 | + code: 'ERR_INVALID_ARG_TYPE', |
| 21 | + name: 'TypeError', |
| 22 | + message: 'The "buffer" argument must be of type string or an instance ' + |
| 23 | + 'of Buffer, TypedArray, or DataView. Received an instance of Object' |
| 24 | + }); |
| 25 | + assert.throws(() => { |
| 26 | + fs.writeSync(fd, buffer, { length: 5 }); |
| 27 | + }, { |
| 28 | + code: 'ERR_OUT_OF_RANGE', |
| 29 | + name: 'RangeError', |
| 30 | + message: 'The value of "length" is out of range. ' + |
| 31 | + 'It must be <= 3. Received 5' |
| 32 | + }); |
| 33 | + assert.throws(() => { |
| 34 | + fs.writeSync(fd, buffer, { offset: 5 }); |
| 35 | + }, { |
| 36 | + code: 'ERR_OUT_OF_RANGE', |
| 37 | + name: 'RangeError', |
| 38 | + message: 'The value of "offset" is out of range. ' + |
| 39 | + 'It must be <= 3. Received 5' |
| 40 | + }); |
| 41 | + assert.throws(() => { |
| 42 | + fs.writeSync(fd, buffer, { offset: 1 }); |
| 43 | + }, { |
| 44 | + code: 'ERR_OUT_OF_RANGE', |
| 45 | + name: 'RangeError', |
| 46 | + message: 'The value of "length" is out of range. ' + |
| 47 | + 'It must be <= 2. Received 3' |
| 48 | + }); |
| 49 | + assert.throws(() => { |
| 50 | + fs.writeSync(fd, buffer, { length: 1, offset: 3 }); |
| 51 | + }, { |
| 52 | + code: 'ERR_OUT_OF_RANGE', |
| 53 | + name: 'RangeError', |
| 54 | + message: 'The value of "length" is out of range. ' + |
| 55 | + 'It must be <= 0. Received 1' |
| 56 | + }); |
| 57 | + assert.throws(() => { |
| 58 | + fs.writeSync(fd, buffer, { length: -1 }); |
| 59 | + }, { |
| 60 | + code: 'ERR_OUT_OF_RANGE', |
| 61 | + name: 'RangeError', |
| 62 | + message: 'The value of "length" is out of range. ' + |
| 63 | + 'It must be >= 0. Received -1' |
| 64 | + }); |
| 65 | + assert.throws(() => { |
| 66 | + fs.writeSync(fd, buffer, { offset: -1 }); |
| 67 | + }, { |
| 68 | + code: 'ERR_OUT_OF_RANGE', |
| 69 | + name: 'RangeError', |
| 70 | + message: 'The value of "offset" is out of range. ' + |
| 71 | + 'It must be >= 0 && <= 9007199254740991. Received -1' |
| 72 | + }); |
| 73 | + |
| 74 | + fs.closeSync(fd); |
| 75 | + |
| 76 | + for (const params of [ |
| 77 | + {}, |
| 78 | + { length: 1 }, |
| 79 | + { position: 5 }, |
| 80 | + { length: 1, position: 5 }, |
| 81 | + { length: 1, position: -1, offset: 2 }, |
| 82 | + { length: null }, |
| 83 | + ]) { |
| 84 | + fd = fs.openSync(dest, 'w+'); |
| 85 | + const bytesWritten = fs.writeSync(fd, buffer, params); |
| 86 | + const bytesRead = fs.writeSync(fd, buffer, params); |
| 87 | + |
| 88 | + // Test compatibility with fs.readSync counterpart with reused params |
| 89 | + assert.strictEqual(bytesWritten, bytesRead); |
| 90 | + if (params.length !== undefined && params.length !== null) { |
| 91 | + assert.strictEqual(bytesWritten, params.length); |
| 92 | + } |
| 93 | + fs.closeSync(fd); |
| 94 | + } |
| 95 | +} |
0 commit comments