tasyncfile.nim 800 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. discard """
  2. file: "tasyncfile.nim"
  3. exitcode: 0
  4. """
  5. import asyncfile, asyncdispatch, os
  6. proc main() {.async.} =
  7. let fn = getTempDir() / "foobar.txt"
  8. removeFile(fn)
  9. # Simple write/read test.
  10. block:
  11. var file = openAsync(fn, fmReadWrite)
  12. await file.write("testing")
  13. file.setFilePos(0)
  14. await file.write("foo")
  15. file.setFileSize(4)
  16. file.setFilePos(0)
  17. let data = await file.readAll()
  18. doAssert data == "foot"
  19. file.close()
  20. # Append test
  21. block:
  22. var file = openAsync(fn, fmAppend)
  23. await file.write("\ntest2")
  24. let errorTest = file.readAll()
  25. yield errorTest
  26. doAssert errorTest.failed
  27. file.close()
  28. file = openAsync(fn, fmRead)
  29. let data = await file.readAll()
  30. doAssert data == "foot\ntest2"
  31. file.close()
  32. waitFor main()