Skip to content

Commit c03e6bf

Browse files
committed
jsonfile: support arbitrary fs like mock-fs
1 parent f20675c commit c03e6bf

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var fs = require('fs')
1+
var _fs = require('fs')
22

33
function readFile (file, options, callback) {
44
if (callback == null) {
@@ -11,6 +11,7 @@ function readFile (file, options, callback) {
1111
}
1212

1313
options = options || {}
14+
var fs = options.fs || _fs
1415

1516
var shouldThrow = true
1617
// DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead
@@ -45,6 +46,8 @@ function readFileSync (file, options) {
4546
options = {encoding: options}
4647
}
4748

49+
var fs = options.fs || _fs
50+
4851
var shouldThrow = true
4952
// DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead
5053
if ('passParsingErrors' in options) {
@@ -72,6 +75,8 @@ function writeFile (file, obj, options, callback) {
7275
callback = options
7376
options = {}
7477
}
78+
options = options || {}
79+
var fs = options.fs || _fs
7580

7681
var spaces = typeof options === 'object' && options !== null
7782
? 'spaces' in options
@@ -90,6 +95,7 @@ function writeFile (file, obj, options, callback) {
9095

9196
function writeFileSync (file, obj, options) {
9297
options = options || {}
98+
var fs = options.fs || _fs
9399

94100
var spaces = typeof options === 'object' && options !== null
95101
? 'spaces' in options

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {},
2020
"devDependencies": {
2121
"mocha": "2.x",
22+
"mock-fs": "^3.8.0",
2223
"rimraf": "^2.4.0",
2324
"standard": "^6.0.8"
2425
},

test/read-file-sync.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var assert = require('assert')
22
var fs = require('fs')
3+
var mockfs = require('mock-fs')
34
var os = require('os')
45
var path = require('path')
56
var rimraf = require('rimraf')
@@ -151,4 +152,14 @@ describe('+ readFileSync()', function () {
151152
assert.strictEqual(data.name, 'jp')
152153
})
153154
})
155+
156+
describe('mockfs', function () {
157+
it('should read from a mockfs', function () {
158+
var mfs = mockfs.fs()
159+
var dataOut = {name: 'JP'}
160+
mfs.writeFileSync('test.json', JSON.stringify(dataOut), 'utf8')
161+
var dataIn = jf.readFileSync('test.json', { fs: mfs })
162+
assert.deepEqual(dataOut, dataIn)
163+
})
164+
})
154165
})

test/read-file.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var assert = require('assert')
22
var fs = require('fs')
3+
var mockfs = require('mock-fs')
34
var os = require('os')
45
var path = require('path')
56
var rimraf = require('rimraf')
@@ -217,4 +218,17 @@ describe('+ readFile()', function () {
217218
})
218219
})
219220
})
221+
222+
describe('mockfs', function () {
223+
it('should read from a mockfs', function (done) {
224+
var mfs = mockfs.fs()
225+
var dataOut = {name: 'JP'}
226+
mfs.writeFileSync('test.json', JSON.stringify(dataOut), 'utf8')
227+
jf.readFile('test.json', { fs: mfs }, function (err, dataIn) {
228+
assert.ifError(err)
229+
assert.deepEqual(dataOut, dataIn)
230+
done()
231+
})
232+
})
233+
})
220234
})

test/write-file-sync.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var assert = require('assert')
22
var fs = require('fs')
3+
var mockfs = require('mock-fs')
34
var os = require('os')
45
var path = require('path')
56
var rimraf = require('rimraf')
@@ -89,4 +90,15 @@ describe('+ writeFileSync()', function () {
8990
assert.strictEqual(data, JSON.stringify(obj) + '\n')
9091
})
9192
})
93+
94+
describe('mockfs', function () {
95+
it('should write to mockfs', function () {
96+
var mfs = mockfs.fs()
97+
var dataOut = { name: 'JP' }
98+
var file = 'somefile.json'
99+
jf.writeFileSync(file, dataOut, { fs: mfs })
100+
var dataIn = JSON.parse(mfs.readFileSync(file, 'utf8'))
101+
assert.deepEqual(dataOut, dataIn)
102+
})
103+
})
92104
})

test/write-file.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var assert = require('assert')
22
var fs = require('fs')
3+
var mockfs = require('mock-fs')
34
var os = require('os')
45
var path = require('path')
56
var rimraf = require('rimraf')
@@ -118,4 +119,18 @@ describe('+ writeFile()', function () {
118119
})
119120
})
120121
})
122+
123+
describe('mockfs', function () {
124+
it('should write to mockfs', function (done) {
125+
var mfs = mockfs.fs()
126+
var dataOut = { name: 'JP' }
127+
var file = 'somefile.json'
128+
jf.writeFile(file, dataOut, { fs: mfs }, function (err) {
129+
assert.ifError(err)
130+
var dataIn = JSON.parse(mfs.readFileSync(file, 'utf8'))
131+
assert.deepEqual(dataOut, dataIn)
132+
done()
133+
})
134+
})
135+
})
121136
})

0 commit comments

Comments
 (0)