• 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.mustCall((err, written, buffers) => {
28    assert.ifError(err);
29
30    assert.deepStrictEqual(bufferArr, buffers);
31    const expectedLength = bufferArr.length * buffer.byteLength;
32    assert.deepStrictEqual(written, expectedLength);
33    fs.closeSync(fd);
34
35    assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
36  });
37
38  fs.writev(fd, bufferArr, null, done);
39}
40
41// fs.writev with array of buffers without position
42{
43  const filename = getFileName(2);
44  const fd = fs.openSync(filename, 'w');
45
46  const buffer = Buffer.from(expected);
47  const bufferArr = [buffer, buffer];
48
49  const done = common.mustCall((err, written, buffers) => {
50    assert.ifError(err);
51
52    assert.deepStrictEqual(bufferArr, buffers);
53
54    const expectedLength = bufferArr.length * buffer.byteLength;
55    assert.deepStrictEqual(written, expectedLength);
56    fs.closeSync(fd);
57
58    assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
59  });
60
61  fs.writev(fd, bufferArr, done);
62}
63
64/**
65 * Testing with wrong input types
66 */
67{
68  const filename = getFileName(3);
69  const fd = fs.openSync(filename, 'w');
70
71  [false, 'test', {}, [{}], ['sdf'], null, undefined].forEach((i) => {
72    assert.throws(
73      () => fs.writev(fd, i, null, common.mustNotCall()), {
74        code: 'ERR_INVALID_ARG_TYPE',
75        name: 'TypeError'
76      }
77    );
78  });
79
80  fs.closeSync(fd);
81}
82
83// fs.writev with wrong fd types
84[false, 'test', {}, [{}], null, undefined].forEach((i) => {
85  assert.throws(
86    () => fs.writev(i, common.mustNotCall()),
87    {
88      code: 'ERR_INVALID_ARG_TYPE',
89      name: 'TypeError'
90    }
91  );
92});
93