3d7a357a9a89fa3226f1b09d596b82cdfe0ccbdf
[platform/upstream/nodejs.git] / test / parallel / test-fs-write.js
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 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');
31 var found, found2;
32
33 fs.open(fn, 'w', 0644, function(err, fd) {
34   if (err) throw err;
35   console.log('open done');
36   fs.write(fd, '', 0, 'utf8', function(err, written) {
37     assert.equal(0, written);
38   });
39   fs.write(fd, expected, 0, 'utf8', function(err, written) {
40     console.log('write done');
41     if (err) throw err;
42     assert.equal(Buffer.byteLength(expected), written);
43     fs.closeSync(fd);
44     found = fs.readFileSync(fn, 'utf8');
45     console.log('expected: "%s"', expected);
46     console.log('found: "%s"', found);
47     fs.unlinkSync(fn);
48   });
49 });
50
51
52 fs.open(fn2, constants.O_CREAT | constants.O_WRONLY | constants.O_TRUNC, 0644,
53     function(err, fd) {
54       if (err) throw err;
55       console.log('open done');
56       fs.write(fd, '', 0, 'utf8', function(err, written) {
57         assert.equal(0, written);
58       });
59       fs.write(fd, expected, 0, 'utf8', function(err, written) {
60         console.log('write done');
61         if (err) throw err;
62         assert.equal(Buffer.byteLength(expected), written);
63         fs.closeSync(fd);
64         found2 = fs.readFileSync(fn2, 'utf8');
65         console.log('expected: "%s"', expected);
66         console.log('found: "%s"', found2);
67         fs.unlinkSync(fn2);
68       });
69     });
70
71
72 process.on('exit', function() {
73   assert.equal(expected, found);
74   assert.equal(expected, found2);
75 });
76