2014-01-21 21:25:09 +00:00
|
|
|
define(["Filer", "util"], function(Filer, util) {
|
2013-11-27 17:18:09 +00:00
|
|
|
|
|
|
|
describe('fs.writeFile, fs.readFile', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-11-27 17:18:09 +00:00
|
|
|
|
|
|
|
it('should be a function', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.writeFile).to.be.a('function');
|
|
|
|
expect(fs.readFile).to.be.a('function');
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should error when path is wrong to readFile', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/no-such-file', 'utf8', function(error, data) {
|
|
|
|
expect(error).to.exist;
|
2014-04-17 15:53:55 +00:00
|
|
|
expect(error.code).to.equal("ENOENT");
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(data).not.to.exist;
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should write, read a utf8 file without specifying utf8 in writeFile', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myfile', contents, function(error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if(error) throw error;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myfile', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should write, read a utf8 file with "utf8" option to writeFile', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myfile', contents, 'utf8', function(error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if(error) throw error;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myfile', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should write, read a utf8 file with {encoding: "utf8"} option to writeFile', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myfile', contents, { encoding: 'utf8' }, function(error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if(error) throw error;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myfile', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should write, read a binary file', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
// String and utf8 binary encoded versions of the same thing:
|
|
|
|
var contents = "This is a file.";
|
|
|
|
var binary = new Uint8Array([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myfile', binary, function(error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if(error) throw error;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myfile', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.deep.equal(binary);
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should follow symbolic links', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if(error) throw error;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.symlink('/myfile', '/myFileLink', function (error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if (error) throw error;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myFileLink', contents, 'utf8', function (error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if (error) throw error;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myFileLink', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|