filer/tests/spec/shell/ls.spec.js

187 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-05-23 20:53:50 +00:00
var util = require('../../lib/test-utils.js');
var expect = require('chai').expect;
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
describe('FileSystemShell.ls', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
it('should be a function', function() {
var shell = util.shell();
expect(shell.ls).to.be.a('function');
});
it('should fail when dirs argument is absent', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
2014-05-23 20:53:50 +00:00
shell.cat(null, function(error, list) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
2014-05-23 20:53:50 +00:00
expect(list).not.to.exist;
done();
2014-02-18 21:48:53 +00:00
});
2014-05-23 20:53:50 +00:00
});
it('should return the contents of a simple dir', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
var contents = 'a';
var contents2 = 'bb';
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
fs.writeFile('/file', contents, function(err) {
if(err) throw err;
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
fs.writeFile('/file2', contents2, function(err) {
if(err) throw err;
shell.ls('/', function(err, list) {
expect(err).not.to.exist;
expect(list.length).to.equal(2);
var item0 = list[0];
expect(item0.name).to.equal('file');
expect(item0.nlinks).to.equal(1);
2014-05-23 20:53:50 +00:00
expect(item0.size).to.equal(1);
expect(item0.mtime).to.be.a('number');
expect(item0.isFile()).to.be.true;
2014-05-23 20:53:50 +00:00
expect(item0.contents).not.to.exist;
var item1 = list[1];
expect(item1.name).to.equal('file2');
expect(item1.nlinks).to.equal(1);
2014-05-23 20:53:50 +00:00
expect(item1.size).to.equal(2);
expect(item1.mtime).to.be.a('number');
expect(item1.isFile()).to.be.true;
2014-05-23 20:53:50 +00:00
expect(item0.contents).not.to.exist;
done();
});
2014-02-18 21:48:53 +00:00
});
});
2014-05-23 20:53:50 +00:00
});
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
it('should return the shallow contents of a dir tree', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
var contents = 'a';
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir', function(err) {
if(err) throw err;
fs.mkdir('/dir/dir2', function(err) {
2014-02-18 21:48:53 +00:00
if(err) throw err;
2014-05-23 20:53:50 +00:00
fs.writeFile('/dir/file', contents, function(err) {
2014-02-18 21:48:53 +00:00
if(err) throw err;
2014-05-23 20:53:50 +00:00
fs.writeFile('/dir/file2', contents, function(err) {
if(err) throw err;
shell.ls('/dir', function(err, list) {
expect(err).not.to.exist;
expect(list.length).to.equal(3);
// We shouldn't rely on the order we'll get the listing
list.forEach(function(item, i, arr) {
switch(item.name) {
2014-05-23 20:53:50 +00:00
case 'dir2':
expect(item.nlinks).to.equal(1);
2014-05-23 20:53:50 +00:00
expect(item.size).to.be.a('number');
expect(item.mtime).to.be.a('number');
expect(item.isDirectory()).to.be.true;
2014-05-23 20:53:50 +00:00
expect(item.contents).not.to.exist;
break;
case 'file':
case 'file2':
expect(item.nlinks).to.equal(1);
2014-05-23 20:53:50 +00:00
expect(item.size).to.equal(1);
expect(item.mtime).to.be.a('number');
expect(item.isFile()).to.be.true;
2014-05-23 20:53:50 +00:00
expect(item.contents).not.to.exist;
break;
default:
// shouldn't happen
expect(true).to.be.false;
break;
}
if(i === arr.length -1) {
done();
}
});
});
2014-02-18 21:48:53 +00:00
});
});
});
});
2014-05-23 20:53:50 +00:00
});
it('should return the deep contents of a dir tree', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
var contents = 'a';
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir', function(err) {
if(err) throw err;
2014-02-18 21:48:53 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir/dir2', function(err) {
2014-02-18 21:48:53 +00:00
if(err) throw err;
2014-05-23 20:53:50 +00:00
fs.writeFile('/dir/dir2/file', contents, function(err) {
2014-02-18 21:48:53 +00:00
if(err) throw err;
fs.writeFile('/dir/file', contents, function(err) {
if(err) throw err;
fs.writeFile('/dir/file2', contents, function(err) {
if(err) throw err;
2014-05-23 20:53:50 +00:00
shell.ls('/dir', { recursive: true }, function(err, list) {
2014-02-18 21:48:53 +00:00
expect(err).not.to.exist;
expect(list.length).to.equal(3);
// We shouldn't rely on the order we'll get the listing
list.forEach(function(item, i, arr) {
switch(item.name) {
2014-02-18 21:48:53 +00:00
case 'dir2':
expect(item.nlinks).to.equal(1);
2014-02-18 21:48:53 +00:00
expect(item.size).to.be.a('number');
expect(item.mtime).to.be.a('number');
expect(item.isDirectory()).to.be.true;
2014-05-23 20:53:50 +00:00
expect(item.contents).to.exist;
expect(item.contents.length).to.equal(1);
var contents0 = item.contents[0];
expect(contents0.name).to.equal('file');
expect(contents0.nlinks).to.equal(1);
2014-05-23 20:53:50 +00:00
expect(contents0.size).to.equal(1);
expect(contents0.mtime).to.be.a('number');
expect(contents0.isFile()).to.be.true;
2014-05-23 20:53:50 +00:00
expect(contents0.contents).not.to.exist;
2014-02-18 21:48:53 +00:00
break;
case 'file':
case 'file2':
expect(item.nlinks).to.equal(1);
2014-02-18 21:48:53 +00:00
expect(item.size).to.equal(1);
expect(item.mtime).to.be.a('number');
expect(item.isFile()).to.be.true;
2014-02-18 21:48:53 +00:00
expect(item.contents).not.to.exist;
break;
default:
// shouldn't happen
expect(true).to.be.false;
break;
}
if(i === arr.length -1) {
done();
}
});
});
});
});
});
});
});
});
});