• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const path = require('path');
6const fs = require('fs');
7const tmpdir = require('../common/tmpdir');
8
9tmpdir.refresh();
10
11const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف';
12
13const getFileName = (i) => path.join(tmpdir.path, `writev_${i}.txt`);
14
15/**
16 * Testing with a array of buffers input
17 */
18
19// fs.writev with array of buffers with all parameters
20{
21  const filename = getFileName(1);
22  const fd = fs.openSync(filename, 'w');
23
24  const buffer = Buffer.from(expected);
25  const bufferArr = [buffer, buffer];
26
27  const done = common.mustSucceed((written, buffers) => {
28    assert.deepStrictEqual(bufferArr, buffers);
29    const expectedLength = bufferArr.length * buffer.byteLength;
30    assert.deepStrictEqual(written, expectedLength);
31    fs.closeSync(fd);
32
33    assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
34  });
35
36  fs.writev(fd, bufferArr, null, done);
37}
38
39// fs.writev with array of buffers without position
40{
41  const filename = getFileName(2);
42  const fd = fs.openSync(filename, 'w');
43
44  const buffer = Buffer.from(expected);
45  const bufferArr = [buffer, buffer];
46
47  const done = common.mustSucceed((written, buffers) => {
48    assert.deepStrictEqual(bufferArr, buffers);
49
50    const expectedLength = bufferArr.length * buffer.byteLength;
51    assert.deepStrictEqual(written, expectedLength);
52    fs.closeSync(fd);
53
54    assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
55  });
56
57  fs.writev(fd, bufferArr, done);
58}
59
60
61// fs.writev with empty array of buffers
62{
63  const filename = getFileName(3);
64  const fd = fs.openSync(filename, 'w');
65  const bufferArr = [];
66  let afterSyncCall = false;
67
68  const done = common.mustSucceed((written, buffers) => {
69    assert.strictEqual(buffers.length, 0);
70    assert.strictEqual(written, 0);
71    assert(afterSyncCall);
72    fs.closeSync(fd);
73  });
74
75  fs.writev(fd, bufferArr, done);
76  afterSyncCall = true;
77}
78
79/**
80 * Testing with wrong input types
81 */
82{
83  const filename = getFileName(4);
84  const fd = fs.openSync(filename, 'w');
85
86  [false, 'test', {}, [{}], ['sdf'], null, undefined].forEach((i) => {
87    assert.throws(
88      () => fs.writev(fd, i, null, common.mustNotCall()), {
89        code: 'ERR_INVALID_ARG_TYPE',
90        name: 'TypeError'
91      }
92    );
93  });
94
95  fs.closeSync(fd);
96}
97
98// fs.writev with wrong fd types
99[false, 'test', {}, [{}], null, undefined].forEach((i) => {
100  assert.throws(
101    () => fs.writev(i, common.mustNotCall()),
102    {
103      code: 'ERR_INVALID_ARG_TYPE',
104      name: 'TypeError'
105    }
106  );
107});
108