integration.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const fs = require('fs')
  4. const path = require('path')
  5. const Promise = require('bluebird')
  6. const request = require('request')
  7. const stream = require('stream')
  8. const modulePath = '../src'
  9. suite('integration:', () => {
  10. let log
  11. setup(() => {
  12. log = {}
  13. })
  14. test('require does not throw', () => {
  15. assert.doesNotThrow(() => {
  16. require(modulePath)
  17. })
  18. })
  19. test('require returns object', () => {
  20. assert.isObject(require(modulePath))
  21. })
  22. suite('require:', () => {
  23. let bfj
  24. setup(() => {
  25. bfj = require(modulePath)
  26. })
  27. test('walk function is exported', () => {
  28. assert.isFunction(bfj.walk)
  29. })
  30. test('walk expects one argument', () => {
  31. assert.lengthOf(bfj.walk, 1)
  32. })
  33. test('match function is exported', () => {
  34. assert.isFunction(bfj.match)
  35. })
  36. test('match expects two arguments', () => {
  37. assert.lengthOf(bfj.match, 2)
  38. })
  39. test('parse function is exported', () => {
  40. assert.isFunction(bfj.parse)
  41. })
  42. test('parse expects one argument', () => {
  43. assert.lengthOf(bfj.parse, 1)
  44. })
  45. test('read function is exported', () => {
  46. assert.isFunction(bfj.read)
  47. })
  48. test('read expects two arguments', () => {
  49. assert.lengthOf(bfj.read, 2)
  50. })
  51. test('eventify function is exported', () => {
  52. assert.isFunction(bfj.eventify)
  53. })
  54. test('eventify expects one argument', () => {
  55. assert.lengthOf(bfj.eventify, 1)
  56. })
  57. test('streamify function is exported', () => {
  58. assert.isFunction(bfj.streamify)
  59. })
  60. test('streamify expects one argument', () => {
  61. assert.lengthOf(bfj.streamify, 1)
  62. })
  63. test('stringify function is exported', () => {
  64. assert.isFunction(bfj.stringify)
  65. })
  66. test('stringify expects two arguments', () => {
  67. assert.lengthOf(bfj.stringify, 2)
  68. })
  69. test('write function is exported', () => {
  70. assert.isFunction(bfj.write)
  71. })
  72. test('write expects two arguments', () => {
  73. assert.lengthOf(bfj.write, 3)
  74. })
  75. test('events are exported', () => {
  76. assert.deepEqual(bfj.events, require('../src/events'))
  77. })
  78. suite('read object:', () => {
  79. let failed, file, result, error
  80. setup(() => {
  81. failed = false
  82. file = path.join(__dirname, 'data.json')
  83. fs.writeFileSync(file, JSON.stringify({
  84. foo: [ 'b', 'a', 'r' ],
  85. baz: null,
  86. qux: 3.14159265359e42
  87. }, null, '\t'))
  88. return bfj.read(file)
  89. .then(res => {
  90. result = res
  91. })
  92. .catch(err => {
  93. failed = true
  94. error = err
  95. })
  96. })
  97. teardown(() => {
  98. fs.unlinkSync(file)
  99. })
  100. test('result was correct', () => {
  101. assert.isFalse(failed)
  102. assert.isUndefined(error)
  103. assert.isObject(result)
  104. assert.lengthOf(Object.keys(result), 3)
  105. assert.isArray(result.foo)
  106. assert.lengthOf(result.foo, 3)
  107. assert.strictEqual(result.foo[0], 'b')
  108. assert.strictEqual(result.foo[1], 'a')
  109. assert.strictEqual(result.foo[2], 'r')
  110. assert.isNull(result.baz)
  111. assert.strictEqual(result.qux, 3.14159265359e42)
  112. })
  113. })
  114. suite('read value:', () => {
  115. let failed, file, result, error
  116. setup(() => {
  117. failed = false
  118. file = path.join(__dirname, 'data.json')
  119. fs.writeFileSync(file, '"foo"')
  120. return bfj.read(file)
  121. .then(res => {
  122. result = res
  123. })
  124. .catch(err => {
  125. failed = true
  126. error = err
  127. })
  128. })
  129. teardown(() => {
  130. fs.unlinkSync(file)
  131. })
  132. test('result was correct', () => {
  133. assert.isFalse(failed)
  134. assert.isUndefined(error)
  135. assert.strictEqual(result, 'foo')
  136. })
  137. })
  138. suite('read error:', () => {
  139. let failed, file, result, error
  140. setup(() => {
  141. failed = false
  142. file = path.join(__dirname, 'data.json')
  143. fs.writeFileSync(file, '"foo" "bar"')
  144. return bfj.read(file)
  145. .then(res => result = res)
  146. .catch(err => {
  147. failed = true
  148. error = err
  149. })
  150. })
  151. teardown(() => {
  152. fs.unlinkSync(file)
  153. })
  154. test('result was correct', () => {
  155. assert.isTrue(failed)
  156. assert.isUndefined(result)
  157. assert.instanceOf(error, Error)
  158. })
  159. })
  160. suite('read missing file:', () => {
  161. let failed, file, result, error
  162. setup(() => {
  163. failed = false
  164. file = path.join(__dirname, 'missing.json')
  165. assert.isFalse(fs.existsSync(file))
  166. return bfj.read(file)
  167. .then(res => result = res)
  168. .catch(err => {
  169. failed = true
  170. error = err
  171. })
  172. })
  173. test('result was correct', () => {
  174. assert.isTrue(failed)
  175. assert.isUndefined(result)
  176. assert.instanceOf(error, Error)
  177. })
  178. })
  179. suite('match predicate:', () => {
  180. let file, results, errors
  181. setup(done => {
  182. file = path.join(__dirname, 'data.json')
  183. fs.writeFileSync(file, JSON.stringify({
  184. foo: 'bar',
  185. baz: 'qux',
  186. wibble: 'blee'
  187. }))
  188. results = []
  189. errors = []
  190. const datastream = bfj.match(
  191. fs.createReadStream(file),
  192. (k, v) => k === 'baz' || v === 'blee',
  193. { minDepth: 1 }
  194. )
  195. datastream.on('data', item => results.push(item))
  196. datastream.on('error', error => errors.push(error))
  197. datastream.on('end', done)
  198. })
  199. test('the correct properties were matched', () => {
  200. assert.deepEqual([ 'qux', 'blee' ], results)
  201. })
  202. test('no errors occurred', () => {
  203. assert.deepEqual(errors, [])
  204. })
  205. })
  206. suite('match nested:', () => {
  207. let file, results, errors
  208. setup(done => {
  209. file = path.join(__dirname, 'data.json')
  210. fs.writeFileSync(file, JSON.stringify({
  211. foo: {
  212. bar: 'baz'
  213. }
  214. }))
  215. results = []
  216. errors = []
  217. const datastream = bfj.match(fs.createReadStream(file), () => true)
  218. datastream.on('data', item => results.push(item))
  219. datastream.on('error', error => errors.push(error))
  220. datastream.on('end', done)
  221. })
  222. test('the correct properties were matched', () => {
  223. assert.deepEqual([ 'baz', { bar: 'baz' }, { foo: { bar: 'baz' } } ], results)
  224. })
  225. test('no errors occurred', () => {
  226. assert.deepEqual(errors, [])
  227. })
  228. })
  229. suite('match ndjson:', () => {
  230. let file, results, errors
  231. setup(done => {
  232. file = path.join(__dirname, 'data.ndjson')
  233. fs.writeFileSync(file, [
  234. JSON.stringify([ 'a', 'b' ]),
  235. JSON.stringify(null),
  236. '',
  237. '',
  238. JSON.stringify('wibble')
  239. ].join('\n'))
  240. results = []
  241. errors = []
  242. const datastream = bfj.match(fs.createReadStream(file), () => true, { ndjson: true })
  243. datastream.on('data', item => results.push(item))
  244. datastream.on('error', error => errors.push(error))
  245. datastream.on('end', done)
  246. })
  247. test('the correct properties were matched', () => {
  248. assert.deepEqual([ 'a', 'b', [ 'a', 'b' ], 'wibble' ], results)
  249. })
  250. test('no errors occurred', () => {
  251. assert.deepEqual(errors, [])
  252. })
  253. })
  254. suite('parse request:', () => {
  255. let error, result
  256. setup(done => {
  257. const jsonstream = new stream.PassThrough()
  258. request({ url: 'https://gitlab.com/philbooth/bfj/raw/master/package.json' })
  259. .pipe(bfj.unpipe((err, res) => {
  260. error = err
  261. result = res
  262. done()
  263. }))
  264. })
  265. test('result was correct', () => {
  266. assert.isNull(error)
  267. assert.deepEqual(result, require('../package.json'))
  268. })
  269. })
  270. suite('parse NDJSON:', () => {
  271. let failed, file, results
  272. setup(() => {
  273. failed = false
  274. file = path.join(__dirname, 'data.ndjson')
  275. results = []
  276. fs.writeFileSync(file, [
  277. JSON.stringify([ 'b', 'a', 'r' ]),
  278. JSON.stringify(null),
  279. '',
  280. '',
  281. JSON.stringify('wibble')
  282. ].join('\n'))
  283. const stream = fs.createReadStream(file)
  284. return bfj.parse(stream, { ndjson: true })
  285. .then(result => {
  286. results.push(result)
  287. return bfj.parse(stream, { ndjson: true })
  288. })
  289. .then(result => {
  290. results.push(result)
  291. return bfj.parse(stream, { ndjson: true })
  292. })
  293. .then(result => {
  294. results.push(result)
  295. return bfj.parse(stream, { ndjson: true })
  296. })
  297. .then(result => {
  298. results.push(result)
  299. return bfj.parse(stream, { ndjson: true })
  300. })
  301. .then(result => results.push(result))
  302. .catch(e => {
  303. failed = true
  304. })
  305. })
  306. teardown(() => {
  307. fs.unlinkSync(file)
  308. })
  309. test('results were correct', () => {
  310. assert.isFalse(failed)
  311. assert.lengthOf(results, 5)
  312. assert.deepEqual(results, [
  313. [ 'b', 'a', 'r' ],
  314. null,
  315. 'wibble',
  316. undefined,
  317. undefined
  318. ])
  319. })
  320. })
  321. suite('stringify value:', () => {
  322. let result
  323. setup(() => {
  324. return bfj.stringify(new Promise(resolve => {
  325. setTimeout(resolve.bind(null, 'foo\t"\nbar'), 20)
  326. }))
  327. .then(res => result = res)
  328. })
  329. test('result was correct', () => {
  330. assert.strictEqual(result, '"foo\\t\\"\\nbar"')
  331. })
  332. })
  333. suite('write object:', () => {
  334. let failed, file, result
  335. setup(() => {
  336. failed = false
  337. file = path.join(__dirname, 'data.json')
  338. return bfj.write(
  339. file,
  340. { foo: [ 'b', 'a', 'r' ], baz: null, qux: 3.14159265359e42 }
  341. )
  342. .then(() => {
  343. result = fs.readFileSync(file, { encoding: 'utf8' })
  344. })
  345. .catch(error => {
  346. failed = true
  347. result = error
  348. })
  349. })
  350. teardown(() => {
  351. fs.unlinkSync(file)
  352. })
  353. test('did not fail', () => {
  354. assert.isFalse(failed)
  355. })
  356. test('result was correct', () => {
  357. assert.strictEqual(result, '{"foo":["b","a","r"],"baz":null,"qux":3.14159265359e+42}')
  358. })
  359. })
  360. })
  361. })