• 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 path = require('path');
26const fs = require('fs');
27const expected = Buffer.from('hello');
28
29const tmpdir = require('../common/tmpdir');
30tmpdir.refresh();
31
32// fs.write with all parameters provided:
33{
34  const filename = path.join(tmpdir.path, 'write1.txt');
35  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
36    const cb = common.mustSucceed((written) => {
37      assert.strictEqual(written, expected.length);
38      fs.closeSync(fd);
39
40      const found = fs.readFileSync(filename, 'utf8');
41      assert.strictEqual(found, expected.toString());
42    });
43
44    fs.write(fd, expected, 0, expected.length, null, cb);
45  }));
46}
47
48// fs.write with a buffer, without the length parameter:
49{
50  const filename = path.join(tmpdir.path, 'write2.txt');
51  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
52    const cb = common.mustSucceed((written) => {
53      assert.strictEqual(written, 2);
54      fs.closeSync(fd);
55
56      const found = fs.readFileSync(filename, 'utf8');
57      assert.strictEqual(found, 'lo');
58    });
59
60    fs.write(fd, Buffer.from('hello'), 3, cb);
61  }));
62}
63
64// fs.write with a buffer, without the offset and length parameters:
65{
66  const filename = path.join(tmpdir.path, 'write3.txt');
67  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
68    const cb = common.mustSucceed((written) => {
69      assert.strictEqual(written, expected.length);
70      fs.closeSync(fd);
71
72      const found = fs.readFileSync(filename, 'utf8');
73      assert.deepStrictEqual(expected.toString(), found);
74    });
75
76    fs.write(fd, expected, cb);
77  }));
78}
79
80// fs.write with the offset passed as undefined followed by the callback:
81{
82  const filename = path.join(tmpdir.path, 'write4.txt');
83  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
84    const cb = common.mustSucceed((written) => {
85      assert.strictEqual(written, expected.length);
86      fs.closeSync(fd);
87
88      const found = fs.readFileSync(filename, 'utf8');
89      assert.deepStrictEqual(expected.toString(), found);
90    });
91
92    fs.write(fd, expected, undefined, cb);
93  }));
94}
95
96// fs.write with offset and length passed as undefined followed by the callback:
97{
98  const filename = path.join(tmpdir.path, 'write5.txt');
99  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
100    const cb = common.mustSucceed((written) => {
101      assert.strictEqual(written, expected.length);
102      fs.closeSync(fd);
103
104      const found = fs.readFileSync(filename, 'utf8');
105      assert.strictEqual(found, expected.toString());
106    });
107
108    fs.write(fd, expected, undefined, undefined, cb);
109  }));
110}
111
112// fs.write with a Uint8Array, without the offset and length parameters:
113{
114  const filename = path.join(tmpdir.path, 'write6.txt');
115  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
116    const cb = common.mustSucceed((written) => {
117      assert.strictEqual(written, expected.length);
118      fs.closeSync(fd);
119
120      const found = fs.readFileSync(filename, 'utf8');
121      assert.strictEqual(found, expected.toString());
122    });
123
124    fs.write(fd, Uint8Array.from(expected), cb);
125  }));
126}
127
128// fs.write with invalid offset type
129{
130  const filename = path.join(tmpdir.path, 'write7.txt');
131  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
132    assert.throws(() => {
133      fs.write(fd,
134               Buffer.from('abcd'),
135               NaN,
136               expected.length,
137               0,
138               common.mustNotCall());
139    }, {
140      code: 'ERR_OUT_OF_RANGE',
141      name: 'RangeError',
142      message: 'The value of "offset" is out of range. ' +
143               'It must be an integer. Received NaN'
144    });
145
146    fs.closeSync(fd);
147  }));
148}
149
150// fs.write with a DataView, without the offset and length parameters:
151{
152  const filename = path.join(tmpdir.path, 'write8.txt');
153  fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
154    const cb = common.mustSucceed((written) => {
155      assert.strictEqual(written, expected.length);
156      fs.closeSync(fd);
157
158      const found = fs.readFileSync(filename, 'utf8');
159      assert.strictEqual(found, expected.toString());
160    });
161
162    const uint8 = Uint8Array.from(expected);
163    fs.write(fd, new DataView(uint8.buffer), cb);
164  }));
165}
166