Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding fs.readdirsync mock and relatives testings

  • Loading branch information...
commit e0c4ba5bd2e0e1f5922036be059a43462613437f 1 parent 6760a79
@dduportal dduportal authored
Showing with 56 additions and 0 deletions.
  1. +35 −0 lib/fs.js
  2. +21 −0 test/fs.spec.coffee
View
35 lib/fs.js
@@ -29,6 +29,13 @@ var File = function(mtime, content) {
};
};
+var Directory = function(mtime) {
+ this.mtime = mtime;
+ this.getStats = function() {
+ return new Stats(false, new Date(this.mtime));
+ };
+}
+
/**
* @constructor
* @param {Object} structure
@@ -99,6 +106,34 @@ var Mock = function(structure) {
});
};
+ this.readdirSync = function(path, callback) {
+ 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;
+ }
+
+ if(pointer instanceof File) {
+ var error = new Error('ENOTDIR, not a directory "' + path + '"');
+
+ error.errno = 27;
+ error.code = 'ENOTDIR';
+
+ throw error;
+ }
+
+ return Object.keys(pointer);
+
+
+ };
+
this.mkdir = function(directory, callback) {
predictableNextTick(function() {
var pointer = getPointer(path.dirname(directory), structure);
View
21 test/fs.spec.coffee
@@ -135,6 +135,27 @@ describe 'fs', ->
fs.readdir '/home/not', callback
waitForFinished()
+ # ===========================================================================
+ # fs.readdirSync
+ # ===========================================================================
+ describe 'readdirSync', ->
+
+ it 'should read dir content and sync return all content files', ->
+ content = fs.readdirSync '/home/vojta/sub'
+ expect(content instanceof Array).toBe true
+ expect(content).toEqual ['first.js','second.js','third.log']
+
+
+ it 'should throw when dir does not exist', ->
+ expect(-> fs.readdirSync '/non-existing').
+ toThrow 'ENOENT, no such file or directory "/non-existing"'
+
+
+ it 'should throw when reading a file', ->
+ expect(-> fs.readdirSync '/home/vojta/some.js').
+ toThrow 'ENOTDIR, not a directory "/home/vojta/some.js"'
+
+
# ===========================================================================
# fs.mkdir()
Please sign in to comment.
Something went wrong with that request. Please try again.