summary refs log tree commit diff stats
path: root/tests/async/tasyncfile.nim
blob: c3cf33512e69e98c55a55042c1d055b4caf21258 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
discard """
  file: "tasyncfile.nim"
  exitcode: 0
"""
import asyncfile, asyncdispatch, os

proc main() {.async.} =
  let fn = getTempDir() / "foobar.txt"
  removeFile(fn)

  # Simple write/read test.
  block:
    var file = openAsync(fn, fmReadWrite)
    await file.write("test")
    file.setFilePos(0)
    await file.write("foo")
    file.setFilePos(0)
    let data = await file.readAll()
    doAssert data == "foot"
    file.close()
  
  # Append test
  block:
    var file = openAsync(fn, fmAppend)
    await file.write("\ntest2")
    let errorTest = file.readAll()
    await errorTest
    doAssert errorTest.failed
    file.close()
    file = openAsync(fn, fmRead)
    let data = await file.readAll()
    
    doAssert data == "foot\ntest2"
    file.close()
  
waitFor main()