summary refs log tree commit diff stats
path: root/tests/stdlib/tgetfileinfo.nim
blob: 49a01906190af2b495537ae3c082a3128bac454f (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
discard """
  output: ""
"""

import os, strutils
# Cases
#  1 - String : Existing File : Symlink true
#  2 - String : Existing File : Symlink false
#  3 - String : Non-existing File : Symlink true
#  4 - String : Non-existing File : Symlink false
#  5 - Handle : Valid File
#  6 - Handle : Invalid File
#  7 - Handle : Valid Handle
#  8 - Handle : Invalid Handle

proc genBadFileName(limit = 100): string =
    ## Generates a filename of a nonexistant file.
    ## Returns "" if generation fails.
    result = "a"
    var hitLimit = true

    for i in 0..100:
      if existsFile(result):
        result.add("a")
      else:
        hitLimit = false
        break
    if hitLimit:
      result = ""

proc caseOneAndTwo(followLink: bool) =
  try:
    discard getFileInfo(getAppFilename(), followLink)
    #echo("String : Existing File : Symlink $# : Success" % $followLink)
  except EOS:
    echo("String : Existing File : Symlink $# : Failure" % $followLink)

proc caseThreeAndFour(followLink: bool) =
  var invalidName = genBadFileName()
  try:
    discard getFileInfo(invalidName, true)
    echo("String : Non-existing File : Symlink $# : Failure" % $followLink)
  except EOS:
    #echo("String : Non-existing File : Symlink $# : Success" % $followLink)

proc testGetFileInfo =
  # Case 1
  caseOneAndTwo(true)

  # Case 2
  caseOneAndTwo(false)

  # Case 3
  caseThreeAndFour(true)

  # Case 4
  caseThreeAndFour(false)

  # Case 5 and 7
  block:
    let
      testFile = open(getAppFilename())
      testHandle = fileHandle(testFile)
    try:
      discard getFileInfo(testFile)
      #echo("Handle : Valid File : Success")
    except EIO:
      echo("Handle : Valid File : Failure")

    try:
      discard getFileInfo(testHandle)
      #echo("Handle : Valid File : Success")
    except EIO:
      echo("Handle : Valid File : Failure")

  # Case 6 and 8
  block:
    let
      testFile: TFile = nil
      testHandle = TFileHandle(-1)
    try:
      discard getFileInfo(testFile)
      echo("Handle : Invalid File : Failure")
    except EIO, EOS:
      #echo("Handle : Invalid File : Success")

    try:
      discard getFileInfo(testHandle)
      echo("Handle : Invalid File : Failure")
    except EIO, EOS:
      #echo("Handle : Invalid File : Success")

testGetFileInfo()