From 4aae53839a8da2ca5da9f4e92c33eaab50094352 Mon Sep 17 00:00:00 2001 From: Rachael Scotchmer <37252365+rscotchmer@users.noreply.github.com> Date: Tue, 12 Mar 2019 15:12:38 -0400 Subject: [PATCH] Issue 627 (#746) * added test to fs.readlink.spec.js * added test for fs.write() to try and write a file opened without the O_WRITE flag * added test to fs.write() that attempts to write with a buffer that is too small * added a test to fs.write() to try and write to a file that doesn't exist * added more specific summary of test for writing to buffer when length is too long, check the type of error that a test returns * Adjusted the message on line 69 to be more clear --- tests/spec/fs.write.spec.js | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/spec/fs.write.spec.js b/tests/spec/fs.write.spec.js index 31f3025..419480a 100644 --- a/tests/spec/fs.write.spec.js +++ b/tests/spec/fs.write.spec.js @@ -65,4 +65,49 @@ describe('fs.write', function() { }); }); }); + + it('should fail if given a file path vs. an fd', function(done) { + const fs = util.fs(); + const buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + + fs.write('/myfile', buffer, 0, buffer.length, 0, function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EBADF'); + done(); + }); + }); + + it('should fail when trying to write more bytes than are available in the buffer (length too long)', function(done) { + const fs = util.fs(); + const buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + + fs.open('/myfile', 'w', function(error, fd) { + if(error) throw error; + + fs.write(fd, buffer, 0, (buffer.length + 10), 0, function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EIO'); + fs.close(fd, done); + }); + }); + }); + + it('should fail to write data to a file opened without the O_WRITE flag', function(done) { + const fs = util.fs(); + const buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + + fs.mknod('/myfile', 'FILE', function(err) { + if (err) throw err; + + fs.open('/myfile', 'r', function(error, fd) { + if(error) throw error; + + fs.write(fd, buffer, 0, buffer.length, 0, function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EBADF'); + done(); + }); + }); + }); + }); });