Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add fs.statSync()

  • Loading branch information...
commit 63dceb1a37efc24f80ae5cc1c49455e4cf4bb048 1 parent a5db59b
@vojtajina authored
Showing with 50 additions and 6 deletions.
  1. +28 −6 lib/fs.js
  2. +22 −0 test/fs.spec.coffee
View
34 lib/fs.js
@@ -57,17 +57,39 @@ var Mock = function(structure) {
// public API
this.stat = function(path, callback) {
- validatePath(path);
+ var statSync = this.statSync;
+
predictableNextTick(function() {
- var pointer = getPointer(path, structure);
- if (!pointer) return callback({});
+ var stat = null;
+ var error = null;
- var stats = pointer instanceof File ? pointer.getStats() :
- new Stats(typeof pointer !== 'object');
- return callback(null, stats);
+ try {
+ stat = statSync(path);
+ } catch(e) {
+ error = e;
+ }
+
+ callback(error, stat);
});
};
+ this.statSync = function(path) {
+ validatePath(path);
+
+ var pointer = getPointer(path, structure);
+
+ if (!pointer) {
+ var error = new Error('ENOENT, no such file or directory "' + path + '"');
+
+ error.errno = 34;
+ error.code = 'ENOENT';
+
+ throw error;
+ }
+
+ return pointer instanceof File ? pointer.getStats() : new Stats(typeof pointer !== 'object');
+ };
+
this.readdir = function(path, callback) {
validatePath(path);
predictableNextTick(function() {
View
22 test/fs.spec.coffee
@@ -82,6 +82,28 @@ describe 'fs', ->
# ===========================================================================
+ # fs.statSync()
+ # ===========================================================================
+ describe 'statSync', ->
+
+ it 'should stat directory', ->
+ stat = fs.statSync '/bin'
+ expect(stat.isDirectory()).toBe true
+
+
+ it 'should stat file', ->
+ stat = fs.statSync '/bin/grep'
+ expect(stat.isDirectory()).toBe false
+
+ stat = fs.statSync '/home/vojta/some.js'
+ expect(stat.isDirectory()).toBe false
+
+
+ it 'should throw an error when path does not exist', ->
+ expect(-> fs.statSync '/notexist').toThrow 'ENOENT, no such file or directory "/notexist"'
+
+
+ # ===========================================================================
# fs.readdir()
# ===========================================================================
describe 'readdir', ->
Please sign in to comment.
Something went wrong with that request. Please try again.