설명 없음

test-fs.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. Platform,
  10. Dimensions,
  11. Image,
  12. } from 'react-native';
  13. const { Assert, Comparer, Info, prop } = RNTest
  14. const fs = RNFetchBlob.fs
  15. const describe = RNTest.config({
  16. group : 'fs',
  17. expand : false,
  18. run : true
  19. })
  20. let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
  21. let dirs = RNFetchBlob.fs.dirs
  22. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  23. describe('Get storage folders', (report, done) => {
  24. report(
  25. <Assert key="system folders should exists"
  26. expect={dirs}
  27. comparer={Comparer.exists} />,
  28. <Assert key="check properties"
  29. expect={['DocumentDir', 'CacheDir']}
  30. comparer={Comparer.hasProperties}
  31. actual={dirs}
  32. />,
  33. <Info key="System Folders">
  34. <Text>{`${JSON.stringify(dirs)}`}</Text>
  35. </Info>
  36. )
  37. done()
  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(
  96. <Assert
  97. key="base64 content test"
  98. expect={raw}
  99. actual={d}/>)
  100. done()
  101. })
  102. })
  103. })
  104. .catch((err) => {
  105. console.log(err)
  106. })
  107. }
  108. })
  109. describe('mkdir and isDir API test', (report, done) => {
  110. let p = dirs.DocumentDir + '/mkdir-test-' + Date.now()
  111. fs.mkdir(p).then((err) => {
  112. report(<Assert key="folder should be created without error"
  113. expect={undefined}
  114. actual={err} />)
  115. return fs.exists(p)
  116. })
  117. .then((exist) => {
  118. report(<Assert key="mkdir should work correctly" expect={true} actual={exist} />)
  119. return fs.isDir(p)
  120. })
  121. .then((isDir) => {
  122. report(<Assert key="isDir should work correctly" expect={true} actual={isDir} />)
  123. return fs.mkdir(p)
  124. })
  125. .then()
  126. .catch((err) => {
  127. report(<Assert key="isDir should not work when folder exists"
  128. expect={err}
  129. comparer={Comparer.hasValue}/>)
  130. done()
  131. })
  132. })
  133. describe('unlink and mkdir API test', (report, done) => {
  134. let p = dirs.DocumentDir + '/unlink-test-' + Date.now()
  135. fs.createFile(p, 'write' + Date.now(), 'utf8').then(() => {
  136. return fs.exists(p)
  137. })
  138. .then((exist) => {
  139. report(<Assert key="file created" expect={true} actual={exist} />)
  140. return fs.unlink(p).then(() => {
  141. return fs.exists(p)
  142. })
  143. })
  144. .then((exist) => {
  145. report(<Assert key="file removed" expect={false} actual={exist} />)
  146. return fs.mkdir(p + '-dir')
  147. })
  148. .then((err) => fs.exists(p + '-dir'))
  149. .then((exist) => {
  150. report(<Assert key="mkdir should success" expect={true} actual={exist} />)
  151. return fs.unlink(p + '-dir')
  152. })
  153. .then(() => fs.exists(p + '-dir'))
  154. .then((exist) => {
  155. report(<Assert key="folder should be removed" expect={false} actual={exist} />)
  156. done()
  157. })
  158. })
  159. describe('write stream API test', (report, done) => {
  160. let p = dirs.DocumentDir + '/write-stream' + Date.now()
  161. let expect = ''
  162. fs.createFile(p, '1234567890', 'utf8')
  163. .then(() => fs.writeStream(p, 'utf8', true))
  164. .then((ws) => {
  165. ws.write('11')
  166. ws.write('12')
  167. ws.write('13')
  168. ws.write('14')
  169. return ws.close()
  170. })
  171. .then(() => {
  172. let d1 = ''
  173. fs.readStream(p, 'utf8').then((stream) => {
  174. stream.open()
  175. stream.onData((chunk) => {
  176. d1 += chunk
  177. })
  178. stream.onEnd(() => {
  179. report(
  180. <Assert key="write data async test"
  181. expect={'123456789011121314'}
  182. actual={d1}/>)
  183. base64Test()
  184. })
  185. })
  186. })
  187. function base64Test() {
  188. fs.writeStream(p, 'base64', false)
  189. .then((ws) => {
  190. for(let i = 0; i< 100; i++) {
  191. expect += String(i)
  192. }
  193. ws.write(RNFetchBlob.base64.encode(expect))
  194. return ws.close()
  195. })
  196. .then(() => {
  197. return fs.readStream(p, 'base64')
  198. })
  199. .then((stream) => {
  200. let d2 = ''
  201. stream.open()
  202. stream.onData((chunk) => {
  203. d2 += chunk
  204. })
  205. stream.onEnd(() => {
  206. report(
  207. <Assert key="file should be overwritten by base64 encoded data"
  208. expect={RNFetchBlob.base64.encode(expect)}
  209. actual={d2} />)
  210. done()
  211. })
  212. })
  213. }
  214. })
  215. describe('mv API test', {timeout : 10000},(report, done) => {
  216. let p = dirs.DocumentDir + '/mvTest' + Date.now()
  217. let dest = p + '-dest-' + Date.now()
  218. let content = Date.now() + '-test'
  219. fs.createFile(p, content, 'utf8')
  220. .then(() => fs.mkdir(dest))
  221. .then(() => fs.mv(p, dest +'/moved'))
  222. .then(() => fs.exists(p))
  223. .then((exist) => {
  224. report(<Assert key="file should not exist in old path" expect={false} actual={exist}/>)
  225. return fs.exists(dest + '/moved')
  226. })
  227. .then((exist) => {
  228. report(<Assert key="file should be moved to destination" expect={true} actual={exist}/>)
  229. return fs.ls(dest)
  230. })
  231. .then((files) => {
  232. report(<Assert key="file name should be correct" expect={'moved'} actual={files[0]}/>)
  233. fs.readStream(dest + '/moved').then((rs) => {
  234. rs.open()
  235. let actual = ''
  236. rs.onData((chunk) => {
  237. actual += chunk
  238. })
  239. rs.onEnd(() => {
  240. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  241. done()
  242. })
  243. })
  244. })
  245. })
  246. describe('cp API test', {timeout : 10000},(report, done) => {
  247. let p = dirs.DocumentDir + '/cpTest' + Date.now()
  248. let dest = p + '-dest-' + Date.now()
  249. let content = Date.now() + '-test'
  250. fs.createFile(p, content, 'utf8')
  251. .then(() => fs.mkdir(dest))
  252. .then(() => fs.cp(p, dest +'/cp'))
  253. .then(() => fs.exists(dest +'/cp'))
  254. .then((exist) => {
  255. report(<Assert key="file should be copy to destination" expect={true} actual={exist}/>)
  256. return fs.ls(dest)
  257. })
  258. .then((files) => {
  259. report(<Assert key="file name should be correct" expect={'cp'} actual={files[0]}/>)
  260. fs.readStream(dest + '/cp').then((rs) => {
  261. rs.open()
  262. let actual = ''
  263. rs.onData((chunk) => {
  264. actual += chunk
  265. })
  266. rs.onEnd(() => {
  267. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  268. done()
  269. })
  270. })
  271. })
  272. })
  273. describe('ASCII data test', (report, done) => {
  274. let p = dirs.DocumentDir + '/ASCII-test-' + Date.now()
  275. let expect = 'fetch-blob-'+Date.now()
  276. fs.createFile(p, 'utf8')
  277. .then(() => {
  278. return fs.writeStream(p, 'ascii', false)
  279. })
  280. .then((ofstream) => {
  281. for(let i=0;i<expect.length;i++) {
  282. ofstream.write([expect[i].charCodeAt(0)])
  283. }
  284. ofstream.write(['g'.charCodeAt(0), 'g'.charCodeAt(0)])
  285. return ofstream.close()
  286. })
  287. .then(() => {
  288. fs.readStream(p, 'ascii').then((ifstream) => {
  289. let res = []
  290. ifstream.open()
  291. ifstream.onData((chunk) => {
  292. res = res.concat(chunk)
  293. })
  294. ifstream.onEnd(() => {
  295. res = res.map((byte) => {
  296. return String.fromCharCode(byte)
  297. }).join('')
  298. report(
  299. <Assert key="data written in ASCII format should correct"
  300. expect={expect + 'gg'}
  301. actual={res}
  302. />)
  303. done()
  304. })
  305. })
  306. })
  307. })
  308. describe('ASCII file test', (report, done) => {
  309. let p = dirs.DocumentDir + '/'
  310. let filename = ''
  311. let expect = []
  312. let base64 = RNFetchBlob.base64
  313. filename = 'ASCII-file-test' + Date.now() + '.txt'
  314. expect = 'ascii test ' + Date.now()
  315. fs.createFile(p + filename, getASCIIArray(expect), 'ascii')
  316. .then(() => {
  317. fs.readStream(p + filename, 'base64').then((rs) => {
  318. let actual = ''
  319. rs.open()
  320. rs.onData((chunk) => {
  321. actual += chunk
  322. })
  323. rs.onEnd(() => {
  324. report(<Assert key="written data verify"
  325. expect={expect}
  326. actual={base64.decode(actual)}/>)
  327. done()
  328. })
  329. })
  330. })
  331. })
  332. describe('format conversion', (report, done) => {
  333. let p = dirs.DocumentDir + '/foo-' + Date.now()
  334. fs.createFile(p, [102, 111, 111], 'ascii')
  335. .then(() => {
  336. fs.readStream(p, 'utf8').then((stream) => {
  337. let res = []
  338. stream.open()
  339. stream.onData((chunk) => {
  340. res+=chunk
  341. })
  342. stream.onEnd(() => {
  343. report(
  344. <Assert key="write utf8 and read by ascii"
  345. expect="foo"
  346. actual={res}/>)
  347. done()
  348. })
  349. })
  350. })
  351. })
  352. describe('stat and lstat test', (report, done) => {
  353. let p = dirs.DocumentDir + '/' + 'ls-stat-test' + Date.now()
  354. let file = null
  355. fs.lstat(dirs.DocumentDir)
  356. // stat a folder
  357. .then((stat) => {
  358. report(
  359. <Assert key="result should be an array"
  360. expect={true}
  361. actual={Array.isArray(stat)}/>)
  362. file = stat[0].path
  363. return fs.stat(file)
  364. })
  365. .then((stat) => {
  366. report(
  367. <Assert key="should have properties"
  368. expect={['size', 'type', 'lastModified', 'filename', 'path']}
  369. comparer={Comparer.hasProperties}
  370. actual={stat}/>)
  371. return fs.stat('13123132')
  372. })
  373. .then(()=>{})
  374. .catch((err) => {
  375. console.log(err)
  376. report(<Assert key="stat error catacable"
  377. expect={true}
  378. actual={true}/>)
  379. done()
  380. })
  381. .then(()=>{})
  382. .catch((err) => {
  383. console.log(err)
  384. report(<Assert key="lstat error catacable"
  385. expect={true}
  386. actual={true}/>)
  387. done()
  388. })
  389. })
  390. describe('fs.slice test', (report, done) => {
  391. let source = null
  392. let parts = fs.dirs.DocumentDir + '/tmp-source-'
  393. let dests = []
  394. let combined = fs.dirs.DocumentDir + '/combined-' + Date.now() + '.jpg'
  395. let size = 0
  396. window.fetch = new RNFetchBlob.polyfill.Fetch({
  397. auto : true,
  398. binaryContentTypes : ['image/', 'video/', 'audio/']
  399. }).build()
  400. fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
  401. .then((res) => res.rawResp())
  402. .then((res) => {
  403. source = res.path()
  404. return fs.stat(source)
  405. })
  406. // separate file into 4kb chunks
  407. .then((stat) => {
  408. size = stat.size
  409. let promise = Promise.resolve()
  410. let cursor = 0
  411. while(cursor < size) {
  412. promise = promise.then(function(start) {
  413. console.log('slicing part ', start , start + 40960)
  414. let offset = 0
  415. return fs.slice(source, parts + start, start + offset, start + 40960)
  416. .then((dest) => {
  417. console.log('slicing part ', start + offset, start + 40960, 'done')
  418. dests.push(dest)
  419. return Promise.resolve()
  420. })
  421. }.bind(this, cursor))
  422. cursor += 40960
  423. }
  424. console.log('loop end')
  425. return promise
  426. })
  427. // combine chunks and verify the result
  428. .then(() => {
  429. console.log('combinding files')
  430. let p = Promise.resolve()
  431. for(let d in dests) {
  432. p = p.then(function(chunk){
  433. return fs.appendFile(combined, chunk, 'uri').then((write) => {
  434. console.log(write, 'bytes write')
  435. })
  436. }.bind(this, dests[d]))
  437. }
  438. return p.then(() => fs.stat(combined))
  439. })
  440. .then((stat) => {
  441. report(
  442. <Assert key="verify file size" expect={size} actual={stat.size}/>,
  443. <Info key="image viewer">
  444. <Image key="combined image" style={styles.image} source={{ uri : prefix + combined}}/>
  445. </Info>)
  446. done()
  447. })
  448. })
  449. function getASCIIArray(str) {
  450. let r = []
  451. for(let i=0;i<str.length;i++) {
  452. r.push(str[i].charCodeAt(0))
  453. }
  454. return r
  455. }