No Description

test-fs.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import {
  5. StyleSheet,
  6. Text,
  7. View,
  8. ScrollView,
  9. Dimensions,
  10. Image,
  11. } from 'react-native';
  12. const { Assert, Comparer, Info, prop } = RNTest
  13. const fs = RNFetchBlob.fs
  14. const describe = RNTest.config({
  15. group : 'fs',
  16. expand : false,
  17. run : true
  18. })
  19. let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
  20. let dirs = null
  21. describe('Get storage folders', (report, done) => {
  22. fs.getSystemDirs().then((resp) => {
  23. dirs = resp
  24. console.log(dirs)
  25. report(
  26. <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
  27. <Assert key="check properties"
  28. expect={['DocumentDir', 'CacheDir']}
  29. comparer={Comparer.hasProperties}
  30. actual={dirs}
  31. />,
  32. <Info key="System Folders">
  33. <Text>{`${JSON.stringify(dirs)}`}</Text>
  34. </Info>
  35. )
  36. done()
  37. })
  38. })
  39. describe('ls API test', (report, done) => {
  40. fs.ls(dirs.DocumentDir).then((list) => {
  41. report(<Assert key="result must be an Array" expect={true} actual={Array.isArray(list)} />)
  42. return fs.ls('hh87h8uhi')
  43. })
  44. .then(()=>{})
  45. .catch((err) => {
  46. report(<Assert key="Wrong path should have error"
  47. expect={err}
  48. comparer={Comparer.exists}/>)
  49. done()
  50. })
  51. })
  52. describe('exists API test', (report, done) => {
  53. let exists = fs.exists
  54. exists(dirs.DocumentDir).then((exist, isDir) => {
  55. report(
  56. <Assert key="document dir should exist" expect={true} actual={exist}/>
  57. )
  58. return exists('blabajsdio')
  59. })
  60. .then((exist, isDir) => {
  61. report(
  62. <Assert key="path should not exist" expect={false} actual={exist}/>
  63. )
  64. done()
  65. })
  66. })
  67. describe('create file API test', (report, done) => {
  68. let p = dirs.DocumentDir + '/test-' + Date.now()
  69. let raw = 'hello ' + Date.now()
  70. let base64 = RNFetchBlob.base64.encode(raw)
  71. fs.createFile(p, raw, 'utf8')
  72. .then(() => {
  73. let d = ''
  74. fs.readStream(p, 'utf8').then((stream) => {
  75. stream.open()
  76. stream.onData((chunk) => {
  77. d += chunk
  78. })
  79. stream.onEnd(() => {
  80. report(<Assert key="utf8 content test" expect={raw} actual={d}/>)
  81. testBase64()
  82. })
  83. })
  84. })
  85. function testBase64() {
  86. fs.createFile(p + '-base64', RNFetchBlob.base64.encode(raw), 'base64')
  87. .then(() => {
  88. fs.readStream(p + '-base64', 'utf8').then((stream) => {
  89. stream.open()
  90. let d = ''
  91. stream.onData((chunk) => {
  92. d += chunk
  93. })
  94. stream.onEnd(() => {
  95. report(<Assert
  96. key="base64 content test"
  97. expect={raw}
  98. actual={d}/>)
  99. done()
  100. })
  101. })
  102. })
  103. .catch((err) => {
  104. console.log(err)
  105. })
  106. }
  107. })
  108. describe('mkdir and isDir API test', (report, done) => {
  109. let p = dirs.DocumentDir + '/mkdir-test-' + Date.now()
  110. fs.mkdir(p).then((err) => {
  111. report(<Assert key="folder should be created without error"
  112. expect={undefined}
  113. actual={err} />)
  114. return fs.exists(p)
  115. })
  116. .then((exist) => {
  117. report(<Assert key="mkdir should work correctly" expect={true} actual={exist} />)
  118. return fs.isDir(p)
  119. })
  120. .then((isDir) => {
  121. report(<Assert key="isDir should work correctly" expect={true} actual={isDir} />)
  122. return fs.mkdir(p)
  123. })
  124. .then()
  125. .catch((err) => {
  126. report(<Assert key="isDir should not work when folder exists"
  127. expect={err}
  128. comparer={Comparer.hasValue}/>)
  129. done()
  130. })
  131. })
  132. describe('unlink and mkdir API test', (report, done) => {
  133. let p = dirs.DocumentDir + '/unlink-test-' + Date.now()
  134. fs.createFile(p, 'write' + Date.now(), 'utf8').then(() => {
  135. return fs.exists(p)
  136. })
  137. .then((exist) => {
  138. report(<Assert key="file created" expect={true} actual={exist} />)
  139. return fs.unlink(p).then(() => {
  140. return fs.exists(p)
  141. })
  142. })
  143. .then((exist) => {
  144. report(<Assert key="file removed" expect={false} actual={exist} />)
  145. return fs.mkdir(p + '-dir')
  146. })
  147. .then((err) => fs.exists(p + '-dir'))
  148. .then((exist) => {
  149. report(<Assert key="mkdir should success" expect={true} actual={exist} />)
  150. return fs.unlink(p + '-dir')
  151. })
  152. .then(() => fs.exists(p + '-dir'))
  153. .then((exist) => {
  154. report(<Assert key="folder should be removed" expect={false} actual={exist} />)
  155. done()
  156. })
  157. })
  158. describe('write stream API test', (report, done) => {
  159. let p = dirs.DocumentDir + '/write-stream' + Date.now()
  160. let expect = ''
  161. fs.createFile(p, '1234567890', 'utf8')
  162. .then(() => fs.writeStream(p, 'utf8', true))
  163. .then((ws) => {
  164. ws.write('11')
  165. ws.write('12')
  166. ws.write('13')
  167. ws.write('14')
  168. return ws.close()
  169. })
  170. .then(() => {
  171. let d1 = ''
  172. fs.readStream(p, 'utf8').then((stream) => {
  173. stream.open()
  174. stream.onData((chunk) => {
  175. d1 += chunk
  176. })
  177. stream.onEnd(() => {
  178. report(
  179. <Assert key="write data async test"
  180. expect={'123456789011121314'}
  181. actual={d1}/>)
  182. base64Test()
  183. })
  184. })
  185. })
  186. function base64Test() {
  187. fs.writeStream(p, 'base64', false)
  188. .then((ws) => {
  189. for(let i = 0; i< 100; i++) {
  190. expect += String(i)
  191. }
  192. ws.write(RNFetchBlob.base64.encode(expect))
  193. return ws.close()
  194. })
  195. .then(() => {
  196. return fs.readStream(p, 'base64')
  197. })
  198. .then((stream) => {
  199. let d2 = ''
  200. stream.open()
  201. stream.onData((chunk) => {
  202. d2 += chunk
  203. })
  204. stream.onEnd(() => {
  205. report(
  206. <Assert key="file should be overwritten by base64 encoded data"
  207. expect={RNFetchBlob.base64.encode(expect)}
  208. actual={d2} />)
  209. done()
  210. })
  211. })
  212. }
  213. })
  214. describe('mv API test', {timeout : 10000},(report, done) => {
  215. let p = dirs.DocumentDir + '/mvTest' + Date.now()
  216. let dest = p + '-dest-' + Date.now()
  217. let content = Date.now() + '-test'
  218. fs.createFile(p, content, 'utf8')
  219. .then(() => fs.mkdir(dest))
  220. .then(() => fs.mv(p, dest +'/moved'))
  221. .then(() => fs.exists(p))
  222. .then((exist) => {
  223. report(<Assert key="file should not exist in old path" expect={false} actual={exist}/>)
  224. return fs.exists(dest + '/moved')
  225. })
  226. .then((exist) => {
  227. report(<Assert key="file should be moved to destination" expect={true} actual={exist}/>)
  228. return fs.ls(dest)
  229. })
  230. .then((files) => {
  231. report(<Assert key="file name should be correct" expect={'moved'} actual={files[0]}/>)
  232. fs.readStream(dest + '/moved').then((rs) => {
  233. rs.open()
  234. let actual = ''
  235. rs.onData((chunk) => {
  236. actual += chunk
  237. })
  238. rs.onEnd(() => {
  239. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  240. done()
  241. })
  242. })
  243. })
  244. })
  245. describe('cp API test', {timeout : 10000},(report, done) => {
  246. let p = dirs.DocumentDir + '/cpTest' + Date.now()
  247. let dest = p + '-dest-' + Date.now()
  248. let content = Date.now() + '-test'
  249. fs.createFile(p, content, 'utf8')
  250. .then(() => fs.mkdir(dest))
  251. .then(() => fs.cp(p, dest +'/cp'))
  252. .then(() => fs.exists(dest +'/cp'))
  253. .then((exist) => {
  254. report(<Assert key="file should be copy to destination" expect={true} actual={exist}/>)
  255. return fs.ls(dest)
  256. })
  257. .then((files) => {
  258. report(<Assert key="file name should be correct" expect={'cp'} actual={files[0]}/>)
  259. fs.readStream(dest + '/cp').then((rs) => {
  260. rs.open()
  261. let actual = ''
  262. rs.onData((chunk) => {
  263. actual += chunk
  264. })
  265. rs.onEnd(() => {
  266. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  267. done()
  268. })
  269. })
  270. })
  271. })
  272. describe('ASCII data test', (report, done) => {
  273. let p = null
  274. let expect = 'fetch-blob-'+Date.now()
  275. fs.getSystemDirs()
  276. .then((dirs) => {
  277. p = dirs.DocumentDir + '/ASCII-test-' + Date.now()
  278. return fs.createFile(p, 'utf8')
  279. })
  280. .then(() => {
  281. return fs.writeStream(p, 'ascii', false)
  282. })
  283. .then((ofstream) => {
  284. for(let i=0;i<expect.length;i++) {
  285. ofstream.write([expect[i].charCodeAt(0)])
  286. }
  287. ofstream.write(['g'.charCodeAt(0), 'g'.charCodeAt(0)])
  288. return ofstream.close()
  289. })
  290. .then(() => {
  291. fs.readStream(p, 'ascii').then((ifstream) => {
  292. let res = []
  293. ifstream.open()
  294. ifstream.onData((chunk) => {
  295. res = res.concat(chunk)
  296. })
  297. ifstream.onEnd(() => {
  298. res = res.map((byte) => {
  299. return String.fromCharCode(byte)
  300. }).join('')
  301. report(
  302. <Assert key="data written in ASCII format should correct"
  303. expect={expect + 'gg'}
  304. actual={res}
  305. />)
  306. done()
  307. })
  308. })
  309. })
  310. })
  311. describe('ASCII file test', (report, done) => {
  312. let p = ''
  313. let filename = ''
  314. let expect = []
  315. let base64 = RNFetchBlob.base64
  316. fs.getSystemDirs().then((dirs) => {
  317. p = dirs.DocumentDir + '/'
  318. filename = 'ASCII-file-test' + Date.now() + '.txt'
  319. expect = 'ascii test ' + Date.now()
  320. return fs.createFile(p + filename, getASCIIArray(expect), 'ascii')
  321. })
  322. .then(() => {
  323. fs.readStream(p + filename, 'base64').then((rs) => {
  324. let actual = ''
  325. rs.open()
  326. rs.onData((chunk) => {
  327. actual += chunk
  328. })
  329. rs.onEnd(() => {
  330. report(<Assert key="written data verify"
  331. expect={expect}
  332. actual={base64.decode(actual)}/>)
  333. done()
  334. })
  335. })
  336. })
  337. })
  338. describe('format conversion', (report, done) => {
  339. let p = ''
  340. fs.getSystemDirs().then((dirs) => {
  341. p = dirs.DocumentDir + '/foo'
  342. return fs.createFile(p, [102, 111, 111], 'ascii')
  343. })
  344. .then(() => {
  345. fs.readStream(p, 'utf8').then((stream) => {
  346. let res = []
  347. stream.open()
  348. stream.onData((chunk) => {
  349. res+=chunk
  350. })
  351. stream.onEnd(() => {
  352. report(
  353. <Assert key="write utf8 and read by ascii"
  354. expect="foo"
  355. actual={res}/>)
  356. done()
  357. })
  358. })
  359. })
  360. })
  361. describe('stat and lstat test', (report, done) => {
  362. let p = ''
  363. let dirs = null
  364. let file = null
  365. fs.getSystemDirs().then((resp) => {
  366. dirs = resp
  367. p = dirs.DocumentDir + '/' + 'ls-stat-test' + Date.now()
  368. return fs.lstat(dirs.DocumentDir)
  369. })
  370. // stat a folder
  371. .then((stat) => {
  372. report(
  373. <Assert key="result should be an array"
  374. expect={true}
  375. actual={Array.isArray(stat)}/>)
  376. file = stat[0].path
  377. return fs.stat(file)
  378. })
  379. .then((stat) => {
  380. report(
  381. <Assert key="should have properties"
  382. expect={['size', 'type', 'lastModified', 'filename', 'path']}
  383. comparer={Comparer.hasProperties}
  384. actual={stat}/>)
  385. return fs.stat('13123132')
  386. })
  387. .then(()=>{})
  388. .catch((err) => {
  389. console.log(err)
  390. report(<Assert key="stat error catacable"
  391. expect={true}
  392. actual={true}/>)
  393. done()
  394. })
  395. .then(()=>{})
  396. .catch((err) => {
  397. console.log(err)
  398. report(<Assert key="lstat error catacable"
  399. expect={true}
  400. actual={true}/>)
  401. done()
  402. })
  403. })
  404. function getASCIIArray(str) {
  405. let r = []
  406. for(let i=0;i<str.length;i++) {
  407. r.push(str[i].charCodeAt(0))
  408. }
  409. return r
  410. }