2014-01-21 21:25:09 +00:00
|
|
|
define(["Filer", "util"], function(Filer, util) {
|
2014-01-30 19:12:32 +00:00
|
|
|
|
|
|
|
describe('fs.appendFile', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
util.setup(function() {
|
|
|
|
var fs = util.fs();
|
|
|
|
fs.writeFile('/myfile', "This is a file.", { encoding: 'utf8' }, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
done();
|
|
|
|
});
|
2014-01-30 19:12:32 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
afterEach(util.cleanup);
|
2014-01-30 19:12:32 +00:00
|
|
|
|
|
|
|
it('should be a function', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.appendFile).to.be.a('function');
|
2014-01-30 19:12:32 +00:00
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should append a utf8 file without specifying utf8 in appendFile', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-01-30 19:12:32 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
var more = " Appended.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.appendFile('/myfile', more, function(error) {
|
2014-01-30 19:12:32 +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 + more);
|
|
|
|
done();
|
|
|
|
});
|
2014-01-30 19:12:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should append a utf8 file with "utf8" option to appendFile', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-01-30 19:12:32 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
var more = " Appended.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.appendFile('/myfile', more, 'utf8', function(error) {
|
2014-01-30 19:12:32 +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 + more);
|
|
|
|
done();
|
|
|
|
});
|
2014-01-30 19:12:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should append a utf8 file with {encoding: "utf8"} option to appendFile', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-01-30 19:12:32 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
var more = " Appended.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.appendFile('/myfile', more, { encoding: 'utf8' }, function(error) {
|
2014-01-30 19:12:32 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myfile', { encoding: 'utf8' }, function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents + more);
|
|
|
|
done();
|
|
|
|
});
|
2014-01-30 19:12:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should append a binary file', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-01-30 19:12:32 +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]);
|
|
|
|
var more = " Appended.";
|
|
|
|
var binary2 = new Uint8Array([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
|
|
|
|
var binary3 = new Uint8Array([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46,
|
|
|
|
32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/mybinaryfile', binary, function(error) {
|
2014-01-30 19:12:32 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.appendFile('/mybinaryfile', binary2, function(error) {
|
|
|
|
if(error) throw error;
|
2014-01-30 19:12:32 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/mybinaryfile', 'ascii', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.deep.equal(binary3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-30 19:12:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should follow symbolic links', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-01-30 19:12:32 +00:00
|
|
|
var contents = "This is a file.";
|
|
|
|
var more = " Appended.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.symlink('/myfile', '/myFileLink', function (error) {
|
2014-01-30 19:12:32 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.appendFile('/myFileLink', more, 'utf8', function (error) {
|
|
|
|
if (error) throw error;
|
2014-01-30 19:12:32 +00:00
|
|
|
|
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 + more);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-30 19:12:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|