Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add tests and simplify Stat function
Browse files Browse the repository at this point in the history
  • Loading branch information
errendir committed Aug 20, 2014
1 parent fe57478 commit 0a9c02b
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 33 deletions.
23 changes: 9 additions & 14 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,22 +418,17 @@ static Handle<Value> Stat(const Arguments& args) {

node::Utf8Value path(args[0]);

int throw_safe = 0;
if (args[2]->IsTrue()) {
if (args[1]->IsFunction()) {
ASYNC_CALL(stat, args[1], 1, *path)
} else {
SYNC_CALL(stat, *path, 1, *path)
return scope.Close(
BuildStatsObject(static_cast<const uv_statbuf_t*>(SYNC_REQ.ptr)));
}
throw_safe = 1;
}

if (args[1]->IsFunction()) {
ASYNC_CALL(stat, args[1], throw_safe, *path)
} else {
if (args[1]->IsFunction()) {
ASYNC_CALL(stat, args[1], 0, *path)
} else {
SYNC_CALL(stat, *path, 0, *path)
return scope.Close(
BuildStatsObject(static_cast<const uv_statbuf_t*>(SYNC_REQ.ptr)));
}
SYNC_CALL(stat, *path, throw_safe, *path)
return scope.Close(
BuildStatsObject(static_cast<const uv_statbuf_t*>(SYNC_REQ.ptr)));
}
}

Expand Down
168 changes: 168 additions & 0 deletions test/simple/test-fs-stat-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var fs = require('fs');
var got_error = false;
var success_count = 0;

var stats;

// statSync
try {
stats = fs.statSync('.');
} catch (e) {
got_error = true;
}
if (!stats) {
got_error = true;
} else {
console.dir(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}

try {
stats = fs.statSync('.', true);
} catch (e) {
got_error = true;
}
if (!stats) {
got_error = true;
} else {
console.dir(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}

var errorCaught = false;
try {
stats = fs.statSync('nonexisting_file');
} catch (e) {
assert.ok(e instanceof Error);
success_count++;
errorCaught = true;
}
if (!errorCaught) {
got_error = true;
}

try {
stats = fs.statSync('nonexisting_file', true);
} catch (e) {
got_error = true;
}
assert.equal(false, stats);
success_count++;

// lstatSync
try {
stats = fs.lstatSync('.');
} catch (e) {
got_error = true;
}
if (!stats) {
got_error = true;
} else {
console.dir(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}

// fstatSync
fs.open('.', 'r', undefined, function(err, fd) {
assert.ok(!err);
assert.ok(fd);

var stats;
try {
stats = fs.fstatSync(fd);
} catch (e) {
got_error = true;
}
if (!stats) {
got_error = true;
} else {
console.dir(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}

assert(this === global);
});

// fstatSync
fs.open('.', 'r', undefined, function(err, fd) {
var stats;
try {
stats = fs.fstatSync(fd);
} catch (err) {
got_error = true;
}
if (stats) {
console.dir(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
fs.close(fd);
});

console.log('stating: ' + __filename);
try {
stats = fs.statSync(__filename);
} catch (e) {
got_error = true;
}
if (!stats) {
got_error = true;
} else {
console.dir(stats);
success_count++;

console.log('isDirectory: ' + JSON.stringify(stats.isDirectory()));
assert.equal(false, stats.isDirectory());

console.log('isFile: ' + JSON.stringify(stats.isFile()));
assert.equal(true, stats.isFile());

console.log('isSocket: ' + JSON.stringify(stats.isSocket()));
assert.equal(false, stats.isSocket());

console.log('isBlockDevice: ' + JSON.stringify(stats.isBlockDevice()));
assert.equal(false, stats.isBlockDevice());

console.log('isCharacterDevice: ' + JSON.stringify(stats.isCharacterDevice()));
assert.equal(false, stats.isCharacterDevice());

console.log('isFIFO: ' + JSON.stringify(stats.isFIFO()));
assert.equal(false, stats.isFIFO());

console.log('isSymbolicLink: ' + JSON.stringify(stats.isSymbolicLink()));
assert.equal(false, stats.isSymbolicLink());

assert.ok(stats.mtime instanceof Date);
}

process.on('exit', function() {
assert.equal(8, success_count);
assert.equal(false, got_error);
});
65 changes: 46 additions & 19 deletions test/simple/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ fs.stat('.', function(err, stats) {
assert(this === global);
});

fs.stat('.', function(err, stats) {
if (err) {
got_error = true;
} else {
console.dir(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
assert(this === global);
}, true);

fs.stat('nonexisting_file', function(err, stats) {
if (err) {
assert.ok(err instanceof Error);
success_count++;
} else {
got_error = true;
}
assert(this === global);
});

fs.stat('nonexisting_file', function(err, stats) {
assert.equal(true, err);
assert.equal(false, stats);
assert(this === global);
success_count++;
}, true);

fs.lstat('.', function(err, stats) {
if (err) {
got_error = true;
Expand Down Expand Up @@ -84,40 +112,39 @@ fs.open('.', 'r', undefined, function(err, fd) {
});

console.log('stating: ' + __filename);
fs.stat(__filename, function(err, s) {
fs.stat(__filename, function(err, stats) {
if (err) {
got_error = true;
} else {
console.dir(s);
console.dir(stats);
success_count++;

console.log('isDirectory: ' + JSON.stringify(s.isDirectory()));
assert.equal(false, s.isDirectory());
console.log('isDirectory: ' + JSON.stringify(stats.isDirectory()));
assert.equal(false, stats.isDirectory());

console.log('isFile: ' + JSON.stringify(s.isFile()));
assert.equal(true, s.isFile());
console.log('isFile: ' + JSON.stringify(stats.isFile()));
assert.equal(true, stats.isFile());

console.log('isSocket: ' + JSON.stringify(s.isSocket()));
assert.equal(false, s.isSocket());
console.log('isSocket: ' + JSON.stringify(stats.isSocket()));
assert.equal(false, stats.isSocket());

console.log('isBlockDevice: ' + JSON.stringify(s.isBlockDevice()));
assert.equal(false, s.isBlockDevice());
console.log('isBlockDevice: ' + JSON.stringify(stats.isBlockDevice()));
assert.equal(false, stats.isBlockDevice());

console.log('isCharacterDevice: ' + JSON.stringify(s.isCharacterDevice()));
assert.equal(false, s.isCharacterDevice());
console.log('isCharacterDevice: ' + JSON.stringify(stats.isCharacterDevice()));
assert.equal(false, stats.isCharacterDevice());

console.log('isFIFO: ' + JSON.stringify(s.isFIFO()));
assert.equal(false, s.isFIFO());
console.log('isFIFO: ' + JSON.stringify(stats.isFIFO()));
assert.equal(false, stats.isFIFO());

console.log('isSymbolicLink: ' + JSON.stringify(s.isSymbolicLink()));
assert.equal(false, s.isSymbolicLink());
console.log('isSymbolicLink: ' + JSON.stringify(stats.isSymbolicLink()));
assert.equal(false, stats.isSymbolicLink());

assert.ok(s.mtime instanceof Date);
assert.ok(stats.mtime instanceof Date);
}
});

process.on('exit', function() {
assert.equal(5, success_count);
assert.equal(8, success_count);
assert.equal(false, got_error);
});

0 comments on commit 0a9c02b

Please sign in to comment.