tasyncfile.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. discard """
  2. output: '''
  3. 13
  4. hello humans!
  5. 13
  6. '''
  7. """
  8. import asyncfile, asyncdispatch, os
  9. proc main() {.async.} =
  10. let fn = getTempDir() / "foobar.txt"
  11. removeFile(fn)
  12. # Simple write/read test.
  13. block:
  14. var file = openAsync(fn, fmReadWrite)
  15. await file.write("testing")
  16. file.setFilePos(0)
  17. await file.write("foo")
  18. file.setFileSize(4)
  19. file.setFilePos(0)
  20. let data = await file.readAll()
  21. doAssert data == "foot"
  22. file.close()
  23. # Append test
  24. block:
  25. var file = openAsync(fn, fmAppend)
  26. await file.write("\ntest2")
  27. let errorTest = file.readAll()
  28. yield errorTest
  29. doAssert errorTest.failed
  30. file.close()
  31. file = openAsync(fn, fmRead)
  32. let data = await file.readAll()
  33. doAssert data == "foot\ntest2"
  34. file.close()
  35. # Issue #5531
  36. block:
  37. removeFile(fn)
  38. var file = openAsync(fn, fmWrite)
  39. await file.write("test2")
  40. file.close()
  41. file = openAsync(fn, fmWrite)
  42. await file.write("t3")
  43. file.close()
  44. file = openAsync(fn, fmRead)
  45. let data = await file.readAll()
  46. doAssert data == "t3"
  47. file.close()
  48. # Issue #7347
  49. block:
  50. var file = openAsync( parentDir(currentSourcePath) / "hello.txt")
  51. echo file.getFileSize()
  52. echo await file.readAll()
  53. echo file.getFilePos()
  54. waitFor main()