1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
23 var assert = require('assert');
24 var path = require('path');
25 var Buffer = require('buffer').Buffer;
26 var fs = require('fs');
27 var fn = path.join(common.tmpDir, 'write.txt');
28 var fn2 = path.join(common.tmpDir, 'write2.txt');
29 var expected = 'ümlaut.';
30 var constants = require('constants');
33 fs.open(fn, 'w', 0644, function(err, fd) {
35 console.log('open done');
36 fs.write(fd, '', 0, 'utf8', function(err, written) {
37 assert.equal(0, written);
39 fs.write(fd, expected, 0, 'utf8', function(err, written) {
40 console.log('write done');
42 assert.equal(Buffer.byteLength(expected), written);
44 found = fs.readFileSync(fn, 'utf8');
45 console.log('expected: "%s"', expected);
46 console.log('found: "%s"', found);
52 fs.open(fn2, constants.O_CREAT | constants.O_WRONLY | constants.O_TRUNC, 0644,
55 console.log('open done');
56 fs.write(fd, '', 0, 'utf8', function(err, written) {
57 assert.equal(0, written);
59 fs.write(fd, expected, 0, 'utf8', function(err, written) {
60 console.log('write done');
62 assert.equal(Buffer.byteLength(expected), written);
64 found2 = fs.readFileSync(fn2, 'utf8');
65 console.log('expected: "%s"', expected);
66 console.log('found: "%s"', found2);
72 process.on('exit', function() {
73 assert.equal(expected, found);
74 assert.equal(expected, found2);