• 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');
24
25const assert = require('assert');
26const fs = require('fs');
27
28fs.stat('.', common.mustCall(function(err, stats) {
29  assert.ifError(err);
30  assert.ok(stats.mtime instanceof Date);
31  assert.ok(stats.hasOwnProperty('blksize'));
32  assert.ok(stats.hasOwnProperty('blocks'));
33  // Confirm that we are not running in the context of the internal binding
34  // layer.
35  // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1
36  assert.strictEqual(this, undefined);
37}));
38
39fs.lstat('.', common.mustCall(function(err, stats) {
40  assert.ifError(err);
41  assert.ok(stats.mtime instanceof Date);
42  // Confirm that we are not running in the context of the internal binding
43  // layer.
44  // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1
45  assert.strictEqual(this, undefined);
46}));
47
48// fstat
49fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
50  assert.ifError(err);
51  assert.ok(fd);
52
53  fs.fstat(fd, common.mustCall(function(err, stats) {
54    assert.ifError(err);
55    assert.ok(stats.mtime instanceof Date);
56    fs.close(fd, assert.ifError);
57    // Confirm that we are not running in the context of the internal binding
58    // layer.
59    // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1
60    assert.strictEqual(this, undefined);
61  }));
62
63  // Confirm that we are not running in the context of the internal binding
64  // layer.
65  // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1
66  assert.strictEqual(this, undefined);
67}));
68
69// fstatSync
70fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
71  const stats = fs.fstatSync(fd);
72  assert.ok(stats.mtime instanceof Date);
73  fs.close(fd, common.mustCall(assert.ifError));
74}));
75
76fs.stat(__filename, common.mustCall(function(err, s) {
77  assert.ifError(err);
78  assert.strictEqual(s.isDirectory(), false);
79  assert.strictEqual(s.isFile(), true);
80  assert.strictEqual(s.isSocket(), false);
81  assert.strictEqual(s.isBlockDevice(), false);
82  assert.strictEqual(s.isCharacterDevice(), false);
83  assert.strictEqual(s.isFIFO(), false);
84  assert.strictEqual(s.isSymbolicLink(), false);
85
86  const jsonString = JSON.stringify(s);
87  const parsed = JSON.parse(jsonString);
88  [
89    'dev', 'mode', 'nlink', 'uid',
90    'gid', 'rdev', 'blksize', 'ino', 'size', 'blocks',
91    'atime', 'mtime', 'ctime', 'birthtime',
92    'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs',
93  ].forEach(function(k) {
94    assert.ok(k in s, `${k} should be in Stats`);
95    assert.notStrictEqual(s[k], undefined, `${k} should not be undefined`);
96    assert.notStrictEqual(s[k], null, `${k} should not be null`);
97    assert.notStrictEqual(parsed[k], undefined, `${k} should not be undefined`);
98    assert.notStrictEqual(parsed[k], null, `${k} should not be null`);
99  });
100  [
101    'dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'blksize', 'ino', 'size',
102    'blocks', 'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs',
103  ].forEach((k) => {
104    assert.strictEqual(typeof s[k], 'number', `${k} should be a number`);
105    assert.strictEqual(typeof parsed[k], 'number', `${k} should be a number`);
106  });
107  ['atime', 'mtime', 'ctime', 'birthtime'].forEach((k) => {
108    assert.ok(s[k] instanceof Date, `${k} should be a Date`);
109    assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`);
110  });
111}));
112
113['', false, null, undefined, {}, []].forEach((input) => {
114  ['fstat', 'fstatSync'].forEach((fnName) => {
115    assert.throws(
116      () => fs[fnName](input),
117      {
118        code: 'ERR_INVALID_ARG_TYPE',
119        name: 'TypeError'
120      }
121    );
122  });
123});
124
125[false, 1, {}, [], null, undefined].forEach((input) => {
126  assert.throws(
127    () => fs.lstat(input, common.mustNotCall()),
128    {
129      code: 'ERR_INVALID_ARG_TYPE',
130      name: 'TypeError'
131    }
132  );
133  assert.throws(
134    () => fs.lstatSync(input),
135    {
136      code: 'ERR_INVALID_ARG_TYPE',
137      name: 'TypeError'
138    }
139  );
140  assert.throws(
141    () => fs.stat(input, common.mustNotCall()),
142    {
143      code: 'ERR_INVALID_ARG_TYPE',
144      name: 'TypeError'
145    }
146  );
147  assert.throws(
148    () => fs.statSync(input),
149    {
150      code: 'ERR_INVALID_ARG_TYPE',
151      name: 'TypeError'
152    }
153  );
154});
155
156// Should not throw an error
157fs.stat(__filename, undefined, common.mustCall(() => {}));
158
159fs.open(__filename, 'r', undefined, common.mustCall((err, fd) => {
160  // Should not throw an error
161  fs.fstat(fd, undefined, common.mustCall(() => {}));
162}));
163
164// Should not throw an error
165fs.lstat(__filename, undefined, common.mustCall(() => {}));
166