24997241f9b9bbb293f469632d4bc1f340dcf922
[platform/upstream/nodejs.git] / test / sequential / test-readdir.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 fs = require('fs');
26
27 var got_error = false,
28     readdirDir = path.join(common.fixturesDir, 'readdir');
29
30 var files = ['are',
31              'dir',
32              'empty',
33              'files',
34              'for',
35              'just',
36              'testing.js',
37              'these'];
38
39
40 console.log('readdirSync ' + readdirDir);
41 var f = fs.readdirSync(readdirDir);
42 console.dir(f);
43 assert.deepEqual(files, f.sort());
44
45
46 console.log('readdir ' + readdirDir);
47 fs.readdir(readdirDir, function(err, f) {
48   if (err) {
49     console.log('error');
50     got_error = true;
51   } else {
52     console.dir(f);
53     assert.deepEqual(files, f.sort());
54   }
55 });
56
57 process.on('exit', function() {
58   assert.equal(false, got_error);
59   console.log('exit');
60 });
61
62
63 // readdir() on file should throw ENOTDIR
64 // https://github.com/joyent/node/issues/1869
65 (function() {
66   var has_caught = false;
67
68   try {
69     fs.readdirSync(__filename);
70   }
71   catch (e) {
72     has_caught = true;
73     assert.equal(e.code, 'ENOTDIR');
74   }
75
76   assert(has_caught);
77 })();
78
79
80 (function() {
81   var readdir_cb_called = false;
82
83   fs.readdir(__filename, function(e) {
84     readdir_cb_called = true;
85     assert.equal(e.code, 'ENOTDIR');
86   });
87
88   process.on('exit', function() {
89     assert(readdir_cb_called);
90   });
91 })();