説明なし

test-fs.js 11KB

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