• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const fs = require('fs');
26
27let caughtException = false;
28
29try {
30  // Should throw ENOENT, not EBADF
31  // see https://github.com/joyent/node/pull/1228
32  fs.openSync('/8hvftyuncxrt/path/to/file/that/does/not/exist', 'r');
33} catch (e) {
34  assert.strictEqual(e.code, 'ENOENT');
35  caughtException = true;
36}
37assert.strictEqual(caughtException, true);
38
39fs.openSync(__filename);
40
41fs.open(__filename, common.mustSucceed());
42
43fs.open(__filename, 'r', common.mustSucceed());
44
45fs.open(__filename, 'rs', common.mustSucceed());
46
47fs.open(__filename, 'r', 0, common.mustSucceed());
48
49fs.open(__filename, 'r', null, common.mustSucceed());
50
51async function promise() {
52  await fs.promises.open(__filename);
53  await fs.promises.open(__filename, 'r');
54}
55
56promise().then(common.mustCall()).catch(common.mustNotCall());
57
58assert.throws(
59  () => fs.open(__filename, 'r', 'boom', common.mustNotCall()),
60  {
61    code: 'ERR_INVALID_ARG_VALUE',
62    name: 'TypeError'
63  }
64);
65
66for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
67  assert.throws(
68    () => fs.open(__filename, ...extra),
69    {
70      code: 'ERR_INVALID_ARG_TYPE',
71      name: 'TypeError'
72    }
73  );
74}
75
76[false, 1, [], {}, null, undefined].forEach((i) => {
77  assert.throws(
78    () => fs.open(i, 'r', common.mustNotCall()),
79    {
80      code: 'ERR_INVALID_ARG_TYPE',
81      name: 'TypeError'
82    }
83  );
84  assert.throws(
85    () => fs.openSync(i, 'r', common.mustNotCall()),
86    {
87      code: 'ERR_INVALID_ARG_TYPE',
88      name: 'TypeError'
89    }
90  );
91  assert.rejects(
92    fs.promises.open(i, 'r'),
93    {
94      code: 'ERR_INVALID_ARG_TYPE',
95      name: 'TypeError'
96    }
97  );
98});
99
100// Check invalid modes.
101[false, [], {}].forEach((mode) => {
102  assert.throws(
103    () => fs.open(__filename, 'r', mode, common.mustNotCall()),
104    {
105      code: 'ERR_INVALID_ARG_TYPE'
106    }
107  );
108  assert.throws(
109    () => fs.openSync(__filename, 'r', mode, common.mustNotCall()),
110    {
111      code: 'ERR_INVALID_ARG_TYPE'
112    }
113  );
114  assert.rejects(
115    fs.promises.open(__filename, 'r', mode),
116    {
117      code: 'ERR_INVALID_ARG_TYPE'
118    }
119  );
120});
121