Skip to content

Commit

Permalink
Add dir.contents for Memory and get tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Sep 27, 2015
1 parent 2617423 commit 652c272
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
11 changes: 4 additions & 7 deletions lib/Memory/Dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ var File = require('./File.js');
var UnderlyingFilesystem = require('./UnderlyingFilesystem.js');

var join = function(ufsPath, extraPath) {
// TODO: The leading / might eliminate this edge case.
if (ufsPath === '') {
return extraPath;
}

return ufsPath + '/' + extraPath;
};

Expand All @@ -34,11 +29,13 @@ var Dir = function(ufs, ufsPath) {
return File(ufs, join(ufsPath, fpath));
};

// TODO: dir.contents
dir.contents = function() {
return ufs.dirContents(ufsPath); // TODO: test throw when asking for contents of file
};

return dir;
};

module.exports = function() {
return Dir(UnderlyingFilesystem(), '');
return Dir(UnderlyingFilesystem(), 'root');
};
46 changes: 41 additions & 5 deletions lib/Memory/UnderlyingFilesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@ module.exports = function() {
ufs.existsFile = function(ufsPath) {
assert(afsPath.check(ufsPath));

// TODO: If there is a directory collision, should this reject? Should the contract of exists
// be that false means you should be able to write to that location without overwriting, with
// the unavoidable caveat of something else being written there during asynchronous delays? Or
// should the result be false if there is a directory in the way?

return delay().then(function() {
// TODO: If there is a directory collision, should this really reject? It seemed natural when
// writing the System implementation. Should the contract of exists be that false means you
// should be able to write to that location without overwriting, with the unavoidable caveat
// of something else being written there during asynchronous delays? Or should the result be
// false if there is a directory in the way?
if (Object.keys(files).some(function(filePath) {
return isPrefix(filePath + '/', ufsPath);
})) {
throw new Error('This file is blocked by another file path.');
}

return !!files[ufsPath];
});
};
Expand All @@ -83,5 +89,35 @@ module.exports = function() {
});
};

var dirContents = function(ufsPath) {
var fullContents = Object.keys(files).filter(function(filePath) {
return isPrefix(ufsPath + '/', filePath);
}).map(function(filePath) {
return filePath.replace(ufsPath + '/', '');
});

var subdirs = [];
var dirFiles = [];

fullContents.forEach(function(filePath) {
if (filePath.indexOf('/') !== -1) {
subdirs.push(filePath.split('/')[0]);
} else {
dirFiles.push(filePath);
}
});

return {
dirs: uniq(subdirs),
files: dirFiles.sort()
};
};

ufs.dirContents = function(ufsPath) {
return delay().then(function() {
return dirContents(ufsPath);
});
};

return ufs;
};
2 changes: 1 addition & 1 deletion test/Memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var describeDir = require('./describers/dir.js');
var describeFile = require('./describers/file.js');

describe('Memory', function() {
xdescribe('Dir', function() {
describe('Dir', function() {
describeDir(Memory.Dir, 2);
});

Expand Down

0 comments on commit 652c272

Please sign in to comment.