暫無描述

test-fs.js 11KB

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