Comparing version 0.5.0 to 0.6.0
@@ -296,6 +296,6 @@ var path = require('path'); | ||
} | ||
if (descriptor.isWrite() && !descriptor.isAppend()) { | ||
if (descriptor.isTruncate()) { | ||
file.setContent(''); | ||
} | ||
if (descriptor.isAppend()) { | ||
if (descriptor.isTruncate() || descriptor.isAppend()) { | ||
descriptor.setPosition(file.getContent().length); | ||
@@ -503,1 +503,32 @@ } | ||
}; | ||
/** | ||
* Truncate a file. | ||
* @param {number} fd File descriptor. | ||
* @param {function(Error)} callback Optional callback. | ||
*/ | ||
Binding.prototype.ftruncate = function(fd, len, callback) { | ||
maybeCallback(callback, this, function() { | ||
var descriptor = this._getDescriptorById(fd); | ||
if (!descriptor.isWrite()) { | ||
throw new Error('EINVAL, invalid argument'); | ||
} | ||
var file = this._system.getItem(descriptor.getPath()); | ||
if (!(file instanceof File)) { | ||
throw new Error('EINVAL, invalid argument'); | ||
} | ||
var content = file.getContent(); | ||
var newContent = new Buffer(len); | ||
content.copy(newContent); | ||
file.setContent(newContent); | ||
}); | ||
}; | ||
/** | ||
* Legacy support. | ||
* @param {number} fd File descriptor. | ||
* @param {function(Error)} callback Optional callback. | ||
*/ | ||
Binding.prototype.truncate = Binding.prototype.ftruncate; |
@@ -102,2 +102,11 @@ var constants = process.binding('constants'); | ||
/** | ||
* Check if file opened for truncating. | ||
* @return {boolean} Opened for truncating. | ||
*/ | ||
FileDescriptor.prototype.isTruncate = function() { | ||
return (this._flags & constants.O_TRUNC) === constants.O_TRUNC; | ||
}; | ||
/** | ||
* Check if file opened with exclusive flag. | ||
@@ -104,0 +113,0 @@ * @return {boolean} Opened with exclusive. |
{ | ||
"name": "mock-fs", | ||
"description": "Mock fs implementation for testing", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/tschaub/mock-fs", |
@@ -43,5 +43,6 @@ # Mock FS | ||
| `fs.renameSync` | 100% | Complete | | ||
| `fs.ftruncate` | 0% | Implement `binding.ftruncate` | | ||
| `fs.ftruncateSync` | 0% | Implement `binding.ftruncate` | | ||
| `fs.truncate` | 0% | Implement `binding.ftruncate` | | ||
| `fs.ftruncate` | 100% | Complete | | ||
| `fs.ftruncateSync` | 100% | Complete | | ||
| `fs.truncate` | 100% | Complete | | ||
| `fs.truncateSync` | 100% | Complete | | ||
| `fs.chown` | 0% | Implement `binding.chown` | | ||
@@ -48,0 +49,0 @@ | `fs.chownSync` | 0% | Implement `binding.chown` | |
@@ -316,2 +316,10 @@ var path = require('path'); | ||
it('does not truncate (r+)', function() { | ||
var binding = new Binding(system); | ||
var fd = binding.open(path.join('mock-dir', 'two.txt'), flags('r+')); | ||
var file = system.getItem(path.join('mock-dir', 'two.txt')); | ||
assert.instanceOf(file, File); | ||
assert.equal(String(file.getContent()), 'two content'); | ||
}); | ||
it('generates error if file does not exist (r+)', function() { | ||
@@ -764,2 +772,32 @@ var binding = new Binding(system); | ||
describe('#ftruncate()', function() { | ||
it('truncates a file', function() { | ||
var binding = new Binding(system); | ||
var pathname = path.join('mock-dir', 'one.txt'); | ||
var fd = binding.open(pathname, flags('r+')); | ||
binding.ftruncate(fd, 3); | ||
var file = system.getItem(pathname); | ||
assert.equal(String(file.getContent()), 'one'); | ||
}); | ||
it('fails if directory', function() { | ||
var binding = new Binding(system); | ||
var fd = binding.open('mock-dir', flags('r')); | ||
assert.throws(function() { | ||
binding.ftruncate(fd, 3); | ||
}); | ||
}); | ||
it('fails if not open for writing', function() { | ||
var binding = new Binding(system); | ||
var pathname = path.join('mock-dir', 'one.txt'); | ||
var fd = binding.open(pathname, flags('r')); | ||
assert.throws(function() { | ||
binding.ftruncate(fd, 4); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -113,2 +113,66 @@ var path = require('path'); | ||
describe('#isTruncate()', function() { | ||
it('not opened for truncating (r)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('r')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
it('not opened for truncating (r+)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('r+')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
it('not opened for truncating (rs)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('rs')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
it('not opened for truncating (rs+)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('rs+')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
it('opened for truncating (w)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('w')); | ||
assert.isTrue(fd.isTruncate()); | ||
}); | ||
it('opened for truncating (wx)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('wx')); | ||
assert.isTrue(fd.isTruncate()); | ||
}); | ||
it('opened for truncating (w+)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('w+')); | ||
assert.isTrue(fd.isTruncate()); | ||
}); | ||
it('opened for truncating (wx+)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('wx+')); | ||
assert.isTrue(fd.isTruncate()); | ||
}); | ||
it('not opened for truncating (a)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('a')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
it('not opened for truncating (ax)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('ax')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
it('not opened for truncating (a+)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('a+')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
it('not opened for truncating (ax+)', function() { | ||
var fd = new FileDescriptor('foo/bar', flags('ax+')); | ||
assert.isFalse(fd.isTruncate()); | ||
}); | ||
}); | ||
describe('#isCreate()', function() { | ||
@@ -115,0 +179,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
213281
6549
116