• 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 * Testing with wrong input types
62 */
63{
64  const filename = getFileName(3);
65  const fd = fs.openSync(filename, 'w');
66
67  [false, 'test', {}, [{}], ['sdf'], null, undefined].forEach((i) => {
68    assert.throws(
69      () => fs.writev(fd, i, null, common.mustNotCall()), {
70        code: 'ERR_INVALID_ARG_TYPE',
71        name: 'TypeError'
72      }
73    );
74  });
75
76  fs.closeSync(fd);
77}
78
79// fs.writev with wrong fd types
80[false, 'test', {}, [{}], null, undefined].forEach((i) => {
81  assert.throws(
82    () => fs.writev(i, common.mustNotCall()),
83    {
84      code: 'ERR_INVALID_ARG_TYPE',
85      name: 'TypeError'
86    }
87  );
88});
89