Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 627 #746

Merged
merged 8 commits into from
Mar 12, 2019
45 changes: 45 additions & 0 deletions tests/spec/fs.write.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,49 @@ describe('fs.write', function() {
});
});
});

it('should fail to write to a file that does not exist', 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.write takes an fd, so passing it a filepath as a string is really testing what we do when we don't pass an fd. If I do this in node, it throws. Probably returning EBADF is OK here, since it literally is a bad file descriptor.

That said, let's adjust the message on line 69 to be more clear: it('should fail if given a file path vs. an fd'

expect(error).to.exist;
expect(undefined).to.equal(undefined);
CDNRae marked this conversation as resolved.
Show resolved Hide resolved
done();
});
});

it('should fail to write a buffer whose length is too small', function(done) {
CDNRae marked this conversation as resolved.
Show resolved Hide resolved
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(undefined).to.equal(undefined);
CDNRae marked this conversation as resolved.
Show resolved Hide resolved
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');
CDNRae marked this conversation as resolved.
Show resolved Hide resolved
done();
});
});
});
});
});