• 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.mustCall((err, fd) => {
36    assert.ifError(err);
37
38    const cb = common.mustCall((err, written) => {
39      assert.ifError(err);
40
41      assert.strictEqual(written, expected.length);
42      fs.closeSync(fd);
43
44      const found = fs.readFileSync(filename, 'utf8');
45      assert.strictEqual(found, expected.toString());
46    });
47
48    fs.write(fd, expected, 0, expected.length, null, cb);
49  }));
50}
51
52// fs.write with a buffer, without the length parameter:
53{
54  const filename = path.join(tmpdir.path, 'write2.txt');
55  fs.open(filename, 'w', 0o644, common.mustCall((err, fd) => {
56    assert.ifError(err);
57
58    const cb = common.mustCall((err, written) => {
59      assert.ifError(err);
60
61      assert.strictEqual(written, 2);
62      fs.closeSync(fd);
63
64      const found = fs.readFileSync(filename, 'utf8');
65      assert.strictEqual(found, 'lo');
66    });
67
68    fs.write(fd, Buffer.from('hello'), 3, cb);
69  }));
70}
71
72// fs.write with a buffer, without the offset and length parameters:
73{
74  const filename = path.join(tmpdir.path, 'write3.txt');
75  fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
76    assert.ifError(err);
77
78    const cb = common.mustCall(function(err, written) {
79      assert.ifError(err);
80
81      assert.strictEqual(written, expected.length);
82      fs.closeSync(fd);
83
84      const found = fs.readFileSync(filename, 'utf8');
85      assert.deepStrictEqual(expected.toString(), found);
86    });
87
88    fs.write(fd, expected, cb);
89  }));
90}
91
92// fs.write with the offset passed as undefined followed by the callback:
93{
94  const filename = path.join(tmpdir.path, 'write4.txt');
95  fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
96    assert.ifError(err);
97
98    const cb = common.mustCall(function(err, written) {
99      assert.ifError(err);
100
101      assert.strictEqual(written, expected.length);
102      fs.closeSync(fd);
103
104      const found = fs.readFileSync(filename, 'utf8');
105      assert.deepStrictEqual(expected.toString(), found);
106    });
107
108    fs.write(fd, expected, undefined, cb);
109  }));
110}
111
112// fs.write with offset and length passed as undefined followed by the callback:
113{
114  const filename = path.join(tmpdir.path, 'write5.txt');
115  fs.open(filename, 'w', 0o644, common.mustCall((err, fd) => {
116    assert.ifError(err);
117
118    const cb = common.mustCall((err, written) => {
119      assert.ifError(err);
120
121      assert.strictEqual(written, expected.length);
122      fs.closeSync(fd);
123
124      const found = fs.readFileSync(filename, 'utf8');
125      assert.strictEqual(found, expected.toString());
126    });
127
128    fs.write(fd, expected, undefined, undefined, cb);
129  }));
130}
131
132// fs.write with a Uint8Array, without the offset and length parameters:
133{
134  const filename = path.join(tmpdir.path, 'write6.txt');
135  fs.open(filename, 'w', 0o644, common.mustCall((err, fd) => {
136    assert.ifError(err);
137
138    const cb = common.mustCall((err, written) => {
139      assert.ifError(err);
140
141      assert.strictEqual(written, expected.length);
142      fs.closeSync(fd);
143
144      const found = fs.readFileSync(filename, 'utf8');
145      assert.strictEqual(found, expected.toString());
146    });
147
148    fs.write(fd, Uint8Array.from(expected), cb);
149  }));
150}
151