-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathread-file-sync.test.js
149 lines (125 loc) · 3.96 KB
/
read-file-sync.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const assert = require('node:assert')
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')
const rimraf = require('rimraf')
const jf = require('../')
/* global describe it beforeEach afterEach */
describe('+ readFileSync()', () => {
let TEST_DIR
beforeEach((done) => {
TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-readfile-sync')
rimraf.sync(TEST_DIR)
fs.mkdir(TEST_DIR, done)
})
afterEach((done) => {
rimraf.sync(TEST_DIR)
done()
})
it('should read and parse JSON', () => {
const file = path.join(TEST_DIR, 'somefile3.json')
const obj = { name: 'JP' }
fs.writeFileSync(file, JSON.stringify(obj))
try {
const obj2 = jf.readFileSync(file)
assert.strictEqual(obj2.name, obj.name)
} catch (err) {
assert(err)
}
})
describe('> when invalid JSON', () => {
it('should include the filename in the error', () => {
const fn = 'somefile.json'
const file = path.join(TEST_DIR, fn)
fs.writeFileSync(file, '{')
assert.throws(() => {
jf.readFileSync(file)
}, (err) => {
assert(err instanceof Error)
assert(err.message.match(fn))
return true
})
})
})
describe('> when invalid JSON and throws set to false', () => {
it('should return null', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
const data = '{not valid JSON'
fs.writeFileSync(file, data)
assert.throws(() => {
jf.readFileSync(file)
})
const obj = jf.readFileSync(file, { throws: false })
assert.strictEqual(obj, null)
})
})
describe('> when invalid JSON and throws set to true', () => {
it('should throw an exception', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
const data = '{not valid JSON'
fs.writeFileSync(file, data)
assert.throws(() => {
jf.readFileSync(file, { throws: true })
})
})
})
describe('> when json file is missing and throws set to false', () => {
it('should return null', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
const obj = jf.readFileSync(file, { throws: false })
assert.strictEqual(obj, null)
})
})
describe('> when json file is missing and throws set to true', () => {
it('should throw an exception', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
assert.throws(() => {
jf.readFileSync(file, { throws: true })
})
})
})
describe('> when JSON reviver is set', () => {
it('should transform the JSON', () => {
const file = path.join(TEST_DIR, 'somefile.json')
const sillyReviver = function (k, v) {
if (typeof v !== 'string') return v
if (v.indexOf('date:') < 0) return v
return new Date(v.split('date:')[1])
}
const obj = {
name: 'jp',
day: 'date:2015-06-19T11:41:26.815Z'
}
fs.writeFileSync(file, JSON.stringify(obj))
const data = jf.readFileSync(file, { reviver: sillyReviver })
assert.strictEqual(data.name, 'jp')
assert(data.day instanceof Date)
assert.strictEqual(data.day.toISOString(), '2015-06-19T11:41:26.815Z')
})
})
describe('> when passing encoding string as option', () => {
it('should not throw an error', () => {
const file = path.join(TEST_DIR, 'somefile.json')
const obj = {
name: 'jp'
}
fs.writeFileSync(file, JSON.stringify(obj))
let data
try {
data = jf.readFileSync(file, 'utf8')
} catch (err) {
assert.ifError(err)
}
assert.strictEqual(data.name, 'jp')
})
})
describe('> w/ BOM', () => {
it('should properly parse', () => {
const file = path.join(TEST_DIR, 'file-bom.json')
const obj = { name: 'JP' }
fs.writeFileSync(file, `\uFEFF${JSON.stringify(obj)}`)
const data = jf.readFileSync(file)
assert.deepStrictEqual(obj, data)
})
})
})