Skip to content

Commit

Permalink
Merge pull request #11 from eventualbuddha/update-api-to-node-10
Browse files Browse the repository at this point in the history
Support passing an options object or encoding.
  • Loading branch information
eldargab committed Oct 6, 2014
2 parents 6039a7f + 6aab191 commit e8f2489
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/fake-fs.js
Expand Up @@ -149,14 +149,16 @@ Fs.prototype.readdirSync = function(dir) {
return Object.keys(item.children)
}

Fs.prototype.readFileSync = function(filename, encoding) {
Fs.prototype.readFileSync = function(filename, options) {
var encoding = options && ((typeof options === 'string') ? options : options.encoding)
var item = this._get(filename)
if (item.isDirectory()) throw FsError('EISDIR')
var buf = item.content
return encoding ? buf.toString(encoding) : buf
}

Fs.prototype.writeFileSync = function(filename, data, encoding) {
Fs.prototype.writeFileSync = function(filename, data, options) {
var encoding = options && ((typeof options === 'string') ? options : options.encoding)
var parent = this._get(dirname(filename))
if (!parent.isDirectory()) throw FsError('ENOTDIR')
if (!this.existsSync(filename)) {
Expand All @@ -165,7 +167,8 @@ Fs.prototype.writeFileSync = function(filename, data, encoding) {
this.file(filename, data, encoding)
}

Fs.prototype.appendFileSync = function(filename, data, encoding) {
Fs.prototype.appendFileSync = function(filename, data, options) {
var encoding = options && ((typeof options === 'string') ? options : options.encoding)
var item = this._itemAt(filename)
if (item) {
if (item.isDirectory()) throw FsError('EISDIR')
Expand Down Expand Up @@ -241,29 +244,28 @@ Fs.prototype.renameSync = function(oldPath, newPath) {
}
})

Fs.prototype.readFile = function(filename, encoding, cb) {
if (typeof encoding != 'string') {
cb = encoding
encoding = undefined
Fs.prototype.readFile = function(filename, options, cb) {
if (typeof options === 'function') {
cb = options
options = undefined
}
var res, err
try {
res = this.readFileSync(filename, encoding)
res = this.readFileSync(filename, options)
} catch(e) {
err = e
}
cb && cb(err, res)
}

Fs.prototype.writeFile = function(filename, data, encoding, cb) {
if (typeof encoding == 'function') {
cb = encoding
encoding = null
Fs.prototype.writeFile = function(filename, data, options, cb) {
if (typeof options === 'function') {
cb = options
options = null
}
encoding = encoding || 'utf8'
var err
try {
this.writeFileSync(filename, data, encoding)
this.writeFileSync(filename, data, options)
} catch(e) {
err = e
}
Expand Down
24 changes: 24 additions & 0 deletions test/fake-fs.js
Expand Up @@ -104,6 +104,12 @@ describe('Fake FS', function() {
fs.readFileSync('bin2')[0].should.equal(10)
})

it('Should support options param in place of encoding', function() {
fs.file('hello.txt', 'hello')
.readFileSync('hello.txt', { encoding: 'utf8' })
.should.equal('hello')
})

it('Should support options param', function() {
fs.file('hello.txt', {
atime: 10,
Expand Down Expand Up @@ -394,6 +400,11 @@ describe('Fake FS', function() {
cb.result().should.equal('a')
})

it('Should respect encoding option', function() {
fs.file('file.txt', 'a').readFile('file.txt', { encoding: 'utf8' }, cb)
cb.result().should.equal('a')
})

it('Should throw ENOENT on non-existent file', function() {
fs.readFile('foo', cb)
cb.error('ENOENT')
Expand All @@ -418,6 +429,12 @@ describe('Fake FS', function() {
fs.readFileSync('a', 'utf8').should.equal('Man')
})

it('Should respect encoding from options object', function() {
fs.dir('.').writeFile('a', 'TWFu', { encoding: 'base64'}, cb)
cb.result()
fs.readFileSync('a', 'utf8').should.equal('Man')
})

it('Should allow to write buffers', function() {
fs.dir('.').writeFile('a', new Buffer([10]), cb)
cb.result()
Expand Down Expand Up @@ -448,6 +465,13 @@ describe('Fake FS', function() {
cb.result()
fs.readFileSync('a', 'utf8').should.equal('helloworld')
})

it('Should respect the encoding option', function() {
fs.file('a', 'hello')
fs.appendFile('a', 'world', { encoding: 'utf8' }, cb)
cb.result()
fs.readFileSync('a', 'utf8').should.equal('helloworld')
})
})

it('Should not update dir times on file update', function(done) {
Expand Down

0 comments on commit e8f2489

Please sign in to comment.