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