Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add fs.mkdir()

  • Loading branch information...
commit d7add029e226dc5b2099205db319bb50344eca17 1 parent 63dceb1
@vojtajina authored
Showing with 64 additions and 1 deletion.
  1. +18 −1 lib/fs.js
  2. +46 −0 test/fs.spec.coffee
View
19 lib/fs.js
@@ -39,7 +39,7 @@ var Mock = function(structure) {
// TODO(vojta): convert structure to contain only instances of File/Directory (not primitives)
var getPointer = function(path, pointer) {
- var parts = path.split('/').slice(1);
+ var parts = path.replace(/\/$/, '').split('/').slice(1);
while (parts.length) {
if (!pointer[parts[0]]) break;
@@ -99,6 +99,23 @@ var Mock = function(structure) {
});
};
+ this.mkdir = function(directory, callback) {
+ predictableNextTick(function() {
+ var pointer = getPointer(path.dirname(directory), structure);
+ var baseName = path.basename(directory);
+
+ if (pointer && typeof pointer === 'object' && !(pointer instanceof File)) {
+ pointer[baseName] = {};
+ callback(null);
+ } else {
+ var error = new Error(util.format('ENOENT, mkdir "%s"', directory));
+ error.code = 'ENOENT';
+ error.errno = 34;
+ callback(error);
+ }
+ });
+ };
+
this.readFile = function(path, encoding, callback) {
var readFileSync = this.readFileSync;
callback = callback || encoding;
View
46 test/fs.spec.coffee
@@ -137,6 +137,52 @@ describe 'fs', ->
# ===========================================================================
+ # fs.mkdir()
+ # ===========================================================================
+ describe 'mkdir', ->
+
+ it 'should be async', ->
+ callback = jasmine.createSpy 'done'
+ fs.mkdir '/bin', callback
+ expect(callback).not.toHaveBeenCalled()
+
+
+ it 'should create directory', ->
+ callback = (err) ->
+ expect(err).toBeFalsy()
+ stat = fs.statSync '/home/new'
+ expect(stat).toBeDefined()
+ expect(stat.isDirectory()).toBe true
+ finished++
+
+ fs.mkdir '/home/new', callback
+ waitForFinished()
+
+
+ it 'should create a root directory', ->
+ callback = (err) ->
+ expect(err).toBeFalsy()
+ stat = fs.statSync '/new-root'
+ expect(stat).toBeDefined()
+ expect(stat.isDirectory()).toBe true
+ finished++
+
+ fs.mkdir '/new-root', callback
+ waitForFinished()
+
+
+ it 'should return error if parent does not exist', ->
+ callback = (err) ->
+ expect(err).toBeTruthy()
+ expect(err.errno).toBe 34
+ expect(err.code).toBe 'ENOENT'
+ finished++
+
+ fs.mkdir '/new/non/existing', callback
+ waitForFinished()
+
+
+ # ===========================================================================
# fs.readFile
# ===========================================================================
describe 'readFile', ->
Please sign in to comment.
Something went wrong with that request. Please try again.