summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-stream-fs-options.js
blob: 65144c0d617e9bf589af7197fdeae220e7731d29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';

require('../common');
const fixtures = require('../common/fixtures');
const path = require('path');
const fs = require('fs');
const assert = require('assert');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const streamOpts = ['open', 'close'];
const writeStreamOptions = [...streamOpts, 'write'];
const readStreamOptions = [...streamOpts, 'read'];
const originalFs = { fs };

{
  const file = path.join(tmpdir.path, 'write-end-test0.txt');

  writeStreamOptions.forEach((fn) => {
    const overrideFs = Object.assign({}, originalFs.fs, { [fn]: null });
    if (fn === 'write') overrideFs.writev = null;

    const opts = {
      fs: overrideFs
    };
    assert.throws(
      () => fs.createWriteStream(file, opts), {
        code: 'ERR_INVALID_ARG_TYPE',
        name: 'TypeError',
        message: `The "options.fs.${fn}" property must be of type function. ` +
        'Received null'
      },
      `createWriteStream options.fs.${fn} should throw if isn't a function`
    );
  });
}

{
  const file = path.join(tmpdir.path, 'write-end-test0.txt');
  const overrideFs = Object.assign({}, originalFs.fs, { writev: 'not a fn' });
  const opts = {
    fs: overrideFs
  };
  assert.throws(
    () => fs.createWriteStream(file, opts), {
      code: 'ERR_INVALID_ARG_TYPE',
      name: 'TypeError',
      message: 'The "options.fs.writev" property must be of type function. ' +
        'Received type string (\'not a fn\')'
    },
    'createWriteStream options.fs.writev should throw if isn\'t a function'
  );
}

{
  const file = fixtures.path('x.txt');
  readStreamOptions.forEach((fn) => {
    const overrideFs = Object.assign({}, originalFs.fs, { [fn]: null });
    const opts = {
      fs: overrideFs
    };
    assert.throws(
      () => fs.createReadStream(file, opts), {
        code: 'ERR_INVALID_ARG_TYPE',
        name: 'TypeError',
        message: `The "options.fs.${fn}" property must be of type function. ` +
        'Received null'
      },
      `createReadStream options.fs.${fn} should throw if isn't a function`
    );
  });
}